Page 1 of 1

Can i call ASM file from my C file

Posted: Wed Sep 18, 2002 5:56 pm
by Willow
i what to call a ASM file from my kernel.c beacause i make a kernel.c and i make some module like detecting floppy and stuf like that !!!!!!
Email me at: [email protected]


thanck!!!

Re:Can i call ASM file from my C file

Posted: Wed Oct 09, 2002 1:47 am
by sysfree
of course you can. :)

Re:Can i call ASM file from my C file

Posted: Wed Oct 09, 2002 2:04 am
by Whatever5k
I suppose you want to call a assembler *function* within a C function...
Well, that's no problem at all:

Code: Select all

; setup.asm

_asmFunction:
  blabla
  .
  .
  blabla
  ret ; return to caller

Code: Select all

/* kernel.c */
void asmFunction(void);

void kmain(void)
{
.
.
.
asmFunction(); /* call the asm function without parameters */
.
.
.
}
That's it...

Re:Can i call ASM file from my C file

Posted: Wed Oct 09, 2002 3:11 am
by Pype.Clicker
just a few things that might be useful:
- in most assemblers, you must declare a symbol to be GLOBAL so that you can access it from out there.

Code: Select all

 global _asmFunction 
- DOS-based C compilers prepend a "_" to their symbols, so main() is actually called _main at the assembler level. this is *not* the case under Linux, (thus if you're under linux, you should call your function "asmFunction" rather than "_asmFunction"
- your C code will expect some registers to be modified (eax, edx and maybe edi ... can't remember) while others are expected to be saved/restored (ebx, ebp, ecx, and all the segment registers)
- arguments are pushed on the stack as dwords (hum, i suggest you don't use structure as arguments to a asm function), so provided that you did

Code: Select all

push ebp
mov ebp, esp
you can access the first argument with [ebp+8], the second one with [ebp+12] (i assume 32-bits mode. in 16 bits it would be [bp+4] and [bp+6])

Re:Can i call ASM file from my C file

Posted: Wed Oct 09, 2002 3:40 am
by Whatever5k
I think the underscore (_) is not needed for ELF...nothing to do with Linux/Windows, or am I wrong?

Re:Can i call ASM file from my C file

Posted: Wed Oct 09, 2002 4:31 am
by Pype.Clicker
abless wrote: I think the underscore (_) is not needed for ELF...nothing to do with Linux/Windows, or am I wrong?
just the fact that ELF is the binary file in Linux and COFF/PE the binary file in Windows&DOS ...

Re:Can i call ASM file from my C file

Posted: Wed Oct 09, 2002 4:44 am
by Whatever5k
Yes, so actually, we're both right ;)