KFS 1
Starting from byte zero
A bootable i386 kernel written from scratch: GRUB loads it through a Multiboot header, and the kernel writes to video memory itself, at address 0xB8000. No standard library, no system call, not even a memcpy: whatever you need has to be written before it can be used.
- Area
- 42
- system
- kernel
- Stack
- C, x86, Assembly
- Grade
- 120%
- Date
- 2026
Nothing is there
An ordinary program inherits a whole world: printf prints, malloc allocates, an operating system has already set everything up. A kernel inherits none of it. The compiler flags say so plainly: -ffreestanding, -nostdlib, -fno-builtin. No standard library, no system calls, not even the assurance that the compiler will turn a copy loop into an efficient memcpy, since that memcpy does not exist yet.
What is left is an i386 processor, a megabyte mark where the kernel gets loaded, and hardware addressed through ports. This first module asks for one thing only, but that one thing takes all the rest: boot, and print.
What GRUB asks for
GRUB will not load just anything. In the first few kilobytes of the file, on a four-byte boundary, it looks for a Multiboot header: the magic number 0x1BADB002, a word of flags, and a checksum picked so that the three cancel out. If the sum does not come to zero, the file is not a kernel and GRUB moves on.
The linker script produces a raw binary rather than an ELF file, loaded at the one megabyte mark. GRUB therefore has no section table to find its way around, so the header itself carries the addresses of the code, the data and the end of the kernel, under the flag meant for exactly that case.
By the time the kernel’s first byte runs, the processor is already in 32-bit protected mode and interrupts are off. The assembly entry point has three things to do: point the stack at the eight kilobytes reserved for it, call into C, and provide an endless loop in case that call ever returns.
Drawing a letter
The screen is not a device you talk to, it is memory. At address 0xB8000 begins a grid of eighty columns by twenty-five rows, two bytes per cell: the character code, then an attribute whose low four bits give the text colour and whose high four give the background. Showing a letter is a store to memory, nothing more.
Everything else about the terminal follows from that. Scrolling means copying twenty-four rows one row higher and filling the last one with spaces. The cursor, on the other hand, really is hardware: its position goes out in two steps, a register number on port 0x3D4 and half of the address on port 0x3D5.

The missing library
memcpy, memset, strlen, strcmp: the handful of functions the kernel needs are twenty lines each, written without cleverness, one loop and one pointer. A sixteen-bit variant joins them, because the screen fills two bytes at a time rather than one.
Then there is printk, the kernel’s printf, and a way to reach the hardware ports. Those last two are single assembly instructions wrapped in a function: in to read a byte from a port, out to write one. The keyboard, the timer and the interrupt controller are all driven entirely through those two doors.
The keyboard and two terminals
The keyboard does not send letters but scancodes, which a table of a hundred and twenty-eight entries translates. Characters pile up in a line until the enter key, and that line is what a command interpreter will read, later on.
Two terminals live side by side, on F1 and F2. Each keeps its own copy of screen memory, so switching means saving the two thousand visible bytes and copying the other set back. The top row is kept for the terminal’s name, and scrolling never touches it.
That is where the kernel is ready for what comes next. It boots, it prints, it listens. What it does not have yet are the tables the processor expects from it.
