Loadimage proc near
; ES:BX = Buffer
;    AX = First Cluster no.
;    This only works on FAT12 disks where 1 sector is per cluster
         mov  WORD ptr [BASE+cs:lHiddenSects], ax                  ; cluster to read
         mov  word ptr [BASE+cs:lLargesects], bx
         push es
         pop  word ptr [BASE+cs:lLargesects+2]

LOAD_IMAGE:

 ClusterLBA:
          dec  ax
          dec  ax
          xor  cx, cx
          mov  cl, byte ptr [BASE+bSectsperclust]
          mov  bx, word ptr [BASE+cs:lLargesects]
          push word ptr [BASE+cs:lLargesects+2]
          pop  es
          xor  dx, dx
          mul  cx
          add  ax, 33       ; beginning of sector data
          call ReadDisk
          mov  ax, word ptr [BASE+lHiddensects+2]
          mov  bx, word ptr [BASE+wSectsize]
          add  ax, bx
          inc  ax
          mov  word ptr [BASE+lHiddensects+2], ax
          mov  ax, word ptr [BASE+cs:lLargesects]
          mov  bx, word ptr [BASE+wSectsize]
          add  ax, bx
          inc  ax
          mov  word ptr [BASE+cs:lLargesects], ax

          ; compute next cluster
          mov     ax, word ptr [BASE+cs:lHiddenSects]                  ; identify current cluster
          mov     cx, ax                              ; copy current cluster
          mov     dx, ax                              ; copy current cluster
          shr     dx, 1                          ; divide by two
          add     cx, dx                              ; sum for (3/2)
          mov     bx, offset [BASE+READBUF+FATBUF] ; 0x0200                          ; location of FAT in memory
          add     bx, cx                              ; index into FAT
          mov     dx, WORD PTR [bx]                       ; read two bytes from FAT
          test    ax, 1
          jnz     ODD_CLUSTER
EVEN_CLUSTER:
          and     dx, 0000111111111111b               ; take low twelve bits
          jmp     DONE
ODD_CLUSTER:
          shr     dx, 4                          ; take high twelve bits
DONE:
          mov     word ptr [BASE+cs:lHiddenSects], dx                  ; store new cluster
          cmp     dx, 0FF0h                          ; test for end of file
          jb      LOAD_IMAGE
_DONE:
          ret
loadimage endp
