Boot sector: hello in 512 bytes

The setup

A boot sector is 512 raw bytes the BIOS loads at physical address 0x7C00 and jumps to. Assemble with -f bin, set the origin, and switch to 16-bit mode, because the CPU starts in real mode:

bits 16 org 0x7C00 start: ; real-mode code here

Printing in real mode

In real mode there are no syscalls: the BIOS provides services through INT 10h. Function 0Eh of INT 10h prints one character in AL to the screen, teletype style:

mov si, msg print_loop: lodsb ; AL = [SI], SI++ or al, al jz done mov ah, 0x0E int 0x10 jmp print_loop

The signature

The last two bytes must be 0x55 then 0xAA, or the BIOS rejects the disk. Pad to 510 bytes and append the signature with TIMES:

times 510-($-$$) db 0 dw 0xAA55

Next: Capstone: command-line calculator

Article author: Arthur Isaev

Banner image