Diving Deeper into Operating Systems – Part 0

Mazdak Pakaghideh

Mazdak Pakaghideh / September 23, 2023

Have you ever wondered how do computers boot up? How does the computer find the operating system when there is none present? Or even how an operating system is developed? I was looking for an answer to them at the time when I had just started learning programming. Asking myself, is it even possible for this calculator app to function standalone without the need of an underlying operating system?

Well to find out, we’ll first need to study a bit about the fundamental concepts of computers.

If you wish to make an apple pie from scratch, you must first invent the universe.

Carl Sagan, American astronomer and planetary scientist.

The OS

We have all accustomed to use different Operating Systems whether for getting on with our daily tasks or having fun in our leisure times. But what are these Operating Systems and what they do ? Operating Systems are obligated to create an interface for softwares to adequately use the required hardware and resources. One way to visualize this is by examining the structure of Atari’s consoles.

When you insert a disk into an old Atari, that disk has complete access to the hardware. Basically the disk acts as both the operating system and the only running game. Because there is no interface for managing multiple running applications, multi-threading, or RAM, if you need to run another game, you technically need to turn off the console and switch the game. Therefore, if you wish to create a game for it, you have to directly control the hardware with your code .

On the other hand, the operating system’s structure is shown in the picture below.

By minnie.tuhs.org

The kernel handles both applications and hardware. Therefore, applications cannot directly access the hardware. Instead, the kernel provides an interface for them to communicate with the hardware. This is why you cannot create an operating system in high-level languages like Python, as you don’t have direct access to the hardware. You are merely importing some modules to be able to call some predefined APIs for accessing the hardware.

So What is The Kernel

The kernel is like the heart of an Operating System. It manages memory, the hard drive, and other hardware parts. but It’s useless without a set of apps. To sum it up, The operating system is built up of the kernel and a set of applications which are usable for the User.

A perfect example that elucidates this concept is the GNU/Linux Project. Richard Stallman, the founder of the Free Software Foundation, initiated the creation of the GNU project, which is an acronym for “GNU is not Unix.” He believed that to create a society of free software, he first needed the most essential application: the operating system (OS). Thus, he began developing the Emacs text editor and the GCC compiler. These were all the tools for an OS though. Meanwhile, when Linus Torvalds published the Linux kernel, everything changed.

They combined the GNU system and the Linux kernel to create a complete, free operating system, as we call it today, GNU/Linux.


Developing an Operating System

Of course we can barely create an operating system like macOS, Linux, Windows or etc. but at least we can try and understand the basics.

If you are unable to create something from scratch, you can never understand it.

Me.

The BIOS

BIOS stands for Basic Input/Output System, which is a program that runs on a computer’s motherboard. It is responsible for initializing hardware components during the boot process and providing a basic set of instructions to the operating system to enable it to start. It also provides a low-level interface for communicating with hardware components, such as hard drives and keyboards.

The Boot Process

When a computer starts up, the BIOS doesn’t know how to load the operating system and requires a boot sector to do so. However, since there is no operating system, there is no path to the boot sector. To load it, the BIOS must know the physical location of the boot sector on the disk, which is cylinder 0, head 0, sector 0, and it’s 512 bytes in size.

By web.cs.ucla.edu

To ensure that a disk is bootable, the BIOS checks for a specific signature. Specifically, the 511th and 512th bytes of the boot sector must be 0xAA55

The BIOS Interrupts

BIOS interrupts are software interrupts that are triggered by the BIOS during the boot process. These interrupts provide a way for the BIOS to communicate with the operating system and other software running on the computer. They can be used to perform tasks such as reading from or writing to disk, initializing hardware components, or performing low-level system functions. When an interrupt is triggered, the processor stops executing the current program and switches to the interrupt handler, which is a special routine that performs the required task before returning control to the main program.

The CPU Registers

CPU registers are small, high-speed storage locations within the CPU that are used to hold data that is being processed or manipulated. They function mostly like RAMs but faster and more temporary. They are typically measured in bits, such as 8-bit or 32-bit registers, and can be used to store things like memory addresses, data being processed, and status information. Registers are an important component of a computer’s architecture, as they allow for fast access to frequently used data and can greatly improve performance.

The Code

It’s obvious that we use assembly for writing the boot sector. The important thing to mention here is that this code must be coded and compiled based on the target computer architecture. for instance, you cannot run this code on an ARM based Raspberry Pi because it’s only for x86 computers and It is coded in x86 assembly.

A Simple Boot Sector in Assembly x86

jmp $ ;Jump to the current address (Infinite loop)****

; Fill with 510 zeros minus the size of the previous code
times 510-($-$$) db 0

; the previously mentioned BIOS signature
dw 0xaa55

Hello World !

In order to print a single character we use the BIOS interrupt0x10which is responsible for various video services.

withAHset to0x0E(tty mode) andALset to the ASCII value of the character to be displayed will display a single character on the screen.

Basically the interrupt0x10reads theAXregister which is 16-bit register consisted ofAH:AL

;helloworld.asm

mov ah, 0x0e ; tty mode
mov al, 'H'
int 0x10
mov al, 'e'
int 0x10
mov al, 'l'
int 0x10
int 0x10 ; 'l' is still on al, remember?
mov al, 'o'
int 0x10

mov al, ' '
int 0x10

mov al, 'W'
int 0x10
mov al, 'o'
int 0x10
mov al, 'r'
int 0x10
mov al, 'l'
int 0x10
mov al, 'd'
int 0x10
mov al, '!'
int 0x10

jmp $ ; jump to current address (infinite loop)

times 510 - ($-$$) db 0
dw 0xaa55

Compiling and Testing

Although we can create an .iso file and boot this boot-sector directly, It’s not recommended. So for now, we stick to QEMU for emulating and NASM as an asssembler.

$ brew install nasm qemu #macOS
$ sudo pacman -S qemu nasm #ArchLinux
$ sudo apt-get install qemu-system nasm #Debian/Ubuntu

and then we proceed to test our Hello World boot sector:

$ nasm -f bin helloworld.asm -o helloworld.bin
$ qemu helloworld.bin

Note: on some systems you may need to use qemu-system-x86_64 command.

If everything goes well, you should see a window like this:

Resources

Discussion (5)


    Hesam Zarnooshe

    September 23, 2023

    Wow🔥

    Sina

    October 13, 2023

    Nice buddy, As simple as possible

      Mazdak

      October 13, 2023

      Thanks !

    Aileen Dubois

    October 27, 2023

    Hello! I understand this is kind of off-topic however I had to ask.
    Does building a well-established blog such as yours require a
    lot of work? I am brand new to writing a blog however I do
    write in my journal every day. I’d like to start a blog so I will
    be able to share my personal experience and views online.

    Please let me know if you have any suggestions or tips for brand new aspiring blog owners.
    Appreciate it!

      Mazdak

      October 28, 2023

      Hello Aillen,
      Well in this day and age, There isn’t any desperate need for individuals to be developers or familiar with web technologies.
      There are many tools out there that potentially could help you materialize what’s in your mind.

      For instance, you can use the CMS I’m currently using “Wordpress”. It’s both well-known and an utterly easy CMS for getting started. There are many themes and plugins developed for this purpose in the WordPress community.
      Or even you can connect your domain to platforms like Medium, you could have an out—of—the—box Blog instantly on the Internet.

      Nevertheless, if you have any experience in Web development, you could be a little more creative.

      In case of any questions, Feel free to reach me.