Stack frame question on x86 code generation

Gang-Ryung Uh uh18104@yahoo.com
Sat Apr 23 18:09:00 GMT 2005


Could anyone help me understand what is the gcc 
strategy to prepare the stack frame?
For the following function,

void function(int a, int b, int c) 
{
  char buffer1[5];
  char buffer2[10];
  int *ret;
   
  ret = &buffer1[0]+28;
  printf("0x%x=return address, *ret);
}

I compiled with gcc -O0 -S option and the compiler
produces the code that I cannot quite follow the 
stack frame layout strategy in gcc.

function:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $56, %esp     // question 1
        leal    -24(%ebp), %eax
        addl    $28, %eax
        movl    %eax, -44(%ebp)
        subl    $8, %esp    // question 2
        movl    -44(%ebp), %eax
        pushl   (%eax)
        pushl   $.LC0
        call    printf

Here are my questions:
question1: Why the stack frame size is 56?
           observation: (1) compiler add 16 bytes
           padding before allocating storage
           for array buffer1 (2) buffer1 need
           5 bytes. However, due to alignment
           issue, they seem to add 3 extra bytes.
           Thus, -24(%ebp) should point to buffer1[0].
  
           Then, why they adding 16 bytes padding?

question2: Why gcc makes the stack frame bigger before

           the function call printf?
           
                  subl    $8, %esp

           Does it related to printf? If it does, then
           could you explain why?

Thanks in advance.
Best regards,


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Gcc-help mailing list