C is a very low level language and you can do many things directly. Any of the C library methods (for example, malloc, printf, crlscr, etc.) must first be implemented to call them from C (see, for example, the concepts of libc). I will give an example below.
Let's see how the C library methods are implemented under the hood. We will look at the clrscr example. When you apply such methods, you will access system devices directly. For example, for clrscr (screen cleaning), we know that video memory is at 0xB8000. Therefore, to write to or clear the screen, we begin by assigning a pointer to this location.
In video.c
void clrscr() { unsigned char *vidmem = (unsigned char *)0xB8000; const long size = 80*25; long loop; for (loop=0; loop<size; loop++) { *vidmem++ = 0; *vidmem++ = 0xF; } }
Now write our mini-core. This will clear the screen when control is transferred to our "core" from the bootloader. In main.c
void main() { clrscr(); for(;;); }
To compile our "core", you can use gcc to compile it into a pure bin format.
gcc -ffreestanding -c main.c -o main.o gcc -c video.c -o video.o ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
If you notice the ld options above, you will see that we specify the default boot location of your kernel as 0x1000. Now you need to create a bootloader. From the loader logic, you can transfer control to your kernel, for example
jump 08h:01000h
Usually you write down the bootloader logic in Asm. Even before that, you may need to see how Boot Boots - Click here .
Better to start with a thinner operating system to explore. See this video of your OS tutorial.
http://www.acm.uiuc.edu/sigops/roll_your_own/
amazedsaint Jul 08 '09 at 8:07 2009-07-08 08:07
source share