当前位置:网站首页>C language function stack frame
C language function stack frame
2022-06-11 07:31:00 【∞ big understand】
Function stack frame
Catalog
- Function to open up memory space
- Creation of function variables
- Function arguments
- Function call
- 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;

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 :
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 :
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 :
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 :
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 :

This is mian Function memory development .
Creation of function variables

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 .
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

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 .
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 :
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

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 .
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 .
边栏推荐
- C language volatile
- Decimal to binary
- 【AtCoder1998】Stamp Rally(整体二分+并查集)
- Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
- MS office level II wrong question record [10]
- C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
- 1、 Sqlserver2008 installation (with password), database creation, C form project test
- Multi thread review summary parsing volatile keyword
- MFC custom string linked list
- Summary of written test questions of shopee 2021 autumn recruitment
猜你喜欢

如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
![[Oracle database] mammy tutorial day03 Sorting Query](/img/ea/24c9495a2ef4f1786f7b7852bde321.png)
[Oracle database] mammy tutorial day03 Sorting Query

May 30-June 5, 2022 AI industry weekly (issue 100): three years
![20200803 T3 my friends [divide and conquer NTT optimization recursion]](/img/35/01201e3136e3dd5cd562a0481f1ee9.jpg)
20200803 T3 my friends [divide and conquer NTT optimization recursion]

The rotation of the earth and the moon (II)

【IoT】智能硬件:如何获取硬件产品的wifi信号强度

Classification of MNIST datasets with keras

big.js--使用/实例

10 advanced concepts that must be understood in learning SQL

Qstring to hexadecimal qstring
随机推荐
【AtCoder2306】Rearranging(拓扑)
Compound RateModel合约解析
@Jsonproperty annotation
MS office level II wrong question record [8]
如何准备PMP新版大纲考试?
Regular Expression Matching
P5431 [template] multiplicative inverse 2
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
Calculate the day of the week for a specific month, year and day
群晖DS918创建m.2 固态硬盘SSD读写缓存
QObject usage skills -- control function class
正则表达式匹配
Android and IOS reverse analysis / security detection / penetration testing framework
【CodeForces1019E】Raining season(边分治+斜率优化)
SQLZOO刷题记录-3
Paper size and book size
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
【AtCoder2305】Decrementing(博弈)
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]