Writing a Simple Operating System — from Scratch

(Jeff_L) #1

Chapter 3


Boot Sector Programming (in


16-bit Real Mode)


Even with the example code provided, you will no doubt have found it frustrating writing
machine code in a binary editor. You’d have to remember, or continuously reference,
which of many possible machine codes cause the CPU to do certain functions. Luckily,
you are not alone, and soassemblers have been written that translate more human
friendly instructions into machine code for a particular CPU.
In this chapter we will explore some increasingly sophisticated boot sector programs
to familiarise ourselves with assembly and the barren, pre-OS environment in which our
programs will run.


3.1 Boot Sector Re-visited


Now, we will re-create the binary-edited boot sector from Section XXX instead using
assembly language, so that we can really appreciate the value even of a very low-level
language.
We can assemble this into actual machine code (a sequence of bytes that our CPU
can interpret as instructions) as follows:


$nasm bootsect.asm -f bin -o bootsect.bin
Wherebootsect.asmis the file into which we saved the source code in Figure 3.
andbootsect.binis the assembled machine code that we can install as a boot sector
on a disk.
Note that we used the-f binoption to instruct nasm to producerawmachine code,
rather than a code package that has additional meta information for linking in other rou-
tines that we would expect to use when programming in a more typical operating system
environment. We need none of that cruft. Apart from the low-level BIOS routines, we
are the only software running on this computer now. We are the operating system now,
albeit at this stage with nothing more to offer than an endless loop --- but we will soon
build up from this.


8

Free download pdf