file i/o ms-dos interrupt 21h has many functions dealing with file and directory i/o services. both...

15
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.

Upload: rosamond-blair

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 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

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.

Page 2: 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

5 Predefined Device Handles

• 0 – keyboard (standard input)

• 1 – console (standard output)

• 2 – Error Output

• 3 – Auxiliary Device (Async)

• 4 - Printer

Page 3: 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

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.

Page 4: 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

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

Page 5: 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

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

Page 6: 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

Open File(if carry, error; if no error, ax = filename)

.datafilename db ‘A:\FILE1.DOC’,0infilehandle dw ? .codemov ah, 3Dh ;function: open filemov al,0 ;choose the input modemov dx, offset filenameint 21h ;call DOSjc display_error ;error? Display a messagemov infilehandle, ax ;no error: save the handle

Page 7: 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

(3Eh) Close File• Only 1 possible error code

– 6 – Invalid handle

.datafilename db ‘A:\FILE1.DOC’,0infilehandle dw ? .codemov ah, 3Eh ;function: close file handlemov bx, infilehandleint 21h ;call DOSjc display_error ;error? Display a message

Page 8: 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

Close File

.datafilename db ‘A:\FILE1.DOC’,0infilehandle dw ? .codemov ah, 3Eh ;function: close file handlemov bx, infilehandleint 21h ;call DOSjc display_error ;error? Display a message

Page 9: 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

(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

Page 10: 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

Read from File

.databufferSize = 512filehandle dw ?buffer db bufferSize dup(0) .codemov ah, 3Fh ;read from file or devicemov bx, filehandle ;BX = file handlemov cx, buffersize ;number of bytes to readmov dx, offset buffer ;point to bufferint 21h ;read the datajc display_error ;error if CX=1cmp ax, cx ;cmp to bytes requestedjbe Exit ;yes? Quit reading

Page 11: 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

(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

Page 12: 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

Move File Pointer/Collect Data

.codemov ah, 42h ;function: move pointermov al,1 ;method: mov bx, filehandle ;BX = file handlemov cx, 0mov dx, -10 ;offset can be negativeint 21hjc display_error ;error if CX=1mov 3Fh ;function: read filemov cx, 10 ;read 10 bytesmov dx, inbufint 21h

Page 13: 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

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.

Page 14: 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

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

Page 15: 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

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.