Anda di halaman 1dari 44

Input/ Output

Lecture 7
2

Input/output
Each microprocessor provides instructions
for I/O with the devices that are attached to
it, e.g. the keyboard and screen.
The 8086 provides the instructions in for
input and for output.
These instructions are quite complicated to
use, so we usually use the operating system to
do I/O for us instead.
3

Input/output ctd
In assembly language we must have a
mechanism to call the operating system to
carry out I/O.
In addition we must be able to tell the
operating system what kind of I/O operation
we wish to carry out, e.g. to read a character
from the keyboard, to display a character or
string on the screen or to do disk I/O.
Video
5

In 8086 assembly language, we do not call


operating system subprograms by name;
instead, we use a software interrupt mechanism
An interrupt signals the processor to suspend its
current activity (i.e. running your program) and
to pass control to an interrupt service program
(i.e. part of the operating system).
6

A software interrupt is one generated by a


program (as opposed to one generated by
hardware).
The 8086 INT instruction generates a software
interrupt.
It uses a single operand which is a number
indicating which MS-DOS subprogram is to be
invoked.
7

For I/O and some other operations, the number


used is 21h.
Thus, the instruction INT 21h transfers control
to the operating system, to a subprogram that
handles I/O operations.
This subprogram handles a variety of I/O
operations by calling appropriate subprograms.

This means that you must also specify which I/O


operation (e.g. read a character, display a
character) you wish to carry out. This is done by
placing a specific number in a register.
The AH register is used to pass this
information.
For example, the subprogram to display a
character is subprogram number 2h.
9
This means that you must also specify which I/O
operation (e.g. read a character, display a
character) you wish to carry out. This is done by
placing a specific number in a register.
The AH register is used to pass this
information.
For example, the subprogram to display a
character is subprogram number 2h.
10

Character Input
The task here is to read a single character
from the keyboard.
There are also three elements involved in
performing character input:
11

Character Input
As for character output, we specify which of MS-
DOSs I/O subprograms we wish to use, i.e. the
character input from the keyboard subprogram.
This is MS-DOS subprogram number 1h. This
number must be stored in the AH register.
We call MS-DOS to carry out the I/O operation
using the INT instruction as for character output.
12

Character Input
The MS-DOS subprogram uses the AL
register to store the character it reads from
the keyboard.
13

Character Input
Example 1 : Write a code fragment to read a
character from the keyboard:
mov ah, 1h ; keyboard input subprogram
int 21h ; character input; character is stored
in al
14

Character Output
The task here is to display a single character
on the screen.
There are three elements involved in carrying
out this operation using the INT instruction:
15

Character Output
We specify the character to be displayed. This
is done by storing the characters ASCII code
in a specific 8086 register. In this case we use
the dl register, i.e. we use dl to pass a
parameter to the output subprogram.
16

Character Output
We specify which of MS-DOSs I/O
subprograms we wish to use. The subprogram
to display a character is subprogram number
2h. This number is stored in the ah register.
17

Character Output
We request MS-DOS to carry out the I/O
operation using the int instruction. This
means that we interrupt our program and
transfer control to the MS-DOS subprogram
that we have specified using the ah register.
18

Character Output
Example 2 : Write a code fragment to
display the character a on the screen:
mov dl, a ; dl = a
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos output character
19

Example 3
The following example combines the two
previous ones, by reading a character from
the keyboard and displaying it.
20

Example 3
Reading and displaying a character:
mov ah, 1h ; keyboard input subprogram
int 21h ; read character into al
mov dl, al ; copy character to dl
mov ah, 2h ; character output subprogram
int 21h ; display character in dl
21

Displaying a string
Input ah =9, dx = offset address of a string
String must end with a $ character.
22
Displaying a string
To display the message Hello!
.data
MSG db Hello world!$"
.code
Mov ah, 9h
Mov dx, offset MSG
Int 21h
Offset operator returns the address of a variable
The instruction LEA loads destination with
address of source
Lea dx, MSG
23

Question
Write a program that inputs and displays a
string
24

I/O Port Addressing


Port addressing is used in conjunction with
the IN and OUT instructions to access input
and output ports.
Any of the memory addressing modes can be
used for the port address for memory mapped
ports.
25

I/O Port Addressing


It is possible to use the IN and OUT instructions
to handle I/O directly at the port level.
The IN instruction transfers 8-bit or 16-bit data
from a port to the AL or the AX register
respectively.
The OUT instruction transfers 8-bit or 16-bit
data from the AL or the AX register, respectively,
to an 8-bit or 16-bit port.
26

Two different addressing modes can be used


to access ports in the I/O address space,
Direct/Fixed addressing mode and
Indirect/Variable addressing mode
using DX
27

