Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and destroy function stack frames in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "C language function stack frame how to create and destroy", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "C language function stack frame how to create and destroy" this article.

Write at the front

We know that each function call needs to open up a space for it in the stack area, which is called the stack frame of this function.

The stack extends from a high address to a low address. Each call of each function has its own independent stack frame, in which all kinds of information is maintained. Register ebp points to the bottom of the current stack frame (high address), and register esp points to the top of the current stack frame (low address).

In this way, we know that register ebp and register esp hold addresses, which are used to maintain function stack frames. For example, if we call the main function and allocate the stack frame space for the main function, the stack frame maintenance is as follows:

Let's analyze the process of creating and destroying function stack frames through a piece of code: (stack frames are different in different compilers, but the ideas are generally the same. This paper is implemented under the vs2013 compiler. )

# include int Add (int x, int y) {int z = 0; z = x + y; return z;} int main (void) {int a = 10; int b = 20; int ret = 0; ret = Add (a, b); / / calculate aplb printf ("% d\ n", ret); return 0;}

We open the call stack during debugging

As you can see, the main function is called inside the _ _ tmainCRTStartup function, and the _ _ tmainCRTStartup function is called inside the mainCRTStartup function.

In order to see more clearly the process of creating and destroying stack frames, let's go to the disassembly code corresponding to the above code:

Int main (void) {009D3F40 push ebp / / push edp into stack frame 009D3F41 mov ebp,esp / / assign the value of esp to edp009D3F43 sub esp,0E4h / / esp-0E4h009D3F49 push ebx 009D3F4A push esi 009D3F4B push edi 009D3F4C lea edi, [ebp+FFFFFF1Ch] 009D3F52 mov ecx,39h 009D3F57 mov eax,0CCCCCCCCh 009D3F5C rep stos dword ptr es: [edi] int a = 10 009D3F5E mov dword ptr [ebp-8], 0Ah int b = 20politics 009D3F65 mov dword ptr [ebp-14h], 14h int ret = 0boar009D3F6C mov dword ptr [ebp-20h], 0 ret = Add (a, b) / / calculate a+b009D3F73 mov eax,dword ptr [ebp-14h] 009D3F76 push eax 009D3F77 mov ecx,dword ptr [ebp-8] 009D3F7A push ecx 009D3F7B call 009D11F9 009D3F80 add esp,8 009D3F83 mov dword ptr [ebp-20h], eax printf ("% d\ n", ret) 009D3F86 mov esi,esp 009D3F88 mov eax,dword ptr [ebp-20h] 009D3F8B push eax 009D3F8C push 9D5860h 009D3F91 call dword ptr ds: [009D9118h] 009D3F97 add esp,8 009D3F9A cmp esi,esp 009D3F9C call 009D1140 return 0 009D3FA1 xor eax,eax} 009D3FA3 pop edi 009D3FA4 pop esi 009D3FA5 pop ebx 009D3FA6 add esp,0E4h 009D3FAC cmp ebp,esp 009D3FAE call 009D1140 009D3FB3 mov esp,ebp 009D3FB5 pop ebp 009D3FB6 ret

The call of main function the creation of main function stack frame

After our understanding just now, when we are ready to call the main function, the stack frame of the function that calls the main function has been opened up.

The ebp is then pressed into the stack frame, saving the address pointing to the ebp at the bottom of the stack, while the esp points to the new top of the stack; then the value of esp is assigned to ebp, resulting in a new ebp; using esp minus a hexadecimal number 0E4H (here is the pre-opening space for the main function). Followed by three stack instructions, press ebx,esi,edi into the stack frame. After the valid address is loaded, all the pre-opened space for the main function is initialized to 0xCCCCCCCC. Finally, three local variables ameme bregory ret are created and initialized.

The calling function of Add function passes parameters

The value of b is stored in register eax, then eax is pushed into the stack; the value of an is stored in register ecx, and then ecx is pushed into the stack; here you can see that the parameters are passed from right to left. Then execute the call instruction, here is to call the Add function, at the same time press the address of the next instruction of the call instruction into the stack, and then press F11 when the call instruction is executed, and then enter the inside of the Add function.

The creation of Add function stack frame

First, press the ebp of the main () function into the stack, save the address pointing to the ebp at the bottom of the stack frame of the main () function, when the esp points to the new top position of the stack; assign the value of esp to the ebp, and generate a new ebp, that is, the ebp; of the stack frame of the Add () function subtracts a hexadecimal number 0E4H from the esp, here is the pre-opening space for the Add () function; then three stack instructions press ebx,esi,edi into the stack frame respectively. After the valid address is loaded, the 0xCCCCCCCC is initialized with all the pre-opened space for the Add function. The variable z is then created, and the result of the addition of an and b of the parameter is stored in z; finally, the result is stored in the eax register, bringing back the return value of the function through the register.

Destruction of Add function stack frame

Edi, esi, and ebx go out of the stack in turn, and esp moves down; then assigns the value of ebp to esp so that esp points to the place where ebp points; then ebp goes out of the stack and gives the contents of the stack to ebp, and at this time ebp points to the bottom of the frame of the main function stack, and finally executes the ret instruction to show the stack once, and jump to the address of the contents out of the stack, that is, the next instruction of call instruction.

Destruction of main function stack frame

The process of destroying the frame of main function stack is the same as that of Add function stack frame, so I won't repeat it here.

These are all the contents of the article "how to create and destroy C language function stack frames". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report