
loadimage proc near
;       loadFile: Loads the file into memory, one cluster at a time.
; si = FATCHAIN entry (cluster)
; ES:BX = load address
cluster_next:
          push ax  ; cluster
          push bx  ; offset
fat_12:

; Load image (cluster, seg:offset) : CF=1 on error else okay!
; first cluster is 2 so decrement cluster by 2
; add cluster to data area start call read disk
; (((cluster - 2)*CPS)+data_start) = logical sector

          dec     ax                      ; First cluster is cluster 2
          dec     ax               
          mov     cx, 1                   ; Sector count
          add     ax, 33                  ; Beginning sector of Data
          call    readdisk
          jc      loadfile_error
          ; If the cluster number was even, the cluster value is now in
          ; bits 0-11 of AX. If the cluster number was odd, the cluster
          ; value is in bits 4-15, and must be shifted right 4 bits. If
          ; the number was odd, CF was set in the last shift instruction.
          pop  bx
          pop  ax
          add  bx, 512
          mov  si, ax
          mov  ax, word ptr [BASE+READBUF+FATBUF][si]
          push ax
;          mov  bx, 3
;          xor  dx, dx
;          mul  bx
;          shr  ax, 1
          ; (N*3)/2 = sector
          jnc     fat_even
          mov     cl, 4
          shr     ax, cl
fat_even: and     ah, 0fh       ; mask off the highest 4 bits
          cmp     ax, 0ff8h     ; check for EOF
          jb      cluster_next      ; continue if not EOF
loadfile_error:
          ret
loadimage endp

FAT12FL proc near
                ; Set ES:DI to the temporary storage for the FAT chain.
                mov  bx, LOADSEG
                push bx
                pop  es
                xor  bx, bx
                mov  si, offset BASE+READBUF+FATBUF      ; set DS:SI to the FAT chain
                call loadimage
                ret
FAT12FL endp
