[2] Compile & Execute code
Big Steps of booting process
- 1. BIOS loads MBR (the first stage bootloader)
- 2. MBR jumps to the second stage bootloader
- 3. The bootloader loads kernel
According to the steps listed above, we first need to make BIOS to load MBR
So let’s take a look of what is MBR and its purpose.
MBR (Master Boot Record)
- MBR is the first sector (512 bytes) of a hard disk
- MBR contains the boot code and the partition table
- The last 2 bytes of MBR are 0x55aa
Since MBR can only be maximum 512 bytes, we cannot contain all the boot code into the MBR. Because of this size limitation, MBR loads another block of boot code which is a second stage of bootloader.
Writing MBR
All BIOS knows is, MBR is the first sector of the hard disk and it’s two end bytes are 0x55aa. Therefore, we can satisfy the requirements by following code.
;bootloader.asm times 510-($$-$) db 0x00 dw 0xaa55
Compile the code
Now let’s try to copmile the code to see if BIOS really can load the first sector.
I used NASM (Netwide Assembler), so we need to compile the code to binary code first.
The command line below will compile the code into binary.
nasm bootloader.asm -f bin -o bootloader.bin
Execute the code
As I mentioned in the previous post, I’m going to use QEMU emulator for testing the bootloader.
Here is the way to install QEMU.
Below is the command line for executing the code on the emulator.
qemu-system-i386 -L . -boot c -m 256 -hda bootloader.bin -soundhw all -localtime -M pc
When you run the code, you should see a screen something like this :
Now we can load our MBR on memory.
In the next post, we are going to make a print function because printing is the easiest way to see if the code is working well.
Reference :