Operating System

 

 

 

 

Writing OS : Bootloader : HEX Editor

 

 

Booting with No Media

 

Before writing anything, let's briefly look at what would happen if we run the PC emulator without any operating system. This is something like what you would see when you turn on PC without Operating System (or with corrupted operating system, hard disk).  

Just run the program qemu-system-i386 (located in c:\qemu) as shown below.

 

 

Then you will get the QEMU (Emulated PC) screen as shown below. This indicate the PC couldn't find any media (FDD, HDD, CD etc )with boot sector.

 

 

 

The simplest Bootloader

 

Now we will create a very simple bootloader just with hex editor without using any programming tool. This might be the simplest bootloader that is ever possible.

Create a new binary file using Neo (or any binary editor you have chosen). The size of the file size should be 512 bytes (because the size of the boot sector is 512 bytes). In Neo, you can specify the size of the file as shown below (you may have different ways to do it if you use different Hex File editor).

 

 

The most important thing is to put the number 55 AA at the last two bytes of the file. Why 55 AA ? Don't ask.. just take it as a predefined specification (55 AA Hex is  101010110101010 in binary which is often used as a special physical marker in many digital communication. It may have this kind of physical reason, but this is just my guess).

And then in the space of 510 byte's space, you can write any machine code that can be runnable on the CPU on the PC. In this example (in most other writing OS tutorial), I put EB FE at the beginning. This is a machine code for Intel CPU which means 'Jump to current location". (See x86 Instruction Set Reference and find 'Jmp', but just do the blind copy for this tutorial) So if this runs, the CPU keep looping at the same point. For users, it looks as if PC hangs at the point.

 

 

Then save the file (I saved it as a file named 'my_boot.bin' in c:\my_os folder). Then, I ran a command as follows. This is like 'Powering on PC with a floppy disk or Harddisk that stores the contents of my_boot.bin on their boot sector).

 

NOTE : Close the file from Hex Editor program before you run this command. Since the Hex Editor program hold the access of the file, QEMU would not be able to get access to the file if the file is open in the hex editor program.

 

 

Then you will get the screen as shown below. You don't see anything special here.. but this is indication is that something is running because you don't see the "No Bootable Device" error any more.

 

 

 

The Bootloader with the single character

 

Now I will modify the boot sector file so that I will pring a single character 'H' when it runs. The contents of the file become as follows. Don't ask why.. just copy it as it is for now. If you are really interested in what this mean, see 'JMP', 'MOV','INT' code in  x86 Instruction Set Reference . In next tutorial, I will explain how you can create this file with Assembly program.

 

 

Then run the command as shown below.

 

 

Then you will get the screen as shown below. As you see here... you see the character 'H' printed on the screen.

 

 

 

Reference

 

[1] OS Development: Low-level Boot-Sector Programming (1/9)

[2] OS Development: Low-level Boot-Sector Programming (2/9)

[3] OS Development: Low-level Boot-Sector Programming (3/9)

[4] OS Development: Low-level Boot-Sector Programming (4/9)

[5] OS Development: Low-level Boot-Sector Programming (5/9)

[6] OS Development: Low-level Boot-Sector Programming (6/9)

[7] OS Development: Low-level Boot-Sector Programming (7/9)

[8] OS Development: Low-level Boot-Sector Programming (8/9)

[9] OS Development: Low-level Boot-Sector Programming (9/9)

[10] Basic OS Development Tutorial Part 1 - Setup & First Boot

[11] Basic OS Development Tutorial Part 2 - Registers, Interrupts, and printing

[12] Basic OS Development Tutorial Part 3 - Printing Strings and Pointers

[13] Basic OS Development Tutorial Part 4 - Kind Of

[14] x86 Instruction Set Reference