当前位置:网站首页>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
边栏推荐
- LeetCode 1175. 质数排列(质数判断+组合数学)
- JS interview collection test question 1
- paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
- The underlying principle of go map (storage and capacity expansion)
- 解决:代理抛出异常错误
- There are duplicate elements in leetcode. Go implementation
- ansible安装与使用
- 培养中小学生对教育机器人的热爱之心
- Pyechats 1.19 generate a web version of Baidu map
- Leetcode 18 problem [sum of four numbers] recursive solution
猜你喜欢

VMware installation win10 reports an error: operating system not found

Pyechats 1.19 generate a web version of Baidu map

el form 表单validate成功后没有执行逻辑
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

How to configure PostgreSQL 12.9 to allow remote connections

洛谷入门3【循环结构】题单题解

Rhcsa --- work on the fourth day

黑马笔记---Map集合体系

Cubemx DMA notes

培养中小学生对教育机器人的热爱之心
随机推荐
Typescript function details
C # picture display occupancy problem
在{{}}中拼接字符
Map in JS (including leetcode examples)
Use of typescript classes
[Yu Yue education] autumn 2021 reference materials of Tongji University
将光盘中的cda保存到电脑中
TypeScript类的使用
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
06 装饰(Decorator)模式
VMware installation win10 reports an error: operating system not found
7.1 simulation summary
奠定少儿编程成为基础学科的原理
Rhcsa --- work on the third day
Mapping location after kotlin confusion
数学知识——快速幂的理解及例题
Knowledge arrangement about steam Education
删除排序数组中的重复项go语言实现
Pyechart1.19 national air quality exhibition
Video cover image setting, put cover images into multiple videos in the simplest way