Page 1 of 1

importing functions

Posted: Tue Oct 22, 2002 9:30 am
by no one special
Hi,

If I have an asm file with code like this

_KillFloppy:
push eax
mov dx, 0x3f2
mov al, 0
out dx, al
pop eax
ret

and I compile it like this

nasm -f elf floppy.asm

and I link in with a c file, how can I use this function? do I need an extern declaration or what?

thanks.

Re:importing functions

Posted: Tue Oct 22, 2002 10:05 am
by dronkit
"extern" declarations are for calling C functions from asm code. in "gas", every asm label is local (private) to the file the code is in. To define a symbol as an "exportable" one, you use the keyword ".globl", for example:

Code: Select all

hello:
   ret
Can't be called from outside the file, but:

Code: Select all

.globl hello
hello:
   ret
then from your C file you declare:

Code: Select all

extern void hello(void);
now you're ready to use the function.

Re:importing functions

Posted: Wed Oct 23, 2002 6:48 am
by Whatever5k
You even don't need the "extern declaration...Just have something like this:

Code: Select all

; this is for NASM
[global _hello]
_hello:
  ; do stuff
  ret
And in the C file:

Code: Select all

void hello(void);

void foo(void)
{
   hello();
}

Re:importing functions

Posted: Wed Oct 23, 2002 12:24 pm
by dronkit
uising the "extern" keyword is a manners matter ;)

Re:importing functions

Posted: Thu Oct 24, 2002 12:23 am
by Pype.Clicker
just be aware that it is not necessary to add a leading underscore to your asm function name when assembling an ELF file (but it is necessary if you're compiling a COFF/PE/COM/OBJ/whatever file from the dos/windows ABI)