Skip to content

Instantly share code, notes, and snippets.

@AiziChen
Last active November 17, 2022 09:01
Show Gist options
  • Save AiziChen/1a4ddff91bda187bc99928f0b1758900 to your computer and use it in GitHub Desktop.
Save AiziChen/1a4ddff91bda187bc99928f0b1758900 to your computer and use it in GitHub Desktop.
Minimal x86 boot sector written by nasm-style assembly

Minimal x86 boot sector written by nasm-style assembly

tools requirement

nasm & qemu emulator

  1. Install nasm, then run these command:
nasm -f bin -o boot.bin boot-sector.asm
  1. Install qemu, run these command:
qemu-system-i386 -hda boot.bin
org 7C00H ; the program will be loaded at 7C00H
start:
mov eax, string_start
mov ch, 1 ; ch contains color of text
mov ebx, 0B8000H + 718H ; B8000H is VGA memory
; 718H is offset to approx center
print:
mov cl, [eax] ; load char into cl
mov [ebx], cx ; store [color:char] from cx into VGA
add ch, 1 ; change color to (ch+1) mod 16
and ch, 0x0F
add eax, 1 ; advance string pointer
add ebx, 2 ; advance VGA pointer
cmp eax, string_end ; until the end of string
jg stop
jmp print
stop:
jmp stop ; infinite loop after printing
string_start db 'My colorful new OS!'
string_end equ $
times 510-($-$$) db 0 ; pad remainder of boot sector with 0s
dw 0xAA55 ; standard PC boot signature
jump $
times 510-($-$$) db 0
dw 0xAA55
@AiziChen
Copy link
Author

booted result:
Screenshot 2022-11-17 at 16 59 47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment