In Assembly Language, Seventh Edition for x86 Processors by Kip Irvine, on page 211, it says under 5.53 The x86 Calling Convention which addresses the Microsoft x64 Calling Convention,
- When calling a subroutine, the stack pointer (
RSP
) must be aligned on a 16-byte boundary (a multiple of 16). TheCALL
instruction pushes an 8-byte return address on the stack, so the calling program must subtract 8 from the stack pointer, in addition to the 32 it already subtracts for the shadow space.
It goes on to show some assembly with a sub rsp, 8
right before the sub rsp, 20h
(for the 32-bytes of shadow space).
Is this a safe convention though? Is the Microsoft stack guaranteed to be aligned on 16-bytes before the CALL
instruction? Or, is the book wrong in assuming that the stack was
- aligned to 16-bytes prior to the
CALL
- had an 8-byte return addresses push onto the stack with the
CALL
- requires an additional
sub rsp, 8;
to get back to 16-byte alignment?
sub rsp, 8
. say possiblesub rsp, 78h
and many others – Lazarettorsp
will be wrong. But the CPU itself will not prevent you from calling other code with wrongly alignedrsp
. – Nipissingsub rsp,8
is as safe, as it is safe to assume that the code calling you behaves as required (i.e. safe, because code breaking it is bug and should be fix). If the code above fails to fulfill that requirement, thensub rsp,8
will fail too to re-alignrsp
, and calling next functions may fail due to that. It may take some time you will actually hit function which does effectively use that alignment (for example for aligned vectorized memory access), so you may often get away by calling functions with misalignedrsp
, but that's just bug, that didn't demonstrate yet, it's not correct code. – Nipissing