Implementing a simple OS

Chandima Maduwanthi
3 min readJul 26, 2020

--

This article explains how to implement a simple operating system to display the hardware information of the machine as an extension of minimalOS-JOSH. I implemented MyOS on Linux operating system.

In order to create an operating system we need to have two files; bootloader & kernel

Bootloader

When we power up the PC, all registers are blanked and the microprocessor is set to a reset state. Then the address 0xFFFF is loaded into the code segment and the instruction present at that location is executed. Taking this fact into consideration the basic software called BIOS (Basic Input Output System) is present at that location. So the BIOS will execute as a result. The BIOS will run a necessary check of all memory for errors, connected devices — like serial ports etc, and after completion of these system checks will search for the Operating System, load it and execute it. The BIOS will not load the complete operating system. It will just load a fragment of code present in the first sector (also called boot sector) of the floppy disk. This fragment of code will be 512 bytes long (if we use a DOS formatted floppy) and the last two bytes of the fragment should be 0xAA55 (also called the boot signature. This tiny fragment of code that has to be present in the boot sector is called the bootloader.

Kernel

The kernel is the core of the OS performing most of the house-keeping work and providing the applications with a good set of functionality. The kernel is generally never unloaded from memory. The kernel (or its functionality) should be available always and applications should have a way of interacting with the kernel in a seamless manner.

Let’s move to the development process

Requirements:

· NASM assembler —to convert our .asm files to .bin files

· QEMU PC emulator —to test our OS

First we have to create a bootloader. I used the same bootload.asm file from JOSH. You can find it here.

Save it with .asm extention. Then open the terminal and change the directory. Type the following command:

nasm -f bin -o bootload.bin bootload.asm

This will convert your .asm file to .bin file. Now the bootloader is ready.

Then we have to create the kernel. You can find my kernel code here. I created it by adding new code lines to the kernel of JOSH, to display basic hardware information of the machine.

As the bootloader, save it with .asm extention. Then run following command to convert it to .bin file:

nasm -f bin -o kernel.bin kernel.asm

We can use build-linux.sh file from MikeOS(MikeOS is an operating system written in assembly language as a learning tool to show how simple 16-bit, real-mode OSes work)to compile them easily without using above mentioned commands. You can get it here. Run following command in terminal to use it.

sudo bash build-linux.sh

This will use NASM to assemble the bootloader and kernel, then write the bootloader to the myos.flp floppy disk image in the disk_images/ directory. (It writes the 512-byte bootloader to the first sector of the floppy disk image to create a boot sector and set up a DOS-like filesystem.) Next, the build script loopback-mounts the myos.flp image onto the filesystem — in other words, mounting the image as if it was a real floppy. The script copies over the kernel (kernel.bin) before unmounting the floppy image.

With that done, the script runs the ‘mkisofs’ utility to generate a CD-ROM ISO image of MyOS, injecting the floppy image as a boot section. So we end up with two files in the disk_images/ directory: one for floppy disks and one for CD-Rs. You can now use them in an emulator or on a real PC.

Run following command to test your OS in QEMU PC emulator.

qemu-system-i386 -soundhw pcspk -drive format=raw,file=disk_images/myos.flp,index=0,if=floppy

The result wil be:

Here is the complete code:

REFERENCES:

--

--

Chandima Maduwanthi
Chandima Maduwanthi

Written by Chandima Maduwanthi

BSc.(Hons) Software Engineering Undergraduate | University Of Kelaniya