Recursive call assembly code analysis

Source: Internet
Author: User

Today, I re-read the recursive chapter in "understanding computer systems in depth" and found that the disassembly code in the book is different from that in my own machine,

The difference is that there are multiple push operations in recursive assembly code in the book, but not in the assembly code of the local machine.

The C code is as follows:

Int fib_rec (int n) <br/>{< br/> int prev_val, Val; </P> <p> If (n <2) <br/> return 1; </P> <p> prev_val = fig (n-2); <br/> val = fig (n-1 ); </P> <p> return prev_val + val; <br/>}

 

The local assembly code is as follows:

Fib_rec: <br/> pushl % EBP <br/> movl % ESP, % EBP <br/> subl $24, % ESP <br/> CMPL $1, 8 (% EBP) <br/> JG. l2 <br/> movl $1,-20 (% EBP) <br/> JMP. l3 <br/>. l2: <br/> movl 8 (% EBP), % eax <br/> subl $2, % eax <br/> movl % eax, (% ESP) <br/> call maid <br/> movl % eax,-8 (% EBP) <br/> movl 8 (% EBP ), % eax <br/> subl $1, % eax <br/> movl % eax, (% ESP) <br/> call fig <br/> movl % eax, -4 (% EBP) <br/> movl-4 (% EBP), % edX <br/> movl-8 (% EBP ), % eax <br/> addl % edX, % eax <br/> movl % eax,-20 (% EBP) <br/>. l3: <br/> movl-20 (% EBP), % eax <br/> Leave <br/> RET

 

Assembly code in the book:

 Fib_rec: <br/> pushl % EBP <br/> movl % ESP, % EBP <br/> subl $16, % ESP <br/> pushl % ESI <br/> pushl % EBX <br/> movl 8 (% EBP), % EBX <br/> CMPL $2, % EBX <br/> jle. l24 <br/> addl $-12, % ESP <br/> Leal-2 (% EBX ), % eax <br/> pushl % eax <br/> call fig <br/> movl % eax, % ESI <br/> addl $-12, % ESP <br/> Leal-1 (% EBX), % eax <br/> pushl % eax <br/> call fib_rec <br/> addl % ESI, % eax <br/> JMP. l25 <br/>. l24: <br/> movl $1, % eax <br/>. l25: <br/> Leal-24 (% EBP), % ESP <br/> popl % EBX <br/> popl % ESI <br/> movl % EBP, % ESP <br/> popl % EBP <br/> RET

 

Movl % eax, (% ESP) is to put % eax (parameter n) to the top of the stack, completed the parameter pressure stack operation.

 

Compared with the assembly code in the book,

1. Fewer registers are used;

2. Fewer commands.

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.