Hi, in this section we will see how to handle the interrupts and exceptions. At this point,
the kernel file is loaded into memory and we are going to jump to the kernel in this lecture.
we relocate the kernel from 10000 to the memory address 200000.
We open the loader file and start from where we left off.
There is only one task we will actually perform here, that is, relocate our kernel.
Remember we have read the kernel file from disk into memory 10000. Let’s check it out.
As you see, the code here is loading the kernel into memory.
We remove the code of printing characters.
Loading the kernel is really simple. Here we use instruction move qword.
Let's get started.
First off, we clear direction flag
so that the move instruction will process the data from low memory address to high memory address,
which means the data is copied in forward direction.
The destination address is stored in rdi register and source address is in rsi register.
so we move rdi
Two hundred thousand.
and this is a hexadecimal number.
move rsi
Ten thousand.
The kernel is in the memory 10000 right now and we want to copy it into the location 200000.
Register rcx acts as a counter, since we want the move instruction to execute multiple times.
Move qword will copy the 8-byte data each time.
So we divide the value by 8
and move the result to rcx.
When we load the kernel from disk into memory, we read 100 sectors. Each sector is 512 bytes.
So the value is equal to the size of 100 sectors.
Now rcx specifies the times move qword instruction will repeat. In order to repeat the move instruction,
we use prefix repeat.
and move
quadword.
Ok, after the move instruction executes, our kernel is copied into the address 200000.
There is one instruction left to write,
which is jump instruction. We jump to 200000,
the execution is transferred to the kernel and we are good to go.
We haven’t had a kernel file up to this point. Let’s write a simple one.
we create a new file.
and save it
as kernel file.
all the code written now is in 64-bit mode.
and the base address of the kernel is
200000.
Just like we did before,
we print the character K to show that we are in the kernel file.
And also we end our kernel by the infinite loop.
Before we build our project, we modify the build script and assemble the kernel file, so we save
these two files.
And open the build script to edit.
The command we use is the same as loader file.
So we copy and paste here.
the output file is called kernel.bin
and the input file is kernel.asm
And also, we need to
write the kernel.bin into the boot image.
And we specify we want to write 100 sectors into the boot image.
Since the first sector is the boot.bin and then the next 5 sectors are the loader.bin.
So the value we use is six, which means we skip the first 6 sectors of the output file.
OK, we save the file and run the script in the terminal.
OK, now we can open the boot folder and run the bochs.
We can see character K is printed on screen.