flat assembler
Message board for the users of flat assembler.

Index > Windows > How to align local variables ?

Author
Thread Post new topic Reply to topic
Kuemmel



Joined: 30 Jan 2006
Posts: 200
Location: Stuttgart, Germany
Kuemmel 13 May 2006, 11:54
Hi people,

in our fractal thread we had an issue that might be interesing for others, too. The local alignment of variables. As far as I understand a proc ... with local variables is assembled regarding the variables and adjustment of the stack pointer to

push ebp
mov esp, ebp
sub esp, xx (depending on how much memory is needed)

I found executables that uses an extra

and esp,0xfffffff0 then to align the stack pointer, like align 16 or what ever value

How can this be done with FASM, even as a hack, but reliable in the 'right' way ?
Post 13 May 2006, 11:54
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 May 2006, 12:55
First, the XX value for ESP must be large enough to accomodate both local variables and the alignment - that's easy enough. But now comes the issue of adressing... if EBP is aligned, locals will be aligned, but we can't access function arguments through EBP.

If ESP is aligned, we need to change LOCALs access to go through ESP instead of EBP (two bytes longer opcode for each instruction)...
Post 13 May 2006, 12:55
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 13 May 2006, 13:15
f0dder wrote:
If ESP is aligned, we need to change LOCALs access to go through ESP instead of EBP (two bytes longer opcode for each instruction)...
But you save one register.... ebp Wink
Post 13 May 2006, 13:15
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 May 2006, 13:17
The_Grey_Beast wrote:
f0dder wrote:
If ESP is aligned, we need to change LOCALs access to go through ESP instead of EBP (two bytes longer opcode for each instruction)...
But you save one register.... ebp Wink


No, you still need EBP to preserve the original ESP value. And no, you cannot use a global variable for this since it will make the code non-reentrant.

_________________
Image - carpe noctem
Post 13 May 2006, 13:17
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 13 May 2006, 13:21
Why do you need to preserve it? Are the locals of variable length?
If they're constant, it's simple to restore esp back through a constant add or somesuch... you just have to keep track of it. sorry for misunderstanding (if so)
Post 13 May 2006, 13:21
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 May 2006, 13:25
The_Grey_Beast wrote:
Why do you need to preserve it? Are the locals of variable length?
If they're constant, it's simple to restore esp back through a constant add or somesuch... you just have to keep track of it. sorry for misunderstanding (if so)


The thing is that we need aligned locals - since a function can be called with just about any ESP value, the stack needs to be (re)aligned at runtime.

_________________
Image - carpe noctem
Post 13 May 2006, 13:25
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 13 May 2006, 19:25
This is where having two stacks would be great.

But the global mem/data is a good enough option if ...
-Your function won't be called in a multithreaded environment
-Your function isn't recursive.

But in those cases you can just use aligned global memory for the local variabls themselves.

If you really need the extra register space, so that the ebp, esp, and FFFFFFF0h solution won't work for you then you can take up only HALF or a QUARTER of a register.

Say you only need BX not EBX but you need every other register.
Code:
Func:
push ebx
mov ebx,esp
sub esp, NUMLOCALS*?+16
;;if all locals need to be 16byte aligned ?=16 otherwise ?=4 dword
shl ebx,16
and esp, 0FFFFFFF0h
... mov BX, word[ blah ];; preserve top half of ebx
shr ebx, 16
and esp, 0FFFF0000h
or esp,ebx
pop ebx
ret
    
Post 13 May 2006, 19:25
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 May 2006, 19:34
A generic solution would be preferable - and yeah, the code that brought this topic up is MT, so we can't do globals.
Post 13 May 2006, 19:34
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.