C development basics-function call stack, development basics-Function

Source: Internet
Author: User
Tags configuration settings

C development basics-function call stack, development basics-Function

It is found that almost all new people will encounter some problems, and they often fail to get started due to lack of basic knowledge. The function call stack is one of them. So I spent some time sorting out the previously written content.

During running, the program has a memory area to implement the function calling mechanism of the program. This area is the data structure area of a LIFO, which can be called the function stack (call stack ). Every function that has not exited will have a data zone in the function stack. This is called the function stack frame. The call stack frame of a function stores some important information about the corresponding function: the local variables used in the function, the function parameters, and the data required to maintain the function stack, for example, the EBP pointer and the return address of the function. For example. Assume that the function currently executed by the program is a Z function, then there will be a structure like this in the function call stack (EBP actually points to the "parent function" call stack frame, and how to do it will be explained later ):

  • Int increase (int ){
  •  
  • Int temp = 4;
  •  
  • Return a + 3;
  • }
  •  
  • Int main (int argc, char * const argv [])
  • {
  • Int sum = increase (3 );
  •  
  • Return 0;
  • }
  • Call the increase function in the main function. Use VS single-step breakpoint to open the Assembly mode. The following code is displayed:

    1. Int sum = increase (3 );
    2. 00D2561E push 3
    3. 00D25620 call increase (0D2142Eh)
    4. 00D25625 add esp, 4
    5. 00D25628 mov dword ptr [sum], eax

    As described above, we can see that before calling a function, the push command First pushes the function parameter to the stack. Call increase. Then we enter the increase function to see what the function body is.

    1. Int increase (int ){
    2. 000455C0 push ebp
    3. 000455C1 mov ebp, esp
    4. 000455C3 sub esp, 0CCh
    5. 000455C9 push ebx
    6. 000455CA push esi
    7. 000455CB push edi
    8. 000455CC lea edi, [ebp-0CCh]
    9. 000455D2 mov ecx, 33 h
    10. 000455D7 mov eax, 0 CCCCCCCCh
    11. 000455DC rep stos dword ptr es: [edi]
    12.  
    13. Int temp = 4;
    14. 000455DE mov dword ptr [temp], 4
    15.  
    16. Return a + temp;
    17. 000455E5 mov eax, dword ptr [a]
    18. 000455E8 add eax, dword ptr [temp]
    19. }
    20. 000455EB pop edi
    21. 000455EC pop esi
    22. 000455ED pop ebx
    23. 000455EE mov esp, ebp
    24. 000455F0 pop ebp
    25. 000455F1 ret

    Before entering the function, the main action is to save the registers. Note that "sub esp, 0xcch" is to move ESP and empty the "location" of the local variable. Why is there only one local variable, but has generated such a large area?

    Stackoverflow has the following explanations:

       

    This extra space is generated by the /Zi compile option. Which enables Edit + Continue. The extra space is available for local variables that you might add when you edit code while debugging.

    You are also seeing the effect of /RTC, it initializes all local variables to 0xcccccccc so that it is easier to diagnose problems due to forgetting to initialize variables. Of course none of this code is generated in the default Release configuration settings.

    From this simple code, we can know what the function call is like. Through the above content, we carefully understand the changes in the ESP and EBP registers, that is, the next instruction

       

    013D55C0 push ebp // construct a new call Frame
    013D55C1 mov ebp, esp

    013D55EE mov esp, ebp // restore to the original call Frame
    013D55F0 pop ebp

    With the addition of parameters, the return address, and the local variables in the stack and out of the stack, this unified and non-complex code generation mode and data structure can be used to cope with any complicated function calls, extremely flexible. I have always thought that this is a very beautiful creation in computer science, and it is also an example of a simple and complex Sutra.

    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.