flat assembler
Message board for the users of flat assembler.

Index > Main > Local Variables

Author
Thread Post new topic Reply to topic
Worr



Joined: 23 Jul 2004
Posts: 5
Location: Ohio, USofA
Worr 23 Aug 2004, 03:48
This may sound like a noobish queston, and probably is, but here it goes. I was looking for something like this before, with a variable that is only used for a certain procedure, and there was period before the variable name, or something like that. I wasn't able to really find out much about it. I don't need so much of a variable that will only last as long as the procedure, but one that will only be created if the procedure is used, since I'm going to be having a lot of procedures that I will be using specifically for debugging. I figured if anything I can just make a certain section of the variables seperate from the others that I can just delete once I'm done debugging, but having a variable created only if there is a fuction would be much easier.

I had also seen something to the extent of:

Code:
proc_1:
   jmp .vars_skip
     ;vars here
   .vars_skip:
    


If that works then I'll use it since the speed doesn't matter at all, just so long as when I don't have the proc there is no space taken up for the variable.

Thanks,
Worr
Post 23 Aug 2004, 03:48
View user's profile Send private message Visit poster's website AIM Address Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Imagist 23 Aug 2004, 03:51
Oo! Interesting question!

*waits for someone to answer it, since he doesn't know*
Post 23 Aug 2004, 03:51
View user's profile Send private message Visit poster's website Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 23 Aug 2004, 04:10
Code:
function1:
   jmp .over_data
.a dd 0
.b dd 1
.over_data: mov eax, [.a]
                 mov ebx, [.b]
ret

function2:
   jmp .over_data
.a dd 3
.b dd 4
.over_data: mov ecx, [.a]
                 mov edx, [.b]
ret    


And so on. This has worked in all my tests, so I see no reason not to make use of it. As long as the variable name starts with '.' it should work just fine.
Post 23 Aug 2004, 04:10
View user's profile Send private message Visit poster's website Reply with quote
Dragontamer



Joined: 24 Aug 2003
Posts: 84
Dragontamer 23 Aug 2004, 12:12
Wow, self modifying code. Smile

Thats not how you do that... Unless you have SMC, windows or linux will surely crash your program.

You cannot treat code segment stuff as data unless you make SMC availiable for your program.

Now the real method is much more complex, and manipulates the stack in a very hard way...

Code:
function1:
push ebp
mov ebp, esp
sub esp, 32; size of bytes you are allocating. I just allocated 32 bytes
virtual at esp
  ;vars here max 32 bytes because i only added 32
end virtual
;rest of function
mov esp, ebp
pop ebp
ret
    


Oh, and you can't "initialize" the variables. It always has to either be rb or db ?

(edit note. I said add esp,32?!?!?? Stupid me, i forgot that the stack is upside down)


Last edited by Dragontamer on 23 Aug 2004, 13:23; edited 1 time in total
Post 23 Aug 2004, 12:12
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 23 Aug 2004, 13:05
Quote:
Wow, self modifying code.

Thats not how you do that... Unless you have SMC, windows or linux will surely crash your program.

You cannot treat code segment stuff as data unless you make SMC availiable for your program.


Hmm, I always use a writeable code segment, so I tend to forget that...
Post 23 Aug 2004, 13:05
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Aug 2004, 20:11
I think he wanted this (with usage of macros)
Code:
DEBUG = 1

;procedure name
proc a
  ;list of arguemnt follows
  var1 dd 0
  var2 dd 0

  ;conditionally define data
  if DEBUG = 1
    debugvar1 dd 0
    debugvar2 dd0
  end if

 ;and here goes the code
enter
  ...
    

And when you are done debugging, you change first line to "DEBUG = 0".

is this what you wanted? (to conditionally define data?)
Post 23 Aug 2004, 20:11
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Worr



Joined: 23 Jul 2004
Posts: 5
Location: Ohio, USofA
Worr 25 Aug 2004, 00:51
All of those are what I wanted. I'm not sure I described it well at all the first time around.

The Jump method by crc is all that I needed, but the use of the virtual thing (for lack of better words) was going to be my next question. The method by vid will be helpful too, since it does what I want, just better and makes more sense, to me atleast.

Thanks Very Happy
Worr
Post 25 Aug 2004, 00:51
View user's profile Send private message Visit poster's website AIM Address Reply with quote
Zetus



