[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 |
---|
| 180 | dec dx |
---|
| 181 | jnz SHORT .TransferNextBytes |
---|
| 182 | .TransferRemainingBytes: |
---|
| 183 | pop cx ; CX = Bytes for last transfer |
---|
| 184 | jcxz .ReturnErrorCodeInAX ; No remaining bytes |
---|
| 185 | call Registers_NormalizeDSSI |
---|
| 186 | call bp |
---|
| 187 | .ReturnErrorCodeInAX: |
---|
| 188 | pop cx |
---|
| 189 | pop dx |
---|
| 190 | pop si |
---|
| 191 | pop ds |
---|
| 192 | ret |
---|
| 193 | .ErrorOccurredDuringTransfer: |
---|
| 194 | pop cx ; Remove bytes for last transfer |
---|
| 195 | jmp SHORT .ReturnErrorCodeInAX |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | ;-------------------------------------------------------------------- |
---|
[51] | 199 | ; FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition: |
---|
| 200 | ; Parameters: |
---|
| 201 | ; BX: File handle |
---|
| 202 | ; Returns: |
---|
| 203 | ; DX:AX: Signed file size (if CF cleared) |
---|
| 204 | ; AX: DOS error code (if CF set) |
---|
| 205 | ; CF: Clear if successfull |
---|
| 206 | ; Set if error |
---|
| 207 | ; Corrupts registers: |
---|
| 208 | ; Nothing |
---|
| 209 | ;-------------------------------------------------------------------- |
---|
| 210 | ALIGN JUMP_ALIGN |
---|
| 211 | FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition: |
---|
| 212 | push cx |
---|
| 213 | |
---|
| 214 | ; Get file size to DX:AX |
---|
| 215 | xor cx, cx |
---|
| 216 | xor dx, dx |
---|
| 217 | mov al, SEEK_FROM.endOfFile |
---|
| 218 | call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX |
---|
| 219 | jc SHORT .ReturnFileError |
---|
| 220 | push dx |
---|
| 221 | push ax |
---|
| 222 | |
---|
| 223 | ; Reset file position |
---|
| 224 | xor dx, dx |
---|
| 225 | mov al, SEEK_FROM.startOfFile |
---|
| 226 | call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX |
---|
| 227 | pop ax |
---|
| 228 | pop dx |
---|
| 229 | |
---|
| 230 | .ReturnFileError: |
---|
| 231 | pop cx |
---|
| 232 | ret |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | ;-------------------------------------------------------------------- |
---|
[41] | 236 | ; FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX: |
---|
| 237 | ; Parameters: |
---|
| 238 | ; AL: SEEK_FROM.(origin) |
---|
| 239 | ; BX: File handle |
---|
[51] | 240 | ; CX:DX: Signed offset to seek starting from AL |
---|
[41] | 241 | ; Returns: |
---|
[51] | 242 | ; DX:AX: New file position in bytes from start of file (if CF cleared) |
---|
| 243 | ; AX: DOS error code (if CF set) |
---|
[41] | 244 | ; CF: Clear if successfull |
---|
| 245 | ; Set if error |
---|
| 246 | ; Corrupts registers: |
---|
| 247 | ; Nothing |
---|
| 248 | ;-------------------------------------------------------------------- |
---|
| 249 | ALIGN JUMP_ALIGN |
---|
| 250 | FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX: |
---|
| 251 | mov ah, SET_CURRENT_FILE_POSITION |
---|
| 252 | int DOS_INTERRUPT_21h |
---|
| 253 | ret |
---|