When a function is called, the creation and destruction of the stack frame

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)
write picture description here

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.
write picture description here
write picture description here

Step 2: Prepare the call to the Add function

Corresponding disassembly code (Part II)

write picture description here

Enter the Add function

Corresponding disassembly code (Part 3)

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326525995&siteId=291194637