loadimage proc near
; ES:BX = Buffer
;    AX = First Cluster no.
;    This only works on FAT12 disks where 1 sector is per cluster
loadnext:
         mov  word ptr [BASE+FileCluster], ax
         mov  word ptr [BASE+FileBufferOfs], bx
         dec  ax
         dec  ax   ; first cluster is 2
         mov  cx, 1
         add  ax, 33  ; beginning of sector data
         call readdisk
         jc   load_file_retn
cont_load:
         mov  ax, word ptr [BASE+FileCluster]
         call get_next_cluster
         jc   chk_endofchain
         ; stc and ax=0 end of chain
         ; stc and ax=1 error
         jmp  short cont_load_file2
chk_endofchain:
         cmp  ax, 0
         je   load_file_retn
         cmp  ax, 1
         je   load_file_error
cont_load_file2:
         mov  bx, word ptr [BASE+FileBufferOfs]
         add  bx, word ptr [BASE+wSectsize]
         jmp  loadnext
load_file_error:
         cmp  ax, 1
         cmc
load_file_retn:
         ret
FileCluster   dw 0
FileBufferOfs dw 0
loadimage endp

get_next_cluster proc near
; AX = CLuster no.
; clc  no error AX = cluster
; stc and ax=0 end of chain
; stc and ax=1 error
nextcluster:
         mov  bx, ax
         mov  dx, 3
         mul  dx
         shr  ax, 1  ; divide by 2
         xchg bx, ax
         ; BX = Buffer
         ; AX = Current cluster
         mov  ax, word ptr [BASE+ReadBuf+FATBUF][BX]
         jnc  nc_even
         shr ax, 1
         shr ax, 1
         shr ax, 1
         shr ax, 1
eoc_check:
         cmp  ax, 0ff8h
         jc   eoc_true
         xor  ax, ax
eoc_true:
         cmc
         ret
nc_even:
         and  ah, 0fh
         jmp  eoc_check
         ret
get_next_cluster endp
