
loadimage  proc near
              ; ES:BX = File Buffer
              ; AX = First Cluster Number
              ; Only for FAT12 Floppy Disks (1 sector per cluster)
load_file_next_cluster:
                mov     word ptr [BASE+File_Cluster], ax
                mov     word ptr [BASE+File_BufferOff], bx
                dec     ax                      ; First cluster is cluster 2
                dec     ax               
                mov     cx, 1                   ; Sector count
                add     ax, 33                  ; Beginning sector of Data
                call    readdisk
                jc      short load_file_retn
cont_load_file:
                mov     ax, word ptr [BASE+File_Cluster]

                ; INPUT -> AX = Cluster Number
                ; OUTPUT -> clc -> No Error, AX = Next Cluster
                ; OUTPUT -> stc & AX=0 -> End Of Cluster Chain
                ; OUTPUT -> stc & AX>0 -> Error
get_FAT12_next_cluster:
                mov     bx, ax
                mov     dx, 3
                mul     dx
                shr     ax, 1 ; Divide by 2
                xchg    bx, ax
                ; BX = Buffer Byte Offset
                ; AX = Current Cluster
                mov     ax, word ptr [BASE+READBUF+FATBUF][BX]
                jnc     short get_FAT12_nc_even
                shr     ax, 1
                shr     ax, 1
                shr     ax, 1
                shr     ax, 1
gnc_fat12_eoc_check:
                cmp     ax, 0FF7h    ; ff7
                jc      short pass_gnc_FAT12_eoc_check  ; jc 
                xor     ax, ax
pass_gnc_FAT12_eoc_check:
                cmc
                mov     word ptr [BASE+File_Cluster], ax
                jmp     short cont_load_file_2
get_FAT12_nc_even:
                and     ah, 0Fh
                jmp     short gnc_fat12_eoc_check

                jnc     short cont_load_file_2
                cmp     ax, 1
                cmc
load_file_retn:
                ret
cont_load_file_2:
                cmp     ax, 0
                je      short load_file_retn
                mov     bx, word ptr [BASE+File_BufferOff]
                add     bx, 512
                jmp     short load_file_next_cluster

loadimage  endp


File_Cluster    dw 0
File_BufferOff  dw 0
