Page 1 of 1

Mixing C and nasm

Posted: Sat Jan 19, 2008 11:49 am
by jaswax
Hello all. I am newly registered here but I have read many posts, searched for many things and have had many questions answered.

I am sorry if this has already been asked but my searches yielded no good results. So now for my first question here!!!

I am developing under Linux using gcc, nasm and ld

I would like to know how to mix NASM with C. Can someone point me to a tutorial for this? or just lay it out for me in a reply? (either is ok)

Example of what I am trying to do:

Code: Select all

; File: Readchr.asm
global rdchr
rdchr:
  mov eax, 0x01
  ...
ret
C file:

Code: Select all

; File: Main.c
extern void rdchr;
int main()
{
  rdchr();
}
I want to be able call functions created in nasm from C.

Thanks in advance for the any help.

Posted: Sat Jan 19, 2008 12:29 pm
by JamesM
Hi,

If you remove the "extern ..." line, that code should compile in C. Just add rdchr as a function prototype beforehand, and bob's your uncle!

My problem is solved.

Posted: Sat Jan 19, 2008 8:16 pm
by jaswax
Thank you. That solved my problem. I like the simple answer. Cheers!