Pelles C forum

Assembly language => Assembly discussions => Topic started by: Vortex on May 18, 2025, 09:59:47 PM

Title: Question regarding stack alignment
Post by: Vortex on May 18, 2025, 09:59:47 PM
Hello,

Assembling the code below, Poasm Version 13.00.57 reserves 72 bytes on the stack after the declaration of the procedure main :
        sub    rsp, 72
        xor    rax, rax
        mov    qword ptr [rsp+40H], rax
        mov    qword ptr [rsp+38H], rax
        mov    qword ptr [rsp+30H], rax
MessageBoxA PROTO :QWORD,:QWORD,:QWORD,:DWORD
MessageBox TEXTEQU <MessageBoxA>

ExitProcess PROTO :QWORD

.data

msg        db 'Message box example',0
title1      db 'Test',0

.code

start:

    sub    rsp,8+4*8
    call    main
    invoke  ExitProcess,0

main PROC hWnd:QWORD,uMsg:QWORD,wParam:QWORD,lParam:QWORD PARMAREA=4*SIZEOF QWORD

LOCAL x:QWORD
LOCAL y:QWORD
LOCAL z:QWORD

    xor    rax,rax
    mov    x,rax
    mov    y,rax
    mov    z,rax

    invoke  MessageBox,0,ADDR msg,ADDR title1,0
    ret

main ENDP

END start

Any specific reason why Poasm does not reserve 56 bytes instead of 72?

        sub    rsp, 56
The correct stack allocation value should be 56 :

Parmarea -> 4*8 + 3 locals*8 = 56 = 48+8 alignes the stack.
Title: Re: Question regarding stack alignment
Post by: Pelle on May 18, 2025, 11:24:12 PM
It simplifies the code: first align (local-size + parmarea-size), later adjust for uses (push) registers and ultimate stack alignment.
Title: Re: Question regarding stack alignment
Post by: Vortex on May 19, 2025, 10:46:22 AM
Hi Pelle,

Many thanks for the information.