Input Output Instructions CPU communicates with the peripherals














































- Slides: 46

Input & Output Instructions • CPU communicates with the peripherals through I/O registers called I/O ports. • There are 2 instructions, IN & OUT, that access the ports directly. • These instructions are used when fast I/O is essential ……. in a game program. 1 CAP 241 21/11/2005

IN & OUT • Most application programs do not use IN and OUT instructions. • Why? 1) port addresses vary among computer models 2) easier to program I/O with service routines 2 CAP 241 21/11/2005

2 categories of I/O service routine 1. The BIOS routines. 2. The DOS routines. 3 CAP 241 21/11/2005

BIOS routines • Are stored in ROM and interact directly with I/O ports. • Used to carry basic screen operations such as moving the cursor & scrolling the screen. 4 CAP 241 21/11/2005

DOS routines • Can carry out more complex tasks. • Printing a character string…. They use the BIOS routines to perform direct I/O operations. 5 CAP 241 21/11/2005

The INT Instruction. • To invoke a DOS or BIOS routine , the INT (interrupt) instruction is used. • FORMAT INT interrupt_number is a number that specifies a routine. 6 CAP 241 21/11/2005

Example • INT 16 h invokes a BIOS routine that performs keyboard input. • We will use a particular DOS routine INT 7 21 h CAP 241 21/11/2005

INT 21 h • Used to invoke a large number of DOS functions. • Put the function number in AH register and then invoke INT 21 h 8 CAP 241 21/11/2005

FUNCTIONS Function number 9 Routines 1 single-key input 2 single-character output 9 character string output CAP 241 21/11/2005

INT 21 h functions • Input values are to be in certain registers & return output values in other registers. 10 CAP 241 21/11/2005

Function 1 • Single-Key Input : AH = 1 Output : AL = ASCII code if character key is pressed. = o if non-character is pressed. 11 CAP 241 21/11/2005

Example 12 MOV AH, 1 ; input key function INT 21 h CAP 241 ; ASCII code in AL 21/11/2005

Example • If character k is pressed, AL gets its ASCII code; the character is also displayed on the screen • If Arrow key or F 1 -F 10, AL will contain 0 • The instructions following the INT 21 h can examine AL and take appropriate action. 13 CAP 241 21/11/2005

Function 2 • INT 21 h, function 1 …. doesn’t prompt the user for input, he might not know whether the computer is waiting for input or it is occupied by some computation. • Function 2 can be used to prompt the user 14 CAP 241 21/11/2005

Function 2 • Display a character or execute a control function Input : AH = 2 DL = ASCII code for the display character or control character Output : AL = ASCII code of the display character or control character 15 CAP 241 21/11/2005

Example MOV AH, 2 ; display character function MOV DL, ‘? ’ ; character is ‘? ’ INT 21 h ; display character • After the character is displayed, the cursor advances to the next position on the line. 16 CAP 241 21/11/2005

Control functions • Function 2 may also be used to perform control functions. • If DL contains the ASCII code of a control character, INT 21 h causes the control function to be performed. 17 CAP 241 21/11/2005

Control functions ASCII code (hex) Symbol Function 7 BEL beep 8 BS backspace 9 HT tab A LF line feed (new line) D CR carriage return (start of current line) 18 CAP 241 21/11/2005

A First Program Read a character and display it at the beginning of the next line 1 -We start by displaying a question mark: MOV AH, 2 ; display character function MOV DL, '? ‘ ; character is ‘? ’ INT 21 H ; display character Move 3 Fh, the ASCII code for “? ” , into DL 19 CAP 241 21/11/2005

Read a character 20 MOV AH, 1 ; read character function INT 21 H ; character in AL CAP 241 21/11/2005

Display the character on next line • First , the character must be saved in another register. MOV BL , AL ; save it in BL • This because the INT 21 h , function 2 , changes AL 21 CAP 241 21/11/2005

Display the character on next line -Move cursor to the beginning of the next line: • Execute carriage return & line feed. • Put their ASCII codes in DL & execute INT 21 h. 22 CAP 241 21/11/2005

Move cursor to the beginning of the next line 23 MOV AH , 2 ; display character function MOV DL , 0 Dh ; carriage return INT 21 h ; execute carriage return MOV DL , 0 Ah ; line feed INT 21 h ; execute line feed CAP 241 21/11/2005

Display the character 24 MOV DL , BL ; get character INT 21 h ; and display it CAP 241 21/11/2005

Program Listing TITLE PGM 4_1 : Echo PROGRAM. MODEL SMALL. STACK 100 H. CODE MAIN PROC ; display prompt MOV AH, 2 MOV DL, '? ' INT 21 H ; input a character MOV AH, 1 INT 21 H MOV BL, AL 25 CAP 241 ; go to a new line MOV AH, 2 MOV DL, 0 DH INT 21 H MOV DL, 0 AH INT 21 H ; display characters MOV DL, BL INT 21 H ; return to DOS MOV AH, 4 CH INT 21 H MAIN ENDP END MAIN 21/11/2005

