Can i call ASM file from my C file

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
Willow

Can i call ASM file from my C file

Post 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!!!
sysfree

Re:Can i call ASM file from my C file

Post by sysfree »

of course you can. :)
Whatever5k

Re:Can i call ASM file from my C file

Post 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...
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:Can i call ASM file from my C file

Post 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])
Whatever5k

Re:Can i call ASM file from my C file

Post by Whatever5k »

I think the underscore (_) is not needed for ELF...nothing to do with Linux/Windows, or am I wrong?
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:Can i call ASM file from my C file

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

Re:Can i call ASM file from my C file

Post by Whatever5k »

Yes, so actually, we're both right ;)
Post Reply