【Advanced C language】 ❤️ Top magic skills! Creation and destruction of function stack frames! ❤️

Kind tips

Hello everyone, I am Cbiltps. In my blog, if there are sentences that are difficult to understand or key points that are difficult to express in words , I will have pictures . So my blog with pictures is very important ! ! !

If you want to learn the function stack frame chapter better and smoothly , I strongly recommend you to read my [C language elementary] debugging skills blog , what effect will it have? You will feel it after reading and studying this chapter! ! !

More importantly , when analyzing the process, it is carried out step by step according to the disassembly language, but this process is difficult to explain in words (very complicated), so I drew a picture for a long time to understand, everything is Show in pictures! ! ! Remember, remember! If you still don't understand, please contact me, we can make a video, etc...

If you are interested in me please see my first blog !

Introduction

What I wrote today is 【C语言进阶】the second part of the content: function stack frame , function stack frame is really a practice of internal strength!

In the past, many places talked about function stack frames, but most of the students couldn't understand them, so they talked less or didn't talk about them later; and found that the company was unwilling to test these things (there were few tests).

But this thing is very important , if you really understand this thing, then you have really mastered it!

Preschool doubts

During the early study, we may have a lot of confusion:

  • How are local variables created?
  • Why are the values ​​of local variables random?
  • How is the function passed parameters?
  • What is the order in which function parameters are passed?
  • What is the relationship between formal parameters and actual parameters?
  • How is the function call done?
  • How does the function return after the end of the call?

If you want to understand these problems, you will know after you finish learning the creation and destruction of function stack frames . In fact, you have cultivated your own internal skills and can understand more knowledge later!

preschool preparation

1. Environment selection

When learning function stack frames , the environment I use is VS2013( VC6.0it is also possible, it is simple enough for the process of creating and destroying function stack frames ), do not use too advanced compilers, the more advanced the compiler, the more Not easy to learn and observe (encapsulation is more complex considering various issues).

At the same time, under different compilers, the creation of stack frames during function calls is slightly different, and the details depend on the implementation of the compiler.

2. Knowledge foundation

If you want to learn function stack frames, you must make a small foreshadowing for everyone: understand registers (parts)!

Data Register:

  • eax
  • ebx
  • ecx
  • edx

pointer register:

  • ebp
  • esp

The focus of this chapter is on the back pointer register ( ebpand esp) . To understand the function stack frame, you must understand these two registers.

These two registers store addresses , and these two addresses are used to maintain the function stack frame .

So ebphow espto maintain the function stack frame?
Which function is being called, espand ebpthe function stack frame of which function is being maintained, espand ebpthe space between and is the space opened up for this function.

start of text


1. General outline understanding (source code and disassembly)


This chapter will use the following piece of code as 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;
}

First call stack observation: In fact, this call logic is quite complicated!
insert image description here
Then draw a concept map to understand (stack area):
insert image description here

The above analysis is just a rough outline, but how is it done?

We need to go to the disassembly to explore:

int main()
{
    
    
000919F0  push        ebp  
000919F1  mov         ebp,esp  
000919F3  sub         esp,0E4h  
000919F9  push        ebx  
000919FA  push        esi  
000919FB  push        edi  
000919FC  lea         edi,[ebp-0E4h]  
00091A02  mov         ecx,39h  
00091A07  mov         eax,0CCCCCCCCh  
00091A0C  rep stos    dword ptr es:[edi]  
	int a = 10;
00091A0E  mov         dword ptr [a],0Ah  
	int b = 20;
00091A15  mov         dword ptr [b],14h  
	int c = 0;
00091A1C  mov         dword ptr [c],0  

	c = Add(a, b);
00091A23  mov         eax,dword ptr [b]  
00091A26  push        eax  
00091A27  mov         ecx,dword ptr [a]  
00091A2A  push        ecx  
00091A2B  call        _Add (0911DBh)  
00091A30  add         esp,8  
00091A33  mov         dword ptr [c],eax  

	printf("%d\n", c);
00091A36  mov         esi,esp  

	printf("%d\n", c);
00091A38  mov         eax,dword ptr [c]  
00091A3B  push        eax  
00091A3C  push        95858h  
00091A41  call        dword ptr ds:[99114h]  
00091A47  add         esp,8  
00091A4A  cmp         esi,esp  
00091A4C  call        __RTC_CheckEsp (091136h)  

	return 0;
00091A51  xor         eax,eax  
}
00091A53  pop         edi  
00091A54  pop         esi  
00091A55  pop         ebx  
00091A56  add         esp,0E4h  
00091A5C  cmp         ebp,esp  
00091A5E  call        __RTC_CheckEsp (091136h)  
00091A63  mov         esp,ebp  
00091A65  pop         ebp  
00091A66  ret  

The above code is the disassembly code, let's start learning!


2. The overall process of creating and destroying function stack frames


insert image description here


3. Creation of function stack frame


3.1 Creation of main function (picture display)

insert image description here

3.2 Creation (decomposition) of the Add function

insert image description here


4. Destruction of the function stack frame


4.1 Destruction of main function (picture display)

insert image description here

5.1 Destruction of Add function (partial disassembly code display)

The previous part is main函数the same as the previous one, so I won't introduce too much!
insert image description here


5. Answers to questions


  • How are local variables created?

First, allocate stack frame space for the function. After initializing a part of the space in the stack frame space, allocate space for local variables in the stack frame.

  • Why are the values ​​of local variables random?

insert image description here
A part of the space initialized in the stack frame space is actually assigned a lot 'C', which is a random value.
And what I saw when I printed it out before: blah blah blah blah blah blah blah blah blah blah blah... It's because the value of the local variable is a random value.

  • How is the function passed parameters?

Look directly at the picture:
insert image description here

  • What is the order in which function parameters are passed?

In fact, when the function has not been called, it has already moved from right to left push .

  • What is the relationship between formal parameters and actual parameters?

The formal parameter is a temporary copy of the actual parameter, with the same value and independent space.
Changing the formal parameters will not affect the actual parameters.

  • How is the function call done?

Understand the diagram above! It's hard to explain!

  • How does the function return after the end of the call?

Understand the diagram above! It's hard to explain!

end of text

Guess you like

Origin blog.csdn.net/Cbiltps/article/details/120581909