flat assembler
Message board for the users of flat assembler.

Index > Main > Getting the address of a local variable?

Author
Thread Post new topic Reply to topic
silkodyssey



Joined: 02 Oct 2003
Posts: 198
Location: St.Vincent & the Grenadines
silkodyssey 15 Oct 2003, 12:40
Is there an easier way of getting the address of a local variable than using the lea instruction? Something like the addr operator in masm.

_________________
silkodyssey
Post 15 Oct 2003, 12:40
View user's profile Send private message MSN Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
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
Post 15 Oct 2003, 12:57
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv 15 Oct 2003, 13:18
Address of local variables?

lea eax, [ebp-8]
Post 15 Oct 2003, 13:18
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
silkodyssey



Joined: 02 Oct 2003
Posts: 198
Location: St.Vincent & the Grenadines
silkodyssey 15 Oct 2003, 13:20
Thanks for replying Smile 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
Post 15 Oct 2003, 13:20
View user's profile Send private message MSN Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
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. Smile
Post 15 Oct 2003, 13:35
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
silkodyssey



Joined: 02 Oct 2003
Posts: 198
Location: St.Vincent & the Grenadines
silkodyssey 15 Oct 2003, 13:44
Ok thats interesting Smile That eases my conscience a bit. So I can safely conclude that its a good idea to use local variables then Very Happy

_________________
silkodyssey
Post 15 Oct 2003, 13:44
View user's profile Send private message MSN Messenger Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv 15 Oct 2003, 15:27
Hopefully your local variable can fit into 127bytes if not the instruction would be longer.
Post 15 Oct 2003, 15:27
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
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.
Post 15 Oct 2003, 15:43
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 15 Oct 2003, 16:37
You can use ADDR operator with variant of invoke macro from the "win32ax.inc" include.
Post 15 Oct 2003, 16:37
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.