1. redirect, preprocess
gcc -E test_asm.c -o test_asm.i
2. to asm
gcc -S test_asm.i -o tset_asm.s
3. to obj
gcc -c test_asm.s -o test_asm.o
4. link
gcc test_asm.o -o test_asm
=====
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc -E test_asm.c -o test_asm.i
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc -S test_asm.i -o test_asm.s
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc -c test_asm.s -o test_asm.o
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc test_asm.o -o test_asm
fred@fred-Vostro-2420:~/myspace/sets/test$ ./test_asm
i=6, j=5
fred@fred-Vostro-2420:~/myspace/sets/test$
1) .c
fred@fred-Vostro-2420:~/myspace/sets/test$ more test_asm.c
#include<stdlib.h>
#include<stdio.h>
int swap(int *a, int *b)
{
int middle = *b;
*b = *a;
*a = middle;
return 0;
}
int main(int argc, char *argv[])
{
int i = 5;
int j;
j = 6;
swap(&i, &j);
printf("i=%d, j=%d\n", i, j);
return 0;
}
fred@fred-Vostro-2420:~/myspace/sets/test$
2) .asm
fred@fred-Vostro-2420:~/myspace/sets/test$ more test_asm.s
.file "test_asm.c"
.text
.globl swap
.type swap, @function
swap:
.LFB2:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $16, %esp
movl 12(%ebp), %eax
movl (%eax), %eax
movl %eax, -4(%ebp)
movl 8(%ebp), %eax
movl (%eax), %edx
movl 12(%ebp), %eax
movl %edx, (%eax)
movl 8(%ebp), %eax
movl -4(%ebp), %edx
movl %edx, (%eax)
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE2:
.size swap, .-swap
.section .rodata
.LC0:
.string "i=%d, j=%d\n"
.text
.globl main
.type main, @function
main:
.LFB3:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $32, %esp
movl $5, 24(%esp)
movl $6, 28(%esp)
leal 28(%esp), %eax
movl %eax, 4(%esp)
leal 24(%esp), %eax
movl %eax, (%esp)
call swap
movl 28(%esp), %edx
movl 24(%esp), %eax
movl %edx, 8(%esp)
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE3:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
.section .note.GNU-stack,"",@progbits
==
b 12(%ebp)
a 8(%ebp)
call swap--------------------------------------like push EIP
(
ebp
local
)
...swap(a,b)
{
push ebp
movl esp, ebp
...
leave(ebp,esp)
ret(->EIP)-------------------------------------like pop EIP, coordinate with CALL swap.
}