source: xtideuniversalbios/trunk/Assembly_Library/Src/File/FileIO.asm@ 119

Last change on this file since 119 was 105, checked in by Tomi Tilli, 14 years ago

Changes to Assembly Library:

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