Download presentation
Presentation is loading. Please wait.
Published byRosamond Blair Modified over 9 years ago
1
File I/O MS-DOS Interrupt 21h has many functions dealing with file and directory I/O services. Both MS-DOS and MS_Windows use 16- bit integers called HANDLES to identify files and I/O devices.
2
5 Predefined Device Handles 0 – keyboard (standard input) 1 – console (standard output) 2 – Error Output 3 – Auxiliary Device (Async) 4 - Printer
3
Errors If one of the I/O functions fail, the carry flag is set, and an error code is returned in AX. You can use this error code to display an appropriate error message. Table 13-4 (pg. 477-478) lists error codes.
4
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 (42h) Move file pointer (716Ch) Extended Mode file creation or open
5
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
6
Open File (if carry, error; if no error, ax = filename).data filenamedb‘A:\FILE1.DOC’,0 infilehandledw?.code movah, 3Dh;function: open file moval,0;choose the input mode movdx, offset filename int21h;call DOS jcdisplay_error;error? Display a message movinfilehandle, ax;no error: save the handle
7
(3Eh) Close File Only 1 possible error code –6 – Invalid handle.data filenamedb‘A:\FILE1.DOC’,0 infilehandledw?.code mov ah, 3Eh;function: close file handle movbx, infilehandle int21h;call DOS jcdisplay_error;error? Display a message
8
Close File.data filenamedb‘A:\FILE1.DOC’,0 infilehandledw?.code mov ah, 3Eh;function: close file handle movbx, infilehandle int21h;call DOS jcdisplay_error;error? Display a message
9
(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
10
Read from File.data bufferSize = 512 filehandledw? bufferdbbufferSize dup(0).code mov ah, 3Fh;read from file or device movbx, filehandle;BX = file handle movcx, buffersize;number of bytes to read movdx, offset buffer;point to buffer int21h;read the data jcdisplay_error;error if CX=1 cmpax, cx;cmp to bytes requested jbeExit;yes? Quit reading
11
(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
12
Move File Pointer/Collect Data.code mov ah, 42h;function: move pointer moval,1;method: movbx, filehandle;BX = file handle movcx, 0 movdx, -10;offset can be negative int21h jcdisplay_error;error if CX=1 mov3Fh;function: read file movcx, 10;read 10 bytes movdx, inbuf int21h
13
Reading the MS_DOS Command Tail When a program runs, any additional text on the command line is automatically stored in the 128-byte command tail area Command Tail found at offset 80h in the PSP (256-byte block at beginning of code) The first byte contains the number of characters typed on the command line. In Codeview- choose runtime arguments from the run menu No arguments saved in redirection of I/O.
14
Get_Commandtail Procedure From Irvine link library Returns a copy of the command tail (DX should point to offset of buffer where command tail is to be copied) Skips over leading spaces Sets carry flag if empty
15
Creating Binary Files Binfile.asm Binary files – data is a binary image of the program data. If file has binary data in it, you would have to convert each integer to a string and write it separately. Create a program that creates and fills an array with 50 doublewords. (200 bytes) Binfile fills an array with random integers, displays the integers on the screen, writes the integers to a binary file, and closes the file. It then reopens the file, reads the integers, and displays them.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.