News:

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

Static Scoping

Started by llkooj, December 06, 2008, 09:28:12 AM

Previous topic - Next topic

llkooj

I want to be able to define variable of the same name in .data and in a procedure. Is it possible?

.data
myVar DWORD ?
.
.
.
.
proc1 PROC
myVar DWORD ?
.
.
.

I tried using LOCAL myVar DWORD ? in proc1 but I had redefinition compiler error

donkey

MASM does not allow it, however it is perfectly fine to do it in GoASM which has much more powerful scoping than MASM.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

llkooj,

Put a leading underscore to one of those variables.

llkooj

Is it true static by putting a leading underscore on the variable, say in the procedure ? My solution has to be true static scoping.

MichaelW

I can't imagine any reason why you would need to, or even want to, use the same name for two separate pieces of data, with one of them global, but it does appear to be possible, at least with ML 6.14 and in a limited context:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .code

myproc proc dummy:DWORD
    LOCAL myvar:DWORD
    mov myvar, 123
    print ustr$(myvar),13,10
    ret
myproc endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      myvar DWORD 456
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    push 0
    call myproc
    print ustr$(myvar),13,10
    invoke myproc, 0
    print ustr$(myvar),13,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


123
456
123
456
Press any key to exit...

eschew obfuscation

jj2007

Quote from: MichaelW on December 06, 2008, 08:58:31 PM
I can't imagine any reason why you would need to, or even want to, use the same name for two separate pieces of data, with one of them global, but it does appear to be possible, at least with ML 6.14 and in a limited context:

The example works also with ml 9.0 and JWasm. But I can only echo Michael's warning: Using the same name for one global and one or more local variables is a recipe for disaster. Adding underscores (one, two, three, why not ____________gaga?) will also not really increase the readibility of your code. Try to find a creative but consistent style, and have a look at Hungarian notation rules, too. Hint: If you don't like lpszMystring, use My$.

japheth

Quote from: llkooj on December 06, 2008, 03:18:09 PM
Is it true static by putting a leading underscore on the variable, say in the procedure ? My solution has to be true static scoping.

What is "true static scoping"? Since static variables are "local" to the module, it's up to the compiler/assembler how they are named. IIRC Open Watcom adds the function (=procedure) name as a prefix to the variable name if the variable is defined inside a function. You can do so as well, but you'll have to do it "by hand", there's no @CurProc predefined symbol which contains the current PROC name.

Vortex

Hi llkooj,

Adding a leading underscore ( or another sign to the end of the symbol ) is intended to differentiate the same named variables in your code.