Build Your Own OS

Niroshan Pushparaj
4 min readJul 15, 2021

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

What is an OS ?
Operating system, program that manages a computer’s resources, especially the allocation of those resources among other programs. Typical resources include the central processing unit, computer memory, file storage, input/output devices, and network connections.

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 command line interface. In the process of making this OS I learned a lot about computer configuration and access memory. I understand how computer hardwares work together.You can also learn them from this article.

Steps
01. 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

02. Create a project folder Ex- baseOS

03. Create a boot loader file in assembly language called loader.s

04. Compile assembly code into object file with following command

nasm -f elf32 loader.s

05. Create a linker script called link.ld

06. Generate a executable file called kernel.elf using loader.o and link.ld with 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 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 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 able to create an os after you will implement it.

Thank You!!!

--

--