If the assembly language to achieve the following C language functions, the compilation environment Ubuntu14.04 (32-bit).
#include <stdio.h>intRefact (intN) { if(n = =1) return 1; Else returnn * refact (N-1);}intMain () {intA =4; printf ("%d\n", Refact (a)); return 0;}
For both recursive and cyclic implementations, the assembly is converted to a jump statement implementation. You can convert the above code to
refact: if ((n10) gotodone; body-statement done:
. Section. DataA:.int 4 format:. Asciz"%d\n". Section. Text.global _start_start:PUSHL%ebp movl%esp,%EBP Subl $8,%ESP #allocate storage space movl A,%edx #get a movl%edx, (%ESP) #save value of a on S TackPagerrefact pushl%eax pushl $formatPagerprintf MOVL $0, (%ESP)PagerExitrefact:PUSHL%ebp movl%esp,%EBP pushl%ebx Subl $4,%ESP #allocate storage space MOVL8(%EBP),%EBX #get n Cmpl $1,%EBXJleDone #Test(n1) >=0Leal-1(%EBX),%eax # (n1) Movl%eax, (%ESP) #save (n-1) on the stackPagerrefactImul%ebx,%eax #use%eax to save resultDone :Addl $4,%esp #release space popl%ebx popl%ebpret
As Refact.s-o REFACT.O
Ld-lc-i/lib/ld-linux.so.2 Refact.o-o Refact
./refact
Recursive implementation of assembly language learning series