汇编语言实现递归阶乘算法代码分析(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

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值