汇编语言实现递归阶乘算法代码分析(8)

 

 

来自于《Intel汇编语言程序设计》(第四版)第八章的代码,但是我总是感觉有错误,红色代码部分从逻辑上看永远不会被执行到,以下为源代码:

 

 

【注:因为使用的是32位寄存器,因此可以容纳的最大阶乘是12!(479001600)】

 

 

TITLE Calculating a Factorial                        ( Fact.asm )

 

INCLUDE Irvine32.inc

.code

main PROC

        push 12                   ; calc 12!

        call Factorial            ; calculate factorial (eax)

ReturnMain:

        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

           jmp L2

L1:      dec eax

           push eax               ; Factorial(n-1)

           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

 

 

为什么感觉有问题呢,红色代码什么时候才会被执行到呢?

你可能感兴趣的:(算法,UP)