I've written the following basic C program:
int main() {
char a = 1;
char b = 5;
return a + b;
}
And it compiles in godbolt as:
main:
pushq %rbp
movq %rsp, %rbp
movb $1, -1(%rbp)
movb $5, -2(%rbp)
movsbl -1(%rbp), %edx
movsbl -2(%rbp), %eax
addl %edx, %eax
popq %rbp
ret
I have a few questions about the compiled asm:
- Is
movb
used for 1byte (char),movw
for 2byte (short),movl
for 4byte (int), andmovq
for 8byte (int) integers? What then is justmov
used for, without an extension? - Why is an offset used for
movb $1 -1(%rbp)
,movb $5 -2(%rbp)
? Why aren't the two numbers just moved into two different registers? For example, there's anaddl %edx, %eax
later on...why aren't the two numbers just moved into those two registers? - Why is
movsbl
used here? Why aren't the numbers just moved directly into the registers? - Is
pushq
/popq
pushing/popping an 8byte pointer onto the stack? If so, what's the point of themovq %rsp, %rbp
?