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.