Implementation of recursive factorial algorithm code analysis in assembly language (8)

Source: Internet
Author: User

The code is from Chapter 8 of Intel assembly language programming (version 4), but I always feel that there are errors. The red code will never be executed logically. The following is the source code:

 

 

[Note: Because 32-bit registers are used, the maximum factorial that can be accommodated is 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

; Es: [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

 

 

Why is there a problem? When will the red code be executed?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.