link c/asm

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.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

link c/asm

Post by lama »

hi ,
i have these two test files:
prepc.asm

Code: Select all

global _start
extern main
_start:
mov ax, 0x10
mov ds, ax
mov ax, 0x18
mov es, ax
mov esi, 0x600
mov edi,  pre
mov ecx, 0x1c00
cld
rep movsb
mov ax, 0x18
mov ds, ax
call main
pre:
times 0x1c00 db 0
pstst.c

Code: Select all

void main(){
	__asm__ ("cli");
	__asm__ ("hlt");
}
so as you can see - i want to do some stuff and then jump to main().What i dont know is, how can i link these files together to create pure binary.(using ld).thanks a lot for showing me how to do that :)
User avatar
scgtrp
Member
Member
Posts: 30
Joined: Sat Mar 28, 2009 7:32 pm

Re: link c/asm

Post by scgtrp »

Compile them both to a.out/ELF/COFF/anything else besides flat binary, then:

Code: Select all

ld -Ttext 0x100000 -m binary obj1.o obj2.o -o kernel.bin
Replace 0x100000 with the address where your kernel is loaded.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: link c/asm

Post by lama »

i tried that, -i compiled pstst.c using 'gcc -c pstst.c -std=c99' (loops)
and preps.asm with 'nasm preps.asm -f coff' .but executing the following link command 'ld -Ttext 0 -m binary preps.o pstst.o -o ps.bin
failes with : ld: unrecognised emulation mode: binary
Supported emulations: elf_i386 i386linux elf_x86_64
whats wrong now?
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: link c/asm

Post by Combuster »

you don't want to set the default format, you want to set the output format: --oformat=binary

Oh and, before you report more problems. GCC Cross-Compiler
"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 ]
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: link c/asm

Post by lama »

used 'ld preps.o pstst.o -o ps.bin --oformat binary -Ttext 0'
preps.o: file not recognized: File format not recognized...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: link c/asm

Post by Solar »

lama wrote:preps.o: file not recognized: File format not recognized...
:roll:

Well?

What's the format of preps.o, that it is not recognized? Did you try to find out? If not, why not? If yes, why didn't you provide the information to us (if it didn't already give you the critical hint as to what the problem was)?

Hint: 'file' is a very useful tool.
Combuster wrote:Oh and, before you report more problems. GCC Cross-Compiler
Every good solution is obvious once you've found it.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: link c/asm

Post by lama »

preps is compiled into coff.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: link c/asm

Post by Solar »

lama wrote:failes with : ld: unrecognised emulation mode: binary
Supported emulations: elf_i386 i386linux elf_x86_64
lama wrote:preps is compiled into coff.
Combuster wrote:Oh and, before you report more problems. GCC Cross-Compiler
Do you notice something?
Every good solution is obvious once you've found it.
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: link c/asm

Post by Combuster »

Do you notice something?
I notice the OP is not ready for OS development. He fails to see the solution even when stabbed twice with the Page Of Answers, besides breaking half the rules.
"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 ]
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: link c/asm

Post by lama »

so i have to build brand new gcc compiler ..? ok
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: link c/asm

Post by lama »

to Combuster:
i just wanted to know how to handle with gcc properly , when i want to mix asm codes with c codes, thats all.i have never programmed in c, so i never had to use gcc, i dont have to use c for my project at all - i can do it in asm , but again : a just wanted to try a different way how to do that.so , please dont blame me from my stupidity.
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: link c/asm

Post by Combuster »

I rest my case.
"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 ]
pguy
Posts: 10
Joined: Tue Oct 06, 2009 1:39 am

Re: link c/asm

Post by pguy »

prepc.asm != preps.asm. Did you change the name?
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: link c/asm

Post by lama »

nn names are correct, thats just spell check or whatewher.but that guy above said , that he's done with me , so great - i will built that gcc cc, or screw c and continue writing asm , or shoot myself .But probably i'm no use, so no mather what i do - it will be wrong.but that's not a part of a topic, sorry about that.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: link c/asm

Post by Solar »

Come on. You were given helpful hints, but it was apparent by your subsequent posts that you ignored them. This happens quite often on this forum, which results in rather little tolerance toward that behaviour.

Yes, this wasn't something that earned you fame, but that doesn't mean you're a failure. Only throwing the towel and stopping to learn new things would mean that.

If there is something you didn't understand, ask the proper question, and you will be answered. But you didn't tell us you don't know about C, for example, which might have softened the response you got.
Every good solution is obvious once you've found it.
Post Reply