BITS 16
ORG 0x7C00

start:
    cli
    mov [boot_drive], dl
    
    ; Set up segments
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, 0x1C00
    
    mov si, loading
    call print
    
    ; Load kernel at 0x00500
    mov ax, 0x0500
    mov es, ax
    xor bx, bx
    
    mov ah, 0x02
    mov al, 8
    mov ch, 0
    mov cl, 2
    mov dh, 0
    mov dl, [boot_drive]
    int 0x13
    jc disk_error
    
    ; Jump to kernel
    cli
    jmp 0x500:0x0000

print:
    lodsb
    test al, al
    jz .done
    mov ah, 0x0e
    int 0x10
    jmp print
.done:
    ret

disk_error:
    mov si, error
    call print
    hlt

boot_drive db 0
loading db "Loading kernel...",0
error db "Disk error",0

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