The realization of assembly language learning series function

Source: Internet
Author: User

The following code demonstrates the function of exchanging a and b two integers, the compilation environment Ubuntu14.04 (32 bits)

If you want to implement the following C code

#include <stdio.h>voidSwapint*XP,int*YP) {    intx = *XP; inty = *YP; *XP =y; *YP =x;}intMain () {intA =534, B =1057; Swap (&a, &b); printf ("%d\n", a); printf ("%d\n", B); return 0;}
    • The assembly code is as follows:
. Section. DataA:.int 534        B:.int 1057        format:. Asciz"%d\n". Section. Text.global _start_start:PUSHL%ebp movl%esp,%EBP Subl $ -,%esp #给栈分配内存24字节保存局部变量 movl $a,%eax movl%eax,4(%ESP) movl $b,%eax movl%eax, (%ESP)Pagerswap #调用swap函数 PUSHL a #打印a pushl $formatPagerprintf PUSHL b #打印b PUSHL $formatPagerprintf MOVL $0, (%ESP)PagerExitSwap:pushl%ebp #保存调用前栈帧地址 movl%esp,%ebp pushl%ebx #根据习惯, save register EBX MOVL8(%EBP),%edx #get XP MOVL A(%EBP),%ecx #get yp movl (%edx),%ebx #get x movl (%ECX),%eax #get y m OVL%eax, (%edx) Movl%ebx, (%ecx) popl %ebx popl%EBPret
    • Compile

As Swap.s-o SWAP.O

    • Link

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

    • Perform

./swap

The realization of assembly language learning series function

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.