News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Static Variables/Compilers

Started by redskull, February 09, 2006, 12:25:07 AM

Previous topic - Next topic

redskull

Back in my compiler days, i remember using alot of static variables which were local variables that were preserved through function calls.  Now with assember, there doesn't seem to be that option, which makes sense if I understand everything (not a good bet, statistically speaking).  'Global' variables are literal addresses within the executable file, and 'local' variables are created on the stack during run-time?  If I want a 'static' variable, it's got to be a global?  And just for my own edification, when a C compiler makes an executable with static variables, it makes them global and just checks the references to them to keep the programmer honest?

As always, i appreciate the mavens who indulge my ignorance
alan
Strange women, lying in ponds, distributing swords, is no basis for a system of government

zooba

That's correct. Static variables are globals which the C compiler won't let you use it outside of it's scope. The closest you can get is to define the global within the procedure. I don't think this will cause MASM to restrict the scope at all, but perhaps the label may not exist outside of the procedure. Should be easy enough to try...

Okay, I've tried it and there appears to be no scope restrictions and forward references are resolved. So you can create a global anywhere in your file (they aren't automatically resolved between source files unless you use EXTERNDEF) and it is accessible throughout that file.

Hope this answers your question :U

Zooba

Ratch

zooba & redskull,
     I don't think that "local" and "global" belong in the assembler terminology.  They should be called "stack variables" or "dynamic variables", and "static variables".  There appears to be a lot of emphasis on whether or not one can access a higher or lower level variable, and/or whether or not it can have same name.  Who cares?  If a program is not supposed to reference something out of its scope, then don't code it to do it, period.   None of us needs a nanny to keep us out of trouble, do we?  Otherwise we shouldn't be assembling.  Just use the stack or Global/LocalAlloc for when you need temporary storage, and use .DATA, .DATA? or .CONST if you need to keep the storage during the entire life of the execution.  Don't get hung up on high level language concepts, constructs, or implementations.  Ratch

raymond

The advantage of the .DATA? section is that it will not affect the size of your program but space will be allocated at the end of the regular .DATA section at run time without having to call any API to allocate such needed space. All the labels you use in that section are considered as being for "static variables".

Occasionally, when I have a program with a lot of "static variables", I would put them in a separate file so that I can have access to them while I'm writing the code. At the begining of the main code file, I insert an INCLUDE line for each of the other files which may be part of the program without the need to write a long list of EXTERNDEFs.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

hutch--

The notion of STATIC variable is itself a high level language concept but simple enough to emulate in assembler. At low level there are two types of variables, those of GLOBAL scope written in the .DATA or .DATA? sections and you have variables allocated on the stack at runtime, usually known as LOCAL variables. MASM and similar allow you to write code like this within a proc,


  LOCAL mylocalvar :DWORD

.data
  myvar dd 12345678
.code

    mov eax, OFFSET myvar
    mov mylocalvar, eax


The variable "mylocalvar" is now a STATIC variable in the high level sense.

Wrap this in a macro where the name of the item in the data section is a macro local (always unique) and always return the OFFSET and you are there.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php