You are on page 1of 7

I C S

2 3 2

L A B 3 :

D O S

I N T E R R U P T S

Instructor: Dr Syed Misbahuddin Objectives: To introduce the students to: DOS functions 01H , 02H, 08H, 09H, 40H, and 0AH. Debugging the program using Microsoft CodeView program.

THE INTERRUPT INSTRUCTION


Pentium processor has two memory architectures: real and protected. In real mode a Pentium works like fast 8086 processor. Real mode uses 16 bit addresses. The Real mode is also called as 16-bit mode, because all 20 bit physical address is constructed by 16 bit address. MS-DOS Operating system was the first operating system to implement RealAddress mode on IBM personal computer. The INT instruction is the instruction which does the most work in any assembler program. INT instruction calls a DOS interrupt service routine (like a function) to perform a special task. INT InterruptNumber Where InterruptNumber ranges from 00H to 0FFH (i.e., from 0 to 255). MS-DOS Operating system provides many common services through INT 21h. INT 21h MS-DOS services are procedures that provide input-output, file handling, and memory management. They are also called MS-DOS function calls. The execution of an INT instruction causes an Interrupt Service Routine (ISR) associated with the InterruptNumber to be executed. Many of the ISRs have multiple sub-functions. To specify which sub-function is to be executed under a particular InterruptNumber, the AH register is assigned a sub-function number before the execution of the INT instruction. Example: MOV AH , 08H INT 21H causes sub-function number 08H of Interrupt number 21H to be executed. In addition, some sub-functions require other values to be passed to the ISR in particular registers. COE 205 Computer Organization & Assembly Language 1

Example: Sub-function 09H of Interrupt 21H displays a $-terminated string on the screen. The sub-function requires the offset of that string to be passed in the DX register: MOV DX , OFFSET STRING MOV AH , 09H INT 21H Note: DPMI (DOS Protected Mode Interface) is an interface allowing a DOS program to run in protected mode and to access extended memory under a multitasking operating system like Microsoft Windows 3.0 and later.

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 mov AX , 4C00H int 21h mov AX, 4C01h int 21h C Language exit(0) exit(1) Meaning Program terminates normally Program terminates with error code 1.

B. CHARACTER INPUT WITH ECHO


To take single input character thru a keyboard, you have to put the following codes: The Codes mov AH, 01h int 21h The Result The program is waiting for the input. Once a user presses a key, 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.

COE 205 Computer Organization & Assembly Language

D. CHARACTER OUTPUT
To display a character, you have to use the DOS function 02h. The Initial requirement AH = 02h DL = Character or ASCII Code The result The character stored in DL will be 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 1. The string must be defined in DATA segment. 2. The string must be terminated by '$'. 3. AH = 09h The result The terminating $ is not displayed, even if it appears within the string. Thus this 3

COE 205 Computer Organization & Assembly Language

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 AH = 40h BX = 1 CX = string length DX = offset address of the beginning of the string The result

Example: .DATA ... STRING_NAME DB 'THE STRING TO BE DISPLAYED' STRINGLEN EQU $ STRING_NAME ... .CODE ... MOV AH , 40H MOV BX , 01H

COE 205 Computer Organization & Assembly Language

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

COE 205 Computer Organization & Assembly Language

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 MOVZX CX, STRING[1] MOV DX, OFFSET STRING[2] INT 21H ; file handle for the screen ; Initialize CX with the string length

LAB EXERCISES:
1. Type the following program and name it as PROG31.ASM. Assembly and link the program. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 .586 DOSSEG OPTION SEGMENT: USE16 .MODEL SMALL .STACK 64 .DATA MESSAGE DB 13, 10, 'Hello! What''s your name please? $' CUSTOMER DB 31, 32 dup(0) Msg1 DB 'Mr. $' Welcome DB ' studies 8086 Assembly language programming.$' .CODE .STARTUP ; Display the string mov AH , 9 mov DX , OFFSET MESSAGE int 21h ; Read a string mov AH, 10 mov DX, OFFSET CUSTOMER int 21h mov AH, 9 mov DX, OFFSET Msg1

COE 205 Computer Organization & Assembly Language

23 24 25 26 27 28 29 30 31 32 33 34

int 21h mov AH, 40H mov BX, 01H ; file handle for the screen movzx CX, CUSTOMER[1] ; Initialize CX with the string length mov DX, OFFSET CUSTOMER[2] int 21h mov AH, 9 mov DX, OFFSET welcome int 21h mov AX , 4C00h int 21h END

2. Write an EXE-format assembly language program that prompts for and reads a users name USERNAME (of maximum length 30 characters). It then displays a message of the form: Mr. USERNAME studies Assembly language programming. Mr. USERNAME is a student of ICS Department at KFUPM. Example: Hello, What's your name please? Ghozy Baalawy Abu Soso Mr. Ghozi Baalawy Abu Soso studies 8086 Assembly language programming. Mr. Ghozi Baalawy Abu Soso is a student of Computer Science Department in UOH

COE 205 Computer Organization & Assembly Language

You might also like