8 Replies - 1176 Views - Last Post: 08 September 2009 - 09:21 PM Rate Topic: -----

#1 chris772  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

Calling assembly from C

Post icon  Posted 08 September 2009 - 08:17 PM

First off, I'm working on an Ubuntu Linux platform and using gcc for compilation. I can't seem to call assembly functions from a file named differently than the function. For example, if I have thisFunction: in an assembly file named other.s, I can not call thisFunction in C. Please note that I have gone through all the steps with underscores, global declarations, extern declarations, et cetera. In the example above, I get an error from gcc saying that the function is an undefined reference to 'thisFunction'. However, if I name the file the name of the function everything is compiled and called without any problem. I don't want to have to create a new assembly file for all 31 interrupt service routines. Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Calling assembly from C

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Calling assembly from C

Posted 08 September 2009 - 08:23 PM

show us the makefile and small example programs that illustrate the problem.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Calling assembly from C

Posted 08 September 2009 - 08:30 PM

I have written the following Tutorial, hopefully it will be useful towards your situation.

If not, please post the code that you are trying to use.
Was This Post Helpful? 0
  • +
  • -

#4 chris772  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

Re: Calling assembly from C

Posted 08 September 2009 - 08:33 PM

Ok.

The make:
all:
	 gcc -I./ -o kernel.o -c kernel.c
	 nasm -f elf start.s

	 ld -T linker.ld -o kernel.bin start.o kernel.o
	 cat stage1 stage2 pad kernel.bin > floppy.img



That was a cut-down version of my actual make file, I used different names in the original post and left out a bunch of commands that compile/link my other files.

Example:

kernel.c (stripped for example)
extern void isr0();

void main()
{
	 isr0();

	 for (;;);
}



start.s (stripped for example)
global isr0

isr0:
	 cli
	 push byte 0
	 push byte 0
	;jmp isrCommonStub



I commented out the jump just because I didn't include the call or anything. That is basically what is going on. If it is compiled it will give me an error saying isr0 is undefined. In reality my files are much different but this is just an example.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Calling assembly from C

Posted 08 September 2009 - 08:43 PM

You need to include your output from nasm. The compiler can't see the function, only your declaration of it.

Quote

nasm -f elf start.s
gcc -I./ -o kernel.o -c kernel.c start.o

Was This Post Helpful? 0
  • +
  • -

#6 chris772  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

Re: Calling assembly from C

Posted 08 September 2009 - 08:54 PM

The start.o is being linked in a separate .ld file but even with declaring the start.o at the end of the gcc line it still finds it as an undeclared reference.

This post has been edited by chris772: 08 September 2009 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Calling assembly from C

Posted 08 September 2009 - 08:56 PM

View Postchris772, on 8 Sep, 2009 - 09:54 PM, said:

The start.o is being linked in a separate .ld file

Is it failing at the linker or the compiler?

Lets deal with the compiler for now. We'll worry about the linker when we get that far :)

Quote

but even with declaring the start.o at the end of the gcc line it still finds it as an undeclared reference.

Can you show me what you did, along with the output.
Was This Post Helpful? 0
  • +
  • -

#8 chris772  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

Re: Calling assembly from C

Posted 08 September 2009 - 09:07 PM

The hard part is separating the problem snippets because I just have too damn much code but I'll try.

It looks like the error is actually coming from the linking process in the object file that was made. The isn't giving anything in the way of errors, but at the end of the make it says isrs.o: In function 'faultHandler': isrs.c:(.text+0x7): undefined reference to '_isr0'. The problem code was actually in a different file (isrs.c) but I renamed it for the sake of clarity.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Calling assembly from C

Posted 08 September 2009 - 09:21 PM

The error :

Quote

undefined reference to '_isr0'


Your code :
global isr0

isr0:



See the problem?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1