News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Stack alignment

Started by TouEnMasm, November 26, 2020, 05:06:58 AM

Previous topic - Next topic

TouEnMasm

Hello,
I need to align the first local with UASM in 32 bits.
There is an option in 64 but in 32 ?
Fa is a musical note to play with CL

Vortex

Hi ToutEnMasm,

What about coding a custom prologue macro inserting the statement and esp,-32 to align the stack?

http://masm32.com/board/index.php?topic=5850.0

TouEnMasm


Thanks ,
I find a way :

Quote
   mov gardeesp,esp        ;keep esp before call
   sub esp,10H  ;add something unused
   and esp,0FFFFFFF0h       ;align esp
   sub esp,0Ch                  ;sub constant who depend of the size of frame,calcul below
    invoke  testfunc,ADDR msg,ADDR capt
     mov esp,gardeesp           ;restore esp

inside the proc calculate the needed constant


Quote
    lea     eax,a                        ;first local
   mov edx,gardeesp
    sub edx,eax
    mov eax,edx                     
    add edx,10h
    and edx,0FFFFFFF0h   
    sub edx,eax         ;edx value of needed constant   
Fa is a musical note to play with CL

TouEnMasm

#3
METHOD with PROLOGUE EPILOGUE
Modifiying esp inside a prologue don't work because EBP need to adress the argument of the function and the local variables.
It is the compiler who give the decalages to ebp and don't take care of a possible change of esp.Mov ebp,esp his followed by the
creation of the locals .All the locals will be good.Only a proc without argument can modify the stack and align .
The method need a different pointer for functions arguments ,below soluce

Quote
   xchg ebp,ebx         ;prepare to adress an argument of function
   mov eax,pg_matrix
   xchg ebx,ebp         ;prepare to adress a local variable




Fa is a musical note to play with CL

TouEnMasm


Final soluce without using ebx

adressing a function argument is as follow:
Quote
   xchg ebp,[esp]         ;prepare to adress an argument of function
   mov eax,pg_matrix
   xchg [esp],ebp         ;prepare to adress a local variable

prologue and epilogue


AlignPrologue MACRO procname, flags, parambytes, localbytes, reglist, userparms:VARARG
;-----------------------------added ebx will point on function argument ---------------------------------
mov edx, esp
sub esp, 8
;esp before invoke 001bfea8
;esp after invoke  001bfe60 ,001bfea8 - 001bfe60= size arg 44h + 4 (call) = - 48h
;esp point on return adress ,(esp + 4) last push arg
;Now need calculate to align without erase anything
;The and will erase unit max 0Fh,min 0  ,si 0 ,esp + 4 erase call
;Three operation     SUB AND +
;si esp 0 terminate ,-8    -0 + 4 ;increase stack -4 just needed for push ebp
;si esp F terminate ,-8 -0Fh + 4 ;increase stack -19
and esp, -16 ; fffffff0H   erase unit and align esp 16
add esp, 4 ;--- for push ebp
;-----------------------normal construction ---------------------------------------------------------
IF localbytes GT 0   
push    ebp
mov     ebp,esp ;ebp will point on local variables
sub     esp,localbytes       
ELSEIF parambytes GT 0 
push    ebp
mov     ebp,esp   
ELSEIFNB <userparms>
IF @InStr(1,<userparms>,<FORCEFRAME>)
push    ebp
mov     ebp, esp         
ENDIF
ENDIF
;----------------------------------------
IFNB <reglist>   
FOR     reg,reglist
push    reg
ENDM
ENDIF 
push edx ;needed for exchange value with ebp
EXITM <localbytes>
ENDM


AlignEpilogue MACRO procname, flags, parambytes, localbytes, reglist, userparms:VARARG
pop edx
IFNB <reglist>   
FOR reg,reglist       
pop     reg           
ENDM
ENDIF   
IF (parambytes GT 0) OR (localbytes GT 0)   
mov     esp,ebp
pop     ebp       
ELSEIFNB <userparms>   
IF @InStr(1,<userparms>,<FORCEFRAME>)       
mov     esp,ebp
pop     ebp           
ENDIF       
ENDIF
;----------------- rajout ------------------------
mov esp, edx
;----------------------------------------
IF (flags AND 10h) OR (parambytes EQ 0)
ret             ; retn - Poasm
ELSE
ret parambytes  ; retn - Poasm
ENDIF
ENDM




Fa is a musical note to play with CL