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
As Swap.s-o SWAP.O
Ld-lc-i/lib/ld-linux.so.2 Swap.o-o Swap
./swap
The realization of assembly language learning series function