源代码如下:
#include <stdio.h> int add(int x,int y) { int t = x + y; return t; } int main(void) { int x=4, y=1; return add(x,y); }
经过 gcc -S hello.c
产生汇编代码如下:
add: pushl %ebp movl %esp, %ebp subl $16, %esp movl 12(%ebp), %eax movl 8(%ebp), %edx leal (%edx,%eax), %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax leave ret main: pushl %ebp //其实有两步,subl $4 %esp,mov %ebp (%esp),保存当前的栈低 movl %esp, %ebp //得到新栈低,将当前栈顶赋予栈低 subl $24, %esp movl $4, -4(%ebp) //x入栈 movl $1, -8(%ebp) //y入栈 movl -8(%ebp), %eax movl %eax, 4(%esp) //实参x入栈 movl -4(%ebp), %eax movl %eax, (%esp) //实参y入栈 call add leave ret
若采用优化 gcc -O2 -S hello.c
产生汇编代码如下:
add: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax popl %ebp ret main: pushl %ebp //保存当前栈低 movl $5, %eax movl %esp, %ebp //得到新栈低 popl %ebp //过程调用结束,恢复旧栈低 ret