Page 1 of 1
mixing nasm with gcc?
Posted: Wed Jan 14, 2009 2:09 pm
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?
Re: mixing nasm with gcc?
Posted: Wed Jan 14, 2009 2:28 pm
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?
Re: mixing nasm with gcc?
Posted: Wed Jan 14, 2009 3:03 pm
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
Re: mixing nasm with gcc?
Posted: Wed Jan 14, 2009 4:19 pm
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
Re: mixing nasm with gcc?
Posted: Wed Jan 28, 2009 5:34 pm
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.
Re: mixing nasm with gcc?
Posted: Wed Jan 28, 2009 5:39 pm
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...
Re: mixing nasm with gcc?
Posted: Wed Jan 28, 2009 5:43 pm
by JohnnyTheDon
extern is unnecessary. When a function is declared (not defined) it is extern by default, unless it is defined later.
Re: mixing nasm with gcc?
Posted: Wed Jan 28, 2009 5:46 pm
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]
Re: mixing nasm with gcc?
Posted: Wed Jan 28, 2009 6:05 pm
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