Calling Assembly from C

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
yetAnotherOS
Posts: 12
Joined: Fri Jul 01, 2005 11:00 pm

Calling Assembly from C

Post by yetAnotherOS »

Just like most of you who observe this website, I am tinkering around with writing an operating system. I have written extremely useful assembly code to detect VESA compatible cards and select an appropriate graphics mode. However, some parts I would prefer to use C for. That's where I get a problem.

Here is an example of the relevant part of my 32 bit code (I use NASM):

Code: Select all

[BITS 32]
extern _somefunction
     call _somefunction
     cmp eax,0x12345678
     jne elsewhere
Let's pretend that this code is in first.asm

In a .c file (let's pretend it's second.c) I have written:

Code: Select all

int _somefunction()
{
     /*perform some useful function
        and return a value.*/
     return 0x12345678;
}
I then assemble and compile this as follows:
nasmw -f coff -o first.o first.asm
gcc -ffreestanding -c -o second.o second.c
ld -Ttext 0x100000 --oformat binary -o result.bin first.o second.o

The problem is that the linker reports:
undefined reference to '_somefunction'
occurring at the appropriate location in the first.asm file.

Do I need to somehow declare that somefunction() should be exported?

I found HOWTO about mixing assembly and C very useful but it does not discuss going the other way i.e. calling C from assembly.

Thanks in advance for your help.
dozerl
Posts: 19
Joined: Tue May 10, 2005 11:00 pm
Location: here

Re: Calling Assembly from C

Post by dozerl »

You might want to try and take off the leading under score from your function int _somefunction in the second.c file and leave it in the asm file.

second.c:

int somefunction(){..}
Last edited by dozerl on Fri Jul 01, 2005 11:00 pm, edited 1 time in total.
yetAnotherOS
Posts: 12
Joined: Fri Jul 01, 2005 11:00 pm

Re: Calling Assembly from C

Post by yetAnotherOS »

Thanks for the suggestion, but I have tried all the combinations of putting underscores in either place, both places, or no places. It doesn't work. Do you know any reference book or website that might help me?
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Calling Assembly from C

Post by carbonBased »

Assuming your on a linux system (or any well supported GNU system) objdump should be useful to you in this case.

Try the following:
objdump --syms first.o
objdump --syms secdond.o

And see what each object contains and is expecting. This should give some useful information, and if you still can't figure it out, please paste in the results to this forum and I'll see what I can do.

By the way... try objdump --help for more information. This is an extremely useful utility that I think all OSDev'ers should know how to use (especially if you plan on supporting shared libraries/objects), or plan to implement your own compiler eventually.

Cheers,
Jeff
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

Re: Calling Assembly from C

Post by prajwal »

I'm using Linux OS.

I tried the sample code u'v posted. It works fine...

Here is what i'v done:

nasm -faout <ur asm file> -o asm.o
gcc -ffreestanding -c -o c.o <ur c file>

ld --oformat binary -o test.bin -Ttext=0x100000 asm.o c.o
Then i got one warning: ld: warning: cannot find entry symbol _start; defaulting to 0000000000100000

test.bin is generated perfectly....

I verified it with ndisasm- nasm disasmmebler.

ndisasm -b32 test.bin
00000000 E807000000 call 0xc
00000005 3D78563412 cmp eax,0x12345678
0000000A 90 nop
0000000B 90 nop
0000000C 55 push ebp
0000000D 89E5 mov ebp,esp
0000000F B878563412 mov eax,0x12345678
00000014 5D pop ebp
00000015 C3 ret

NOTE: gcc -c will generate aout format object file
-faout option to nasm will generate aout format object file.....

both should be same for ld to work..... but any way in this case the error
thrown by ld is object file format not recognized....
yetAnotherOS
Posts: 12
Joined: Fri Jul 01, 2005 11:00 pm

Re: Calling Assembly from C

Post by yetAnotherOS »

thanks, everyone, for your help! I checked using objdump and realized that the symbols were incorrect because I had been using a file from an old build. In the end it all works just like the last reply suggests. Sorry for any confusion: it was my mistake.
Post Reply