当前位置:网站首页>Function stack frame (worth collecting)
Function stack frame (worth collecting)
2022-07-18 14:53:00 【luoganttcc】
Function stack frame creation and destruction
In early learning , We may have a lot of confusion ?
such as :
- How local variables are created ?
- Why are the values of local variables random ?
- How functions pass parameters ? What is the order of passing parameters ?
- What is the relationship between formal parameters and arguments ?
- How do function calls do ?
- How to return after the function call is completed ?
Today, I know how to create and destroy function stack frames , In fact, I have cultivated my internal skills , You can also understand more knowledge in the later stage .
The environment we use today is VS2013, Don't use too advanced compilers , The more advanced the compiler , The harder it is to learn and observe .
At the same time, under different compilers , There is a slight difference in the creation of stack frames in the process of function call , The details depend on the implementation of the compiler .
register
First, let's look at registers .

This pyramid is part of the computer memory , Hard disk , Memory ,cache( cache ), register
The reading speed of this picture increases from bottom to top , But the storage space decreases , And the cost is higher .
Must have known something before , Here we only introduce the register .
The register is CPU The internal components of , yes CPU The place where instructions and data are taken during operation , fast , Registers can be used to temporarily store instructions 、 Data and address . stay CPU in , There are usually general-purpose registers , Such as instruction register IR; special function register , Such as program counter PC、sp etc. .
Our common registers are eax ,ebx, ecx, edx,ebp,esp.
The last two registers are used to store addresses , These two addresses are used to Maintain function stack frame Of .
Function stack frame creation and destruction
We have learned that , Every function call , Create a space in the stack area .
So we naturally want to define functions , To facilitate observation , We try to divide the code into enough details .
-
#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;
-
}

Pictured , Functions are created on the stack , If this is main Function stack frame of function , So when the program runs to main Function time , register ebp and esp The address stored in is main The beginning and end address of the function .
Empathy , When the program runs to which function ebp and esp Just point to the space of that function , And this space is called the of this function Function stack frame .
According to the usage habit of stack space , Use the high address first and then the low address , So usually ,ebp be called Pointer at the bottom of the stack ,esp be called Top pointer of stack .
Then we debug , Look at the call stack of the function .

We found that ,main Function called , So who called it ?
Let's keep going , When the code is finished, you will find that __tmainCRTstartup This function calls main function
And then main The return value of the function is given to mainret.

At the same time ,__tmainCRTstartup Also by mainCRTstartup This function calls .

So you can roughly understand the space allocation of the program's function in the stack .

Now let's specifically analyze how to do it .
Let's press F10, Then find... In the debug window Go to disassembly Options

And then there's this interface , This is a C The corresponding assembly code in language .

In order to facilitate our observation , We turn off the display symbol name option .

The first step is push
Because execution main Function time __tmainCRTStartup Will call first main function , So register ebp and esp The first point should be __tmainCRTStartup Function space , As shown in the figure below .
and push It means to press the stack , That is, here will ebp Put it on the top of the stack , And as the top of the stack moves , At the top of the stack esp It should also be moved to the top of the stack , stay 32 The address size under bit is 4 byte , So this is also moving up ( reduce )4 byte .

mov
Here is the esp The value of ebp.

sub
According to the code , Yes, it will esp The value of minus 0E4h( Hexadecimal ), the esp Move up 0E4h.
And here ebp and esp The space contained has changed , In fact, the space here is for main Function opens up space .

push
here push Three times , take ebx,esi,edi Put it in the stack .

lea(load effictive address Load valid address ),mov,mov,rep stos

According to the code, it will ebp Minus the address of 0E4h I.e edi, Down 39h Time ( until ebp), take dword( One word by 2 Bytes ,dword by 4 byte ) All bytes are set to 0cccccccch.

The following is the official start of implementation C Valid code for language

According to the code , Yes, it will 0Ah Put it in ebp-8 The location of , And the size is 4 Bytes , It actually means for a Open up space .
And if you create a variable , Not initialized , Put the default value in the variable , That's what's going on here cccccccc, So when printing, it will be hot .

