I am reading Computer Systems: A Programmer's Perspective 3rd Edition by Randal E. Bryant and David R. O'Hallaron.
In chapter 3 and section 7.5, there is figure demonstrating how stack frames are allocated as follows:
I couldn't understand that why are line 4 and 12 needed. It seems that those lines are not required because the surplus 8 bytes of stack memory are not used at all.
As comment indicates, IMHO, it seems that it is inevitably allocated to align stack frame by 24 bytes:
- additional 16 bytes for
pushq %rbp
andpushq %rbx
respectively - , and 8 bytes for
subq $8, %rsp
So, my question can be summarized to "Why stack frames are aligned by 24 bytes?"
CodePudding user response:
24 bytes is not a power of 2 and hence cannot be an alignment. The real alignment is to 16 bytes and you forgot to account for the call
instruction also pushing 8 bytes on the stack. So in total, the stack pointer is moved by 32 bytes, preserving alignment to 16 bytes.
CodePudding user response:
This is consistent with x64 ABI (both Microsoft and SystemV). Stack must be aligned on 16 bytes boundary before calling a function ("call Q" in your example). Let's assume that the stack is initially at a 16 bytes boundary. When the program reaches "P" label (due to a "call P" instruction), RSP points 8 byte below, because "call" subtracts 8 to RSP and saves RIP (8 bytes) at *RSP. Then, there are two "pushq" (line 2 and 3), each decreases RSP by 8, so RSP is still misaligned. That's why The compiler must subtract 8 to align RSP before executing "call Q" at line 8 and 10.
This article describes x86/x64 ABIs quite well: https://en.wikipedia.org/wiki/X86_calling_conventions