[41] | 1 | ; File name : FileIO.asm
|
---|
| 2 | ; Project name : Assembly Library
|
---|
| 3 | ; Created date : 1.9.2010
|
---|
[66] | 4 | ; Last update : 6.12.2010
|
---|
[41] | 5 | ; Author : Tomi Tilli
|
---|
| 6 | ; Description : Functions for file access.
|
---|
| 7 |
|
---|
| 8 | ; Section containing code
|
---|
| 9 | SECTION .text
|
---|
| 10 |
|
---|
| 11 | ;--------------------------------------------------------------------
|
---|
| 12 | ; FileIO_OpenWithPathInDSSIandFileAccessInAL
|
---|
| 13 | ; Parameters:
|
---|
| 14 | ; AL: FILE_ACCESS.(mode)
|
---|
| 15 | ; DS:SI: Ptr to NULL terminated path or file name
|
---|
| 16 | ; Returns:
|
---|
| 17 | ; AX: DOS error code if CF set
|
---|
| 18 | ; BX: File handle if CF cleared
|
---|
| 19 | ; CF: Clear if file opened successfully
|
---|
| 20 | ; Set if error
|
---|
| 21 | ; Corrupts registers:
|
---|
| 22 | ; AX, BX
|
---|
| 23 | ;--------------------------------------------------------------------
|
---|
| 24 | ALIGN JUMP_ALIGN
|
---|
| 25 | FileIO_OpenWithPathInDSSIandFileAccessInAL:
|
---|
| 26 | xchg dx, si ; Path now in DS:DX
|
---|
| 27 | mov ah, OPEN_EXISTING_FILE
|
---|
| 28 | int DOS_INTERRUPT_21h
|
---|
| 29 | xchg si, dx
|
---|
[51] | 30 | mov bx, ax ; Copy file handle to BX
|
---|
[41] | 31 | ret
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | ;--------------------------------------------------------------------
|
---|
| 35 | ; FileIO_CloseUsingHandleFromBX
|
---|
| 36 | ; Parameters:
|
---|
| 37 | ; BX: File handle
|
---|
| 38 | ; Returns:
|
---|
| 39 | ; AX: DOS error code if CF set
|
---|
| 40 | ; CF: Clear if file closed successfully
|
---|
| 41 | ; Set if error
|
---|
| 42 | ; Corrupts registers:
|
---|
| 43 | ; AX
|
---|
| 44 | ;--------------------------------------------------------------------
|
---|
| 45 | ALIGN JUMP_ALIGN
|
---|
| 46 | FileIO_CloseUsingHandleFromBX:
|
---|
| 47 | mov ah, CLOSE_FILE
|
---|
| 48 | int DOS_INTERRUPT_21h
|
---|
| 49 | ret
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | ;--------------------------------------------------------------------
|
---|
[66] | 53 | ; FileIO_ReadDXCXbytesToDSSIusingHandleFromBX
|
---|
| 54 | ; Parameters:
|
---|
| 55 | ; BX: File handle
|
---|
| 56 | ; DX:CX: Number of bytes to read
|
---|
| 57 | ; DS:SI: Ptr to destination buffer
|
---|
| 58 | ; Returns:
|
---|
| 59 | ; AX: DOS error code if CF set
|
---|
| 60 | ; CF: Clear if successfull
|
---|
| 61 | ; Set if error
|
---|
| 62 | ; Corrupts registers:
|
---|
| 63 | ; AX
|
---|
| 64 | ;--------------------------------------------------------------------
|
---|
| 65 | ALIGN JUMP_ALIGN
|
---|
| 66 | FileIO_ReadDXCXbytesToDSSIusingHandleFromBX:
|
---|
| 67 | push bp
|
---|
| 68 | mov bp, FileIO_ReadCXbytesToDSSIusingHandleFromBX
|
---|
| 69 | call SplitLargeReadOrWritesToSmallerBlocks
|
---|
| 70 | pop bp
|
---|
| 71 | ret
|
---|
| 72 |
|
---|
| 73 | ;--------------------------------------------------------------------
|
---|
[41] | 74 | ; File position is updated so next read will start where
|
---|
| 75 | ; previous read stopped.
|
---|
| 76 | ;
|
---|
| 77 | ; FileIO_ReadCXbytesToDSSIusingHandleFromBX
|
---|
| 78 | ; Parameters:
|
---|
| 79 | ; BX: File handle
|
---|
| 80 | ; CX: Number of bytes to read
|
---|
| 81 | ; DS:SI: Ptr to destination buffer
|
---|
| 82 | ; Returns:
|
---|
| 83 | ; AX: Number of bytes actually read if successfull (0 if at EOF before call)
|
---|
| 84 | ; DOS error code if CF set
|
---|
| 85 | ; CF: Clear if successfull
|
---|
| 86 | ; Set if error
|
---|
| 87 | ; Corrupts registers:
|
---|
| 88 | ; Nothing
|
---|
| 89 | ;--------------------------------------------------------------------
|
---|
| 90 | ALIGN JUMP_ALIGN
|
---|
| 91 | FileIO_ReadCXbytesToDSSIusingHandleFromBX:
|
---|
[51] | 92 | xchg dx, si ; DS:DX now points to destination buffer
|
---|
[41] | 93 | mov ah, READ_FROM_FILE_OR_DEVICE
|
---|
| 94 | int DOS_INTERRUPT_21h
|
---|
| 95 | xchg si, dx
|
---|
| 96 | ret
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | ;--------------------------------------------------------------------
|
---|
[66] | 100 | ; FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX
|
---|
| 101 | ; Parameters:
|
---|
| 102 | ; BX: File handle
|
---|
| 103 | ; DX:CX: Number of bytes to write
|
---|
| 104 | ; DS:SI: Ptr to source buffer
|
---|
| 105 | ; Returns:
|
---|
| 106 | ; AX: DOS error code if CF set
|
---|
| 107 | ; CF: Clear if successfull
|
---|
| 108 | ; Set if error
|
---|
| 109 | ; Corrupts registers:
|
---|
| 110 | ; AX
|
---|
| 111 | ;--------------------------------------------------------------------
|
---|
| 112 | ALIGN JUMP_ALIGN
|
---|
| 113 | FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX:
|
---|
| 114 | push bp
|
---|
| 115 | mov bp, FileIO_WriteCXbytesFromDSSIusingHandleFromBX
|
---|
| 116 | call SplitLargeReadOrWritesToSmallerBlocks
|
---|
| 117 | pop bp
|
---|
| 118 | ret
|
---|
| 119 |
|
---|
| 120 | ;--------------------------------------------------------------------
|
---|
[41] | 121 | ; File position is updated so next write will start where
|
---|
| 122 | ; previous write stopped.
|
---|
| 123 | ;
|
---|
| 124 | ; FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
|
---|
| 125 | ; Parameters:
|
---|
| 126 | ; BX: File handle
|
---|
| 127 | ; CX: Number of bytes to write
|
---|
| 128 | ; DS:SI: Ptr to source buffer
|
---|
| 129 | ; Returns:
|
---|
| 130 | ; AX: Number of bytes actually written if successfull (EOF check)
|
---|
| 131 | ; DOS error code if CF set
|
---|
| 132 | ; CF: Clear if successfull
|
---|
| 133 | ; Set if error
|
---|
| 134 | ; Corrupts registers:
|
---|
| 135 | ; Nothing
|
---|
| 136 | ;--------------------------------------------------------------------
|
---|
| 137 | ALIGN JUMP_ALIGN
|
---|
| 138 | FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
|
---|
| 139 | xchg dx, si ; DS:DX now points to source buffer
|
---|
| 140 | mov ah, WRITE_TO_FILE_OR_DEVICE
|
---|
| 141 | int DOS_INTERRUPT_21h
|
---|
| 142 | xchg si, dx
|
---|
| 143 | ret
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | ;--------------------------------------------------------------------
|
---|
[66] | 147 | ; SplitLargeReadOrWritesToSmallerBlocks
|
---|
| 148 | ; Parameters:
|
---|
| 149 | ; BX: File handle
|
---|
| 150 | ; BP: Ptr to transfer function
|
---|
| 151 | ; DX:CX: Number of bytes to transfer
|
---|
| 152 | ; DS:SI: Ptr to transfer buffer
|
---|
| 153 | ; Returns:
|
---|
| 154 | ; AX: DOS error code if CF set
|
---|
| 155 | ; CF: Clear if successfull
|
---|
| 156 | ; Set if error
|
---|
| 157 | ; Corrupts registers:
|
---|
| 158 | ; AX
|
---|
| 159 | ;--------------------------------------------------------------------
|
---|
| 160 | ALIGN JUMP_ALIGN
|
---|
| 161 | SplitLargeReadOrWritesToSmallerBlocks:
|
---|
| 162 | push ds
|
---|
| 163 | push si
|
---|
| 164 | push dx
|
---|
| 165 | push cx
|
---|
| 166 |
|
---|
| 167 | xchg ax, cx ; DX:AX now holds bytes to transfer
|
---|
| 168 | mov cx, SPLIT_SIZE_FOR_LARGE_TRANSFERS
|
---|
| 169 | div cx ; AX = Number of full transfers
|
---|
| 170 | push dx ; Bytes for last transfer
|
---|
| 171 | test ax, ax
|
---|
| 172 | jz SHORT .TransferRemainingBytes
|
---|
| 173 | xchg dx, ax ; DX = Number of full transfers
|
---|
| 174 |
|
---|
| 175 | ALIGN JUMP_ALIGN
|
---|
| 176 | .TransferNextBytes:
|
---|
| 177 | call Registers_NormalizeDSSI
|
---|
| 178 | call bp ; Transfer function
|
---|
| 179 | jc SHORT .ErrorOccurredDuringTransfer
|
---|
[67] | 180 | add si, SPLIT_SIZE_FOR_LARGE_TRANSFERS
|
---|
[66] | 181 | dec dx
|
---|
| 182 | jnz SHORT .TransferNextBytes
|
---|
| 183 | .TransferRemainingBytes:
|
---|
| 184 | pop cx ; CX = Bytes for last transfer
|
---|
| 185 | jcxz .ReturnErrorCodeInAX ; No remaining bytes
|
---|
| 186 | call Registers_NormalizeDSSI
|
---|
| 187 | call bp
|
---|
| 188 | .ReturnErrorCodeInAX:
|
---|
| 189 | pop cx
|
---|
| 190 | pop dx
|
---|
| 191 | pop si
|
---|
| 192 | pop ds
|
---|
| 193 | ret
|
---|
| 194 | .ErrorOccurredDuringTransfer:
|
---|
| 195 | pop cx ; Remove bytes for last transfer
|
---|
| 196 | jmp SHORT .ReturnErrorCodeInAX
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | ;--------------------------------------------------------------------
|
---|
[51] | 200 | ; FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
|
---|
| 201 | ; Parameters:
|
---|
| 202 | ; BX: File handle
|
---|
| 203 | ; Returns:
|
---|
| 204 | ; DX:AX: Signed file size (if CF cleared)
|
---|
| 205 | ; AX: DOS error code (if CF set)
|
---|
| 206 | ; CF: Clear if successfull
|
---|
| 207 | ; Set if error
|
---|
| 208 | ; Corrupts registers:
|
---|
| 209 | ; Nothing
|
---|
| 210 | ;--------------------------------------------------------------------
|
---|
| 211 | ALIGN JUMP_ALIGN
|
---|
| 212 | FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
|
---|
| 213 | push cx
|
---|
| 214 |
|
---|
| 215 | ; Get file size to DX:AX
|
---|
| 216 | xor cx, cx
|
---|
| 217 | xor dx, dx
|
---|
| 218 | mov al, SEEK_FROM.endOfFile
|
---|
| 219 | call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
|
---|
| 220 | jc SHORT .ReturnFileError
|
---|
| 221 | push dx
|
---|
| 222 | push ax
|
---|
| 223 |
|
---|
| 224 | ; Reset file position
|
---|
| 225 | xor dx, dx
|
---|
| 226 | mov al, SEEK_FROM.startOfFile
|
---|
| 227 | call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
|
---|
| 228 | pop ax
|
---|
| 229 | pop dx
|
---|
| 230 |
|
---|
| 231 | .ReturnFileError:
|
---|
| 232 | pop cx
|
---|
| 233 | ret
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | ;--------------------------------------------------------------------
|
---|
[41] | 237 | ; FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX:
|
---|
| 238 | ; Parameters:
|
---|
| 239 | ; AL: SEEK_FROM.(origin)
|
---|
| 240 | ; BX: File handle
|
---|
[51] | 241 | ; CX:DX: Signed offset to seek starting from AL
|
---|
[41] | 242 | ; Returns:
|
---|
[51] | 243 | ; DX:AX: New file position in bytes from start of file (if CF cleared)
|
---|
| 244 | ; AX: DOS error code (if CF set)
|
---|
[41] | 245 | ; CF: Clear if successfull
|
---|
| 246 | ; Set if error
|
---|
| 247 | ; Corrupts registers:
|
---|
| 248 | ; Nothing
|
---|
| 249 | ;--------------------------------------------------------------------
|
---|
| 250 | ALIGN JUMP_ALIGN
|
---|
| 251 | FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX:
|
---|
| 252 | mov ah, SET_CURRENT_FILE_POSITION
|
---|
| 253 | int DOS_INTERRUPT_21h
|
---|
| 254 | ret
|
---|