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 understand the function stack frame of C language

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

Share

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

This article introduces the relevant knowledge of "how to understand the function stack frame of C language". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

I. Creation of function stack frame 1. Register

Generally speaking, there are six kinds of registers in computers.

Eax, ebx, ecx,edx,ebp,esp

The ebp,esp registers hold addresses, and at the same time, these two addresses are used to maintain the function stack frame.

2. function stack frame

Each function call needs to open up a space in the stack area for it, and this space is the function stack frame.

ebp: bottom of stack pointer

esp: Top of stack pointer

There are two stack operations on this data structure

1. pop stack

2. push stack

As shown in the figure above, when the main function is created, it will open up a function stack frame for it, and the address range of its function stack frame is identified by the top pointer: esp, and the bottom pointer: ebp. At the same time, the positions indicated by ebp and esp change continuously as function stack frames are created and destroyed.

It should be understood that in VS compiler, the main function is also called by other functions.

After the stack frame of the main function is created, the space in the stack frame is overwritten with a specific value, which is 0xcccccc (hexadecimal representation).

I believe that when you run the code, you have encountered printing a lot of "hot, hot.

Once the stack frame is created, variables created in the function can be stored in the stack frame.

3. Call a function in a function

1. If the function is called with parameters, the parameters are pushed first, where the parameters are temporary copies of the arguments.

2. At the same time, push the address of the next operation after calling the function. (Based on this address, we can find the corresponding operation in memory)

3. Push the ebp address corresponding to the function stack frame before the call, which is to facilitate us to find the address of the corresponding function stack frame after the function call ends.

4. Perform the function stack frame creation operation again.

Function parameters are not created in the stack frame, but are created in the stack before the stack frame is created (corresponding to the first step) and can be found in the corresponding location when they are to be used.

5. The return value of the function is stored in a register (the return value does not disappear when the stack frame is released).

Second, the destruction of the function stack frame

1. Pop some registers used in function calls off the stack.

2. Modify the value of the corresponding ebp,esp so that it reverts to the position pointed to before the function call. Specifically, after the stack pop operation, esp will point to the position pointed to by the current ebp, and then pop the address of the ebp stored in the stack, so that ebp also points to the correct position. This is also why the address of the current ebp is stored before the function is called. At the same time, because of the pop operation, the position of esp also changes accordingly.

3. The top element corresponds to the address of the next instruction after the function call (we push it before calling the function), and we can perform the corresponding operation according to the address. Then pop the stack and change the esp address.

4. After performing the third step, the address pointed to by esp changes. At the same time, the parameter space is released, and the function ends accordingly.

"How to understand the C language function stack frame" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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