이 유클리드 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
코드를 사용하면 코드가 도움이 될 수 있다고 생각하는 것 같습니다. –
그래, 내가 그것을 논평하려고하자. 감사합니다 – Koolkirtzz
@ JoachimIsaksson 나는 그것을 주석. – Koolkirtzz