Input output instructions
Download
1 / 46

Input & Output Instructions - PowerPoint PPT Presentation


  • 119 Views
  • Uploaded on

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. IN & OUT.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Input & Output Instructions' - huyen


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Input output instructions
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.

CAP241


In out
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

CAP241


2 categories of i o service routine
2 categories of I/O service routine

  • The BIOS routines.

  • The DOS routines.

CAP241


Bios routines
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.

CAP241


Dos routines
DOS routines

  • Can carry out more complex tasks.

  • Printing a character string…. They use the BIOS routines to perform direct I/O operations.

CAP241


The int instruction
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.

CAP241


Example
Example

  • INT 16h

    invokes a BIOS routine that performs keyboard input.

  • We will use a particular DOS routine

    INT 21h

CAP241


Int 21h
INT 21h

  • Used to invoke a large number of DOS functions.

  • Put the function number in AH register and then invoke INT 21h

CAP241


Functions
FUNCTIONS

Function number Routines

1single-key input

2single-character output

9 character string output

CAP241


Int 21h functions
INT 21h functions

  • Input values are to be in certain registers & return output values in other registers.

CAP241


Function 1
Function 1

  • Single-Key Input

    Input : AH = 1

    Output : AL = ASCII code if character key is pressed.

    = o if non-character is pressed.

CAP241


Example1
Example

MOV AH,1 ; input key function

INT 21h ; ASCII code in AL

CAP241


Example2
Example

  • If character k is pressed, AL gets its ASCII code; the character is also displayed on the screen

  • IfArrow key or F1-F10, ALwill contain 0

  • The instructions following the INT 21h can examine AL and take appropriate action.

CAP241


Function 2
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

CAP241


Function 21
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

CAP241


Example3
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.

CAP241


Control functions
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.

CAP241


Control functions1
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)

CAP241


A first program
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

CAP241


Read a character
Read a character

MOV AH,1 ; read character function

INT 21H ; character in AL

CAP241


Display the character on next line
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

CAP241


Display the character on next line1
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.

CAP241


Move cursor to the beginning of the next line
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

CAP241


Display the character
Display the character

MOV DL , BL ; get character

INT 21h ;and display it

CAP241


Program listing
Program Listing

TITLE PGM4_1 : Echo PROGRAM

.MODEL SMALL

.STACK 100H

.CODE

MAIN PROC

;display prompt

MOV AH,2

MOV DL,'?'

INT 21H

; input a character

MOV AH,1

INT 21H

MOV BL,AL

; go to a new line

MOV AH,2

MOV DL,0DH

INT 21H

MOV DL,0AH

INT 21H

; display characters

MOV DL,BL

INT 21H

; return to DOS

MOV AH,4CH

INT 21H

MAIN ENDP

END MAIN

CAP241


When a program terminates it should return control to dos
When a program terminates, it should return control to DOS

MOV AH,4CH ; DOS exit function

INT 21H ; exit to DOS

CAP241


Displaying a string
Displaying a string

INT 21h , Function 9:

Display a string

Input :DX = offset address of string.

The string must end with a ‘$’ character.

CAP241


CAP241


Example4
Example , the control function is performed.

  • Print HELLO! on the screen.

    MSG DB ‘HELLO!$’

CAP241


The lea instruction
The , the control function is performed.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

CAP241


Lea destination source
LEA destination , source , the control function is performed.

  • 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.

CAP241


Example5
Example , the control function is performed.

MSG DB ‘HELLO!$’

LEA DX , MSG ; puts the offset ;address of variable ; MSG in DX

  • This example contains data segments initialize DS.

CAP241


P rogram s egment p refix
P , the control function is performed.rogram 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

CAP241


Ds initialization
DS initialization , the control function is performed.

  • 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 #.

CAP241


Print the message
Print the message , the control function is performed.

  • With DS initialized, we may print the “HELLO!” message:

    LEA DX,MSG ;get message

    MOV AH,9 ;display string function

    INT 21h ;display string

CAP241


Sample Program , the control function is performed.

directive giving title for printed listings

program title (comment)

  • TITLE PGM4-2: PRINT STRING PROGRAM         

  • ; This program displays “Hello!”

  • .MODEL SMALL

  • .STACK 100H

comment line

memory model: small programs use at most 64K code and 64K data

set the stack size

CAP241


Sample program
Sample Program , the control function is performed.

starts the data segment where variables are stored

carriage return and line feed

reserve room for some bytes

  • .DATA

  • MSG DB “HELLO!”,0DH,0AH,’$’

  • .CODE

  • MAIN PROC

  •     MOV  AX,@DATA

  •     MOV  DS,AX ;initialize DS

  • LEA DX,MSG ;get message

  •     MOV  AH,9 ;display string function

  •     INT  21H ;display message

  •     MOV  AH,4CH

  •     INT  21H ;DOS exit

  • MAIN ENDP

  • END MAIN

variable name

starts the code segment

Declares the beginning of the procedure which is called main

marks the end of the current procedure

marks the end of the program. “main” specifies the program execution is to begin with the procedure “main”

CAP241


CAP241


Case conversion program
Case Conversion Program , the control function is performed.

ENTER A LOWER CASE LETTER : a

IN UPPER CASE IT IS : A

CAP241


Case conversion program1
Case Conversion Program , the control function is performed.

  • Use EQU to define CR & LF as names for the constants 0DH & 0AH.

    CR EQU 0DH

    LF EQU 0AH

CAP241


The messages and input character can be stored in the , the control function is performed.Data Segment like this:

MSG1DB'ENTER A LOWER CASE LETTER : $‘

MSG2DB CR,LF , 'IN UPPER CASE IT IS : ‘

CHARDB?,'$'

CAP241


Our program begins by displaying the first message and reading the character:

LEA DX,MSG1 ; get first message

MOV AH,9 ; display string function

INT 21H ;display first message

MOV AH,1 ; read character function

INT 21H ; read a small letter into AL

CAP241


Convert to upper case
Convert to upper case reading the character:

SUB AL,20H ; convert into uppercase

MOV CHAR,AL ; and store it

CAP241


Display second message uppercase
Display second message & uppercase reading the character:

LEADX,MSG2 ; get second message

MOVAH,9 ; display string function

INT 21H ;display message & uppercase letter

CAP241


Program Listing reading the character:

CAP241


. reading the character:MODEL SMALL

.STACK 100H

.DATA

CR EQU 0DH

LF EQU 0AH

MSG1 DB 'ENTER A LOWER CASE LETTER : $'

MSG2 DB CR,LF,'IN UPPER CASE IT IS : '

CHAR DB ?,'$'

.CODE

MAIN PROC

; initialize DS

MOV AX,@DATA

MOV DS,AX

;print user prompt

LEA DX,MSG1

MOV AH,9

INT 21H

; input a character and convert to upper case

MOV AH,1

INT 21H

SUB AL,20H

MOV CHAR,AL

; display on the next line

LEA DX,MSG2

MOV AH,9

INT 21H

; return TO DOS

MOV AH,4CH

INT 21H

MAIN ENDP

END MAIN

CAP241


ad