Orange’s Operating System Implementation Notes

x86 processors and operating modes

The 8086 is a 16-bit processor. The 80386 adds 32-bit registers and a 32-bit address space. In real mode, the 8086 uses 16-bit registers and a 20-bit address bus for a 1 MiB address space. The 80386 in protected mode can address 4 GiB through a 32-bit linear address space.

A protected-mode segment register combines a visible 16-bit selector with a hidden descriptor cache that software cannot read directly. The global descriptor table (GDT) supports segmentation in protected mode. The interrupt descriptor table (IDT) identifies exception and interrupt handlers. Protected-mode software cannot call real-mode interrupt services directly.

Segment privileges

A conforming code segment can admit a direct transfer from an equally privileged or less privileged caller, subject to the privilege checks, without raising the caller's privilege. A direct transfer to a nonconforming code segment has stricter privilege checks and normally stays at the same privilege level. For ordinary data segments, access requires both the current privilege and the requested privilege to meet the descriptor's threshold.

Assembly reference

EAX
The accumulator, used implicitly by various arithmetic instructions. AX is its lower 16 bits. AH and AL are the upper and lower bytes of AX.
EBX
A general-purpose register often used as an addressing base.
ECX
The implicit counter for REP-prefixed instructions and LOOP.
EDX
Holds the remainder in common forms of integer division.
ESI and EDI
The source and destination indices in string instructions, conventionally DS:ESI and ES:EDI.
EBP
Commonly used as a function frame pointer.
ESP
The stack pointer. The stack grows toward lower addresses, and an ordinary 32-bit push subtracts four bytes.
DB, DW, DD
Define one, two, and four bytes respectively.
CMP
Subtracts the second operand from the first for flag calculation and discards the result.
INC and DEC
Increment or decrement an operand by one.
LODSB
Loads the byte at DS:SI or DS:ESI into AL. The direction flag controls how the source index is adjusted. LODSB does not change the arithmetic flags.

Conditional branches

For unsigned comparisons, JA requires CF=0 and ZF=0, JAE/JNB tests above or equal, JB/JNAE tests below, and JBE/JNA tests below or equal. JE/JZ branches on ZF=1 and JNE/JNZ on ZF=0. JC and JNC test the carry flag, JO and JNO test overflow, JS and JNS test sign, and JP/JPE and JNP/JPO test even and odd parity.

Memory descriptors

An address range descriptor (ARDS) contains the low and high 32-bit halves of the base address, the low and high halves of the byte length, and a type field. Type 1 identifies RAM available to the OS. Type 2 identifies reserved or in-use regions. Unrecognized types should be treated as reserved.

Interrupts and exceptions

Hardware interrupts arrive from devices. Software can explicitly invoke an interrupt where privilege checks allow it. Exceptions arise in step with instruction execution, while external interrupts can arrive asynchronously. A fault such as a page fault can often be handled and restarted, a trap is reported after the relevant instruction, and an abort denotes a serious error without a reliable restart point.

Bochs and the build workflow

For boot-code debugging, use a disassembly to identify the instruction pointer of interest. The original workflow stops a test guest at a deliberate loop using jmp $, enters the Bochs debugger, inspects registers with r, sets EIP to the intended instruction, and steps with s. The examples below cover disassembly, mounting and updating the boot image, running Bochs, assembling with NASM, compiling with GCC, and linking with LD.

ndisasm -o 0x7c00 boot.bin >> disboot.asm
sudo mount -o loop a.img /mnt/floppy/
sudo cp kernel.bin /mnt/floppy/
sudo umount /mnt/floppy/
cp /home/username/Desktop/boot.asm ./
dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc
bochs -f bochsrc
nasm loader.asm -o loader.bin

nasm -f elf kliba.asm -o kliba.o
gcc -c start.c -o start.o
ld -s -Ttext 0x30400 -o kernel.bin kernel.o string.o start.o