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

时间:2012-10-07 17:00:02

标签: 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从未进入正确的位置。

1 个答案:

答案 0 :(得分:0)

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

_start:
pushl $4, 
popl %ebx

call f

f:
        movl %esp,%ebp
        movl 8(%ebp), %eax 
        cmpl $1, %eax  
        je finish
        decl %eax      
        pushl %eax    
        call factorial  
        popl %ebx                   
        incl %ebx 
        imul %ebx, %eax
finish:
movl   %ebp, %esp
popl %ebp
ret
相关问题