A Minimal Boot Sector

The natural first step of building an operating system is to find a way to run programs on “bare hardware”. The task turns out to be quite easy despite its daunting first impression. After researching on the net and trying out various different tutorials on operating systems writing, I decided that all of them are too complicated for my purpose. Most of them assume using a C-like languages as the implementing language, and thus contain code that fits into that kind of framework. Since I will not be using C, I have more freedom of choosing how my boot sector works. After learning the good parts from the tutorials and applying my own simplifications, I arrived at my first boot sector. It is very simple and does very little (It just boots the machine and displays a colorful banner.), but it illustrates my design philosophy quite well.

The criteria I had for the boot sector are:

  • Minimal. It should not do more than illustrating how to boot a computer.
  • Interesting. It should do something interesting so that I can know that the computer is indeed booted. Currently it just displays some colorful text.
  • Independent. Because my OS will be a departure from most popular software systems in existence, it should use as little “library code” as possible. Examples of “library code” include:
    • The BIOS service routines such as “int 10H”. Instead of using them to display text, I decided to write into the VGA memory directly.
    • Complex instructions such as string operations (movsb, scasb, etc.). These instructions are complicated. They clobber certain registers and are not necessarily more efficient. In other words, I’m pursuing a design similar to RISC.
    • The processor’s “push”, “pop” and “ret” instructions. These instructions forces certain language-specific calling conventions and should be avoided.
    • Segmentation and paging. These processor features forces C-style memory management. The high-level programming language I’m designing will hopefully eliminate the need for C entirely. It will take care of memory management itself at object-level. Paging hardware and TLB will be no longer needed in the system.

The code of the boot sector turns out to be very short (only 21 lines of code excluding comments and blank lines).

org 7C00H                      ; the program will be loaded at 7C00H

start:
  mov eax, string_start
  mov ch, 1                    ; ch contains color of text
  mov ebx, 0B8000H + 718H      ; B8000H is VGA memory
                               ; 718H is offset to "center"

print:
  mov cl, [eax]                ; load char into cl
  mov [ebx], cx                ; store [color:char] from cx into VGA
  add ch, 1                    ; change color to (ch+1) mod 16
  and ch, 0x0F
  add eax, 1                   ; advance string pointer
  add ebx, 2                   ; advance VGA pointer
  cmp eax, string_end          ; until the end of string
  jg stop
  jmp print

stop:
  jmp stop                     ; infinite loop after printing

  string_start db 'My colorful new OS!'
  string_end equ $

  times 510-($-$$) db 0        ; pad remainder of boot sector with 0s
  dw 0xAA55                    ; standard PC boot signature

It is in NASM syntax and needs nasm to be assembled into machine code. After that it can be booted by QEMU, a processor emulator. The only two necessary command lines are (assuming the code is stored in a file named myfirst.asm):

nasm -f bin -o myfirst.bin myfirst.asm
qemu -hda myfirst.bin

Of course, you can also burn the disk image (myfirst.bin) onto a CD and boot a real machine from it!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s