Turn from: here
Here is an example of a simple C language code to analyze the function call process
Code:
#include <stdio.h>intFuncintPARAM1,intParam2,intparam3) { intVAR1 =param1; intVAR2 =param2; intVAR3 =param3; printf ("var1=%d,var2=%d,var3=%d", VAR1,VAR2,VAR3); returnvar1;} intMainintargcChar*argv[]) { intresult = Func (1,2,3); return 0; }
First of all, the distribution of variables in the stack is from high address to low address distribution, EBP is a pointer to the bottom of the stack, is not changed in the procedure call, also known as the frame pointer. ESP points to the top of the stack, the program executes when it moves, esp decreases the allocation space, ESP increases the free space, and ESP is called the stack pointer.
The following steps parse the function's call procedure
1. Function main execution, main parameters from right to left step into the stack, and finally press the return address
2. On line 15th, 3 parameters are pressed into the stack from left to right, and from Param3 to param1, the stack is distributed as follows:
3. Then the return address into the stack: at this time the stack is distributed as follows:
4. When the 3rd line function is called, after entering the function through the jump instruction, the function address enters the stack, the EBP enters the stack, then the current ESP value to the EBP, the corresponding assembly instruction :
Push Ebpmov EBP ESP
At this point the top and bottom of the stack point to the same position, the stack is distributed as follows:
5. Line 5th begins execution, int var1 = param1; int var2 = param2; int var3 = PARAM3; In order of declaration . The corresponding assembly:
mov 0x8 (%EBP),%eaxmov%eax,-0x4 (%EBP)
where the [ebp+0x8] address of the content is assigned to EAX, that is, the value of Param to eax, and then put the value of eax in the [EBP-4] This address, that is, the EAX value assigned to VAR1, complete c code int var1 = param1, the other variables are identical.
6. Line 9th, output, line 10th executes the corresponding assembly code:
mov -0x4 (%EBP),%eax
Finally, the return value of the function is saved by EAX register.
7. Call execution function complete, local variable var3,var2,var1 once out of the stack, EBP restore the original value, return address out of the stack, find the original execution address, param1,param2,param3 in turn, the function call execution completed. Entries
The "Go" C + + function call Procedure Analysis