Direct/Fixed Port addressing mode

In direct port addressing, the port number is


an 8-bit (00h-0FFh) immediate operand.
Fixed access to ports numbered 0-255 (28 -1).
256 ports can be addressed.
28

Example
IN AL, 15H ; port address should be between 0 -
255
This stands for input the data from the byte
wide input port at address 15H of the I/O
address space to register AL.
29
Instructions using fixed-port format have the
form:
IN AL , PortNumber ; AL Data8
from Port whose address is PortNumber
IN AX , PortNumber ; AX Data8
from Port whose address is PortNumber
30
OUT PortNumber , AL ; Port Data8
from AL to Port whose address is PortNumber
OUT PortNumber , AX ; Port Data8 from
AX to Port whose address is PortNumber
Where PortNumber is a constant in the range
00H to 0FFH, specifying a port address.
31

Indirect Port addressing mode


In the variable-port format, the port address is
specified by a 16-bit address (0000h-0FFFFh)
located in the DX register.
The port number is taken from register DX and
can range from 0 to 65 535 (216-1) 65 536 ports
can be addressed.
32

Indirect Port addressing mode


It means input the data from the byte wide input
port whose address is specified by the contents
of register DX. For instance, if DX equals 1234H
the contents of the port at this I/O address are
loaded into AL.
33
Instructions using variable-port format have the
form:
Using Indirect/Variable port addressing
for the source operand in an IN
instruction, we get:
IN AL, DX
IN AL , DX ; AL Data8 from
Port whose address is in DX
IN AX , DX ; AX Data16
from Port whose address is in DX
34
OUT DX , AL ; Port Data8
from AL to Port whose address is in DX
OUT DX , AX ; Port Data16
from AX to Port whose address is in DX
35

Parallel and serial ports


There are two types of I/O ports: parallel and
serial.
Thus, an I/O device can be interfaced to a
computer with either a parallel or a serial
connection.
The former transmits several bits in parallel,
while the latter transmits only one bit at a
time.
36

Parallel and serial ports


Parallel ports are usually used to connect
nearby devices.
Serial connections are common for many
types of devices, and a given computer may
have several serial ports.
37

Some common port addresses


The keyboard interfaces the computer
through ports 60H (keyboard data port), 61H
(the keyboard control port), and port 62H
(the keyboard status port).
The speaker is controlled by the same 8255
chip that is used to control the keyboard. Bits
0 and 1 of port 61H, the keyboard control
port, are wired to the speaker.
38

Some common port addresses


The parallel printer adapter LPT1 interfaces
the computer through ports 3BCH (printer
data port), 3BDH (printer status port), and
port 3BEH (printer control port).
The serial port COM1 has ports from 3F8H to
3FFH. The 8253 timer chip has port 40H to
port 43H.
39

Questions
1. Write instructions to output the data FFH to
port ABH.
2. Write instructions to output the data FEH to an
output port with port address B000H.
3. Write instructions to read data from two 8-bit
ports at addresses AAH and A9H respectively,
and then output the data as a word to a 16-bit
port with address B000H.
40

Question1:
Write instructions to output the data FFH to
port ABH.

MOV AL , 0FFH
OUT 0ABH , AL
41

Question 2:
Write instructions to output the data FEH to
an output port with port address B000H.

MOV DX , 0B000H
MOV AL , 0FEH
OUT DX , AL
42

Question 3:
Write instructions to read data from two 8-bit
ports at addresses AAH and A9H respectively,
and then output the data as a word to a 16-bit
port with address B000H.

IN AL , 0AAH ; AL Data8 from Port


0AAH
MOV AH , AL ; AH Data8 from Port 0AAH
IN AL , 0A9H ; AL Data8 from Port
0A9H
MOV DX , 0B000H ; DX Port address 0B000H
OUT DX , AX ; Port _0B000H AX
43

Conclusion
Each register also has a special purpose:
AX word multiply/divide/IO
AL byte multiply/divide/IO, translate, decimal
arithmetic
AH byte multiply/divide/IO
BX index register for MOVE/translate
CX count register for string operations and
loops
CL variable shift and rotate
DX port address for IN and OUT (indirect)
word multiply/divide
References
Rafiquzzaman, M., 2005. Memory, I/O, & Parallel
Processing. In Fundamentals of Digital Logic &
Microcomputer Design. John Wiley & Sons. pp.299-
365.
Sen, S.K., 2010. Understanding 8085/8086
microprocessors and peripheral ICs through
Questions & Answers. 2nd ed. New Dehli: New Age
International Publishers.
Tocci, R.J. & Ambrosio, F.J., 2003. Microprocessors
and Microcomputers: Hardware and Software. 6th
ed. Prentice Hall.

Anda mungkin juga menyukai