当前位置:网站首页>C language function stack frame

C language function stack frame

2022-06-11 07:31:00 ∞ big understand

Function stack frame

Catalog

  1. Function to open up memory space
  2. Creation of function variables
  3. Function arguments
  4. Function call
  5. Function returns the memory space

1, Function opens up space

We all know that the program starts with main Started , So first place main Function to open up memory , Actually in main Before the function, you also need to call main function , There is not much to say here .

Open up memory space , Two registers are required to maintain , Namely esp and ebp, The two of them are main The beginning and end of the function memory space .

Write a little program , Help you understand

#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);
	return 0;

 Insert picture description here

This disassembly code is to open up space for memory , Let's step by step analyze .

First , Not invoked main The function is shown in the figure before :
 Insert picture description here

perform push ebp

push Is to press the stack on the top of the stack , Pressing the stack also means pointing to the top of the stack esp I also walked up one , Pictured :
 Insert picture description here
perform mov ebp,esp

mov Will be The value after the comma is assigned to the front , So is ebp=esp, So now ebp and esp Pointing to the same location . Pictured :
 Insert picture description here
perform sub esp,0E4h

sub Is the previous value , become Minus the comma Value , therefore esp Will go up 0E4h A unit of , The address is used from high to low , This opens up main Of memory space . Pictured :
 Insert picture description here
Next are three push At the top of the stack ebx,esi,edi.

perform lea edi,[ebp-0E4h]

lea Instructions and mov Some similar , Pay attention to the difference ,mov Is to assign the content to the front ,lea Is a valid address , Assign to front .

And then there was Two mov Give to separately ecx and eax Save the content ,ecx Record the number of cycles ,eax The initialization content is installed .

Next, notice the execution rep stos dword ptr es:[edi]

This operation is to initialize the contents of the memory space , take edi To ebp All contents of are initialized to 0cccccch. The effect is as shown in the picture :

 Insert picture description here

This is mian Function memory development .

Creation of function variables

 Insert picture description here

This assembly code is the creation of function variables .

dword ptr [a] It means to access the pointer ,[] The address in the . So use mov The command assigns the value of the variable . You can see where the address of the variable is .
 Insert picture description here
You can see it clearly , stay ebp-8 The location of the creates a variable a, It should be noted that , Not all functions are in ebp-8 To create the first variable , It depends on the compiler .

Function arguments

 Insert picture description here
This disassembly code will a,b The values of are stored in registers respectively ecx and eax, Then press the stack to the top . Careful Lao tie can find ,ebp-14h Namely c The address of ,ebp-8 yes b The address of .
 Insert picture description here
Next is call 001211EA , Just put this address in the stack area .

In fact, it has been passed on now , Two parameters are stored in the register , Its address is ebp-8, and ebp-14h.

Function call

In this program , Is to call add function , The assembly code is as follows :
 Insert picture description here
It is easy to find that the first part is function add Open up memory space . Then you create the variables z. Then there is the implementation of addition , It can be seen that it is the use of eax To calculate , And then I'll talk about eax The value is assigned to z, because ebp The value of a esp So the expression of the address of the parameter is different from that before . Then return z Value .

It can be seen that z The value of is stored in the register eax in .

Function to return memory space

 Insert picture description here
This is a add Destruction of functions ,pop and push contrary , It is to pop up the elements at the top of the stack , also esp Point down one unit . Pop up the top of the stack edi,esi,ebx, take ebp The value is assigned to esp therefore esp and ebp All point to add The bottom of the function , Again pop ebp, perform ret Back to .
 Insert picture description here
esp+8 It is the destruction of those two formal parameters , From here we can see that a formal parameter is a temporary copy of an argument . This is complete add Function to return memory .

Summary

You can write a small program , Debug it yourself , It can help understand function stack pressing . Add more ,ret Is to return the address of the next cell , stay main Functional call We put in an address in the , That address is ret The address to return . So the logic is very rigorous . And in add The function comes first push One. ebp This is preservation main Functional ebp, That's why ,pop ebp, It will return to main The reason at the bottom of the function .

原网站

版权声明
本文为[∞ big understand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020520334900.html