2014-10-30 2 views
0

이 유클리드 GCD 프로그램을 언어 어셈블리에 쓰고 있는데 문제가 무엇인지 알 것 같지만 문제를 해결하는 방법을 모르겠습니다. 문제는 내가 GCD를 재귀 적으로 호출하고 GCD를 호출 할 때마다 ESP가 4 바이트 아래로 이동한다는 것입니다. 각 호출마다 스택에 반환 주소를 저장해야하기 때문입니다. 따라서 내 EBP는 이전 호출에서 4 바이트를 가리 킵니다. 누군가이 코드를 수정하도록 도와 줄 수 있습니까?유클리드 GCD (언어 어셈블리). 코드가 작동하지 않습니다

;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 
+0

코드를 사용하면 코드가 도움이 될 수 있다고 생각하는 것 같습니다. –

+0

그래, 내가 그것을 논평하려고하자. 감사합니다 – Koolkirtzz

+0

@ JoachimIsaksson 나는 그것을 주석. – Koolkirtzz

답변

0

스택에 매개 변수를 전달할 수 있습니다. this C program을 재귀 함수의 프로토 타입으로 사용하고 described here 기술을 사용하여 각 재귀 호출에서 매개 변수를 전달하십시오.

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; 
} 
+0

의 내용을 반환합니다. 유클리드 알고리즘을 이해합니다. 내 유일한 문제는 모든 재귀 호출에서 이전 호출의 리턴 주소가 스택에 추가되므로 numbern에 액세스하고 numberm 매개 변수가 주 방법으로 스택에 푸시 될 수 없다는 것입니다. – Koolkirtzz

+0

[스택에 매개 변수를 올바르게 전달하는 방법에 대한 링크 된 게시물] (https://courses.engr.illinois.edu/ece390/books/artofasm/CH11/CH11-4.html)을 읽으셨습니까? –

+0

나는 그것을 읽었지만 거기에는 내가 이해하지 못했던 정보가있다. – Koolkirtzz

관련 문제