Author Topic: stack and stack variables alignment  (Read 13014 times)

Timothee Besset

  • Guest
stack and stack variables alignment
« on: October 02, 2007, 04:40:55 PM »
I need to move several MSVC inline assembly functions over to nasm on Linux x86. I'm having a problem with local variables alignment.

gcc aligns the stack on 8 byte boundary, and I need local variables in my functions that are 16 byte aligned. Usually all the code relies on index based addressing with ebp, but if I have to snap ebp to a 16 byte boundary, I can't use it for both function parameters and stack variables.

Atm I am planning to use some of the general purpose registers for the stack variables. I am wondering if there is a better way to do this though?


  • Guest
Re: stack and stack variables alignment
« Reply #1 on: January 30, 2008, 07:06:09 PM »
Are you using a lot of pushes/pops? If not, you can use esp for local vars and ebp for passed in parameters, like:

%define LocalVar1 [esp] ; 128bit value for SSE
%define LocalVar2 [esp + 16] ; A second value for SSE

%define Param1 [ebp + 4] ; Passed in 32bit var1
%define Param2 [ebp + 8] ; Passed in 32bit var2

  push ebp
  mov ebp, esp
  sub esp, BytesOfLocalVars ; Make sure we have the space we need (32 for this sample)
  and esp, ~0x0F ; Align to 16

; Access your local vars with LocalVar1, LocalVar2 (esp relative)
  ; Access your parameters with Param1 and Param2

mov esp, ebp
  pop ebp

Sorry for any typos, I'm just doing this off the top of my head...