Analysis on function calls of C ++

Source: Internet
Author: User

I learned how to call functions in the past, but I forgot to use them for a long time ~~ I wonder if anyone else has this experience. It is nice to know a lot about the underlying details of the compiler when writing C/C ++ programs. Otherwise, it may be frustrating. Although it is relatively simple, let's also recall...

We know that the most common way to pass parameters in a function call is to use a stack. The local variables of a function are also created on the stack. How can this problem be solved?

Suppose we have such a small program:

Void test (int ){
Int B;
}

Int main (INT argc, char * argv [])
{
Int;
Test ();
Return 0;
}

The Decompilation results in the debug version of vc6.0 are as follows (some comments are added ):

6: void test (int A) {; function tes
00401020 push EBP; Save the frame pointer of the main function Stack
00401021 mov EBP, esp; activate the current stack frame pointer
00401023 sub ESP, 44 h; set aside 44h "private space" for test: 44 h = 4*17 = 4*11 h (contact us below)
00401026 push EBX; Save the register value
00401027 push ESI
00401028 push EDI
00401029 Lea EDI, [ebp-44h]
0040102c mov ECx, 11 h; 11 h = 17 indicates the number of cycles
00401031 mov eax, 0 cccccccch
00401036 rep STOs dword ptr [EDI]; loop, fill in "private space" (INT) 3
7: int B;
8 :}

.......

10: int main (INT argc, char * argv [])
11 :{
00401050 push EBP;
00401051 mov EBP, esp; activate the current stack frame pointer
00401053 sub ESP, 44 h; set a 44 h "private space" for the main function"
00401056 push EBX
00401057 push ESI
00401058 push EDI
00401059 Lea EDI, [ebp-44h]
0040105c mov ECx, 11 h
00401061 mov eax, 0 cccccccch
00401066 rep STOs dword ptr [EDI]
12: int A; // The variable definition does not have the corresponding assembly code, the compiler will represent (INT) A in EBP-4
13: Test ();
00401068 mov eax, dword ptr [ebp-4]; dword ptr [ebp-4] That is the value of int A in the main function
0040106b push eax; int A as the parameter pressure Stack
00401_c call @ ILT + 0 (test) (00401005); call the test function
00401071 add ESP, 4; Because int A is pushed into four bytes before the call, ESP will be rolled back to the position before the parameter is pushed out after the test function exits.
14: Return 0;
00401074 XOR eax, eax
15 :}
00401076 pop EDI
00401077 pop ESI
00401078 pop EBX
00401079 add ESP, 44 h
00401_c cmp ebp, ESP
00401_e call _ chkesp (004010a0)
00401083 mov ESP, EBP
00401085 pop EBP
00401086 RET
In the stack, each function has a stack frame to save all its local objects and temporary objects used in expression calculation. Generally, the compiler uses the EBP register to indicate the current active stack rollback. During compilation, the compiler resolves all local objects to a fixed offset relative to the stack callback pointer (EBP). The function indirectly accesses local objects through the stack callback pointer.

When the compiler compiles a function, it will add some code at the beginning to create and initialize the stack metadata for it. These codes are called Prologue. Similarly, it also puts code at the end of the function to clear the stack metadata. These codes are called epilogue ).
Generally, the preface is as follows:

Push EBP; Save the original stack token pointer to the stack
MoV EBP, esp; activate a new stack token
Sub ESP, 10; subtract a number to point ESP to the end of the stack consumer
The first command saves the original stack callback pointer EBP to the stack. The second command activates the stack callback of the called function by directing ebp to the ebp storage location of the main function; the third command deducts esp from a number, So ESP points to the end of the current stack, and this number is the size of all local and temporary objects used by the function. During compilation, the compiler knows the type and volume of all local objects of the function. Therefore, it can easily calculate the stack volume.
The conclusion is exactly the opposite of the preface. It must remove the current stack shard from the stack:
Mov ESP, EBP 
Pop EBP; activate the stack callback of the Main Function
RET; returns the primary function

It directs ESP to the storage location of the stack callback pointer of the main function (that is, the position of the stack callback pointer of the called function), and pops up EBP to activate the stack callback of the main function, then return the main function.
Once the CPU encounters a return command, it has to do the following two things: pop up the return address from the stack, and then jump to that address. The return address is automatically used to press the stack when the Calling command of the main function is called. When a call command is executed, it first pushes the address of the command followed by it (the return address of the called function) into the stack, and then jumps to the start position of the called function. The main function also puts the parameters of the called function into the stack, so the parameters are also part of the stack callback. After the function is returned, the main function needs to remove these parameters by adding the total volume of all parameters to ESP.

If there is a function call chain, Foo ()-> bar ()-> widget (); its stack is as follows:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.