And then there's creating b Variables and c Variable
Similarly, it will 14h Put it in ebp-14h The location of ; take 0 Put it in ebp-20h The location of .

Then there's the call Add function
First, according to the code, it will ebp-14h( namely b) Put the value of eax Inside .
And then eax Pressing stack .

Empathy , take ebp-8( namely a) Put the value of ecx Inside .
And then ecx Pressing stack .

In fact, these two steps are parameter transmission .

Add The steps in the function are actually and main The creation of functions is similar to , No more repetition here .

Here is the general ebp+8(a) Put the value of eax Inside , And then ebp+0Ch(b) Value added to eax in , The final will be eax Put the value of ebp-8(z) in .
When you return, you will ebp-8(z) Put it in eax in , Because when the function ends z Will be destroyed , And the register will not be destroyed .

there pop Is to stack registers , And then ebp Assign to esp
Then continue out of the stack , Pop the top of the stack to ebp inside . And at this time The top of the stack is what was stored before main Functional ebp
pop after ebp Point to main At the bottom of the stack
ret: Jump to where the function will be called . Corresponding to call, Return to the corresponding call The next instruction called , If there is a return value , Put in eax in .
Destruction of formal parameters
the esp Move down the , send esp and ebp There is less space between , So as to destroy the formal parameters .
![]()
The final will be eax Put the value of ebp-20h(c) in .
边栏推荐
- STM32 application development practice tutorial: multi computer communication application development based on CAN bus
- One click VR panorama display
- 风控人不能不知的黑产大揭秘
- The difference between arrayslist and LinkedList
- Learning experience sharing 6: experience sharing of Dr. dry goods
- Hal firmware library
- 5. Redis architecture design to usage scenario - storage principle - data type infrastructure
- IDEA集成Gerrit插件
- 4. Redis architecture design to use scenarios - string, list, set, Zset, hash use scenarios
- 配置MaskRCNN环境吐槽(GeForce MX250+win10+tensorflow1.5.0 GPU版)
猜你喜欢

Kotlin | launch build report for kotlin compiler task

Variables in shell scripts

配置MaskRCNN环境吐槽(GeForce MX250+win10+tensorflow1.5.0 GPU版)

启动失败 Failed to determine a suitable driver class 问题解决方案

Tens of billions of data were compressed to 600gb, and tdengine was launched on GCL energy mobile energy platform

Ziguang Tongchuang FPGA development jump pit Guide (V) -- DDR3 controller IP simulation

STM32应用开发实践教程:基于 CAN 总线的多机通信应用开发
![[每周一更]-(第3期):Web开发安全注意事项](/img/2e/64e2f7aca24abd6b68d844e0b78a3e.jpg)
[每周一更]-(第3期):Web开发安全注意事项

antd a-upload 多选multiple为ture时 限制上传个数

03-GuliMall 开发环境配置
随机推荐
ViewGroup event distribution sorting
Conditional ternary operator...
Desai wisdom number - discount (gradient stacking chart): per capita disposable income of national residents
真值与条件表达式
选择语句 if else
Tens of billions of data were compressed to 600gb, and tdengine was launched on GCL energy mobile energy platform
One click VR panorama display
用swift5 新写一个app需要用到需要考虑的
Finding the median in data flow
LeetCode(剑指 Offer)- 03. 数组中重复的数字
AcWing 3433. 吃糖果 递推|找规律
The degradation mechanism is not designed properly, and the online system crashes instantly
2022T电梯修理操作证考试题及在线模拟考试
Failure of CUDA installation nsight visual studio edition failed
AcWing 3652. 最大连续子序列 动态规划
最小区间问题
Ziguang Tongchuang FPGA development jump pit Guide (V) -- DDR3 controller IP simulation
HAL 固件库
Redis is configured to save RDB snapshots, but it is currently not able to persist
配置MaskRCNN环境吐槽(GeForce MX250+win10+tensorflow1.5.0 GPU版)