Make your own OS in 30 days – day2: Assembly Language

Day 2 changes the previous one into x86 assembly code as below. That will go into the boot sector and the other sectors are created by the author’s image edit tool.

; hello-os
; TAB=4

ORG		0x7c00			; Where the program is loaded
						; If ORG is available, $ will be the address of the current code
						; 0x00007c00 - 0x00007dff : boot sector should be loaded here

; For standard FAT12 floppy disk
; --- boot sector ---

JMP		entry
DB		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

entry:
MOV		AX,0			; init registers
MOV		SS,AX
MOV		SP,0x7c00
MOV		DS,AX
MOV		ES,AX

MOV		SI,msg
putloop:
MOV		AL,[SI]
ADD		SI,1
CMP		AL,0
JE		fin
MOV		AH,0x0e			; Function to display a character
MOV		BX,15			; Color code
INT		0x10			; Video BIOS call (INT 0x10), see http://en.wikipedia.org/wiki/BIOS_interrupt_call
JMP		putloop
fin:
HLT					; Halt CPU when something happens
JMP		fin			; Infinite loop

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

RESB	0x7dfe-$			; Pad 0x00 until 0x7dfe

DB		0x55, 0xaa

We are making it 32bit on Day 3.

Leave a Reply

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