begginers question

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
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

begginers question

Post by richardhp »

i have done some tutorials etc and have a basic grasp of assembly and i have managed to write a bootloader for a floppy that displays some text on the screen.
i am not a huge fan of assembly and i'm wondering where i can go from here ie writing it in C or something similar. I am aware however that gcc produces a file designed to be run in an operating system and that if i simply put it onto a disk and booted it wouldn't work.
how would i replace my assembly program with something more like:

void main() {
printf("Hello");

while(1){
}
}
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

I strongly advise you to read Bran's Kernel Development Tutorial.

When you run an executable on e.g. Windows, the exe file is loaded in to RAM at appropriate locations (i.e. code and data may be relocated) and you have the entire Windows API at your disposal.

The main things with developping a C kernel are:

* You have no standard library available. Do not #include any headers you have not written yourself (or ported). This means, in your example, printf() will not work as you have not yet written any kind of video driver.
* Either you need to compile in flat binary format, or your boot loader needs to be capable of exe relocation to load your kernel. This will mean writing a linker script and probably an executable stub (which will call your main() function) in assembly.

The tutorial given above will take you through the compiler and linker commands you will need for both of these.

HTH,
Adam
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

Post by richardhp »

thanks, i will read more into that.
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

Post by richardhp »

i have been following the tutorial, but when i try and link the assembly code with the c code i get the following error:
undefined reference to '_main'
however i have added it to my compile script which looks like this:

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -c -o lib/main.o src/main.c
nasm -f aout -o lib/kernel.o src/kernel.s
ld -T lib/link.ld -o kernel.bin lib/main.o lib/kernel.o


I am not sure what is wrong.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

do you use a cross-compiler or a normal compiler. try to declare the function main as extern int main(void) otherwise if you are doing c++ extern "C" int main(void).

hope it helps.
Author of COBOS
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

Post by richardhp »

worked fine thanks
Post Reply