hi all, i've read many message about it before post this one. Seems assembler don't have the chance to declare a local static variable.
I need LOCAL STATIC because i need a faster use of the variable than a GLOBAL one.
So from what i've understood,
GLOBAL is allocated somewhere on the global adress space
LOCAL is allocated on the stack at run time, so my variable is on the cache, faster to use than a global one (what i need).
LOCAL STATIC should be fast as a LOCAL (stack->cache, but i'm not really sure of this), but the variable mantain the value assigned the first use, and the variable will not been reinitialized at every call of the procedure.

If is not true that a LOCAL STATIC is allocated on the stack, i don't need to use LOCAL becouse GLOBAL will work in the same way.

Hope someone clarify.

Thanks B7
Posted on 2003-08-14 02:59:06 by Bit7
LOCAL is allocated on the stack at run time, so my variable is on the cache, faster to use than a global one (what i need).

I belive that any data you're reading is in cache, (so stack is to). And data you're writing is
Just movntq can write directly into memory

But still possible:
start:
;mov EBP,ESP
push STATIC
.....
some code, calls/ret, push/pop, etc
MOV EAX, - hmm. where is the address of STATIC?
some code, calls/ret, push/pop, etc
.....
pop reg32 ; correct stack
ret
end

The problem is ESP - it changes
But what is stack? I think it is just area in memory, that can be accessed through ESP
Why not use just another reg?
I use EBP as a pointer to global variables, instead of ESP and proceduress w/o stack frame
so i can use MOV EAX, EBP , MOV EAX, EBP...
and my data are always in cache, just one problem - bank conflicts :(

May be variables can be just STATIC/DINAMIC , but not LOCAL/GLOBAL ? - this comes from HLL

S.T.A.S.
Posted on 2003-08-14 03:36:23 by S.T.A.S.
Under the hood, a static local variable is a global variable, but it has local scope on the compiler level. It's basically the same thing though.
Posted on 2003-08-14 09:09:04 by iblis
I was wondering on another thing. Since static variables are (at least in WC++) globals, can I define multiple gobal variables with same name? I mean something like this:


void f00(void)
{
static unsigned int x, y;
Blah...Blah...Blah;
}

void f01(void)
{
static unsigned int x, y;
Blah...Blah...Blah;
}

I can allways use names such f00_x and f01_x, but...
Posted on 2003-08-14 09:19:52 by iwabee
Yes, you can do that.
Posted on 2003-08-14 11:50:00 by iblis
umm...yes, but is there another way?
Posted on 2003-08-14 13:31:40 by iwabee
maybe a tricky way can be... with the respective format of directives

1) put the static locals vars like you do with the names f00_x and f01_x.

2) with a %define foo f00_x in the first prcedure, and in the second %undef foo, then %define foo f01_x

This is like i think you can handle, at least is some way that a compiler can do, call in diferent ways in the data sections, and then with a like mnemonic or tag acces the respective var in the specific context of the specific function. Get the idea??.


f00_x dd 4
f01_x dd 8


functionOne:
%define foo f00_x
; use if like only foo (the preprocesor do the work of replace fort the correct word ;), and at the end of the proc
%undefine foo

;more here... (you can not acces foo (you undefine the name)
functionTwo:
%define foo f01_x
;more here...., and at the end of the proc:
%undefine foo
; and you still having foo like acces only in the functions, like local, but they are in the data with a diferent name ;)


Nice day.
Posted on 2003-08-14 13:43:36 by rea
... the cache, faster...

iwabee
may be C compiler will use prefetchnta instruction to speed up runtime?
Posted on 2003-08-14 20:15:28 by S.T.A.S.
Damn, I didn't wrote anything about cahce and optimization. I just wanted to find easy way to define static local variables, like:
.data?

f00_x dd ?
;...

.code
;....
f00 proc
STATIC x:f00_x
;....
f00 endp
;...
But since masm32 dos not support anything like that, I guess I have to use %define and %undefine.
Posted on 2003-08-15 13:27:36 by iwabee

I just wanted to find easy way to define static local variables


push ?

i belive that "static local variables" is just HLL stuff

f00_x is on stack - so say it's local

until we use mov , smth - it'll be static :cool:
Posted on 2003-08-15 18:55:33 by S.T.A.S.

Under the hood, a static local variable is a global variable, but it has local scope on the compiler level. It's basically the same thing though.


so if its a global variable really, does this mean that when i have sumthing like, say LOCAL wd:WNDCLASSEX inside a proc of mine when using MASM32, it will basically just add it to the data section and give it a special name so all references inside the proc that refer to it will internally use that special name?

im not too clear on how LOCAL works in MASM...thanks
Posted on 2003-08-16 13:50:55 by AnotherWay83
There's a great difference between a 'local variable' and a 'static local variable'. MASM doesn't have static local variables (at least not as feature, but you can of course achieve the same effect). Local != stack. Local means: local scope, i.e. only accessable within the same scope. A 'local variable' is implemented using the stack, since the stack is temporary and so is the local scope. This is the type of locals masm has (LOCAL statement). A 'static local variable' is implemented as a global variable, but one that can only be accessed within the scope it was declared. MASM has no such thing built in.

