flat assembler
Message board for the users of flat assembler.

Index > Windows > return local variable.

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 01 Sep 2012, 16:03
Hi, I'm having issue with local variables. I've tried to return local variable and show MessageBox pointed to that local variable, but it somehow fails, as I guess, it's a stack problem there.
Here's what I mean.
Code:
proc firstproc
stdcall secondproc ;will return in eax.
;MessageBox shows different text from unknown address in stack.
invoke MessageBox, 0, eax, 0, 0x40 ;MB_OK+MB_ICONINFORMATION
ret
endp


proc secondproc
locals
msg db "Hello, World!",0
endl
lea eax,[msg]
ret ;eax = local variable
endp
    


How can I fix this ?
Post 01 Sep 2012, 16:03
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 01 Sep 2012, 16:24
The locals variables are, well, "local". They does not exists outside of their scope - i.e. "secondproc" in your example. You should not refer to them after secondproc exits.
Also, the local variables should be undefined, but I am not sure about "locals" macro from FASM package. Maybe it generates the initialization code. (If so, IMHO, it is not a good practice at all, because it hides important code from the user).
Post 01 Sep 2012, 16:24
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 01 Sep 2012, 16:33
Hi, I'm aiming C-style returns to avoid much global variables. I tried moving them with ESI, EDI and rep movsb and it succeed. How C programs return local variables then ?
Thanks.
Post 01 Sep 2012, 16:33
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 01 Sep 2012, 18:45
I don't know C. In Pascal you can't return pointer to local variables. You can return the value of the local variable, but it is not the same.
Post 01 Sep 2012, 18:45
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 01 Sep 2012, 19:36
Overflowz wrote:
How C programs return local variables then ?
Thanks.

Code:
int GetValue (void)
{
        static int value = 666;
        return value;
}    

The scope is local but storage is global.
You could do something like this in fasm to hide variables...
Code:
GetValue:
  mov eax, [.private_data]
  ret
.private_data:
  dd 666        ;gets stored in code section!
    


And we could still access it globally like...
Code:
  mov eax, [GetValue.private_data]    


Hope this helps...

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 01 Sep 2012, 19:36
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 02 Sep 2012, 01:21
That's exactly what I was looking for! Thank you!
Post 02 Sep 2012, 01:21
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 02 Sep 2012, 04:06
This is beginners stuff. I thought you knew this already.

Not being rude or something.

http://programming-motherfucker.com/become.html

lol
Post 02 Sep 2012, 04:06
View user's profile Send private message Reply with quote
kalambong



Joined: 08 Nov 2008
Posts: 165
kalambong 02 Sep 2012, 08:30
A real nice page, unfortunately, not very pc Smile
Post 02 Sep 2012, 08:30
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
AsmGuru62 02 Sep 2012, 11:58
Global variables are awesome!
No avoiding them in ASM.

So, Overflowz, you wish to avoid something like a string "some text" put into a global storage?
The only way to do it is put all strings into a file and load it at startup:
Code:
FILE MESSAGES.TXT:
-------------------------------------------------
{0001}A line of text for some message.
{0002}Another line of text for a message box.
-------------------------------------------------
    

And then access the text by an index.
Any 'normal' EXE however has all these strings as global variables.

The values in {} are just for your reference, so you know which index refers to which text.
When loading the file simply skip 6 bytes from the line beginning to get an address to string.
Post 02 Sep 2012, 11:58
View user's profile Send private message Send e-mail 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.