mixing nasm with gcc?

Programming, for all ages and all languages.
Post Reply
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

mixing nasm with gcc?

Post by yemista »

Has anyone here developed by writing their asm functions with nasm, and their c code with gcc? So long as you know how gcc sets up function calls and create your assembly functions to coincide with that, would it make any difference as to what assembler you use? Or will you run into problems later on when trying to link many different files together?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: mixing nasm with gcc?

Post by Combuster »

I use yasm and gcc, but the same assembly code should work with nasm since it uses the same dialect (as compared to gas/masm/fasm which need changes to have everything compile and have the result work the same)

Also, I link 166 files at one stage without problems - does that sound enough for you? :)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: mixing nasm with gcc?

Post by yemista »

Yea, it just sounds like I need to do a little research on it, Still, Im glad I know its possible so Im not chasing any ghosts
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: mixing nasm with gcc?

Post by Sam111 »

I use nasm, tasm , and yasm for assembly programing.
And gcc , ld , and microsofts cl , link.

What it really boils down to is knowing the inner workings of compilers and linkers.
And knowing the object file format of your exe.

If you want to write a kernel then it really doesn't matter which object format you use elf , obj , o , aout,...etc . However if you are going to call external function from c then you need to know what object format supports this. For example using nasm -f bin your.asm you won't beable to call c functions because the extern keyword is not supported. But if you do nasm -f coff or elf or aout you can call c functions from asm. However when you link them together you must produce a bin file which is the raw machine code instructions (no exe header stuff)

recap so for compiler/linker --->
Once you have the object files you have to use a linker to combined the objects into one file.
Thus a linker just merges your objects .data , .text , and rdata sections into one and resolves externel references set entry point...etc. The compiler/assemblier is just to translate your code into machine code but then the linker merges them all in to one file and set the entry point of where the program starts off at.

gcc is like a compiler and linker all in one. You can just compile gcc -c your.c and
then use ld -o your your.o to link to get an exe file that you can run.
Or you can do it all in one with gcc your.c (-c switch is for telling gcc just to assembly/compile but not to link without the switch you need to link ld to get the exe )
without the -c it compiles and links to produce an a.exe . If you want to -o nameoffile names the exe so it doesn't default to a.exe.

Also don't forget to disable linking with any dependent libraries like libc ...etc unless you want a huge kernel.

Take your time their is alot to learn.
Learn how to use each and every switch in a compiler/linker like gcc (really under stand it all )
Then read some object formats you will probably want to do this as your testing out switches ..etc.

Then I think you will be all set. Another big issue in writing a kernel is alignment but you will get to that when you get into the section headers .data , .text ,...etc.

gcc also has things called linker scripts that do wonders when you want to specify exact locations for things and entry points.

I recommend compiling all of the source file in the same format then using a linker script to create an entry point and output a bin file.
Good luck hope this helps.
And keep reading
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: mixing nasm with gcc?

Post by yemista »

Would you mind showing one of your makefiles? I am getting errors about undefined reference for functions I wrote in assembly, but in the assembly file I declared them as global and in my .h file i declared them as extern. Or do you know of any good links with information on this? I am finding a lot about calling c functions in assembly or calling assembly functions from c, but I cant find out how to declare the assembly prototypes in a header file, have an object file include that header, and then have access to those functions.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: mixing nasm with gcc?

Post by Firestryke31 »

The biggest issue with undefined external functions is whether or not the compiler uses a leading underscore. The easiest way to test this is to take one of the functions the linker can't find and add/remove a '_' to the beginning. If it compiles, celebrate. Unfortunately that's the only advice I can give right now...
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: mixing nasm with gcc?

Post by JohnnyTheDon »

extern is unnecessary. When a function is declared (not defined) it is extern by default, unless it is defined later.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: mixing nasm with gcc?

Post by Firestryke31 »

[off topic]
I prefer having extern on functions not declared in that file because it lets me know that I need to look somewhere else for it. Just a personal preference, though, and doesn't work so well for C++ class methods...
[/off topic]
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: mixing nasm with gcc?

Post by yemista »

Firestryke31 wrote:The biggest issue with undefined external functions is whether or not the compiler uses a leading underscore. The easiest way to test this is to take one of the functions the linker can't find and add/remove a '_' to the beginning. If it compiles, celebrate. Unfortunately that's the only advice I can give right now...
Yes you were right. I know that when assembling with as you need to put an underscore to make it link with gcc code, but i guess in nasm you dont. I removed the underscores and it worked out fine. Thank you
Post Reply