I'm trying to understand what a C compiler will do when it compiles to assembly. The code I've compiled to assembly is this:
void main() {
int x = 10;
int y = 10;
int a = x + y;
}
Which produces the following assembly:
.Ltext0:
.globl main
main:
.LFB0:
0000 55 pushq %rbp
0001 4889E5 movq %rsp, %rbp
0004 C745F40A movl $10, -12(%rbp)
000b C745F80A movl $10, -8(%rbp)
0012 8B45F8 movl -8(%rbp), %eax
0015 8B55F4 movl -12(%rbp), %edx
0018 01D0 addl %edx, %eax
001a 8945FC movl %eax, -4(%rbp)
001d 5D popq %rbp
001e C3 ret
However I'm having some trouble understanding what in particular is going on in this snippet. I understand all the labels, and some of the assembly. Here's what I think it does:
- push rbp? - is this for a stack frame or something?
- set stack pointer to base pointer? (i.e. clear stack)
- moves 10 into stack? Offset by -12? Why 12, and why is it negative?
- moves 10 into stack, though this time at -8 instead of -12 (difference of 4, perhaps bytes or something?)
- move value at -8 into eax
- move value at -12 into edx
- add eax and edc
- move the value from eax into the stack
- pop rbp? end of function stack frame possibly?
- return from the function??
Can anyone clarify certain points of this assembly, maybe the reasoning the compiler has in choosing -8, -12, why it chooses eax and edc over some other registers, why it pushes and pops rbp, etc?