Thomas
Posted on 2003-08-16 14:28:11 by Thomas
Thomas!
I do belive you understand programming much better tham me,

but as I understand this
Originally posted by Bit7
I need LOCAL STATIC because i need a faster use of the variable than a GLOBAL one.
So from what i've understood,
GLOBAL is allocated somewhere on the global adress space
LOCAL is allocated on the stack at run time, so my variable is on the cache, faster to use than a global one (what i need).
LOCAL STATIC should be fast as a LOCAL (stack->cache, but i'm not really sure of this), but the variable mantain the value assigned the first use, and the variable will not been reinitialized at every call of the procedure.

so I just assume to use push STATIC to create LOCAL STATIC variable at runtime,
Sinse push is hardcoded, let's say "the variable will not been reinitialized at every call of the procedure". We can use mactro (or prologue) for this.
Still I'm trying to avoyd this technique to keep off hardcoding (i tend to use some dinamic "objects", keeping LOCAL variables, and since some variables are initialized just at creation of "object", I call them CONSTANTs.
But at the other side I may say, that
mov r32, i32 uses LOCAL STATIC (but don't use cache)
Variables are abstraction, we've got just registers and memory at the asm level.;)
Posted on 2003-08-16 22:56:16 by S.T.A.S.

so I just assume to use push STATIC to create LOCAL STATIC variable at runtime,
Sinse push is hardcoded, let's say "the variable will not been reinitialized at every call of the procedure". We can use mactro (or prologue) for this.


I'm not aware of any function in MASM to define or exclude a variable from being redefined on proc rentry. If you dont want a variable to be reinitilized, code so that doesn't happen.

As Thomas said, STATIC and LOCAL are about scope. In a HLL, the compiler with make the code to retain proper scope of variable(or retain the rule that you cannot access variables out of scope). In assembly, thats your job.
Posted on 2003-08-17 00:18:04 by ThoughtCriminal

Thomas!
I do belive you understand programming much better tham me,

but as I understand this

so I just assume to use push STATIC to create LOCAL STATIC variable at runtime,
Sinse push is hardcoded, let's say "the variable will not been reinitialized at every call of the procedure". We can use mactro (or prologue) for this.
Still I'm trying to avoyd this technique to keep off hardcoding (i tend to use some dinamic "objects", keeping LOCAL variables, and since some variables are initialized just at creation of "object", I call them CONSTANTs.
But at the other side I may say, that
mov r32, i32 uses LOCAL STATIC (but don't use cache)
Variables are abstraction, we've got just registers and memory at the asm level.;)

I can't follow everything you say here but if your main reason is the speed of the cache it has no use copying it on the stack.. Since you need to reference the global variable to copy it, that will then be put into the cache so there's no extra performance. In fact, copying a global onto the stack and then use it is probably less efficient than using the global variable each time. In both cases, the global variable will be in the cache after the first reference, but in the former, an extra copy instruction is needed (global->local), and the addresses you need to reference are dynamic (esp/ebp+/-something), while the global variable has a static address.

Thomas
Posted on 2003-08-17 04:14:49 by Thomas
Well, that depends. If you have reference that global (static) variable more than 4 times in your code, then with "S.T.A.S's method" (:)) you can produce smaller code.
Example:


FF3500004000 | push dword ptr [00400000]
8F0500004000 | pop dword ptr [00400000]
8B45F0 | mov eax, dword ptr [ebp - 10h]
vs.
8B0500004000 | mov eax, dword ptr [00400000]

See? "dword ptr " is 3 bytes smaller than dword ptr [00400000].
Posted on 2003-08-17 07:41:32 by iwabee
Sorry, guys. It's not easy for me to speak your language :(

push CONSTANT
NOT push
this is gREAT MASM sintax. i love it, too :grin:
or: mov r32, CONSTANT
NOT mov r32,

at asm i assume GLOBAL= existing all the runtime
(if I know address, I can access them from any point of my programm)
LOCAL=accessable just from some proc (have no constant address) - real way to get this w/o tricks - use stack
But all of them are NOT CONSTANTs (if we do not use Read/Only pages)
CONSTANTS in asm=LABELS, they are determined at compile time and have no easy way to be changed in standart PE at runtime

I belive that any data you're reading is in cache, (so stack is to)


The only thing i wanted to say
Variables are abstraction, we've got just registers and memory at the asm level.

let's say EAX is LOCAL, but EBP is GLOBAL :grin:

I'm not programmer, I understand just von Neumann:stupid:

P.S. What is Handle? What I get in EAX after "invoke GetModuleHandle, 0"?
Debugger can give me the REAL ansver, but what about Win32 Programmer's Reference?
There lot of things we can miss using HLL
(may be this is right for ASM, too) ;)
Posted on 2003-08-17 23:06:04 by S.T.A.S.