0

我目前正在编写一个汇编程序来递归计算数字的阶乘。通过调试器,我看到了发生了什么。所以例如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 永远不会进入正确的位置。

4

1 回答 1

0

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

_开始:
推 4 美元,
人口 %ebx

调用 f

F:
        movl %esp,%ebp
        movl 8(%ebp), %eax
        cmpl $1, %eax  
        完成
        decl %eax      
        pushl %eax    
        调用阶乘  
        人口 %ebx                   
        包括%ebx
        imul %ebx, %eax
结束:
movl %ebp, %esp
流行 %ebp
ret
于 2012-10-15T16:35:20.243 回答