Source
Very simple C language code, the function is to exchange two number:
1 #include <stdio.h>23void swap (intint * b) { 4 *a = *a + *b-(*b = *a); 5 return ; 6 }
Assembly Code Parsing
executed in the GCC compilation environment, the Gcc-s-o test.s test.c command generates the relevant assembly code.
1. file"test.c"2 . Text3 . Globl _swap4. def _swap;. SCL 2; . type 32; . Endef5 _swap:6 LFB6:7 . Cfi_startproc8 PUSHL%EBP9. cfi_def_cfa_offset8Ten. cfi_offset5, -8 One movl%esp,%EBP A. cfi_def_cfa_register5 -Movl8(%EBP),%eax--place the address of a point in the register EAX - MOVL (%EAX),%edx--Take the value of a to the address out of the register edx theMovl A(%EBP),%eax--place the address pointed by B in the register EAX - MOVL (%EAX),%eax--Take out B to point to the value in the address in the register EAX - Leal (%EDX,%EAX),%ECX-Calculates the value of *a + *b and registers the register ECX -Movl8(%EBP),%eax--puts the address pointed to a eax + MOVL (%EAX),%edx--Take out the value of a pointing address into edx -Movl A(%EBP),%eax--puts the address that B points to eax +Movl%edx, (%EAX)--use a to point to the value in the address to overwrite the value of the address that B points to.PS:(*b = *a) AMovl A(%EBP),%eax--puts the address that B points to eax atMOVL (%EAX),%eax--puts the value of B in the address into EAX (PS:The value stored in the address at which B points to is a) - Subl%eax,%ECX--with the value of *a+*b minus B points to the value in the address (PS: At this point the value of the address of B is the initial *a), the result (the result is equal to the initial B pointer to the value *B) exists in ECX - movl%ecx,%edx--putting the final result in edx -Movl8(%EBP),%eax--place a pointer address into EAX
- movl%edx, (%EAX)--To change the value of a pointer to the final result in edx PS: At this point a pointer to the value of the initial *b,b pointer to the value of the initial point of the *a,a,b pointer to the result has been reversed. - NOP in popl%EBP -. cfi_restore5 to. CFI_DEF_CFA4,4 + ret - . Cfi_endproc the LFE6: *. ident"GCC: (GNU) 4.8.1"
Parsing the assembly code behind the C language