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

debugger を使用する場合、info reg (gdb の下) を使用して、レジスタが正しい値を持っているかどうかを確認します。

_始める:
プッシュ $4,
popl %ebx

コール f

f:
        movl %esp,%ebp
        movl 8(%ebp), %eax
        cmpl $1、%eax  
        終了します
        decl %eax      
        pushl %eax    
        階乗を呼び出す  
        popl %ebx                   
        %ebx を含む
        imul %ebx, %eax
終了:
movl %ebp, %esp
popl %ebp
戻る
于 2012-10-15T16:35:20.243 に答える