Xosdev chapter 2

Xosdev Chapter 2 [Basic Kernel] - by mr. xsism

- - - - - - - - - - - -
[Planning/Setting goals]
- - - - - - - - - - - -
Congrats, you have gotten farther than many people that have tried
OSdev. So what now? Now you start to see the results of your hard work;
you load the kernel. "Wow," I hear you say, "that can't be too hard :)."
Well, it is and it isn't. If you keep it simple and use C it'll be a
piece of pie. If you use C++, it just might be hell. You have to load
class, new, and delete support to name a few, where as in C you don't
because everything can be made into asm without having to add support
for it. I know of a few people that use C++ and they like it. It is up
to you to research C++ kernel dev. From here it is all ahead full :)
First we need some code that will be loaded by our bootloader.
Something like this:
In C:
void k_main()
{
int num;
char ch;
char *str="Kernel Loaded";
return;
}
All that does is declare 3 variables and returns. That's just
fine and dandy. Here are some other things that we can do:
1-output "Hello, World"
2-clear the screen
3-output colored text to the screen
- - - - - - - - - - - - - - -
[First goal: Displaying text]
- - - - - - - - - - - - - - -
No screen output, what's the fun in that?? Nothing >:/ What we
need is Screen I/O. It has to be the easiest thing to code in the
kernel. Just involves righting a simple ascii character to screen's
memory and the character's color.
The way that you do this is simply by placing an ascii byte
for the character followed by its attribute at 0xB8000. You don't
always put it there, because each time you print one character you
should increment the text pointer by two(char byte + attrib byte).
In C:
void _k_main()
{
int num;
char ch;
char *text_video = (char*)0xB8000;
char attrib = 0x07;
char *str="Kernel Loaded";
while(*str!=0)
{
*text_video = *str;
*text_video++;
*text_video = attrib;
*text_video++;
*str++;
}
return;
}
- - - - - - - - -
[Clearing screen]
- - - - - - - - -
Clearing the screen is another very easy task involving the
text pointer. For each ascii character in text video, just set it
to zero and the attribute byte to the current attribute.
In C:
void clear_screen(char clear_to, char attrib)
{
char *text_video = (char*)0xB8000;
int pos=0;
while(posgcc *.c
2-compile all asm files into a format like aout (not bin, C doesn't
output to bin by default)
>nasm *.asm -f aout
3-link all C files and asm files together into a file(ie:kernel.o)
>ld -T linkscript.ld -o kernel.o a.o b.o c.o
4-compile & copy the bootloader to the front of the kernel object
file(ie:kernel.img)
>nasm boot.asm
>copy /b boot.bin+kernel.o kernel.img
5-write the image file to a bootable device(ie:floppy disk)
>floppyout kernel.img a: -sector 0 -head 0 -track 0
6-now take that bootable device and put it in a microwave oven for
30 seconds, wait for it to melt, and enjoy!
+-------------------------+
| ||===========|| [00:30] |
| || || 7 8 9 |
| || || 4 5 6 |
| || || 1 2 3 |
| || || X 0 X |
| ||===========|| (start) |
+-------------------------+
\_/ \_/
Ok, are you awake? I hope you dismissed step 6(if not please
notify me so i can bash you over your head). I hope this was adequate
enough for you. If you need more help, look at the given example code
for this chapter.