x64汇编中的堆栈对齐

van*_*ale 8 assembly

如何28hrsp以下计算中减去(十进制40)的值:

    option casemap:none

    includelib kernel32.lib
    includelib user32.lib

externdef MessageBoxA : near
externdef ExitProcess : near

    .data

text    db 'Hello world!', 0
caption db 'Hello x86-64', 0

    .code

main proc
    sub rsp, 28h        ; space for 4 arguments + 16byte aligned stack
    xor r9d, r9d        ; 4. argument: r9d = uType = 0
    lea r8, [caption]   ; 3. argument: r8  = caption
    lea rdx, [text]     ; 2. argument: edx = window text
    xor rcx, rcx        ; 1. argument: rcx = hWnd = NULL
    call MessageBoxA
    xor ecx, ecx        ; ecx = exit code
    call ExitProcess
main endp

    end
Run Code Online (Sandbox Code Playgroud)

来自:http://www.japheth.de/JWasm/Win64_1.html

根据我的理解,我只需要减去,20h因为我使用的每个值需要8个字节到4 20h.那么为什么28h要减去它又是如何导致16字节对齐的呢?

另请参阅少于四个参数的函数是否需要保留堆栈空间?

lur*_*ker 10

我相信这是因为在main调用之前,堆栈是对齐的.然后,之后的call行为call是将一个8字节的指针(调用者的地址)推入堆栈.所以在开始时main,它是16字节对齐的8字节.因此,而不是20h你需要28h,将实际总数带到28h + 8h(从call)或30h.对准.:)