当前位置:网站首页>Creation and destruction of function stack frames
Creation and destruction of function stack frames
2022-07-02 05:08:00 【桜キャンドル yuan】
The creation and destruction of function stack frames are under the environment of different compilers , The principle is basically the same .
1. register
| eax | |
| ebx | |
| ecx | |
| edx | |
| The following two registers store addresses , These two addresses are used to maintain the function stack frame | |
| ebp | Pointer at the bottom of the stack |
| esp | Top pointer of stack |
Every function call , Create a space in the stack area
We write the following code as an example :
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;
}
After executing the above code , Our stack space will be ours main Open up a space , among ebp To record our main The high address of the occupied space ,esp To record our main Low address occupied by space
The usage habit of stack area is to use high address first , Then use low address , That is to use our space below first , Then use the space above
stay VS2013 in ,main Functions are also called by other functions :
mainCRTStartup Called __tmainCRTStartup Called main function
Stack pressing is to put an element from the top of the stack push
Out of stack is to pop an element from the top of the stack pop
In our main Function before , our __tmainCRTStartup The function is already on the stack , our ebp and esp The pointer records __tmainCRTStartup High address and low address of function ( At the bottom of this picture is the high address , It's a low address )

First, we perform stack pressing :push ebp, Take us ebp Press into the top of our stack , Now our ebp We are stored in __tmainCRTStartup High address of

When we ebp After entering the stack , Our stack top pointer esp At the same time, it also moves up

mov ebp,esp
take esp The value is assigned to ebp, It is equivalent to ebp The pointer of moves up to the following figure

sub esp,0E4h
take esp subtract 0E4h,0E4h It's an octal number 228
It's equivalent to us esp Move the pointer up , As shown in the figure below , Among them, we found our esp and ebp The pointer no longer maintains the original __tmainCRTStartup Space , At this time, the purple space is actually for us main The space opened up by the function

push ebx
push esi
push edi
Put our ebx,esi,edi Three are pushed into the stack , But at the same time , We need to notice our stack top pointer esp With each stack , Point to the space at the top of the stack in time

lea edi,[ebp-0E4h]
lea yes load effective address, It means loading valid addresses . So the above code is to load the following valid address into edi Go inside
Here we give ebp Minus the 0E4h, Be careful , We have 0E4h It has appeared before , It is our main Function stack frame space , Give us the ebp subtract 0E4h Just move up to main Top of function stack frame

mov ecx,39h
take 39h Put in ecx in
mov eax,0CCCCCCCCh
take 0CCCCCCCCh Put in eax in
rep stos dword ptr es:[edi]
Will just from edi At the beginning 39h Time of dword(double word Two words , Four bytes ) All the data of are changed to 0CCCCCCCCh, That is to say, we just named main All the contents of the function are changed to CCCCCCCC

int a=10;
mov dword ptr [ebp-8]0Ah
take 0Ah This hexadecimal number , It's the decimal system 10 Put it in ebp-8 The location of

int b=20;
dword ptr [ebp-14h],14h
int c=0;
dword ptr [ebp-20h],0
Empathy

c=Add(a,b);
mov eax,dword ptr [ebp-14h]
take ebp-14h, That is to say b Put the value of eax In the middle , Also is to 20 Put in eax in
push eax
take eax Pressure into the stack
mov ecx,dword ptr [ebp-8]
take ebp-8 Put the value in ecx in , That is to put our a,10, Put in ecx in
push ecx
take ecx Push
The same as the above step , Are parameters of functions

call 00C210E1
Call function (call The address for 00C2144B), And will call The address of the next instruction of the instruction is pushed onto the stack .
This means call After the instruction is executed, it will jump to the address of the function , When you jump back from the address of the function , Direct execution call Next instruction for

Then we enter add Internal function
The following code is similar to our main Function creation is very similar
push ebp
take ebp Pressure into the stack
mov ebp,esp
take esp The value is assigned to ebp
That means ours ebp Yes esp The location of

sub esp,0CCh
to sub subtract 0CCh
That's for us add Function opens up space

push ebx
push esi
push edi
Separately ebx,esi,edi Pressure into the stack

lea edi,[ebp+FFFFFF34h]
Load valid address , take ebp+FFFFFF34h The address of is loaded into our edi in
mov ecx,33h
take 33h Deposit our ecx in
mov eax,0CCCCCCCCh
take 0CCCCCCCCh Assign a value to eax This register
rep stos dword ptr es:[edi]
Let's start with edi This position starts down to ebp All the middle values are assigned 0CCCCCCCCh

int z=0;
mov dword ptr [ebp-8],0
take 0 Put in ebp-8 The location of , That's our z

mov eax,dword ptr [ebp+8]
take ebp+8 Put the value of eax in , That's our a
add eax,dword ptr [ebp+0Ch]
Will we ebp+0Ch The value at is added to our eax In the middle , That is to put our b give a, This is a , our eax by 30
dword ptr [ebp-8],eax
After the addition , Then we eax Put the value of into us ebp-8 The location of , That's our z The address of
Here we can notice that we press the stack first b Push the , Recompression a, So we pass from right to left
That's our add(a,b) First of all b Push the , then a Press in
So formal parameters are not what we are add Created inside the function , Instead, go back to find the parameters when the parameters are transferred after the parameters are generated

return z;
mov eax,dword ptr [ebp-8]
So here return z The order of , That is to put our ebp-8 The value of is put into our eax in , That is now our 30 Put in our eax in , because z It will be destroyed after going out later , So we need to put the result of our calculation into our register for storage .()
pop edi
Pop up the elements at the top of the stack and put them into our edi In the register , At this time, the original data at the top of the stack is edi, That is to say, it will just edi It's just data reading , The same is true for the following two
pop esi
pop ebx
mov esp,ebp
take ebp Assign a value to esp, That means ours esp It points to us ebp The same place

pop ebp
Pop up the element at the top of the stack and assign it to ebp, That is our original ebp-main That is to say, now our ebp and esp Re maintain our main Function space .
ret
Before that we will call The address of the next instruction of the instruction is saved , Now we ret once , Now we have successfully found our former call Next instruction of instruction

add esp,8
esp+8 That is, let our stack top pointer move down to us b Below , That is to put the two formal parameters we have used before x,y Space destruction of

mov dword ptr [ebp-20h],eax
take eax Put the value of ebp-20h The location of , That's for us c assignment

thus , We add The creation and destruction of function stack frames are completed
边栏推荐
- 在{{}}中拼接字符
- Lay the foundation for children's programming to become a basic discipline
- DJB Hash
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
- Lm09 Fisher inverse transform inversion mesh strategy
- Cannot activate CONDA virtual environment in vscode
- Online incremental migration of DM database
- TypeScript函数详解
- VMware installation win10 reports an error: operating system not found
- 将光盘中的cda保存到电脑中
猜你喜欢

Change deepin to Alibaba image source

06 装饰(Decorator)模式
![[Yu Yue education] autumn 2021 reference materials of Tongji University](/img/50/5136359b89a5d047fe648637643ad0.jpg)
[Yu Yue education] autumn 2021 reference materials of Tongji University

面试会问的 Promise.all()

数学知识(欧拉函数)

VMware installation win10 reports an error: operating system not found

List of common bugs in software testing

el-cascader回显只选中不显示的问题

解析少儿编程中的动手搭建教程

Common errors of dmrman offline backup
随机推荐
How to configure PostgreSQL 12.9 to allow remote connections
Pytest learning ----- pytest assertion of interface automation testing
Domestic all Chinese automatic test software apifox
画波形图_数字IC
Oracle stored procedure and job task setting
How to modify data file path in DM database
Pyechats 1.19 generate a web version of Baidu map
国产全中文-自动化测试软件Apifox
Virtual machine installation deepin system
C # picture display occupancy problem
Case sharing | intelligent Western Airport
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
How to make an RPM file
2022-003arts: recursive routine of binary tree
Draw a wave chart_ Digital IC
Briefly introduce chown command
Analyzing the hands-on building tutorial in children's programming
List of common bugs in software testing
Collectors. Groupingby sort
Comp 250 parsing