flat assembler
Message board for the users of flat assembler.
Index
> Main > Getting the address of a local variable? |
Author |
|
JohnFound 15 Oct 2003, 12:57
Hi.
I don't know about masm, but the local variables are in the stack and the ebp points to the end of the local block (stack frame). So the address of local variable is not constant and may be changed during different runs of the procedure. So there is no other way to get the address of the variable, but lea instruction. Note that you need the address of the local variable only if you want to use it as argument to another function. For other purposes you may use simply: Code: mov [.somelocal], ebx Regards |
|||
15 Oct 2003, 12:57 |
|
roticv 15 Oct 2003, 13:18
Address of local variables?
lea eax, [ebp-8] |
|||
15 Oct 2003, 13:18 |
|
silkodyssey 15 Oct 2003, 13:20
Thanks for replying I guess I'll just use the lea instruction then. I'm a fan of local variables. I like my executables to be as small as possible so I don't want to store too much data in the data section. But I'm not sure that this is the right approach to take. A lot of api functions require the address of data and if i use local variables too much I'll be using the lea instruction a lot. Is this a bad way to write code when declaring the variable in the data section would negate the need for the lea instruction?
_________________ silkodyssey |
|||
15 Oct 2003, 13:20 |
|
JohnFound 15 Oct 2003, 13:35
[code]
lea eax, [ebp-imm8] ; 3 bytes push eax ; 1 byte ------------ total: 4 bytes push imm32 ; 5 bytes. Using local variables results 1 instruction more, but 1 byte less. |
|||
15 Oct 2003, 13:35 |
|
silkodyssey 15 Oct 2003, 13:44
Ok thats interesting That eases my conscience a bit. So I can safely conclude that its a good idea to use local variables then
_________________ silkodyssey |
|||
15 Oct 2003, 13:44 |
|
roticv 15 Oct 2003, 15:27
Hopefully your local variable can fit into 127bytes if not the instruction would be longer.
|
|||
15 Oct 2003, 15:27 |
|
JohnFound 15 Oct 2003, 15:43
Well, you are right, but using local variables is good mainly not because of smaller size. It's good programming practice for every language, including assembly (or maybe particularly for assembly), especially in big projects. Using local variables make program more clear and readable and helps preventing bugs.
|
|||
15 Oct 2003, 15:43 |
|
Tomasz Grysztar 15 Oct 2003, 16:37
You can use ADDR operator with variant of invoke macro from the "win32ax.inc" include.
|
|||
15 Oct 2003, 16:37 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.