c语言函数调用全过程
2016-02-25 18:22
423 查看
函数栈桢:
从高地址到低地址依次是:
函数参数
返回地址
ebp(ebp始终指向栈中存放eip的地址减去4的地址处。此处的eip指的调用函数的的返回地址。)
局部对象
下面分析一段代码的过程:
int g(int x,int y)
{
int c=1;
return x +y+ c;
}
int f(int x)
{
int a=5;
int b=4;
return g(a,b);
}
int main(void)
{
return f(8) + 1;
}
对应的汇编代码(去掉链接信息):
g:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
f:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
main:
pushl %ebp
call f: 等价于 push %eip; mov f,%eip; (返回地址入栈,跳转执行)
enter :pushl %ebp; movl %esp, %ebp(函数栈桢入栈)
leave:等价于 movl %ebp,%esp; pop %ebp;恢复调用函数esp,栈桢出栈
ret:等价于 mov (%esp), %eip ;popl %eip;(恢复eip,出栈)
函数调用过程:
首先函数参数入栈:
对应代码:
subl 4,movl4, %esp
movl 8, (%esp)
接着:函数返回地址入栈
call f;
接着:ebp入栈(好处:函数参数可以通过ebp取)
如取最左边参数:就是8(%ebp)
最后函数局部对象入栈;
从高地址到低地址依次是:
函数参数
返回地址
ebp(ebp始终指向栈中存放eip的地址减去4的地址处。此处的eip指的调用函数的的返回地址。)
局部对象
下面分析一段代码的过程:
int g(int x,int y)
{
int c=1;
return x +y+ c;
}
int f(int x)
{
int a=5;
int b=4;
return g(a,b);
}
int main(void)
{
return f(8) + 1;
}
对应的汇编代码(去掉链接信息):
g:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $1, -4(%ebp) movl 12(%ebp), %eax movl 8(%ebp), %edx addl %eax, %edx movl -4(%ebp), %eax addl %edx, %eax leave ret
f:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $5, -4(%ebp) movl $4, -8(%ebp) movl -8(%ebp), %eax movl %eax, 4(%esp) movl -4(%ebp), %eax movl %eax, (%esp) call g leave ret
main:
pushl %ebp
subl $4, %esp movl $8, (%esp) call f addl $1, %eax leave ret
call f: 等价于 push %eip; mov f,%eip; (返回地址入栈,跳转执行)
enter :pushl %ebp; movl %esp, %ebp(函数栈桢入栈)
leave:等价于 movl %ebp,%esp; pop %ebp;恢复调用函数esp,栈桢出栈
ret:等价于 mov (%esp), %eip ;popl %eip;(恢复eip,出栈)
函数调用过程:
首先函数参数入栈:
对应代码:
subl 4,movl4, %esp
movl 8, (%esp)
接着:函数返回地址入栈
call f;
接着:ebp入栈(好处:函数参数可以通过ebp取)
如取最左边参数:就是8(%ebp)
最后函数局部对象入栈;
相关文章推荐
- C语言实现链表节点的删除
- 保存变量值到.txt文本中
- 1046. Shortest Distance (20)
- 【转载】C/C++字符串操作函数
- C,C++宏中#与##的学习记录
- 使用cpplint检测代码规范
- C++ PIMPL模式
- java unicode c++ socket
- 视频的中断退出
- C++ explicit关键字
- 图书管理系统 C语言
- 使用c++实现一个FTP客户端(二)
- 运行时runtime
- 48行计算24点C语言代码(转载)
- 九度题目1061:成绩排序
- ios的c语言基础
- 5个月iOS开发入门总结(C++转行iOS)
- Duilib vlc c++ 字符编码
- c++的类在jsb中的内存管理
- 1045. Favorite Color Stripe (30)