importing functions

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
no one special

importing functions

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

Re:importing functions

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

Re:importing functions

Post 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();
}
dronkit

Re:importing functions

Post by dronkit »

uising the "extern" keyword is a manners matter ;)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:importing functions

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