Hello World Boot Sector

2023-03-08

I wanted to adventure into learning some x86 16-bit real mode assembly for the
original IBM PC BIOS. This means even PCs with CSM still can boot this!

I used this site for referencing IBM BIOS routines.


16-bit x86 assembly

I save my assembly in hello.asm

bits 16				; Tell nasm we're doing 16-bit mode
org 7C00h			; The BIOS loads the boot sector to this address then jumps

start:
	mov ah, 0x0		; set video mode
	mov al, 0x7		; 80x25 monochrome text (supports even MDA!)
	int 10h			; BIOS interrupt video routine

	mov ah, 0x2		; move cursor
	mov dx, 0x0		; set cursor to top-left corner
	int 10h

init_video:
	mov ah, 0xE		; TTY text printing
	mov si, msg		; put our message in the stack index

loop:
	lodsb			; will load msg into al and increment si for us
	or al, al		; if al is null 0, we've reached the end, so halt CPU
	jz halt
	int 10h
	jmp loop

halt:
	hlt


msg: db "Hello, world!", 0

times 510-($-$$) db 0	; fill up to 510 bytes
dw 0xAA55				; special boot sector signature

Assemble and Test

I assemble with nasm:
nasm -f bin -o hello.bin hello.asm
Then I can test it in QEMU with SeaBIOS:
qemu-system-i386 -drive format=raw,file=hello.bin

QEMU displaying Hello, world!