org 0000h
jmp     start
oem db      "DOSOPREY"
bytespersector dw      200h
sectorspercluster db      1
reservedsectors dw      0001
totalfats db      02
maxrootentries dw      224d
totalsectorssmall dw      0b40h
mediadescriptor db      0f0h
sectorsperfat dw      09h
sectorspertrack dw      12h
numheads dw      02
dd      0
dd      0
drivenumber db      0
flags db      0
sig db      29
id dd      0ffffffffh
volumelabel db      "DOS OF PREY"
systemid db      "FAT12   "
start:
        cli
	mov     ax, 0x07C0
        mov     ds, ax
        mov     es, ax
        mov     fs, ax
        mov     gs, ax
        mov     ax, 0000
        mov     ss, ax
        mov     sp, 0ffffh
        sti
        xor     cx, cx
        cwd
        mov     ax, 32d
        mul     word[maxrootentries]
        div     word[bytespersector]
        xchg    ax, cx
        mov     al, byte[totalfats]
        mul     word[sectorsperfat]
        add     ax, word[reservedsectors]
        mov     word[datasector], ax
        add     word[datasector], cx
        mov     bx, 200h
        call    read_sectors
.loop:
        push    cx
        mov     cx, 11d
        mov     si, filen
        push    di
        rep     cmpsb
        pop     di
        je      load_fat
        pop     cx
        add     di, 20h
        loop    .loop
        jmp     error
load_fat:
        mov     dx, word[di+1ah]
        mov     word[cluster], dx
        xor     ax, ax
        mov     al, 2
        mul     word[sectorsperfat]
        mov     cx, ax
        mov     ax, 1
        mov     bx, 200h
        call    read_sectors
        mov     ax, 100h
        mov     es, ax
        mov     bx, 0000
        push    bx
load_file:
        mov     ax, word[cluster]
        pop     bx
        call    clusterlba
        xor     cx, cx
        mov     cl, byte[sectorpercluster]
        call    read_sectors
        push    bx
        mov     ax, word[cluster]
        mov     cx, ax
        mov     dx, ax
        shr     dx, 01h
        add     cx, dx
        mov     bx, 200h
        add     bx, cx
        mov     dx, word[bx]
        test    ax, 01h
        jnz     odd_cluster
        and     dx, 0000111111111111b
        jmp     done
odd_cluster:
        shr     dx, 0004h
done:
        mov     word[cluster], dx
        cmp     dx, 0ff0h
        jb      load_file
        push    100h
        push    0h
        retf
error:
        mov     si, fmsg
        call    printf
        xor     ah, ah
        int     16h
        int     19h
printf:
        lodsb
        cmp     al, 0
        je      p_done
        mov     ah, 0eh
        int     10h
        jmp     printf
p_done:
        ret
read_sectors:
        mov     di, 05
sect_loop:
        push    ax bx cx
        call    lbachs
        mov     ah, 02h
        mov     al, 01
        mov     ch, byte[absolutetrack]
        mov     cl, byte[absolutesector]
        mov     dh, byte[absolutehead]
        mov     dl, byte[drivenumber]
        int     13h
        jnc     sect_success
        xor     ax, ax
        int     13h
        dec     di
        pop     cx bx ax
        jnz     sect_loop
        int     19h
sect_success:
        pop     cx bx ax
        add     bx, 200h
        inc     ax
        loop    read_sectors
        ret
clusterlba:
        sub     ax, 2
        xor     cx, cx
        mov     cl, 9
        mul     cx
        add     ax, word[datasector]
        ret
lbachs:
        xor     dx, dx
        div     word[sectorspertrack]
        inc     dl
        mov     byte[absolutesector], dl
        xor     dx, dx
        div     word[numheads]
        mov     byte[absolutehead], dl
        mov     byte[absolutetrack], al
        ret
fmsg    db      "Boot Failed!", 0
absolutesector  db 0
absolutehead    db 0
absolutetrack   db 0
datasector      dw 0
cluster         dw 0
filen           db "KERNEL  BIN"
times   510-($-$$) db 0
dw 0aa55h
