News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Confused on stack alignment on Win x64

Started by MazeGen, May 04, 2006, 04:09:45 PM

Previous topic - Next topic

MazeGen

I'm quite confused on Stack Allocation (alignment):

Quote from: msdn: Stack AllocationThe stack will always be maintained 16-byte aligned, except within the prolog (for example, after the return address is pushed), and except where indicated in Function Types for a certain class of frame functions.

Besides the exception, does it really means we have to always do MOV [something], RAX instead of PUSH RAX in the function body???

LATER:

I have just found the following on Jeremy Gordon's GoAsm site: http://www.jorgon.freeserve.co.uk/GoasmHelp/64bits.htm#autoal
QuoteThe stack pointer (RSP) must be 16-byte aligned when making a call to an API. With some APIs this does not matter, but with other APIs wrong stack alignment will cause an exception.
...
Because of this requirement, the Win64 documentation states that you can only call an API within a stack frame. This is because it is assumed that only within a stack frame can the stack be guaranteed to be aligned properly. A call out of the stack frame will misalign the stack by 8 bytes.

That's important, I have originally thought this alignment is necessary because of exception handling in x64.

bozo

the stack alignment issue is not entirely new.
for example, on NT systems (atleast) the stack had to aligned on 4 byte boundary, otherwise the API would cause exception.

now i don't know much about win64 programming yet, but just installed xp64 yesterday & started writing asm programs using ML64

it is disapointing that it does not support invoke syntax or indeed control flow, .IF/.WHILE...etc
its like coding with TASM, but in 64-bit.

MazeGen

x64 stack defaults to 64-bit width so it is easy to keep the stack 8-byte aligned. The problem lies in fact that at time of performing CALL instruction the stack has to be 16-byte aligned. This makes pure asm programming (without macros) quite difficult and requies new coding style. See, for example, FDBG sources or look inside some system executables.

I almost finished my macro project to support FASTCALL calling convention on ML64. I'm setting up its web home now. The convention is supported in the same way like in GoAsm.