Build Your Own Operating System(OS)

Vinojan Veerapathirathasan
5 min readJul 16, 2021

--

In this article, I would like to guide you to build a simple x86 operating system using assembly and C languages. Therefore first we should think about OS.

I have been using Ubuntu 18.04 as the operating system for doing OS development. It is very easy to access the files and easy to use a command-line interface. In the process of making this OS, I learned a lot about computer configuration and access memory. I understand how computer hardware work together. You can also learn them from this article.

What is an Operating System?

An operating system is a system that manages computer hardware, software resources, and provides common services for computer programs running on it. Today, Microsoft Windows is the most leading desktop operating system, next is macOS by Apple Inc. and Linux in third place.

The primary purpose of creating an operating system is to create an effective working environment for the user. An operating system is composed of a collection of software that works and communicates with each other. And the key to creating an operating system is to know how to develop software using computer programming.

Setup Booting

Steps of Computer Booting

I first realized that operating systems are made up of two main parts, the kernel, and the computer program. The kernel is the heart of the OS. It is the primary program that loads when the PC starts, manages system resources and handles requests from computer programs and applications. Computer programs run on top of the kernel and are not intended to do useful work, but rather programs that are required to connect the kernel with user applications and peripherals.

#1 — Set up the Environment

Setup the environment first. In the terminal of Ubuntu using this commands to install essential tools.

sudo apt-get install build-essential nasm genisoimage bochs bochs-sdl

#2 — Create a project folder

#3 — Create a boot loader file in assembly language

Then I saved the following assembly code in a file called loader.s

#3 — Compile assembly code into object file with following command

nasm -f elf32 loader.s

#4 — Link the Kernel

We want GRUB to load the kernel at a memory address larger than or adequate to 0x00100000, because addresses less than 1 megabyte are employed by GRUB itself, BIOS, and memory-mapped I/O. The following linker script is needed to continue the process and Save the linker script into a file called link.id

Generate an executable file called kernel.elf using loader.o and link.ld with the following command

ld -T link.ld -melf_i386 loader.o -o kernel.elf

Obtaining Grub

Download stage2_eltorita file and copy to the project folder baseOS. Then follow the commands

mkdir -p iso/boot/grub 
cp stage2_eltorito iso/boot/grub/
cp kernel.elf iso/boot/

A configuration file menu.lst in the iso/boot/grub path for GRUB must be created. This file tells GRUB where the kernel is located.

We will create an ISO image usinge “genisoimage” command

genisoimage -R                              \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o baseOS.iso \
iso

after this command you can see a ISO image named baseOS.iso now you can run this file in a virtual machine. We use bochs for this action.

before running bochs you first check the folders and files are locate correctly. You can use following command for this in your project directory.

tree

Then you can see the structure below

.
├── iso
│ └── boot
│ ├── grub
│ │ ├── menu.lst
│ │ └── stage2_eltorito
│ └── kernel.elf
├── kernel.elf
├── link.ld
├── loader.o
└── loader.s

Running bochs

After finishing every steps successfully, now we can run the operating system in the bochs emulator using the ISO image you created. Bochs needs a configuration file to start called bochsrc.txt :

after save the file we can run Bochs with the following command:

bochs -f bochsrc.txt -q

then you can type “c” in the terminal then hit enter. You can see the following window:

Is there any problem with generating a window? If your machine didn’t have sdl library then you can change sdl => sdl2. After this change that will work.

after quieting Bochs, display the log file by the following command

cat bochslog.txt

if you find EAX=cafebabe in the log file your OS is running successfully.

further knowledge you can see files here. Hope you all understand this blog and you will be able to create an os after you will implement it.

Thank You!!!

--

--

Vinojan Veerapathirathasan

Software Engineer at EL | Designer | Medium Writer | AI Enthusiast | Entrepreneur | Specializing in Modern Web Application Development