Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 5. WRITING, BUILDING, AND LOADING YOUR


KERNEL 52


BEGIN_PM:

mov ebx , MSG_PROT_MODE ; Use our 32-bit print routine to
call print_string_pm ; announce we are in protected mode

call KERNEL_OFFSET ; Now jump to the address of our loaded
; kernel code , assume the brace position ,
; and cross your fingers. Here we go!

jmp $ ; Hang.

; Global variables
BOOT_DRIVE db 0
MSG_REAL_MODE db "Started in 16-bit Real Mode", 0
MSG_PROT_MODE db "Successfully landed in 32-bit Protected Mode", 0
MSG_LOAD_KERNEL db "Loading kernel into memory.", 0

; Bootsector padding
times 510-($-$$) db 0
dw 0xaa55

Before running this command in Bochs, ensure that the Bochs configuration file has
the boot disk set to your kernel image file, as in Figure XXX.


floppya: 1_44=os-image , status=inserted
boot: a

One question you might be wondering is why we loaded as many as 15 segments
(i.e. 512 * 15 bytes) from the boot disk, when our kernel image was much smaller than
this; actually it was less than one sector in size, so to load 1 sector would have done
the job. The reason is simply that it does not hurt to read those additional sectors
from the disk, even if they have not been initialised with data, but it may hurt when
trying to detect that we didn’t read enough sectors at this stage when we later add
to, and therefore increase the memory footprint size of, our kernel code: the computer
would hang without warning, perhaps halfway though a routine that was split across an
unloaded sector boundary --- an ugly bug.
Congratulations if an ’X’ was displayed in the top-left corner of the screen, since
though appearing pointless to the average computer user this signifies a hugh step from
where we started: we have now boot-strapped into a higher-level language, and can start
to worry less about assembly coding and concern ourselves more with how we would
like to develop our operating system, and learning a little more about C, of course; but
this is the best way to learn C: looking up to it as a higher level language rather than
looking down upon it from the perspective of an even higher abstraction, such as Java
or a scripting language (e.g. Python, PHP, etc.).

Free download pdf