This blog attempts to illustrate the question of how a stack frame is built and how it is eliminated when a C function is called. These details are related to the implementation of the operating system platform and compiler, and the following description is for the GCC compiler running on Linux. The standard of the C language does not describe how it is implemented. Therefore, different compilers, different operating systems may have their own way to build the stack frame.
Let's look at a typical stack frame:
The above figure is a typical stack frame, in the picture, the top of the stack, the address space grows downward.
Look at the corresponding function code for this stack:
int foo (intint int arg3);
and assume that Foo has two local int variables (each accounting for 4 bytes).
In the scene above the stack frame, main calls Foo, and the program control is still in Foo. Here, main is the caller (caller), and Foo is the callee (callee).
ESP is used by Foo to indicate the top of the stack, and EBP is equivalent to a "datum pointer ". The arguments passed from main to Foo and the local variables of Foo itself can be found using this reference pointer, plus the offset.
Because the callee is allowed to use EAX, ECX, and edx registers, if the caller wants to save the values of these registers, they must explicitly save them in the stack before calling the child functions. On the other hand, if, in addition to the registers mentioned above, the callee wants to use other registers, such as EBX, ESI, and EDI, the callee must save these extra registers in the stack and restore them before the call returns. That is, if the callee uses only the contracted EAX, ECX, and edx registers, they are held and restored by the caller, but if the callee also uses additional registers, then the caller must save and restore the values of those registers themselves.
The arguments passed to Foo are pressed onto the stack, and the last parameter is the advanced stack, so the first parameter is at the top of the stack. The local variables declared in Foo and some temporary variables that need to be used during function execution are also present in the stack.
A return value of less than or equal to 4 bytes is saved to the EAX register, and if it is greater than 4 bytes and less than 8 bytes, edx is also used to hold the return value. If the return value occupies a larger control, the caller will pass an extra parameter to the callee, which points to the address where the return value will be saved. In C, it is a function call:
x = foo (A, B, c);
be converted to:
Foo (&x, A, b, c);
Note that this occurs only if the return value occupies more than 8 bytes. Some compilers do not use edx to save the return value, so this conversion is used when the return value is greater than 4 bytes.
Http://www.cnblogs.com/dahai/archive/2011/07/29/2120651.html
Http://www.cnblogs.com/DylanWind/archive/2008/12/08/1349822.html
Http://www.cnblogs.com/shitouer/archive/2010/04/05/1704554.html
http://johnlxj.diandian.com/post/2011-03-20/16690291
Http://www.cnblogs.com/dolphin0520/archive/2011/04/04/2005061.html
Http://blog.chinaunix.net/uid-29584360-id-4207364.html
http://blog.csdn.net/zsy2020314/article/details/9429707
Http://www.360doc.com/content/12/1009/13/1317564_240416017.shtml
c Function call and stack