Format of function call Frames

Source: Internet
Author: User

Recently, I seem to be very interested in the format of the function frames generated in the stack during the function call process. After studying the assembly code for one afternoon, I think I should have a little bit of understanding.

First, paste a simple piece of code to generate the assembly code, and then start to analyze the frame.

#include <stdio.h>int global_val = 3;void display(int a, int b){    int i = 0;    int j = 0;    a = b;}void int_max(int a, int b){    int max_ = 0;        display(a,b);    printf("hello.world!\n");}

This code is meaningless, but it is only for me to see the call process.

int_max:1.pushl%ebp2.movl%esp, %ebp3.subl$40, %esp4.movl$0, -12(%ebp)5.movl12(%ebp), %eax6.movl%eax, 4(%esp)7.movl8(%ebp), %eax8.movl%eax, (%esp)9.calldisplay10.movl$.LC0, (%esp)11.callputs

This section is the assembler of the int_max function. I have made a small analysis on this Assembly. what I know now is that during the function call process

1. First, pressure the function parameters to stack

2. Save some items on site (register value) and press the return address into the stack.

3. Save the EBP that calls this function

4. functions such as local variables


Before analyzing, I need to explain the functions of the two registers:

1. The ESP register is a pointer to the top of the memory. This is what I call it. This Pointer Points to the top position of the current function record. And at any time, the value is at the top.

2. The EBP register stores the frame bottom pointer, that is, it points to the bottom of the frame of the current function, rather than pointing to the bottom of the stack. (In fact, I am skeptical about this. At the end of the day, I am analyzing why I suspect this problem)

The two pointers must be clarified. Otherwise, it is difficult to understand this problem during the parsing process.

Now start to analyze the assembly code,

1.pushl%ebp2.movl%esp, %ebp

1. these two lines of code are available in any function, which means that the frame of the previous function frame is first pushed to the stack, and then the current top pointer is assigned to EBP, that is, now both pointers point to the top of the cursor.

        3.subl$40, %esp4.movl$0, -12(%ebp)5.movl12(%ebp), %eax6.movl%eax, 4(%esp)7.movl8(%ebp), %eax8.movl%eax, (%esp)        

2. Note that the bottom of the stack is a high address, so the stack is stretched down. Therefore, reduction is the process of compaction. ESP minus 0x40 and 0x40 indicate that 64 bytes of space is retained. You may wonder why there is only one local variable. In fact, when calling a function, you should not only put the return address into the stack, but also put some register values into the future for future recovery. But it is not displayed in the code. And the specific location and size of this is not very clear.

Code 4: Place 0 in the position (EPB-12), while Code 5 puts (parameter B) in eax, #6 stores B in the place of ESP + 4, and No. 7 and No. 8 stores a in the position of ESP. In fact, this part can be seen as a function parameter for pressure compaction.

If it is such a process as a whole, the compiler will first compress the function parameters when calling the function, in addition, the compaction process is an inverted process (though it seems to be related to a specific compilation), and then the following command is called.

9.calldisplay

This pointer is used to call the FUNCTION command. The call command is not as simple as you think. He has to do a lot of things, as if to save the register and compress the return address, the subsequent process is what we analyzed.



Haha, there are still some problems that have not been solved. For example, the pointing position of the EBP pointer is hard to understand. He is located on the return address. I think he should be a frame from the beginning of the parameter, in addition, the returned address is in front of EBP, but the value of the Register is behind EBP. This is also a problem. I don't know how can this problem be solved ????







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.