os-kernel-demo
Source code

Source Code

Every file that makes up the kernel. Click a filename to view its source - ready to copy, paste, and build.

bootloader.asm

NASM

The bootloader is the first code the CPU executes after the BIOS. It fits in 512 bytes, loads the kernel from disk, sets up protected mode, and jumps to the kernel.

[bits 16]
[org 0x7C00]

start:
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, 0x7C00

    ; Load kernel from disk (sector 2 onwards)
    mov ah, 0x02      ; BIOS read sector
    mov al, 0x20      ; sectors to read (32)
    mov ch, 0x00      ; cylinder
    mov cl, 0x02      ; start sector
    mov dh, 0x00      ; head
    mov bx, 0x1000    ; load address (0x1000:0x0000 = 0x10000)
    int 0x13

    ; Switch to protected mode
    cli
    lgdt [gdt_desc]
    mov eax, cr0
    or al, 1
    mov cr0, eax
    jmp 0x08:protected_mode

[bits 32]
protected_mode:
    mov ax, 0x10      ; data segment
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    mov esp, 0x90000

    ; Jump to kernel entry at 0x00100000
    jmp 0x00100000

; Simple GDT
gdt_start:
    dq 0x0000000000000000  ; null descriptor
    dq 0x00CF9A000000FFFF  ; code segment
    dq 0x00CF92000000FFFF  ; data segment
gdt_end:

gdt_desc:
    dw gdt_end - gdt_start - 1
    dd gdt_start

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