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

Last change on this file since 581 was 526, checked in by krille_n_@…, 12 years ago

Changes:

  • Update of the copyright notices to include the year 2013.
File size: 8.5 KB
Line 
1; Project name : Assembly Library
2; Description : Functions for file access.
3
4;
5; XTIDE Universal BIOS and Associated Tools
6; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
7;
8; This program is free software; you can redistribute it and/or modify
9; it under the terms of the GNU General Public License as published by
10; the Free Software Foundation; either version 2 of the License, or
11; (at your option) any later version.
12;
13; This program is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16; GNU General Public License for more details.
17; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18;
19
20
21; Section containing code
22SECTION .text
23
24;--------------------------------------------------------------------
25; FileIO_CreateWithPathInDSSIandAttributesInCX
26; Parameters:
27; CX: File attribute flags
28; DS:SI: Ptr to NULL terminated path or file name
29; Returns:
30; AX: DOS error code if CF set
31; BX: File handle if CF cleared
32; CF: Clear if file opened successfully
33; Set if error
34; Corrupts registers:
35; AX, BX
36;--------------------------------------------------------------------
37ALIGN JUMP_ALIGN
38FileIO_CreateWithPathInDSSIandAttributesInCX:
39 mov ah, CREATE_OR_TRUNCATE_FILE
40 SKIP2B bx
41 ; Fall to FileIO_OpenWithPathInDSSIandFileAccessInAL
42
43;--------------------------------------------------------------------
44; FileIO_OpenWithPathInDSSIandFileAccessInAL
45; Parameters:
46; AL: FILE_ACCESS.(mode)
47; DS:SI: Ptr to NULL terminated path or file name
48; Returns:
49; AX: DOS error code if CF set
50; BX: File handle if CF cleared
51; CF: Clear if file opened successfully
52; Set if error
53; Corrupts registers:
54; AX, BX
55;--------------------------------------------------------------------
56FileIO_OpenWithPathInDSSIandFileAccessInAL:
57 mov ah, OPEN_EXISTING_FILE
58 xchg dx, si ; Path now in DS:DX
59 int DOS_INTERRUPT_21h
60 xchg si, dx
61 mov bx, ax ; Copy file handle to BX
62 ret
63
64
65;--------------------------------------------------------------------
66; FileIO_ReadDXCXbytesToDSSIusingHandleFromBX
67; Parameters:
68; BX: File handle
69; DX:CX: Number of bytes to read
70; DS:SI: Ptr to destination buffer
71; Returns:
72; AX: DOS error code if CF set
73; CF: Clear if successful
74; Set if error
75; Corrupts registers:
76; AX
77;--------------------------------------------------------------------
78ALIGN JUMP_ALIGN
79FileIO_ReadDXCXbytesToDSSIusingHandleFromBX:
80 push bp
81 mov bp, FileIO_ReadCXbytesToDSSIusingHandleFromBX
82 call SplitLargeReadOrWritesToSmallerBlocks
83 pop bp
84 ret
85
86
87;--------------------------------------------------------------------
88; FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX
89; Parameters:
90; BX: File handle
91; DX:CX: Number of bytes to write
92; DS:SI: Ptr to source buffer
93; Returns:
94; AX: DOS error code if CF set
95; CF: Clear if successful
96; Set if error
97; Corrupts registers:
98; AX
99;--------------------------------------------------------------------
100ALIGN JUMP_ALIGN
101FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX:
102 push bp
103 mov bp, FileIO_WriteCXbytesFromDSSIusingHandleFromBX
104 call SplitLargeReadOrWritesToSmallerBlocks
105 pop bp
106 ret
107
108
109;--------------------------------------------------------------------
110; File position is updated so next read will start where
111; previous read stopped.
112;
113; FileIO_ReadCXbytesToDSSIusingHandleFromBX
114; Parameters:
115; BX: File handle
116; CX: Number of bytes to read
117; DS:SI: Ptr to destination buffer
118; Returns:
119; AX: Number of bytes actually read if successful (0 if at EOF before call)
120; DOS error code if CF set
121; CF: Clear if successful
122; Set if error
123; Corrupts registers:
124; Nothing
125;--------------------------------------------------------------------
126ALIGN JUMP_ALIGN
127FileIO_ReadCXbytesToDSSIusingHandleFromBX:
128 mov ah, READ_FROM_FILE_OR_DEVICE
129 SKIP2B f
130 ; Fall to FileIO_WriteCXbytesFromDSSIusingHandleFromBX
131
132;--------------------------------------------------------------------
133; File position is updated so next write will start where
134; previous write stopped.
135;
136; FileIO_WriteCXbytesFromDSSIusingHandleFromBX
137; Parameters:
138; BX: File handle
139; CX: Number of bytes to write
140; DS:SI: Ptr to source buffer
141; Returns:
142; AX: Number of bytes actually written if successful (EOF check)
143; DOS error code if CF set
144; CF: Clear if successful
145; Set if error
146; Corrupts registers:
147; Nothing
148;--------------------------------------------------------------------
149FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
150 mov ah, WRITE_TO_FILE_OR_DEVICE
151 xchg dx, si ; DS:DX now points to buffer
152 int DOS_INTERRUPT_21h
153 xchg si, dx
154 ret
155
156
157;--------------------------------------------------------------------
158; SplitLargeReadOrWritesToSmallerBlocks
159; Parameters:
160; BX: File handle
161; BP: Ptr to transfer function
162; DX:CX: Number of bytes to transfer
163; DS:SI: Ptr to transfer buffer
164; Returns:
165; AX: DOS error code if CF set
166; CF: Clear if successful
167; Set if error
168; Corrupts registers:
169; AX
170;--------------------------------------------------------------------
171ALIGN JUMP_ALIGN
172SplitLargeReadOrWritesToSmallerBlocks:
173 push ds
174 push si
175 push dx
176 push cx
177
178 xchg ax, cx ; DX:AX now holds bytes to transfer
179 mov cx, SPLIT_SIZE_FOR_LARGE_TRANSFERS
180 div cx ; AX = Number of full transfers
181 push dx ; Bytes for last transfer
182 test ax, ax
183 jz SHORT .TransferRemainingBytes
184 xchg dx, ax ; DX = Number of full transfers
185
186ALIGN JUMP_ALIGN
187.TransferNextBytes:
188 call NormalizeDSSI
189 call bp ; Transfer function
190 jc SHORT .ErrorOccurredDuringTransfer
191 add si, SPLIT_SIZE_FOR_LARGE_TRANSFERS
192 dec dx
193 jnz SHORT .TransferNextBytes
194.TransferRemainingBytes:
195 pop cx ; CX = Bytes for last transfer
196 jcxz .ReturnErrorCodeInAX ; No remaining bytes
197 call NormalizeDSSI
198 call bp
199.ReturnErrorCodeInAX:
200 pop cx
201 pop dx
202 pop si
203 pop ds
204 ret
205.ErrorOccurredDuringTransfer:
206 pop cx ; Remove bytes for last transfer
207 jmp SHORT .ReturnErrorCodeInAX
208
209;--------------------------------------------------------------------
210; NormalizeDSSI
211; Parameters
212; DS:SI: Ptr to normalize
213; Returns:
214; DS:SI: Normalized pointer
215; Corrupts registers:
216; Nothing
217;--------------------------------------------------------------------
218ALIGN JUMP_ALIGN
219NormalizeDSSI:
220 push dx
221 push ax
222 NORMALIZE_FAR_POINTER ds, si, ax, dx
223 pop ax
224 pop dx
225 ret
226
227
228;--------------------------------------------------------------------
229; FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition
230; Parameters:
231; BX: File handle
232; Returns:
233; DX:AX: Signed file size (if CF cleared)
234; AX: DOS error code (if CF set)
235; CF: Clear if successful
236; Set if error
237; Corrupts registers:
238; Nothing
239;--------------------------------------------------------------------
240ALIGN JUMP_ALIGN
241FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
242 push cx
243
244 ; Get file size to DX:AX
245 xor cx, cx
246 xor dx, dx
247 mov al, SEEK_FROM.endOfFile
248 call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
249 jc SHORT .ReturnFileError
250 push dx
251 push ax
252
253 ; Reset file position
254 xor dx, dx
255 mov al, SEEK_FROM.startOfFile
256 call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
257 pop ax
258 pop dx
259
260.ReturnFileError:
261 pop cx
262 ret
263
264
265;--------------------------------------------------------------------
266; FileIO_CloseUsingHandleFromBX
267; Parameters:
268; BX: File handle
269; Returns:
270; AX: DOS error code if CF set
271; CF: Clear if file closed successfully
272; Set if error
273; Corrupts registers:
274; AX
275;--------------------------------------------------------------------
276ALIGN JUMP_ALIGN
277FileIO_CloseUsingHandleFromBX:
278 mov ah, CLOSE_FILE
279 SKIP2B f ; cmp ax, <next instruction>
280 ; Fall to FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
281
282
283;--------------------------------------------------------------------
284; FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
285; Parameters:
286; AL: SEEK_FROM.(origin)
287; BX: File handle
288; CX:DX: Signed offset to seek starting from AL
289; Returns:
290; DX:AX: New file position in bytes from start of file (if CF cleared)
291; AX: DOS error code (if CF set)
292; CF: Clear if successful
293; Set if error
294; Corrupts registers:
295; Nothing
296;--------------------------------------------------------------------
297FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX:
298 mov ah, SET_CURRENT_FILE_POSITION
299 int DOS_INTERRUPT_21h
300 ret
Note: See TracBrowser for help on using the repository browser.