我的堆栈没有返回正确的值,这影响了计算

  • 本文关键字:影响 计算 堆栈 返回 assembly
  • 更新时间 :
  • 英文 :


我目前正在编写一个汇编程序,以递归方式计算一个数字的阶乘。通过调试程序,我看到了正在发生的事情。例如3阶乘。

public  f              ; make sure function name is exported
f:
push    ebp          ; push frame pointer
mov     ebp, esp     ; update ebp
push    edi
mov     ecx, [ebp+8] ; gettting my parameter at p0
mov     edi, ecx     ; making a copy
cmp     edi, 1       ; check if n is greater than 0
jle     finished
dec     ecx          ; subtrack 1 frm parameter
push    ecx          ; passing new value of n to parameter p0
mov     eax, 0
call    f
mul     edi          ; multiplying n * n-1
jmp     finished2
finished:   
mov    eax, 1
finished2:  
pop    edi
mov    esp, ebp
pop    ebp
ret

假设我有3作为输入,我应该有类似的东西

eax * 3(ecx)
eax * 2(ecx)

但我注意到,在击球后回来的路上。。值2被放在正确的位置,但值3永远不会进入正确的位置。

当使用调试器时,我会使用info-reg(在gdb下)来确保寄存器是否具有正确的值

_start:pushl$4,杨树%ebx调用ff:movl%esp,%ebpmovl 8(%ebp),%eaxcmpl$1,%eaxje完成下降%eaxpushl%eax调用阶乘杨树%ebx包括%ebx模拟%ebx,%eax饰面:movl%ebp,%esp杨树%ebpret

最新更新