90 likes | 189 Views
File I/O Services. (3Ch) Create File (3Dh) Open File (3Eh) Close File (3Fh) Read from File or Device (40h) Write to File or Device. 3Dh Open File. Three File Modes (Stored in AL) 0 – Input (Read Only) 1 – Output (Write Only) 2 – Input/Output Error Codes (CF=1, AX holds codes)
E N D
File I/O Services • (3Ch) Create File • (3Dh) Open File • (3Eh) Close File • (3Fh) Read from File or Device • (40h) Write to File or Device
3Dh Open File • Three File Modes (Stored in AL) • 0 – Input (Read Only) • 1 – Output (Write Only) • 2 – Input/Output • Error Codes (CF=1, AX holds codes) • 1 – Invalid Function Number • 2 – File Not Found • 3 - Path Not Found • 4 – Too Many open files • 5 - Access Denied
Open File .data filename db ‘A:\FILE1.DOC’,0 infilehandle dw ? .code mov ah, 3Dh ;function: open file mov al,0 ;choose the input mode mov dx, offset filename int 21h ;call DOS jc display_error ;error? Display a message mov infilehandle, ax ;no error: save the handle
(3Eh) Close File • Only possible error code • 6 – Invalid handle .data filename db ‘A:\FILE1.DOC’,0 infilehandle dw ? .code mov ah, 3Eh ;function: close file handle mov bx, infilehandle int 21h ;call DOS jc display_error ;error? Display a message
Close File .data filename db ‘A:\FILE1.DOC’,0 infilehandle dw ? .code mov ah, 3Eh ;function: close file handle mov bx, infilehandle int 21h ;call DOS jc display_error ;error? Display a message
(3Fh) Read From File or Device • Can read from keyboard or disk file • First use 3D to open file, then use 3F to read • Errors (CF=1) • 5 – Access Denied • 6 – invalid handle • If CF=0, AX contains # of bytes read • Useful for echecking for EOF
Read from File .data bufferSize = 512 filehandle dw ? buffer db bufferSize dup(0) .code mov ah, 3Fh ;read from file or device mov bx, filehandle ;BX = file handle mov cx, buffersize ;number of bytes to read mov dx, offset buffer ;point to buffer int 21h ;read the data jc display_error ;error if CX=1 cmp ax, cx ;cmp to bytes requested jbe Exit ;yes? Quit reading
(42h) Move File Pointer • AL = Method • 0 – Offset from beginning of file • 1 – Offset from current location • 2 – Offset from end of file • CX:DX holds offset • BX holds filehandle • Errors (CF=1) • 1 – invalid function number • 6 – Invalid Handle • After sucessful operation, CF=0, DS:AX= location of file pointer offset from beginning of file
Move File Pointer/Collect Data .code mov ah, 42h ;function: move pointer mov al,1 ;method: mov bx, filehandle ;BX = file handle mov cx, 0 mov dx, -10 ;offset can be negative int 21h jc display_error ;error if CX=1 mov 3Fh ;function: read file mov cx, 10 ;read 10 bytes mov dx, inbuf int 21h