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
movbused for 1byte (char),movwfor 2byte (short),movlfor 4byte (int), andmovqfor 8byte (int) integers? What then is justmovused 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, %eaxlater on...why aren't the two numbers just moved into those two registers? - Why is
movsblused here? Why aren't the numbers just moved directly into the registers? - Is
pushq/popqpushing/popping an 8byte pointer onto the stack? If so, what's the point of themovq %rsp, %rbp?