UMBC CMSC 211 |
At the start of execution, each program has five standard handles which are open:
stdin | 0 | the keyboard |
stdout | 1 | the display |
stderr | 2 | the display |
stdaux | 3 | standard AUX:, usually COM1: |
stdprn | 4 | standard printer device PRN:, usually LPT1: |
This device independence leads to Input/Output redirection and a specialized redirection known as pipes. The program can be started at the command line with:
A pipe is a connection from one program to another,
When we want to use a filename, we put into the .DATA segment in the form of:
theName | DB | 'A:\source\myprog.asm',0 |
The following is the instructions for using the macros:
;; _Open filename, accessmethod[, segment] ;; accessmethod is one of the three integers ;; 0, 1, or 2, or one of the corresponding r 2, or ;; one of the corresponding symbolic values Read, ;; Write, or ReadWrite ;; If successful, returns Carry Flag clear ;; and handle in AX ;; ;; ;; _Creat filename[, segment] ;; If successful, returns Carry Flag clear ;; and handle in AX
The macro _Open uses a couple of new pseudo-ops: IFB (if blank), ELSE (goes with any IF), ENDIF (terminates the IF definition), and IFIDNI (if identical, case insensitive).
_Open MACRO filename, accesstype, segm _LdAddr dx,_LdSeg ds, mov ah, 3dh IFB <&accesstype&> mov al, 0 ELSE IFIDNI <&accesstype&>, mov al, 0 ELSE IFIDNI <&accesstype&>, mov al, 1 ELSE IFIDNI <&accesstype&>, mov al, 2 ELSE mov al, accesstype ENDIF ENDIF ENDIF ENDIF int 21h ENDM
There is also a close DOS call. While all open files are closed automatically when the program executes, it is good programming style to close the files explicitly and as soon as possible. It takes a handle as a parameter.
There are two measures available to protect files, one is to set the file to read-only with the DOS command
The filename can not have any leading, embedded, or trailing white space (blanks or tabs). Also, the access type must identically if you use the symbolic version or the integers 0 - 2. Otherwise you will get weird results. The segment is optional, and is used if the filename is defined in a segment where ds is not pointing to currently.
In UTIL.LIB, there is a procedure, CCheck, with no parameters, that will handle checking for errors. However, this procedure will print out a message and terminate the program if there is an error. You may want to have to program try to gracefully recover after an error, such as enter in a new filename if an existing file could not be found.