Page 1 of 1

nasm coding

Posted: Wed Mar 12, 2003 1:49 pm
by slacker
i have tried looking at the nasm manual but i found this confusing.
what exactly do "extern" and "global" do?

Re:nasm coding

Posted: Wed Mar 12, 2003 2:16 pm
by stonedzealot
Well, this is probably the worst answer possible, but here we go:

global *I think* would just define a variable that is global. Don't listen to me though. Extern I do know about.

extern defines a variable/function that can has been defined elsewhere e.g.

Code: Select all

[extern _osmain]
call _osmain
Now, the function osmain is defined in another unit (e.g. your kernel or another file full of ASM/C)

Re:nasm coding

Posted: Wed Mar 12, 2003 2:35 pm
by Tim
That's right. Using [tt]global[/tt] on a piece of data or code is the same as not using [tt]static[/tt] in C. Using [tt]extern[/tt] is the same as [tt]extern[/tt] in C.

Re:nasm coding

Posted: Wed Mar 12, 2003 5:14 pm
by slacker
so what exactly does global do?

Re:nasm coding

Posted: Wed Mar 12, 2003 6:13 pm
by Tim
It lets you access the symbol from another file.

Re:nasm coding

Posted: Thu Mar 13, 2003 11:57 am
by pini
"extern" means that the symbol is located in another object file (bind together in the linking step)
"global" means that the symbol is exported and so is available for other object files (by using extern spec.)

I think that's nothing to deal with the static keyword of C

Re:nasm coding

Posted: Thu Mar 13, 2003 1:55 pm
by slacker
so let me see if i get this and understand its use.
if i have an asm file:
is this what is supposed to be used for?

*****************************
global mainfunc

mainfunc:
hlt
****************************
can i do this in a c file:

int main()
{
asm("jmp mainfunc");
};

Re:nasm coding

Posted: Thu Mar 13, 2003 11:14 pm
by Chris Giese
'extern' in NASM works the same as 'extern' in C -- it means that an object is declared in a different file.

'global' in NASM is sort of the opposite of 'static' in C. By default, all functions and global variables in a C program are public: they can be accessed by code in other files. You put 'static' in front of a function or global variable to hide it from other files (this is data hiding; a good programming practice). Assembly language is just the opposite: everything is private to the file unless you declare it 'global'.

What's really confusing is that C 'static' works differently for local variables than for functions or global variables.

[tt]global mainfunc

mainfunc:
hlt[/tt]

Well, [tt]hlt[/tt] will halt only until the next interrupt occurs. I'd put a [tt]ret[/tt] after it. This also makes it easier to call from C:

[tt]extern void mainfunc(void);

int main()
{
mainfunc();
}; [/tt]

Re:nasm coding

Posted: Fri Mar 14, 2003 5:11 pm
by slacker
k i get it...thanks