The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: llkooj on December 06, 2008, 09:28:12 AM

Title: Static Scoping
Post by: llkooj on December 06, 2008, 09:28:12 AM
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
Title: Re: Static Scoping
Post by: donkey on December 06, 2008, 09:30:11 AM
MASM does not allow it, however it is perfectly fine to do it in GoASM which has much more powerful scoping than MASM.
Title: Re: Static Scoping
Post by: Vortex on December 06, 2008, 11:35:44 AM
llkooj,

Put a leading underscore to one of those variables.
Title: Re: Static Scoping
Post by: 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.
Title: Re: Static Scoping
Post by: 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:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    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...

Title: Re: Static Scoping
Post by: jj2007 on December 06, 2008, 11:18:13 PM
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$.
Title: Re: Static Scoping
Post by: japheth on December 07, 2008, 07:36:07 AM
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.
Title: Re: Static Scoping
Post by: Vortex on December 08, 2008, 09:34:26 PM
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.