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.
importing functions
Re:importing functions
"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:
Can't be called from outside the file, but:
then from your C file you declare:
now you're ready to use the function.
Code: Select all
hello:
ret
Code: Select all
.globl hello
hello:
ret
Code: Select all
extern void hello(void);
Re:importing functions
You even don't need the "extern declaration...Just have something like this:
And in the C file:
Code: Select all
; this is for NASM
[global _hello]
_hello:
; do stuff
ret
Code: Select all
void hello(void);
void foo(void)
{
hello();
}
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:importing functions
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)