flat assembler
Message board for the users of flat assembler.
Index
> Windows > How to align local variables ? |
Author |
|
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)... |
|||
13 May 2006, 12:55 |
|
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)... |
|||
13 May 2006, 13:15 |
|
f0dder 13 May 2006, 13:17
The_Grey_Beast wrote:
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. _________________ - carpe noctem |
|||
13 May 2006, 13:17 |
|
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) |
|||
13 May 2006, 13:21 |
|
f0dder 13 May 2006, 13:25
The_Grey_Beast wrote: Why do you need to preserve it? Are the locals of variable length? 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. _________________ - carpe noctem |
|||
13 May 2006, 13:25 |
|
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 |
|||
13 May 2006, 19:25 |
|
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.
|
|||
13 May 2006, 19:34 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.