当前位置:网站首页>C language: function stack frame
C language: function stack frame
2022-07-27 15:45:00 【FA FA is a silly goose】
1. What is a function stack frame
stay C In language , Every running function has a stack frame corresponding to it , The return address and local variables of the function are stored in the stack frame . Logically speaking , Stack frame is a function execution environment : Function parameter 、 A local variable of a function 、 Where to return after the function is executed, and so on .
2. Function stack frame creation and destruction

When the function is called , It will open up a space on the stack for this function , During the operation of this function , register ebp Save the address at the bottom of the stack , register esp Save the stack top address . And it should be clear ,esp and ebp Registers can store only one address at a time , therefore , anytime , This pair of pointers will all point to the stack frame structure of the same function . also ebp Generally, the system changes its value , and esp It will move with the data in and out of the stack , in other words esp Always point to the top of the stack . Let's take an example :
#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;
}
The following code is main The corresponding disassembly code in the function :
int main()
{
00B118B0 push ebp //ebp Pressing stack
00B118B1 mov ebp,esp // take esp The value is assigned to ebp
00B118B3 sub esp,0E4h // take esp The value of minus 0E4h, to main Function stack frame allocation space
00B118B9 push ebx // Pressing stack
00B118BA push esi // Pressing stack
00B118BB push edi // Pressing stack
00B118BC lea edi,[ebp-24h]
00B118BF mov ecx,9
00B118C4 mov eax,0CCCCCCCCh
00B118C9 rep stos dword ptr es:[edi] // from edi From start to down 9 The values of all spaces are changed to eax, namely 0CCCCCCCCh
00B118CB mov ecx,0B1C003h
00B118D0 call 00B1131B // Get into main function
int a = 10;
00B118D5 mov dword ptr [ebp-8],0Ah //ebp-8 by a The location of , take a The value of is assigned to 10
int b = 20;
00B118DC mov dword ptr [ebp-14h],14h //ebp-14h by b The location of , take b The value of is assigned to 20
int c = 0;
00B118E3 mov dword ptr [ebp-20h],0 // ditto
c = Add(a, b);
00B118EA mov eax,dword ptr [ebp-14h] // The process of transferring parameters , First the b The value of is passed to eax
00B118ED push eax // then eax Pressing stack
00B118EE mov ecx,dword ptr [ebp-8] // The ginseng , First the a The value of is passed to ecx
00B118F1 push ecx // then ecx Pressing stack
00B118F2 call 00B110B4
00B118F7 add esp,8
00B118FA mov dword ptr [ebp-20h],eax
printf("%d\n", c);
00B118FD mov eax,dword ptr [ebp-20h]
00B11900 push eax
00B11901 push 0B17B30h
00B11906 call 00B110D2
00B1190B add esp,8
return 0;
00B1190E xor eax,eax
}
Here is Add Disassembly code of function :
int Add(int x, int y)
{
00B11770 push ebp // Record the last ebp Value
00B11771 mov ebp,esp // assignment
00B11773 sub esp,0CCh
00B11779 push ebx
00B1177A push esi
00B1177B push edi
00B1177C lea edi,[ebp-0Ch]
00B1177F mov ecx,3
00B11784 mov eax,0CCCCCCCCh
00B11789 rep stos dword ptr es:[edi] // And main The function is the same , from edi Start down 3 All units are assigned CCC
00B1178B mov ecx,0B1C003h
00B11790 call 00B1131B
int z = 0;
00B11795 mov dword ptr [ebp-8],0
z = x + y;
00B1179C mov eax,dword ptr [ebp+8]
00B1179F add eax,dword ptr [ebp+0Ch] // Do addition calculation
00B117A2 mov dword ptr [ebp-8],eax
return z;
00B117A5 mov eax,dword ptr [ebp-8]
}
The stack frame destruction process is as follows :
00B117A8 pop edi // Out of the stack
00B117A9 pop esi // Out of the stack
00B117AA pop ebx // Out of the stack
00B117AB add esp,0CCh // The destruction Add function
00B117B1 cmp ebp,esp
00B117B3 call 00B11244 // go back to main function
00B117B8 mov esp,ebp
00B117BA pop ebp // Out of the stack
00B117BB ret
Tell the truth , My expression ability is limited , In addition, there are too many underlying contents involved , Is difficult , There will inevitably be mistakes and inappropriate places , Welcome to correct ~~
边栏推荐
- Use deconstruction to exchange the values of two variables
- 使用Prometheus监控Spark任务
- Leetcode 781. rabbit hash table in forest / mathematical problem medium
- 设置提示框位置随鼠标移动,并解决提示框显示不全的问题
- 实现自定义Spark优化规则
- 《吐血整理》C#一些常用的帮助类
- UDP message structure and precautions
- [正则表达式] 单个字符匹配
- Use double stars instead of math.pow()
- [0 basic operations research] [super detail] column generation
猜你喜欢
随机推荐
Interview focus - TCP protocol of transport layer
Spark troubleshooting finishing
【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Spark 任务Task调度异常分析
shell脚本读取文本中的redis命令批量插入redis
Spark动态资源分配的资源释放过程及BlockManager清理过程
Network equipment hard core technology insider router 20 dpdk (V)
UDP message structure and precautions
Spark 3.0 DPP implementation logic
[正则表达式] 匹配分组
Explanation of various attributes of "router link"
数组名是首元素地址吗?
Unity3d learning note 10 - texture array
Go language slow start -- go operator
Use double stars instead of math.pow()
文字批量替换功能
Binder initialization process
C:浅谈函数
Go language slow start - Basic built-in types
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3









