C + +: How are the process of function calling in C + +

Source: Internet
Author: User

Following images is from http://www.xuebuyuan.com/528715.html


In general, the process of calling function was put the parameters into the stack, move the EBP and ESP (jump into another function), protect the information of Father function, restore the information of Father function. (Parameter into the stack, function jump, protect the scene, reply to the scene)


There is three special pointer in COMPILER:EIP (instruction pointer) is the instruction pointer to the address of next instrction (that is, pointing to the next The address of the command to be executed); EBP (bottom pointer) is a base-point pointer, and points to the bottom of the stack are used to refer to the stack, and the ESP (stack pointer) is a stack pointer Tack used to point to the top of the stack.


For example, this is the code:



First, we are at the main function. The Every function has the its EBX (basic register), the Esi,edi (destination register), respectively, the base register, the source variable address register, the destination variable address register. They is some important information of a particular function. When we call the G_func, we firstly need to transport the parameters into it. As I mentioned in my C + + Note, when we are using the transportation of value, we'll create copies of the parameters for The function. Therefore, here are the first step:put the parameters into the function stack.


Three push instructions, respectively, three parameters are pressed into the stack, you can find that the parameters of the stacking sequence is from right to left. At this point we can check the data in the stack to verify it. As shown in 3, from the real-time Register table on the right we can see the ESP (stack top pointer) value of 0X0012FEF0, and then found in the memory table from the memory address 0X0012FEF0, we can see in memory in order to store the 0x00000001 (that is, parameter 1), 0x00000002 (that is, Parameter 2), 0x00000003 (that is, Parameter 3), that is, the stack at the top of the store is three parameter values, indicating the success of the stack.




If We translate the code into instructions, we'll get:




We Firstly push three parameters into stack. Then we call a instruction at the address of 00401005. Following this address, we can see this:



We can see that is a jump instruction at the instruction address of 00401005. It jumps to 00401030. That's the begin instruction address of G_func.


Until now, we see how the instructions like before entering the instructions of function. Firstly push the parameters into stack and then use a call instruction to a jump instruction to jump to the actual instructio n Address of the function. So, until now, the stack was like above. It has three parameters in it now.


Now, we turn to protect information before really entering function.


This step consists of three actual movement:

1) The implicit of the first step, and there is no explicit instruction to command its completion. is to press the EIP address (the address of the next instruction of the main function) into the stack after the previous call instruction. In this example, add is followed by call. The address is 00401093.



2) EBP is the original bottom pointer of the main function. If you change the function, the bottom of the stack is changed. We want to save the base address of the previous function as the first step in the protection field. So the second step is to press the EBP of the previous function into the stack.

Next mov ebp, ESP takes the top address of the stack at this time as the stack base of the function, determines the stack area of the G_func function (EBP is the bottom of the stack, ESP is the top of the stack). The next instruction is sub ESP, 48h, which literally means moving the top pointer upward by 48h Byte. Then why do you move it? What is this middle memory area for? This area is space spaced, separating the stack area of two functions by a distance, as shown in 7. The interval size is fixed at 40h, or 64Byte, and then the area of memory that stores local variables is reserved. The G_func function has two local variables x and y, so the length of the ESP to be moved is 40h+8=48h.


Note that the area where the local variables are stored is 64Bytes removed separately. Does not occupy 64Byte of space. Make sure that at least a 64Bytes interval between the stack space of the function is at least.


Post-move stack space distribution




3) The previously mentioned EBX (Basic register), Esi,edi (destination register), respectively, the base register, the source change register, the purpose of the variable address register to press the address above the current ESP. As a third step in the preservation site.


At this point we can see that the stack memory is this:




The following content is talking on how to restore the information, I just directly copied form http://www.xuebuyuan.co M/528715.html







C + +: How are the process of function calling in C + +

Related Article

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.