Why do I have to play with “rsp” to call a c++ function?

前端 未结 1 1054
时光说笑
时光说笑 2021-01-21 08:41

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).

相关标签:
1条回答
  • 2021-01-21 09:24

    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 pushes, 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.

    0 讨论(0)
提交回复
热议问题