Recursive implementation of assembly language learning series

Source: Internet
Author: User

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:
    • Assembly Code REFACT.S
. 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        
    • Compile

As Refact.s-o REFACT.O

    • Link

Ld-lc-i/lib/ld-linux.so.2 Refact.o-o Refact

    • Perform

./refact

Recursive implementation of assembly language learning series

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.