Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 2. COMPUTER ARCHITECTURE AND THE BOOT


PROCESS 6


2.3.1 Bochs: A x86 CPU Emulator


Bochs requires that we set up a simple configuration file,bochsrc, in the local directory,
that describes details of how real devices (e.g. the screen and keyboard) are to be
emulated and, importantly, which floppy disk image is to be booted when the emulated
computer starts.
Figure 2.2 shows a sample Bochs configuration file that we can use to test the boot
sector written in Section XXX and saved as the filebootsect.bin


# Tell bochs to use our boot sector code as though it were
# a floppy disk inserted into a computer at boot time.
floppya: 1_44=boot_sect.bin , status=inserted
boot: a

Figure 2.2: A simple Bochs configuration file.


To test our boot sector in Bochs, simply type:
$bochs
As a simple experiment, try changing the BIOS magic number in our boot sector to
something invalid then re-running Bochs.
Since Bochs’ emulation of a CPU is close to the real thing, after you’ve tested code
in Bochs, you should be able to boot it on a real machine, on which it will run much
faster.


2.3.2 QEmu


QEmu is similar to Bochs, though is much more efficient and capable also of emulating
architectures other than x86. Though QEmu is less well documented than Bochs, a need
for no configuration file means it is easier to get running, as follows:


$qemu <your-os-boot-disk-image-file>

2.4 The Usefulness of Hexadecimal Notation


We’ve already seen some examples ofhexadecimal, so it is important to understand why
hexadecimal is often used in lower-level programming.
First it may be helpful to consider why counting in ten seems so natural to us,
because when we see hexadecimal for the first time we always ask ourselves: why not
simply count to ten? Not being an expert on the matter, I will make the assumption that
counting to ten has something to do with most people having a total of ten fingers on
their hands, which led to the ideas of numbers being represented as 10 distinct symbols:
0,1,2,...8,

Free download pdf