代码
点击查看代码
#include <iostream>
using namespace std;
int sum(int a,int b){
int temp=0;
temp= a +b;
return temp;
}
int main(){
int a =10;
int b=20;
int ret =sum(a,b);
return 0;
}
以上代码考虑2个问题:
问题1:main函数调用sum,sum执行完成以后,怎么知道回到哪个函数中?
问题2:sum函数执行完后,回到main后,怎么知道从哪一行指令继续执行的?
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
函数参数压栈
//------------------------------------------------------------------------------------------------------------------
函数参数压栈
//------------------------------------------------------------------------------------------------------------------
下一条汇编指令地址入栈
//------------------------------------------------------------------------------------------------------------------
main函数栈底指针入栈
//------------------------------------------------------------------------------------------------------------------
移动ebp,进入被调函数
//------------------------------------------------------------------------------------------------------------------
为sum函数开辟函数栈帧
//------------------------------------------------------------------------------------------------------------------
将返回值放入寄存器
//------------------------------------------------------------------------------------------------------------------
回退esp栈指针
//------------------------------------------------------------------------------------------------------------------
将ebp指回main函数栈底
//------------------------------------------------------------------------------------------------------------------
取出下一条执行的汇编指令地址并执行
//------------------------------------------------------------------------------------------------------------------
此时 esp ,ebp 又指向了main函数的栈底指针和栈顶指针
//------------------------------------------------------------------------------------------------------------------
评论(0)