The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on June 17, 2008, 01:29:20 PM

Title: static in C
Post by: n00b! on June 17, 2008, 01:29:20 PM
Hi, I want to convert the following C Code to MASM.
Do I have to declare the variable as a global one, or can I do it on another way?

void test() {
  static int i;
  i += 2;
}
Title: Re: static in C
Post by: jj2007 on June 17, 2008, 02:53:41 PM
Depends on whether you need a global or local variable...

.data?
varI    dd ?  ; varI will be zero at progstart

.data
varI    dd 123  ; varI will be 123 at progstart

MyProc
LOCAL varI:DWORD
mov varI, 0   ; varI will be undefined whenever you enter this proc, so you better put a value  :wink
Title: Re: static in C
Post by: zooba on June 18, 2008, 06:58:20 AM
Yes, it will need to be global.

MASM doesn't have the same scoping rules as C, so you may want to include the name of the function in the variable name to make clear where it belongs.

Cheers,

Zooba :U
Title: Re: static in C
Post by: Tedd on June 18, 2008, 12:37:08 PM
The closest you can get is..

test proc
    .data
        i   DWORD 0
    .code
    add DWORD PTR [i],2
    ret
test endp


So I'd just go for a 'global' - with a slightly more meaningful name than 'i' :wink
Title: Re: static in C
Post by: GregL on June 18, 2008, 06:12:25 PM
You could also use the GLOBAL macro.


test PROC

    GLOBAL i DWORD 0
   
    add i, 2
    ret
test ENDP