[org 0x7c00]
section BOOTSECTOR start=7C00h

OS_SEGMENT equ 0x100

; https://github.com/cfenollosa/os-tutorial
boot:
    ; Load from disk
    mov ah, 0x02 ; read
    mov al, 0x02 ; No of sectors (1)
    mov cl, 0x02 ; Sector start (1 = boot sector, 2 = OS)
    mov ch, 0x00 ; Cylinder 0
    mov dh, 0x00 ; Head 0
    
    mov bx, OS_SEGMENT ; Set target segment
    mov es, bx
    mov bx, 0x0 ; Target offset in segment
    
    int 0x13      ; Send read interrupt
    jc disk_error
    
    ; Now that the OS is loaded in 0x1000, let's call main!
    jmp 0x100:0x0

disk_error:
    mov si, DISK_ERR_MSG
    call bs_print
    jmp $
    
; https://stackoverflow.com/a/53403853
bs_print:
    mov     ah, 0x0e            ; BIOS TTY function
    
    .L0:
        lodsb
        or      al, al
        jnz     .J0
        ret
    .J0:
    
    int     0x10
    jmp     .L0

jmp $ ; Halt

; Boot sector libs


; Boot sector data
DISK_ERR_MSG:
    db 'DISK ERROR: Failed to load kernel!', 0

; Boot sector padding and magic number
times 510-($-$$) db 0
dw 0xaa55