DOS INTERRPUTS
5.1 Introduction
The DOS (Disk Operating System) provides a large number of procedures to access devices, files and memory. These procedures can be called in any user program using software interrupts “INT n” instruction. The DOS interrupt INT 21H provides a large number of services. A function code has been allocated to each service provided by INT 21H. The function code should be loaded into the AH register before calling INT 21H to avail the service provide by the function.
The steps involved in accessing DOS services are :
1. Load a DOS function number in AH register. If there is a sub-function then it is loaded in AL register.
2. Load the other registers as indicated in the DOS service formats.
3. Prepare buffers, ASCIIZ (ASCII string terminated by zero) and control blocks if necessary.
4. Set the location of the disk area if necessary.
5. Invoke DOS service INT 21H which will return the required parameters in the specified register.
5.2.1 Read a character from the Standard Input Device
The INT 21H with a function code 01H reads a character from the standard input device (keyboard) and echoes (send) the character to the standard output device (monitor). It waits for the character if no character is available on the input device. AH register should be loaded with 01H before invoking DOS service INT 21H.
5.2.2 Read a character from the Standard Input Device without echo.
The INT 21H with a function code 08H reads a character from the standard input device (keyboard) and will not send the character to the standard output device (monitor). It waits for the character if no character is available on the input device. AH register should be loaded with 08H before invoking DOS service INT 21H.
Program to read a character form keyboard using DOS interrupt function 21H
ASSUME CS:CODE.DS:DATA
CODE SEGMENT
START:MOV AH,07H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
Program to read a character form keyboard and echo the same using DOS interrupt function 21H
ASSUME CS:CODE.DS:DATA
CODE SEGMENT
START:MOV AH,01H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
5.3 Write a character to the Standard Output Device
The INT 21H with a function code 02H writes a character to the standard output device (monitor). AH register should be loaded with 02H and DL register should be loaded with ASCII code of the character to be displayed before invoking DOS service INT 21H.
Program to write a character to a console using DOS interrupt function 21H
ASSUME CS:CODE.DS:DATA
CODE SEGMENT
START: MOV DL,31H ; ASCII value of character to be displayed.
MOV AH,02H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
5.4 Creation of a new file
5.4.1 File Handles
A file handle is a number from 0000H to FFFFH by which DOS identifies a open file. DOS uses the file handle number simply as a pointer to a data area in DOS where all the file detail are stored. Once a file is opened and a file handle I assigned to the file, the specific handle must be used until the file is closed. Once a file is closed, the file handle number is freed up for use with another file name.
To create a file the program must perform the following action
1. Name the file path name using ASCII character. If no directory is specified in the path name, the current directory is used by the DOS.
2. Terminate the file path name with a 00 byte.
3. Use INT 21H, function 3CH, to have DOS assign a handle.
Function 3CH will return a file handle to the register AX used in the current program. In order to create a new file, register CX may contain any of the following file attribute bytes :
Program to for creation of a new file using DOS interrupt function 21H
ASSUME CS:CODE,DS:DATA
DATA SEGMENT
DATABLOCK DB 20H DUP(?)
MESSAGE1 DB 0AH,0DH,"FILE NOT CREATED",0AH,0H,"$"
MESSAGE2 DB 0AH,0DH,"INSTRUMENTATION TECHNOLOGY"0AH,0H,’$"
FILENAME DB "E:BIEIT.TXT","$" DATA ENDS
CODE SEGMENT START:MOV AX,DATA
MOV DS,AX
MOV DX,OFFSET FILENAME MOV CX,0000H
MOV AH,3CH INT 21H
JNC WRITE MOV AX,DATA
MOV DS,AX
MOV DX,OFFSET MESSAGE1 MOV AH,09H
INT 21H JMP STOP
WRITE:MOV BX,AX
MOV DX,OFFSET MESSAGE2 MOV AH,09H
INT 21H
STOP: MOV AH,4CH INT 21H
CODE ENDS
END START
Read/ write data from/to file
Once a file is created, opened, and a handle assigned by function 3CH, function 40H permit us to write data to the file using the register contents shown below :
Program to create a new file and write the data into the file using DOS interrupt function 21H
ASSUME CS:CODE,DS:DATA
DATA SEGMENT
BUFF DB 80 DUP(?)
PATH DB ‘BIET.DAT’,00
HANDLE DW 00
COUNT DW 0010H
DATA ENDS
CODE SEGMENT
START: MOV AX,DATA
MOV DS,AX
; Creates a new file named ‘BIET.DAT’ in the current
directory
MOV DX,OFFSET PATH
MOV CX,0000H
MOV AH,3CH
INT 21H
;Read data form keyboard and store it in memory
LEA SI,BUFF
MOV CX,COUNT
BACK: MOV AH,01H
INT 21H
MOV [SI],AL
INC SI
LOOP BACK
; open the created file BIET.DAT
MOV AH,30H
LEA DX,PATH
MOV AL,01
INT 21H
MOV HANDLE, AX
; Write Data into the opened file BIET.DAT
MOV AH,40H
MOV BX,HANDLE
LEA DX,BUFF
MOV CX,COUNT
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
; Write Data into the opened file BIET.DAT
MOV AH,40H
MOV BX,HANDLE
LEA DX,BUFF
MOV CX,COUNT
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
5.6 DOS interrupts for Serial / Parallel Communication
5.6.1 To write Byte to Printer using INT 21H, Function 05H
Function 05h output a byte to the device STDPRN, which is same as LPT1, the system printer.