C + + function call procedure--function stack (i) __jquery

Source: Internet
Author: User
Tags readable
function calls are not unfamiliar, the caller passes some arguments to the callee, executes the callee's code, and finally the caller returns the result to the caller. For a program, the compiler assigns a memory to it, logically can be divided into code snippets, data segments, heaps, stack code snippets: Save program text, instruction pointer Eip is to point to code snippets, readable executable writable data segment: Save initialized global variables and static variables, readable writable can not be executed BSS: Uninitialized global variables and static variable heap (HEAP): Dynamically allocating memory, increasing the direction of address growth, readable writable executable stack (stack): Store local variables, function parameters, current state, function call information, etc., to reduce the address of the direction of growth, very important, readable writable executable As shown in the figure

Register DeviceEAX: Additive (accumulator) register, commonly used for function return value EBX: Base Address (base) register, which accesses memory for base address ECX: Counter (Counter) registers, commonly used as a counter in string and loop operations EDX: Data registers, Commonly used in multiplication and division and I/O pointers ESI: source Change Register DSI: Destination register ESP: stack pointer register, pointing to Stack top EBP: base pointer register, pointing to the bottom of the current stack EIP: instruction register, pointing to the address of the next instruction
function call Process Press Stack example:
1 #include <stdio.h>
 2 
 3 int func (int param1, int param2,int param3)
 4 {
 5         int var1 = Param1;
  6         int var2 = param2;
 7         int var3 = PARAM3;
 8  
 9         printf ("var1=%d,var2=%d,var3=%d", VAR1,VAR2,VAR3);
Ten return         var1;
An  
int main (int argc, char* argv[])
{
int result         = func (1,2,3);  
0 return         ; 
18}

The following step is to parse the call procedure for a function

1. Function main execution, main parameters from right to left to step into the stack, and finally press into the return address

2. Perform line 15th, 3 parameters are pressed into the stack from Left-to-right order, 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. The 3rd line function call, through the jump instruction into the function, after the function address into the stack, ebp into the stack, and then the current ESP value to EBP, the corresponding assembly instructions:
At this time the stack top and the bottom of the stack point to the same position, the stack is distributed as follows:




Line 5th begins execution, int var1 = param1; int var2 = param2; int VAR3 = param3, stored sequentially in declared order. The corresponding assembly:

<span style= "font-family:arial black;" >mov 0x8 (%EBP),%eax
mov%eax,-0x4 (%EBP) </span>
It assigns the contents of the [ebp+0x8] address to the EAX, which assigns the Param value to the eax, and then places the EAX value in the address of [EBP-4], which assigns the EAX value to the VAR1, completes the c code int var1 = param1, and the other variables are identical.




6. Line 9th, output results, line 10th executes the corresponding assembly code:
<span style= "font-family:arial black;" >mov  -0x4 (%EBP),%eax</span>

Finally, the return value of the function is saved through the EAX register.

7. Call execution function completed, local variable var3,var2,var1 a stack, EBP restore the original value, return to the address stack, find the original execution address, param1,param2,param3 sequentially out of the stack, function call execution completed. Figure




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.