The code fragment is suspicious in that it fails to keep the stack pointer balanced across a call sequence, which is probably not the intended behaviour. Typically you would either match push
and pop
operations, push
many times and then addq $k, %rsp
(where k
is the combined size of every previous push
), or keep the frame size constant across the execution of the function and use explicit accesses.
Given that you need to keep keep the stack pointer 16-byte aligned, keeping a constant-sized frame is probably the most straightforward method. It might look like
# in the function prologue
subq $K, %rsp
...
mov %rdi, SLOT(%rsp)
call subroutine
mov SLOT(%rsp), %rdi
...
# in the epilogue
addq $K, %rsp
Where K
is enough space for all the local stack state plus enough space for the largest set of outgoing arguments, rounded up to 16, and SLOT
is a spot on the stack reserved for the value of %rdi
.
The return value of an integer type usually placed in rax
, not on the stack. Other types are returned differently (such as by a hidden pointer) - check the calling convention of the compiler you are interfacing with.