I just started my assembly journey like recently, so obviously I\'m a newbie, I\'ve been writing fairly simple and basic programs and I just noticed something weird (to me).
The Windows x64 calling convention requires 16B alignment of RSP before a CALL instruction (but consequently guarantees rsp%16 == 8
on function entry, after call
pushes a return address). This explains the sub rsp,8
around the function call.
It also requires 32B of shadow space (aka home space) reserved for the use of the called function, and that's what the sub rsp, 32 + 16
is doing.
It would be smart to just combine those together, and sub rsp, 32 + 16 + 8
on function entry, and then don't mess with RSP until the epilogue. (In a function that did an odd number of push
es, that take care of the +8
to realign the stack.)
[rsp+32]
and higher bytes are safe from being stepped on by a call
, lower bytes aren't.
The called function can freely make use of those 32 bytes above its return address. That explains why you get garbled output if you just push/pop around the CALL, because then your data will be in the shadow space.
See the x86 tag wiki for ABI / calling convention links.