nasm coding

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
slacker

nasm coding

Post by slacker »

i have tried looking at the nasm manual but i found this confusing.
what exactly do "extern" and "global" do?
stonedzealot

Re:nasm coding

Post 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)
Tim

Re:nasm coding

Post 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.
slacker

Re:nasm coding

Post by slacker »

so what exactly does global do?
Tim

Re:nasm coding

Post by Tim »

It lets you access the symbol from another file.
pini

Re:nasm coding

Post 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
slacker

Re:nasm coding

Post 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");
};
Chris Giese

Re:nasm coding

Post 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]
slacker

Re:nasm coding

Post by slacker »

k i get it...thanks
Post Reply