Implementation of functions of the Assembly Language Learning Series and assembly language functions
The following code is used to demonstrate functions that exchange integers a and B. The compiling environment is Ubuntu14.04 (32-bit)
If you want to implement the following C code
#include <stdio.h>void swap(int *xp, int *yp){ int x = *xp; int y = *yp; *xp = y; *yp = x;}int main(){ int a = 534, b = 1057; swap(&a, &b); printf("%d\n", a); printf("%d\n", b); return 0;}
- The Assembly Code is as follows:
. Section. data :. int 534 B :. int 1057 format :. asciz "% d \ n ". section. text. global _ start_start: pushl % ebp movl % esp, % ebp subl $24, % esp # allocate 24 bytes of memory to the stack to save the local variables movl $ a, % eax movl % eax, 4 (% esp) movl $ B, % eax movl % eax, (% esp) call swap # call the swap function pushl a # print a pushl $ format call printf pushl B # print B pushl $ format call printf movl $0, (% esp) call exit swap: pushl % ebp # Save the pre-call stack frame address movl % esp, % ebp pushl % ebx # Save the register ebx movl 8 (% ebp) according to your habits ), % edx # get xp movl 12 (% ebp), % ecx # get yp movl (% edx), % ebx # get x movl (% ecx ), % eax # get y movl % eax, (% edx) movl % ebx, (% ecx) popl % ebx popl % ebp ret
As swap. s-o swap. o
Ld-lc-I/lib/ld-linux.so.2 swap. o-o swap
./Swap