Creation and destruction of stack frames
Here we write a simple addition function to analyze:
#include<stdio.h>
int Add(int x,int y)
{
int z=0;
z=x+y;
return z;
}
int main()
{
int a=10;
int b=20;
int c=0;
c=Add(a,b);
printf("%d\n",c);
system("pause");
return 0;
}
To study the calling process of the function, we need to use the corresponding assembly code, see the figure below (Part 1)
Step 1: Create a stack frame for the main function
First, the maintenance of the stack frame requires two registers, ebp and esp. When
calling a function, ebp points to the bottom of the stack, and esp points to the top of the stack.
Step 2: Prepare the call to the Add function
Corresponding disassembly code (Part II)
Enter the Add function
Corresponding disassembly code (Part 3)