21/11/2005 CAP241 1 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.
46
Embed
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
21/11/2005CAP2411
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.
21/11/2005CAP2412
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
21/11/2005CAP2413
2 categories of I/O service routine
1. The BIOS routines.
2. The DOS routines.
21/11/2005CAP2414
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.
21/11/2005CAP2415
DOS routines
• Can carry out more complex tasks.
• Printing a character string…. They use the BIOS routines to perform direct I/O operations.
21/11/2005CAP2416
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.
21/11/2005CAP2417
Example
• INT 16h
invokes a BIOS routine that performs
keyboard input.
• We will use a particular DOS routine
INT 21h
21/11/2005CAP2418
INT 21h
• Used to invoke a large number of DOS functions.
• Put the function number in AH register and then invoke INT 21h
21/11/2005CAP2419
FUNCTIONS
Function number Routines
1 single-key input
2 single-character output
9 character string output
21/11/2005CAP24110
INT 21h functions
• Input values are to be in certain registers & return output values in other registers.
21/11/2005CAP24111
Function 1
• Single-Key Input
Input : AH = 1Output : AL = ASCII code if character key is
pressed.
= o if non-character is pressed.
21/11/2005CAP24112
Example
MOV AH,1 ; input key function
INT 21h ; ASCII code in AL
21/11/2005CAP24113
Example
• If character k is pressed, AL gets its ASCII code; the character is also displayed on the screen
• If Arrow key or F1-F10, AL will contain 0
• The instructions following the INT 21h can examine AL and take appropriate action.
21/11/2005CAP24114
Function 2
• INT 21h, 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
21/11/2005CAP24115
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
21/11/2005CAP24116
Example
MOV AH,2 ; display character function
MOV DL, ‘?’ ; character is ‘?’
INT 21h ; display character
• After the character is displayed, the cursor advances to the next position on the line.
21/11/2005CAP24117
Control functions
• Function 2 may also be used to perform control functions.
• If DL contains the ASCII code of a control character, INT 21h causes the control function to be performed.
21/11/2005CAP24118
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)
21/11/2005CAP24119
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 21H ; display character
Move 3Fh, the ASCII code for “?” , into DL
21/11/2005CAP24120
Read a character
MOV AH,1 ; read character function
INT 21H ; character in AL
21/11/2005CAP24121
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 21h , function 2 , changes AL
21/11/2005CAP24122
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 21h.
21/11/2005CAP24123
Move cursor to the beginning of the next line
MOV AH , 2 ; display character function
MOV DL , 0Dh ; carriage return
INT 21h ; execute carriage return
MOV DL , 0Ah ; line feed
INT 21h ; execute line feed
21/11/2005CAP24124
Display the character
MOV DL , BL ; get character
INT 21h ; and display it
21/11/2005CAP24125
Program ListingTITLE PGM4_1 : Echo PROGRAM
.MODEL SMALL.STACK 100H
.CODEMAIN PROC
;display promptMOV AH,2
MOV DL'?',INT 21H
;input a characterMOV AH,1INT 21HMOV BL,AL
;go to a new lineMOV AH,2MOV DL,0DHINT 21HMOV DL,0AHINT 21H
;display charactersMOV DL,BLINT 21H
;return to DOSMOV AH,4CHINT 21HMAIN ENDPEND MAIN
21/11/2005CAP24126
When a program terminates, it should return control to DOS
MOV AH,4CH ; DOS exit function
INT 21H ; exit to DOS
21/11/2005CAP24127
Displaying a string
INT 21h , Function 9:
Display a string
Input : DX = offset address of string.
The string must end with a ‘$’ character.
21/11/2005CAP24128
• If the string contains the ASCII code of a control character , the control function is performed.
21/11/2005CAP24129
Example
• Print HELLO! on the screen.
MSG DB ‘HELLO!$’
21/11/2005CAP24130
The LEA instruction
• INT 21h, function 9, expects the offset address of the character string to be in DX.
• To get it there, we use
LEA destination , source
21/11/2005CAP24131
LEA 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.
21/11/2005CAP24132
Example
MSG DB ‘HELLO!$’LEA DX , MSG ; puts the offset
;address of variable ; MSG in DX
• This example contains data segments initialize DS.
21/11/2005CAP24133
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
21/11/2005CAP24134
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 #.
21/11/2005CAP24135
Print the message
• With DS initialized, we may print the “HELLO!” message:
LEA DX,MSG ;get message
MOV AH,9;display string function
INT 21h ;display string
21/11/2005CAP24136
TITLE PGM4-2: PRINT STRING PROGRAM
; This program displays “Hello!”
.MODEL SMALL
.STACK 100H
program title (comment)
comment line
memory model: small programs use at most 64K code and 64K data