I have been writing small cC programs and then disassembling them to try to understand what is actually happening under the hood.
I am using mingw-w64 on windowWindows 7 for my work, and I have run into a question about why certain amounts of space are being allocated on the stack.
program1: int main() { int i,j; return 0; }
int main()
{
int i,j;
return 0;
}
The start of the disassembled code for the main function is:
00000000004015b0 <main>:
4015b0: 55 push %rbp
4015b1: 48 89 e5 mov %rsp,%rbp
4015b4: 48 83 ec 20 sub $0x20,%rsp
The last line looks like it is allocating 32 bytes on teh stack.
Program 2: The same program except it assigns values to tehthe two variables and returns the sum of the variables. int main() { int i,j; i = 5; j = 6; return i+j; }
int main()
{
int i,j;
i = 5;
j = 6;
return i+j;
}
Start of disassembled code: 00000000004015b0 : 4015b0: 55 push %rbp 4015b1: 48 89 e5 mov %rsp,%rbp 4015b4: 48 83 ec 30 sub $0x30,%rsp
00000000004015b0 <main>:
4015b0: 55 push %rbp
4015b1: 48 89 e5 mov %rsp,%rbp
4015b4: 48 83 ec 30 sub $0x30,%rsp
Why does this program allocate 48 bytes instead of the 32 bytes from the first program?
In both cases i and j are stored at %rbp-4 and %rbp-8 respectively, and the math done in the second program is all done in the registers.
Note I am using gcc 6.3.0 to compile the code and objdump 2.28 to disassemble it.
All code can be seen in my github repository http://github.com/draikes/rev-eng