簡體   English   中英

遞歸匯編程序

[英]Recursive Assembly Program

以下代碼直接來自本書:

 INCLUDELIB C:\Irvine\Kernel32.lib
 INCLUDELIB C:\Irvine\Irvine32.lib
 INCLUDE C:\Irvine\Irvine32.inc

 .code
 main PROC
     push 5             ; calc 5!
     call Factorial     ; calculate factorial (EAX)
     call WriteDec      ; display it
     call Crlf
     exit
 main ENDP

 ;----------------------------------------------------------
 Factorial PROC
 ; Calculates a factorial.
 ; Receives: [ebp+8] = n, the number to calculate
 ; Returns: eax = the factorial of n
 ;----------------------------------------------------------
 push ebp
mov ebp,esp
mov eax,[ebp+8]     ; get n
cmp eax,0                 ; n > 0?
ja L1                ; yes: continue
mov eax,1                 ; no: return 1 as the value of 0!
jmp L2               ; and return to the caller

L1: dec  eax             ; Factorial(n-1)
push eax
call Factorial

; Instructions from this point on execute when each
; recursive call returns.

ReturnFact:
mov ebx,[ebp+8]     ; get n
mul ebx              ; EDX:EAX = EAX * EBX

L2: pop ebp         ; return EAX
ret 4                ; clean up stack
Factorial ENDP
END main

現在,當我去調試代碼時,它不起作用。 EAX中的值最終為78,而值為5! 是120。我需要將某些寄存器初始化為0還是缺少更大的寄存器? 朝正確方向輕推將不勝感激。 謝謝!

沒關系,我想通了(感覺很愚蠢)。 該值以十六進制格式而不是十進制格式返回。 十六進制的78轉換為十進制的120,因此程序運行正常。 對不起(不必要的)帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM