A very tiny OS written in Zig, which can be run on QEMU.
- Zig 0.13
- Fun
- only ~110 LoC
- Zig : 37 Loc
- Zig Build : 31 LoC
- Assembly : 31 Loc
- Linker Script : 25 Loc
- Very minimalist
- Easy to understand
- Great for Learning
- Altought does not anything useful it shows how Zig can be used to write an OS
- POC
./build.sh./run.shboot.s (Assembly)
- Bootloader (like GRUB) finds the Multiboot header
- Loads the kernel into memory at 1MB (as specified in
link.ld) - Jumps to
_start _startcalls Zigkmain()function- kernel takes control and runs
link.ld (Linker Script)
- Declares
_startas the program's entry point - When a bootloader loads the kernel, it will jump to this symbol
- This corresponds to the
_startlabel inboot.sassembly file - Memory Layout Configuration
SECTIONSblock defines how sections are arranged in memory. = 1Msets the location counter to 1 megabyte- 1MB is chosen because it's above the first megabyte, which contains:
- Real-mode interrupt vectors (0x0000-0x03FF)
- BIOS data area (0x0400-0x04FF)
- Video memory (0xA0000-0xFFFFF)
- BIOS ROM (0xF0000-0xFFFFF)
- Section Organization - Text Section (Executable Code)
.text:Contains all executable code- Bootloaders scan the first 8KB for this header
- Data Section (Initialized Data)
.data:Contains initialized global and static variables
- Compilation: source files are compiled into object files
- Linking: The linker uses this script to combine object files
- Memory Layout: Creates a binary with this structure:
0x100000 (1MB): Multiboot header
kernel code (_start, kmain, etc.)
Read-only data (strings, constants)
Initialized variables
Space reserved for uninitialized variables
- Bootloader: Loads the binary at 1MB, finds the Multiboot header, jumps to
_start - Execution: kernel runs starting from the
_startsymbol