News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How EBP works.

Started by xerox, April 14, 2011, 03:38:25 PM

Previous topic - Next topic

RuiLoureiro

Quote from: dedndave on April 14, 2011, 05:40:23 PM
it is the greatest common denominator
not the least common factor
in spite of the name he gave the routine,
you had to have read the text that was deleted from his original thread
that's how i knew what he was after   :P
I got EAX=0 :wink

dedndave

assemble as console app
        INCLUDE \masm32\include\masm32rt.inc

        .CODE

_main   PROC

        push    90
        push    36
        call    GCD
        print   ustr$(eax),13,10
        inkey
        exit

_main   ENDP

;-------------------------------------------------------------------

GCD     PROC

        push    ebp
        mov     ebp,esp
        mov     ecx,[ebp+12]  ; get m
        mov     eax,[ebp+8]   ; get n
        or      ecx,ecx
        jz      G1

        xor     edx,edx       ; prepare for division
        div     ecx           ; n/m
        push    edx           ; push new remainder as new m
        push    ecx           ; push old remainder as new n
        call    GCD

G1:     pop     ebp
        ret     8             ; clean up stack

GCD     ENDP

;-------------------------------------------------------------------

        END     _main

supposedly, m must be greater than n, but i get the same result either way - lol

qWord

common people - these is all wikipedia stuff  :bg
non-recursive:
Quotegcd proc uses ebx a:DWORD,b:DWORD

    mov eax,a
    mov ecx,b
    .while ecx
        mov ebx,ecx
        xor edx,edx
        div ecx
        mov ecx,edx
        mov eax,ebx
    .endw
    ret
   
gcd endp
FPU in a trice: SmplMath
It's that simple!

dedndave

yah - i got that
but, he wanted a recursive version   :P
the non-recursive version is much faster, i am sure
and - i could speed up the recursive code by not using EBP but that doesn't matter
if i wanted the function, i would use a loop, too

RuiLoureiro

#19
sorry i called the old LCF

qword,
      we dont need to use EBX
     
gcd1    proc m:DWORD, n:DWORD

    mov eax, m
    mov ecx, n
   
    .while ecx       
        xor     edx, edx
        div     ecx
        mov     eax, ecx
        mov     ecx, edx       
    .endw
    ret   
gcd1    endp