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

Last change on this file since 373 was 293, checked in by krille_n_@…, 13 years ago

Commit 1/2 (Library, Configurators and Serial Server):

  • Changed Emulate.inc so that making 286 and 386 versions now works. Additionally, only one processor type define is needed in the makefile.
  • Minor optimizations.
  • Fixed spelling and did some cleaning.
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_ReadDXCXbytesToDSSIusingHandleFromBX
32; Parameters:
33; BX: File handle
34; DX:CX: Number of bytes to read
35; DS:SI: Ptr to destination buffer
36; Returns:
37; AX: DOS error code if CF set
38; CF: Clear if successful
39; Set if error
40; Corrupts registers:
41; AX
42;--------------------------------------------------------------------
43ALIGN JUMP_ALIGN
44FileIO_ReadDXCXbytesToDSSIusingHandleFromBX:
45 push bp
46 mov bp, FileIO_ReadCXbytesToDSSIusingHandleFromBX
47 call SplitLargeReadOrWritesToSmallerBlocks
48 pop bp
49 ret
50
51;--------------------------------------------------------------------
52; File position is updated so next read will start where
53; previous read stopped.
54;
55; FileIO_ReadCXbytesToDSSIusingHandleFromBX
56; Parameters:
57; BX: File handle
58; CX: Number of bytes to read
59; DS:SI: Ptr to destination buffer
60; Returns:
61; AX: Number of bytes actually read if successful (0 if at EOF before call)
62; DOS error code if CF set
63; CF: Clear if successful
64; Set if error
65; Corrupts registers:
66; Nothing
67;--------------------------------------------------------------------
68ALIGN JUMP_ALIGN
69FileIO_ReadCXbytesToDSSIusingHandleFromBX:
70 xchg dx, si ; DS:DX now points to destination buffer
71 mov ah, READ_FROM_FILE_OR_DEVICE
72 int DOS_INTERRUPT_21h
73 xchg si, dx
74 ret
75
76
77;--------------------------------------------------------------------
78; FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX
79; Parameters:
80; BX: File handle
81; DX:CX: Number of bytes to write
82; DS:SI: Ptr to source buffer
83; Returns:
84; AX: DOS error code if CF set
85; CF: Clear if successful
86; Set if error
87; Corrupts registers:
88; AX
89;--------------------------------------------------------------------
90ALIGN JUMP_ALIGN
91FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX:
92 push bp
93 mov bp, FileIO_WriteCXbytesFromDSSIusingHandleFromBX
94 call SplitLargeReadOrWritesToSmallerBlocks
95 pop bp
96 ret
97
98;--------------------------------------------------------------------
99; File position is updated so next write will start where
100; previous write stopped.
101;
102; FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
103; Parameters:
104; BX: File handle
105; CX: Number of bytes to write
106; DS:SI: Ptr to source buffer
107; Returns:
108; AX: Number of bytes actually written if successful (EOF check)
109; DOS error code if CF set
110; CF: Clear if successful
111; Set if error
112; Corrupts registers:
113; Nothing
114;--------------------------------------------------------------------
115ALIGN JUMP_ALIGN
116FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
117 xchg dx, si ; DS:DX now points to source buffer
118 mov ah, WRITE_TO_FILE_OR_DEVICE
119 int DOS_INTERRUPT_21h
120 xchg si, dx
121 ret
122
123
124;--------------------------------------------------------------------
125; SplitLargeReadOrWritesToSmallerBlocks
126; Parameters:
127; BX: File handle
128; BP: Ptr to transfer function
129; DX:CX: Number of bytes to transfer
130; DS:SI: Ptr to transfer buffer
131; Returns:
132; AX: DOS error code if CF set
133; CF: Clear if successful
134; Set if error
135; Corrupts registers:
136; AX
137;--------------------------------------------------------------------
138ALIGN JUMP_ALIGN
139SplitLargeReadOrWritesToSmallerBlocks:
140 push ds
141 push si
142 push dx
143 push cx
144
145 xchg ax, cx ; DX:AX now holds bytes to transfer
146 mov cx, SPLIT_SIZE_FOR_LARGE_TRANSFERS
147 div cx ; AX = Number of full transfers
148 push dx ; Bytes for last transfer
149 test ax, ax
150 jz SHORT .TransferRemainingBytes
151 xchg dx, ax ; DX = Number of full transfers
152
153ALIGN JUMP_ALIGN
154.TransferNextBytes:
155 call NormalizeDSSI
156 call bp ; Transfer function
157 jc SHORT .ErrorOccurredDuringTransfer
158 add si, SPLIT_SIZE_FOR_LARGE_TRANSFERS
159 dec dx
160 jnz SHORT .TransferNextBytes
161.TransferRemainingBytes:
162 pop cx ; CX = Bytes for last transfer
163 jcxz .ReturnErrorCodeInAX ; No remaining bytes
164 call NormalizeDSSI
165 call bp
166.ReturnErrorCodeInAX:
167 pop cx
168 pop dx
169 pop si
170 pop ds
171 ret
172.ErrorOccurredDuringTransfer:
173 pop cx ; Remove bytes for last transfer
174 jmp SHORT .ReturnErrorCodeInAX
175
176;--------------------------------------------------------------------
177; NormalizeDSSI
178; Parameters
179; DS:SI: Ptr to normalize
180; Returns:
181; DS:SI: Normalized pointer
182; Corrupts registers:
183; Nothing
184;--------------------------------------------------------------------
185ALIGN JUMP_ALIGN
186NormalizeDSSI:
187 push dx
188 push ax
189 NORMALIZE_FAR_POINTER ds, si, ax, dx
190 pop ax
191 pop dx
192 ret
193
194
195;--------------------------------------------------------------------
196; FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
197; Parameters:
198; BX: File handle
199; Returns:
200; DX:AX: Signed file size (if CF cleared)
201; AX: DOS error code (if CF set)
202; CF: Clear if successful
203; Set if error
204; Corrupts registers:
205; Nothing
206;--------------------------------------------------------------------
207ALIGN JUMP_ALIGN
208FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
209 push cx
210
211 ; Get file size to DX:AX
212 xor cx, cx
213 xor dx, dx
214 mov al, SEEK_FROM.endOfFile
215 call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
216 jc SHORT .ReturnFileError
217 push dx
218 push ax
219
220 ; Reset file position
221 xor dx, dx
222 mov al, SEEK_FROM.startOfFile
223 call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
224 pop ax
225 pop dx
226
227.ReturnFileError:
228 pop cx
229 ret
230
231
232;--------------------------------------------------------------------
233; FileIO_CloseUsingHandleFromBX
234; Parameters:
235; BX: File handle
236; Returns:
237; AX: DOS error code if CF set
238; CF: Clear if file closed successfully
239; Set if error
240; Corrupts registers:
241; AX
242;--------------------------------------------------------------------
243ALIGN JUMP_ALIGN
244FileIO_CloseUsingHandleFromBX:
245 mov ah, CLOSE_FILE
246 SKIP2B f ; cmp ax, <next instruction>
247 ; Fall to FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
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 successful
260; Set if error
261; Corrupts registers:
262; Nothing
263;--------------------------------------------------------------------
264FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX:
265 mov ah, SET_CURRENT_FILE_POSITION
266 int DOS_INTERRUPT_21h
267 ret
Note: See TracBrowser for help on using the repository browser.