i have tried looking at the nasm manual but i found this confusing.
what exactly do "extern" and "global" do?
nasm coding
Re:nasm coding
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.
Now, the function osmain is defined in another unit (e.g. your kernel or another file full of ASM/C)
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
Re:nasm coding
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
"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
"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
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");
};
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
'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]
'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]