Section Exercises
True or False:
Multiple Choice
Why is it or is it not the stack misaligned in the following cases?
main:
...
subq $16, %rsp
movq $10, %rdi
pushq %rdi
call fooThe stack is misaligned.
It can be seen in the code associated with this answer option that we first reserve 16 bytes of space on the stack for two variables by subtracting 16. Before calling foo, rdi is pushed on the stack, meaning that we add another 8 bytes on the stack. As a result, there were 24 bytes added to the stack instead of a number of bytes equal to a multiple of 16.
main:
...
subq $8, %rsp
movq %rsp, %rdi
subq $8, %rsp
call fooThe stack is aligned.
Although we only need space on the stack for a value, by subtracting only 8 bytes, the calling convention is broken. To make the stack 16 aligned, another 8 bytes are subtracted before foo is called.
main:
...
pushq $4
subq $16, %rsp
movq $10, %rdi
pushq %rdi
call fooThe stack is aligned.
By pushing 4 on the stack (8 bytes) and then subtracting 16 and then pushing rdi (8 bytes), we reserve 32 bytes on the stack, which is a multiple of 16.
main:
...
subq $16, %rsp
pushq $4
movq $10, %rsi
pushq %rsi
popq %rdi
call fooThe stack is misaligned.
In the above code example, we observe that 16 bytes are allocated on the stack and that 4 and rsi are pushed on the stack, meaning that we reserve 32 bytes on the stack. By popping the top of the stack in rdi, we reduce the stack size by 8 bytes, resulting in only 24 bytes being reserved on the stack.
Last updated