语言汇编中的欧几里得GCD.代码不起作用



我正在用Language assembly编写这个欧几里得GCD程序,我想我知道问题出在哪里,但我不知道如何解决。问题是我从内部递归调用GCD,每次调用GCD时,ESP都会向下移动4个字节,因为每次调用都必须将返回地址存储在堆栈上。因此,我的EBP将从上一次调用向下指向4个字节。有人能帮我修复这个代码吗?

;Kirtan Patel
;Create a Euclidian GCD Program
;10/30/2014
.586
.MODEL FLAT
.STACK 4096
.DATA
numberm DWORD 14
numbern DWORD 10
.CODE
main PROC
         push numbern ;push 10 onto the stack
         push numberm ;push 14 onto the stack
         call gcd     ; call gcd function
         add esp, 8   ;pop off the parameters from the stack.
         ret          ;exit the program
main ENDP
gcd PROC
        push ebp      ;push ebp onto the stack to preserve previous contents of ebp 
        mov ebp, esp  ;copy esp to ebp to access the parameters 10 and 14 later on
        push edx      ;save the registers 
        push ebx
        push ecx
        mov ecx, DWORD PTR[ebp+12] ;copy 10 to ecx
        cmp ecx, 0                 ;compare to see if the divisor is zero
        jnz recur                  ;if it is not zero then recursively call gcd
        mov eax, DWORD PTR[ebp+8]  ; if it zero then copy 14 to eax and return
        pop ecx                    ;restore the contents of registers before exiting the function
        pop ebx
        pop edx
        pop ebp
        ret
recur: mov eax, DWORD PTR[ebp+8]   ;copy 14 to eax
       cdq                         ; prepare the edx register for division to store the remainder
       div ecx                     ;eax/ecx (14/10)
       mov DWORD PTR[ebp+12], edx  ;copy the remainder into numbern on the stack
       mov DWORD PTR[ebp+8], ecx   ;copy the new divisor into numberm on the stack
       pop ecx                     ;restore registers
       pop ebx
       pop edx
       pop ebp
       call gcd                    ;recursively call gcd
gcd ENDP

END

您可以在堆栈上传递参数。使用这个C程序作为递归函数的原型,并使用这里描述的技术在每次递归调用中传递参数。

int findgcd(int x,int y){
     while(x!=y){
          if(x>y)
              return findgcd(x-y,y);
          else
             return findgcd(x,y-x);
     }
     return x;
}

最新更新