Make your own OS in 30 days – day1: How PC starts up

Make your own OS in 30 daysI bought a very nice book Meke your own OS in 30 days.

Someone created a Mac version here.

When you type below in helloos0.img and boot from there,

    1 0000000: eb4e 9048 454c 4c4f 4950 4c00 0201 0100  .N.HELLOIPL.....                                                                                              
    2 0000010: 02e0 0040 0bf0 0900 1200 0200 0000 0000  ...@............
    3 0000020: 400b 0000 0000 29ff ffff ff48 454c 4c4f  @.....)....HELLO
    4 0000030: 2d4f 5320 2020 4641 5431 3220 2020 0000  -OS   FAT12   ..
    5 0000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    6 0000050: b800 008e d0bc 007c 8ed8 8ec0 be74 7c8a  .......|.....t|.
    7 0000060: 0483 c601 3c00 7409 b40e bb0f 00cd 10eb  ....
...

It starts up and shows “hello, world”.

OS_day01_1

The image was created by compiling the following nas file. The has file is a source fails for nask which was created by the author by copying a popular assembler nasm.

; hello-os
; TAB=4

; For standard FAT12 floppy disk

; --- boot sector ---

DB		0xeb, 0x4e, 0x90
DB		"HELLOIPL"		; Boot sector name (8bytes)
DW		512			; Sector size (must be 512)
DB		1			; Cluster size (must be 1)
DW		1			; Where FAT begins (usually 1)
DB		2			; Number of FATs (must be 2)
DW		224			; Root directory size (usually 224 entries)
DW		2880			; Drive size (must be 2880 sectors)
DB		0xf0			; Media type(must be 0xf0)
DW		9			; Length of FAT area(must be 9 sectors)
DW		18			; Sectors per track(must be 18)
DW		2			; Number of heads (must be 2)
DD		0			; Must be 0 since there is no partition
DD		2880			; Drive size again
DB		0,0,0x29		; Not sure
DD		0xffffffff		; Probably volume serial number
DB		"HELLO-OS   "		; Name of the disc (11 bytes)
DB		"FAT12   "		; Name of the format (8 bytes)
RESB	18				; Reserve 18 bytes

; Program body

DB		0xb8, 0x00, 0x00, 0x8e, 0xd0, 0xbc, 0x00, 0x7c
DB		0x8e, 0xd8, 0x8e, 0xc0, 0xbe, 0x74, 0x7c, 0x8a
DB		0x04, 0x83, 0xc6, 0x01, 0x3c, 0x00, 0x74, 0x09
DB		0xb4, 0x0e, 0xbb, 0x0f, 0x00, 0xcd, 0x10, 0xeb
DB		0xee, 0xf4, 0xeb, 0xfd

; Messages

DB		0x0a, 0x0a		; LF x 2
DB		"hello, koide"
DB		0x0a			; LF
DB		0

RESB	0x1fe-$				; Pad 0x00 until 0x001fe

DB		0x55, 0xaa

; --- end of boot sector ---
; --- 2nd sector and later ---

DB		0xf0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00
RESB	4600
DB		0xf0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00
RESB	1469432
; --- end of the image

When PC starts up, it reads the boot sector of the disk. If the word at 0x1fe is ’55aa’, it assumes it’s a bootable device and continue. According to the author, it’s decided by the PC designer and he doesn’t know why it’s 55aa.

We are changing the DB,DW,DD into more meaningful instructions on day 2.

Leave a Reply

Your email address will not be published. Required fields are marked *