当前位置:网站首页>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 ~~
边栏推荐
- Spark RPC
- Text batch replacement function
- Pictures to be delivered
- Analysis of spark task scheduling exceptions
- 复杂度分析
- Leetcode 81. search rotation sort array II binary /medium
- 【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
- Spark 3.0 测试与使用
- “router-link”各种属性解释
- Transactions_ Basic demonstrations and transactions_ Default auto submit & manual submit
猜你喜欢
随机推荐
Binder初始化过程
C语言:动态内存函数
Spark troubleshooting finishing
Huawei's general card identification function enables multiple card bindings with one key
Spark TroubleShooting整理
shell脚本读取文本中的redis命令批量插入redis
Singles cup, web:web check in
Troubleshooting the slow startup of spark local programs
[正则表达式] 匹配开头和结尾
js寻找数组中的最大和最小值(Math.max()方法)
网络原理(1)——基础原理概述
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
go语言慢速入门——go运算符
Implementation of spark lazy list files
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
使用双星号代替Math.pow()
UDP message structure and precautions
【剑指offer】面试题39:数组中出现次数超过一半的数字
[正则表达式] 匹配分组
Unity3d learning note 10 - texture array









