C Language-call procedure for functions, creation and destruction of stack frames.

Source: Internet
Author: User
How does a function in memory go through a series of process calls? Pointer Register ESP,EBP
1. First figure out the two very important pointer register ESP,EBP in the function call. Some understanding of these two registers:
The ESP is a stack pointer, which is used to point to the stack top
EBP is the frame pointer, which points to the stack bottom of the stack. function Stack Frame
2. The function calls the process needs to open up the space, uses in this function's call the temporary variable preservation, the field protection. This stack space is what we call a function stack frame.
The stack space is used by high addresses to low addresses. Function call procedure

When the function is called, do the following:

1. Press the frame pointer into the stack: push EBP
2. Save current stack pointer with EBP: MOV ebp, esp
3. Stack pointer decrement, the resulting memory is used to store the local state of the called function: Sub ESP, 4Ch
4. When the function is called, the first stack is the address of the next instruction after the called function (the next executable statement of the function call statement, the address of the next instruction in the calling instruction), and then the parameters of the function, in most C compilers, the argument is the right to the left into the stack, the first pressure parameter B in the pressure parameter a , and then a local variable in the function. Note that static variables are not in the stack.
5. When the function call is complete, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point.

C Language-call procedure for functions, creation and destruction of stack frames.

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.