In trying to learn more about how C generates assembly code, I was exploring the outputs of various snippets of C code using godbolt.
One peculiar thing I came across was encountered with this piece of code:
#include <stdio.h>
int main()
{
puts("Hello, world!\n");
return 0;
}
int x()
{
return 0;
}
Compiling with -O3
yielded the following assembly:
.LC0:
.string "Hello, world!\n"
main:
subq $8, %rsp
movl $.LC0, %edi
call puts
xorl %eax, %eax
addq $8, %rsp
ret
x:
xorl %eax, %eax
ret
My question is this:
Why does C allocate 8 bytes (subq $8, %rsp
) on the stack before calling puts, which it doesn't end up using. Is this required to call puts? Or is it for moving the const into memory (which I don't think is right, as the constant is already defined, and the register is already axiomatically allocated).
So, what is the purpose of the subq $8, %rsp
line here?
I included the int x
function as well, to show that it does not allocate this when not calling puts?