Joined: 03 Jun 2004
Posts: 37
Zetus 25 Aug 2004, 07:21
locals not need to avoid by jump. Always in stack. If we have used this variable in proc, it reserves stack space, if not used - no reserve.

proc Xe,
enter
mov eax,[.A]
return
.A dd 0
.B dd 0

".B" variable not used, and only 1 dd reserved in stack. Compiler "forgots" your locals after you define a next proc.
Post 25 Aug 2004, 07:21
View user's profile Send private message ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 25 Aug 2004, 18:21
zetus, you're wrong. What you did is placing variables in code, which also requires to have code segment writeable. Absolutely bad thing to do unless you know what you are doing.
Post 25 Aug 2004, 18:21
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 25 Aug 2004, 22:01
Why is it considered bad to have a writeable code segment? I understand that some security risks can arise, but that's the only downside I see to it.
Post 25 Aug 2004, 22:01
View user's profile Send private message Visit poster's website Reply with quote
Dragontamer



Joined: 24 Aug 2003
Posts: 84
Dragontamer 26 Aug 2004, 02:27
Ditto. But having data that close to the code is bound to screw up your cache memory.
Post 26 Aug 2004, 02:27
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 26 Aug 2004, 04:03
I prefer to avoid having data on code segment unless strictly needed.
Post 26 Aug 2004, 04:03
View user's profile Send private message Yahoo Messenger Reply with quote
Zetus



Joined: 03 Jun 2004
Posts: 37
Zetus 26 Aug 2004, 09:44
We discuss a LOCALS,not?
In TASM terms, It's must be in stack, no difference where I place it in text.

Most of locals we don't need out of procedure...

Or we need two types of variables: loacals WITH real bytes reserve, and locals, temporary existing in stack,while in proc.

No check by myself ))
Post 26 Aug 2004, 09:44
View user's profile Send private message ICQ Number Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 26 Aug 2004, 16:25
[quote="Zetus"]We discuss a LOCALS,not?
No only stack variables are locals
you are talking about what in 'c' language is called 'static', but you place it
in code segment instead of data segment, that is useless and the code
can not be shared in a multithread program.
Post 26 Aug 2004, 16:25
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 26 Aug 2004, 20:33
also recursion is impossible, code portability is reduced (when someone place it in nonwritable space), it slows down code greatly, doesn't work with CS != DS (ok, this one isn't that serious), and also stack variables are usually faster than one in data (note mentioning those in code) because stack is usually cached.
Post 26 Aug 2004, 20:33
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Mac2004



Joined: 15 Dec 2003
Posts: 314
Mac2004 30 Aug 2004, 10:52
I have been using this local variable method for quite long time and it works. Jmp instruction must be used due to cpu treating our local variables as instructions. I mainly do plain binary coding(=OS development) without need to worry about code and data segments. Everything has worked neatly so far. Smile

Worr, This method really works and I would recommend using it. It's simple to use!! Smile


regards,
Mac2004
Post 30 Aug 2004, 10:52
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 30 Aug 2004, 11:31
yes it works, but is less effective and more dangerous, as i have said, you must know what you are doing.
Post 30 Aug 2004, 11:31
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Mac2004



Joined: 15 Dec 2003
Posts: 314
Mac2004 30 Aug 2004, 12:06
Quote:

yes it works, but is less effective and more dangerous, as i have said, you must know what you are doing.


That's right vid!! True assembler programmer needs to know he's/she's doing. Not knowing can cause quite a mess eg. destroying hard drive partitition tables.

regards,
Mac2004
Post 30 Aug 2004, 12:06
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 30 Aug 2004, 18:49
well, then we agreed.
btw, i am using variables in code too, but don't tell anyone... I just didn't want to learn others such things until they understand it.
Post 30 Aug 2004, 18:49
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Worr



Joined: 23 Jul 2004
Posts: 5
Location: Ohio, USofA
Worr 30 Aug 2004, 21:57
Yeah, I know how it works and it's risks, but I know enough of what I'm doing, so it works for me. I'll use it for quick simple things, but I know that it isn't the proper way.

Worr
Post 30 Aug 2004, 21:57
View user's profile Send private message Visit poster's website AIM Address 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-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.