Page 1 of 1
begginers question
Posted: Tue Feb 13, 2007 7:42 am
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){
}
}
Posted: Tue Feb 13, 2007 7:57 am
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
Posted: Tue Feb 13, 2007 7:59 am
by richardhp
thanks, i will read more into that.
Posted: Wed Feb 14, 2007 7:43 am
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.
Posted: Wed Feb 14, 2007 8:34 am
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.
Posted: Wed Feb 14, 2007 2:16 pm
by richardhp
worked fine thanks