Dos Function Calls (Int 21H) : A. Terminate Program and Return To Dos

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

DOS FUNCTION CALLS (INT 21H)

DOS function calls preserve the contents of all the registers except the AX register and any
other register or registers in which they explicitly return data.

A. TERMINATE PROGRAM AND RETURN TO DOS


Every time you want to terminate the program and return to DOS, you have to put the
following codes:

Assembly Language C Language Meaning


mov AX , 4C00H exit(0) Program terminates normally
int 21h
mov AX, 4C01h exit(1) Program terminates with error code 1.
int 21h

B. CHARACTER INPUT WITH ECHO


To take single input character thru a keyboard, you have to put the following codes:

The Codes The Result


mov AH, 01h The program is waiting for the input. Once a user presses a key,
int 21h the ASCII Code of the input character is returned in the AL
register and the input character is displayed as well.
NOTE: This service makes the program waits for the input. The user just needs to press the
intended key WITHOUT pressing "enter" key.

C. CHARACTER INPUT WITHOUT ECHO


MOV AH , 08H
INT 21H
The code of the input character is returned in the AL register.

D. CHARACTER OUTPUT
To display a character, you have to use the DOS function 02h.

The Initial requirement The result


AH = 02h The character stored in DL will be
DL = Character or ASCII Code displayed.

Example:
The following code fragment will display a string 'Hey'.
mov DL, 'H'
mov AH, 2
int 21h
mov DL, 'e'
mov AH, 2
int 21h
mov AH, 2
mov DL, 'y'
int 21h

There are some special characters that will give special effect when displayed.
Character Name Meaning
7 Bell Sounds beep
8 Backspace (\b) Moving the cursor one position backward.
9 Horizontal Tab (\t) Moving the cursor 5 position forward
10 Line feed (\n) Moving the cursor to the next line
13 Carriage return (\r) Moving the cursor to the beginning of the line

Example:
The following code fragment will move the cursor to the beginning of the next output line:
mov AH, 02H
mov DL, 0DH
int 21H
mov DL, 0AH
mov AH, 2
int 21H

E. DISPLAYING A STRING (SERVICE 09h)


There are two ways to display a string.

The initial requirement The result


1. The string must be defined in DATA segment. The terminating $ is not
2. The string must be terminated by '$'. displayed, even if it appears
3. AH = 09h within the string. Thus this
4. DX = Offset address of the beginning of the string function cannot be used to
display the $ character on the
screen.

Example:
DATA
...
STRING_NAME DB 'THE STRING TO BE DISPLAYED$'
...
CODE
MOV AH , 09H
MOV DX , OFFSET STRING_NAME
INT 21H
Note:
 If the terminating $ is omitted, the operation displays characters in the memory, after the
string, until it finds a $ character, if any.

 To move the cursor to the beginning of the next output line, after displaying a string, put
0Dh and 0Ah after the string and before the terminating $. Example:
PROMPT DB 'PLEASE, ENTER YOUR NAME: ' , 0Dh , 0Ah , '$'

 Another way of moving the cursor to the beginning of the next output line is to display ,
using DOS function 09H, a string of the form:
STRING1 DB 0Dh , 0Ah , '$'

F. DISPLAYING A STRING (SERVICE 40h)


Another way to display a string:
The initial requirement The result
AH = 40h
BX = 1
CX = string length
DX = offset address of the beginning of the string

Example:
.DATA
...
STRING_NAME DB 'THE STRING TO BE DISPLAYED'
STRINGLEN EQU $ – STRING_NAME
...
.CODE
...
MOV AH , 40H
MOV BX , 01H
MOV CX , STRINGLEN ; string length
MOV DX , OFFSET STRING_NAME
INT 21H
 The EQU directive defines a value that the assembler can use to substitute in other
instructions.
 An operand containing a dollar symbol, $, refers to the current value in the location
counter. Thus, in the above example $ - STRING_NAME evaluates to the number of
bytes between STRING_NAME and STRINGLEN which is the number of bytes (i.e.,
characters) in ‘THE STRING TO BE DISPLAYED’

G. READING A STRING (SERVICE 0Ah)


To read a string, we have to do two steps:

a. Define an array to store that string in DATA segment.


b. Invoke DOS function 0AH in CODE segment.

One way of defining the array is:

BUFFER_NAME DB Num1 , Num2 DUP(?)


where:
a. Num1 = the maximum number of string characters to be read + 1
b. Num2 = Num1 + 1
c. The maximum value for Num1 is FFh i.e., 255
d. The last string character is the Carriage Return (0Dh).

The program will wait for the input. The user must press "Enter" key to end the inputting
process. This is the reason for the point d above.

Example:
Define an array called STRING to store a string of maximum length 50 characters to be used by
the service 0Ah of INT 21h.
Solution:
STRING DB 51, 52 DUP(?)

52 bytes

51 0Dh stored in this


byte if the length of the
string is 50
50 bytes
51 bytes

Actual string length is stored in this byte


To read a string from the keyboard into an array called BUFFER as defined above, we invoke
DOS function 0AH as:
MOV AH , 0AH
MOV DX , OFFSET STRING
INT 21H

The operation echoes the entered characters on the screen and advances the cursor. If more
characters than the specified maximum (in byte 0) are entered, the speaker beeps and the
additional characters are not read.

To display the above array generally by using DOS function 40H:

MOV AH, 40H


MOV BX, 01H ; file handle for the screen
MOVZX CX, STRING[1] ; Initialize CX with the string length
MOV DX, OFFSET STRING[2]
INT 21H

You might also like