When a program terminates, it should return control to DOS 26 MOV AH, 4 CH ; DOS exit function INT 21 H ; exit to DOS CAP 241 21/11/2005

Displaying a string INT 21 h , Function 9: Display a string Input : DX = offset address of string. The string must end with a ‘$’ character. 27 CAP 241 21/11/2005

• If the string contains the ASCII code of a control character , the control function is performed. 28 CAP 241 21/11/2005

Example • Print MSG 29 HELLO! on the screen. DB ‘HELLO!$’ CAP 241 21/11/2005

The LEA instruction • INT 21 h, function 9, expects the offset address of the character string to be in DX. • To get it there, we use LEA 30 destination , source CAP 241 21/11/2005

LEA • • 31 destination , source LEA …. . Load Effective Address Destination … is a general register. Source ………… is a memory location. It puts a copy of the source offset address into destination. CAP 241 21/11/2005

Example MSG LEA DB ‘HELLO!$’ DX , MSG ; puts the offset ; address of variable ; MSG in DX • This example contains data segments initialize DS. 32 CAP 241 21/11/2005

Program Segment Prefix • When a program is loaded in memory, DOS prefaces it with PSP. The PSP contains information about program. • DOS places in DS & ES segment # of PSP. • DS must be loaded with the segment # of data segment 33 CAP 241 21/11/2005

DS initialization • A program containing a data segment begins with: MOV AX, @DATA MOV DS, AX • @Data is the name of the data segment defined by. DATA. It is translated into a segment #. 34 CAP 241 21/11/2005

Print the message • With DS initialized, we may print the “HELLO!” message: LEA MOV INT 35 DX, MSG ; get message AH, 9; display string function 21 h ; display string CAP 241 21/11/2005

Sample Program directive giving title for printed listings program title (comment) TITLE PGM 4 -2: PRINT STRING PROGRAM ; This program displays “Hello!” comment line . MODEL SMALL memory model: small . STACK 100 H programs use at most 64 K code and 64 K data set the stack size 36 CAP 241 21/11/2005

Sample Program starts the data segment where variables are stored reserve room for some bytes carriage return and line feed. DATA MSG DB “HELLO!”, 0 DH, 0 AH, ’$’ variable name starts the code segment. CODE Declares the beginning of the procedure which is MAIN PROC MOV AX, @DATA called main MOV DS, AX ; initialize DS LEA DX, MSG ; get message MOV AH, 9 ; display string function INT 21 H ; display message MOV AH, 4 CH INT 21 H ; DOS exit marks the end of the current procedure MAIN ENDP marks the end of the program. “main” specifies END MAIN the program execution is to begin with the procedure “main” 37 CAP 241 21/11/2005

• Sample execution: A> PGM 4_2 HELLO! 38 CAP 241 21/11/2005

Case Conversion Program ENTER A LOWER CASE LETTER : a IN UPPER CASE IT IS : A 39 CAP 241 21/11/2005

Case Conversion Program • Use EQU to define CR & LF as names for the constants 0 DH & 0 AH. CR EQU LF EQU 40 0 DH 0 AH CAP 241 21/11/2005

The messages and input character can be stored in the Data Segment like this: MSG 1 MSG 2 CHAR 41 DB DB DB 'ENTER A LOWER CASE LETTER : $‘ CR, LF , 'IN UPPER CASE IT IS : ‘ ? , '$' CAP 241 21/11/2005

Our program begins by displaying the first message and reading the character: LEA MOV INT 42 DX, MSG 1 AH, 9 21 H AH, 1 21 H ; get first message ; display string function ; display first message ; read character function ; read a small letter into AL CAP 241 21/11/2005

Convert to upper case SUB AL, 20 H ; convert into uppercase MOV CHAR, AL ; and store it 43 CAP 241 21/11/2005

Display second message & uppercase LEA DX, MSG 2 ; get second message MOV AH, 9 ; display string function INT 21 H ; display message & uppercase letter 44 CAP 241 21/11/2005

Program Listing 45 CAP 241 21/11/2005

. MODEL SMALL. STACK 100 H ; input a character and. DATA convert to upper case CR EQU 0 DH MOV AH, 1 LF EQU 0 AH INT 21 H MSG 1 DB 'ENTER A LOWER CASE LETTER : $' SUB AL, 20 H MSG 2 DB CR, LF, 'IN UPPER CASE IT IS : ' MOV CHAR, AL CHAR DB ? , '$' ; display on the next line. CODE LEA DX, MSG 2 MAIN PROC MOV AH, 9 ; initialize DS INT 21 H MOV AX, @DATA ; return TO DOS MOV AH, 4 CH MOV DS, AX INT 21 H ; print user prompt MAIN ENDP LEA DX, MSG 1 END MAIN MOV AH, 9 INT 21 H 46 CAP 241 21/11/2005