6
(gdb) disas /m main
Dump of assembler code for function main():
2   {
   0x080483f4 <+0>: push   %ebp
   0x080483f5 <+1>: mov    %esp,%ebp
   0x080483f7 <+3>: sub    $0x10,%esp

3       int a = 1;
   0x080483fa <+6>: movl   $0x1,-0x4(%ebp)

4           int b = 10;
   0x08048401 <+13>:    movl   $0xa,-0x8(%ebp)

5           int c;
6           c = a + b;
   0x08048408 <+20>:    mov    -0x8(%ebp),%eax
   0x0804840b <+23>:    mov    -0x4(%ebp),%edx
   0x0804840e <+26>:    lea    (%edx,%eax,1),%eax
   0x08048411 <+29>:    mov    %eax,-0xc(%ebp)

7           return 0;
   0x08048414 <+32>:    mov    $0x0,%eax

8   }
   0x08048419 <+37>:    leave  

Notce the 3rd assembler instruction, it allocated 16 bytes instead of the expected 12 bytes. Why is that? I thought the 3rd line is allocating automatic variables...

Even if I removed the assignment, the allocation is still 16 bytes.

Thanks.


Edit

// no header. nothing
int main()
{
        int a = 1;
        int b = 10;
        int c;
        c = a + b;
        return 0;
}

g++ -g -o demo demo.cpp


Follow up... I read a couple more threads on stack alignment (sorry, I am now studying computer arch and organization class...so I am not familiar with this at all)

Stack Allocation Padding and Alignment

I am supposed it's the compiler's setting. So default, the minimum is 16-byte.

If we have

int a = 1;
int b = 10;
int c = 10;
int d = 10;
// --
int e = 10;

Up to int d, we would have exactly 16-bytes, and the allocation is still 0x10. But when we give another delectation, int e = 10, esp is now allocated 32 bytes (0x20).

This shows me that esp, the stack pointer, is only used for automatic variables.


Follow-up 2

Call stack and Stack frame

Each stack frame

Storage space for all the automatic variables for the newly called function.

The line number of the calling function to return to when the called function returns.

The arguments, or parameters, of the called function.

But after we allocated int a through int d, it already took us 16 bytes. Main has no function parameters, so that's zero. But the line to return, where did this information go?

Community
  • 1
  • 1
CppLearner
  • 16,273
  • 32
  • 108
  • 163
  • Can we see the source code for `main()`? – Mysticial Oct 01 '11 at 17:58
  • 1
    For the answer to your second edit, check this link: (http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames) It shows exactly how it works and where the return address and everything else is. – Mysticial Oct 01 '11 at 18:53

2 Answers2

9

I although I haven't seen the source code for main() yet, I believe this is due to stack alignment.

Under your settings, the stack probably needs to be aligned to 8 bytes. Therefore esp is being incremented by 16 bytes rather than 12 bytes. (even though 12 bytes is enough to hold all the variables)

On other systems (with SSE or AVX), the stack will need to be aligned to 16 or 32 bytes.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • +1: If main called more functions, each function will expect the stack to be aligned. Rather than adjust the stack a little bit each time, 12 bytes for variables, then 4 more bytes before a function call, it adjusts it 16 up front. – Zan Lynx Oct 01 '11 at 18:04
  • @Mysticial. Thanks. I just wrote the follow-up. You are right. I believe it's a compiler setting. – CppLearner Oct 01 '11 at 18:25
  • 1
    Based on your edit, it looks like the stack alignment is 16 bytes rather than 8 bytes. :) – Mysticial Oct 01 '11 at 18:27
  • @Zan Lynx. Hmmm that's interesting. Because in the follow-up, the "4 more bytes" did not happen with int a ---> int d. It still allocated up to 16 bytes. I understand that on the entry to each function call, we have a stack frame that stores the parameters, the address where we return, the automatic variables... – CppLearner Oct 01 '11 at 18:27
  • @Mysticial I just finished this assignment. Thank you very much. – CppLearner Oct 08 '11 at 17:16
0

Nothing mistical - first four bytes are allocated for return code :)

Lev
  • 727
  • 5
  • 17
  • I don't think that's the case. See my follow up... I understand that the return (which line to return) is one of the information stored in the stack frame. – CppLearner Oct 01 '11 at 18:24
  • 3
    Not likely - it appears the return is placed in eax. – Steve Fallows Oct 01 '11 at 18:28
  • @SteveFallows Thanks. I posted followup 2. I understand what each stack frame stores. But when we declared int a through d, that was already 16 bytes. – CppLearner Oct 01 '11 at 18:42