Microprocessor
Microprocessor
>
r ■v
r : * *
fJt
* mr
i
A
=*/ ri . ■
r* •C-.
o
•*
■**
V
SMBS WZ
1®
§0 m. *
4
J A
'9
ri .
*
# IT.
m o
*r
'J
.
%
I
M
F< ■/
r /7
>
IJ&M
■h
■
■
K. UDAYA KUMAR B. S. UMASHANKAR
THE 8085
MICROPROCESSOR
Architecture, Programming
and Interfacing
K. UDAYA KUMAR,
Principal,
B.N.M. Institute of Technology,
Bangalore, India.
B. S. UMASHANKAR,
Professor,
Department of Computer Science,
B.N.M. Institute of Technology,
Bangalore, India.
PEARSON
Copyright © 2008 Dorling Kindersley (India) Pvt. Ltd.
Licensees of Pearson Education in South Asia
No part of this eBook may be used or reproduced in any manner whatsoever without the publisher’s prior
written consent.
This eBook may or may not include all assets that were part of the print version. The publisher reserves the
right to remove any material present in this eBook at any time.
ISBN 9788177584554
elSBN 9788131799772
Head Office: A-8(A), Sector 62, Knowledge Boulevard, 7th Floor, NOIDA 201 309, India
Registered Office: 11 Local Shopping Centre, Panchsheel Park, New Delhi 110 017, India
Dedicated to
the Goddess of Learning
This page is intentionally left blank.
Contents
Microprocessors, microcontrollers, and digital signal processor chips are used in business machines,
automotive electronics, home appliances, electronic toys, and a variety of industrial applications. In
this book, we confine ourselves to the study of 8-bit microprocessors Intel 8085, Zilog Z-80 and
—
Motorola 6800, as well as the popular 8-bit microcontroller the Intel 8051.
This book has been written after teaching the subject of microprocessors for more than two
decades, keeping in mind the difficulties faced by students in grasping the subject. We have pre¬
sented the material in a lucid language, using short, simple sentences to facilitate easy reading and
understanding. Each concept has been articulated with a number of examples with emphasis on clar¬
ity, in a logical sequence. To this end, the book is divided into four parts. The first part consists of
Chapters 1 to 13, and deals with the fundamentals of a microprocessor. Chapters 14 to17 make up
the second part, and focuses on assembly language programs. The programmable and non-program-
mable ports are examined in part three from Chapters 18 to 21, while the concluding portion of the
book, consisting of Chapters 22 to 30 deals with support chips.
Chapter 1 introduces the developments in electronics starting with the transistor and the early inte¬
grated circuits and provides an insight into the evolution of microprocessors, microcontrollers and
digital signal processors.
Chapter 2 familiarizes students with the various parts of a computer, their main functions and the
evolution of computer languages.
Chapter 3 explains clearly the unsigned and the various signed number representations for integers
and provides an overview of signed floating-point numbers.
Chapter 4 touches upon the history of the microprocessor and deals with the fundamentals of the
8085 microprocessor, which is the main focus of this book. The various registers and the program¬
mer’s view of 8085 are also introduced here.
Chapter 5 describes a typical 8085-microprocessor kit and its usage by indicating the steps needed
to write and execute a simple assembly language program.
Chapter 6 gives the classification of 8085 instructions and elaborates on the data transfer group of
instructions with meaningful examples. The various addressing modes of 8085 are also explained.
Chapter 7 deals with the arithmetic group of instructions and explains the various flags used in the
8085 microprocessor.
Chapters 8 to 10 focus on the logical, stack, and branch group of instructions respectively, explain¬
ing them with suitable examples.
Chapter 11 dwells on the concept of chip selection and the use of 74138 to generate chip select
logic.
Chapter 12 discusses the need for I/O ports, their addressing and compares I/O mapped I/O with
memory mapped I/O.
Chapter 13 furnishes a detailed architecture of 8085, and explains the various machine cycles
needed for executing a variety of instructions.
Chapter 14 explains simple assembly language programs that are executed on a microprocessor kit
and also illustrates some of the commonly used monitor routines.
xii
Chapter 1 5 brings out the use of a personal computer in writing an assembly language program,
translating it to machine language using an assembler, and then downloading it to the microprocessor
kit for execution.
Chapter 16 deals with complex assembly language programs. For these programs students have to
use the PC to enter the program, do the translation using the assembler, download the machine code
s
&
to the microprocessor kit, and run the program using the commands issued by the PC in serial mode.
Chapter 17 is about more complex assembly language problems. For each of these problems, the
8
a flowchart and the program are provided along with trace for test data. This simplifies the understand¬
ing of the given solution.
Chapter 18 expounds on data transfer schemes and discusses in detail about the use of interrupts in
the 8085 microprocessor. The interrupt related instructions are explained here, and we look at a num¬
ber of assembly language programs that make use of interrupts.
—
Chapter 19 presents a detailed explanation of the working and application of the Intel 8212 a non¬
programmable I/O port.
—
Chapter 20 is about the popular Intel 8255 a programmable peripheral interface chip. The
description, operational modes and the control words are delineated.
Chapter 21 describes some of the commonly used interface modules like logic controller, analog-
to-digital converter, digital-to-analog converter and stepper motor. A number of interesting programs
using these interface modules are illustrated.
Chapter 22 first deals with interfacing 7-segment display and matrix keyboard using latches and
—
tri-state gates. Then the Intel 8279 the programmable keyboard and display controller chip is
described at length. A number of useful routines using the 8279 chip are also explained.
—
Chapter 23 is about the Intel 8259 the programmable interrupt controller. It gives an overview of
the working of 8259, and explains the function of its pins and the programming of 8259 with and with¬
out slave 8259s.
—
Chapter 24 covers the programmable DMA Controller the Intel 8257. In this chapter the concept
of Direct Memory Access (DMA), the DMA controller chip and its programming are examined in
depth.
—
Chapter 25 describes the Intel 8253 a programmable interval timer. It explains the need for a pro¬
grammable timer and succinctly spells out the various modes of operation of 8253.
—
Chapter 26 examines the Intel 8251 the Universal Synchronous Asynchronous Receiver Trans¬
mitter (USART). It explains the asynchronous and synchronous modes of transmission and reception,
and describes the programming of the 825 1.
Chapter 27 reviews the Zilog Z-80 microprocessor. With an in-depth knowledge of the 8085
microprocessor acquired from the first 26 chapters, students would be in a position to understand the
programmer’s view, new addressing modes, and the new instruction types available in Z-80. The
chapter ends with a few programming examples that provide a critical comparison of the Z-80 and
the Intel 8085.
Chapter 28 talks about the M6800 microprocessor from Motorola, which has a very simple archi¬
tecture compared to 8085 or the Z-80. It describes the pins, the programmer’s view, addressing modes,
and the instruction set of M6800 and ends with a few programming examples that demonstrate its
power in spite of its simplicity.
Chapter 29 is devoted to the popular Intel 805 1 microcontroller. It discusses the basics of the 805 1
providing details about its functional blocks, the programmer’s view, addressing modes, and the
instruction set. A number of assembly language programming examples are provided to make students
comfortable with the instruction set of 805 1.
Chapter 30, the concluding chapter, reviews the advanced topics in 805 1. It deals with the interrupt
structure, timers, serial interface, structure and operation of ports, and power saving modes of 8051.
xiii
The chapter ends with the programming of EPROM in 8751, which is the EPROM version of the 8051
microcontroller.
Comments and feedback on the various topics discussed in this book are welcome.
ACKNOWLEDGEMENTS ■o
3
The authors are grateful for the timely help, encouragement and support extended by Narayan Rao R. sr
o
Maanay, Secretary, B.N.M. Institute of Technology, as well as Prof. T.J. Rama Murthy, Director, and
Dr. K. Ranga, Dean of the institution. They are thankful to the reviewers for their constructive sug¬
gestions, which helped in enhancing the contents of this book. Finally, the authors are indebted to their
family members for their encouragement and forbearance.
K. Udaya Kumar
B. S. Umashankar
This page is intentionally left blank.
□
Fundamentals of
a Microprocessor
Chapter Heads
1 Evolution of Microprocessors
2 Fundamentals of a Computer
3 Number Representation
4 Fundamentals of Microprocessor
5 First Assembly Language Program
6 Data Transfer Group of Instructions
7 Arithmetic Group of Instructions
8 Logical Group of Instructions
9 NOP and Stack Group of Instructions
10 Branch Group of Instructions
1 1 Chip Select Logic
12 Addressing of I/O Ports
13 Architecture of 8085
INTRODUCTION
This part comprises of chaps. 1 to 14. Chapters 1 and 2 are intro¬
ductory chapters which discuss the evolution of microprocessors
and the fundamentals of a computer, respectively. The following
chapters deal with the framework and internal architecture of a
microprocessor.
1 Evolution of
Microprocessors
■ 8-bit microprocessors
■ 16-bit microprocessors
■ 32-bit microprocessors
■ Recent microprocessors
■ Microcontrollers and digital signal processors
This chapter gives a crisp outline of the various stages in the evolution of today’s microprocessors.
Explicit information is given right from the integrated circuits through the 4-, 8-, 16- and 32-bit micro¬
processors to the present-day microprocessors and microcontrollers.
The 1939—45 world war posed stringent envorinmental and operation requirements like standardiza¬
tion, miniaturization, reliability, maintainability and the like on electronic communication equipment
components. Key improvements took place in the design and manufacture of electronic components.
After the war, the semiconductor transistor came into widespread use.
The concept of integrated circuit (IC), also known as ‘chip’, which integrates a circuit of several
electronic components into a solid block was envisaged in 1952. In 1959, the invention of planar
process with aluminium metallization by Robert Noyce and Jean Hoemi at Fairchild Semiconductor
enabled large-scale production of ICs.
The progress of integration of circuitry was very rapid. The small-scale integration (SSI) chip having
digital logic gates circuitry was introduced in 1964. Gordon Earle Moore at Fairchild Semiconductor
predicted that the number of transistors on a silicon chip would increase from 50 in 1965 to 65,000 in
1975. It was recognized as his first articulation of Moore’s law suggesting that the number of transistors
on a chip will double every year. Moore’s prediction indeed was true and medium-scale integration (MSI)
chip with a complete register circuit appeared in 1968. The large-scale integration (LSI) memory chip
(256-bit RAM) was produced by Fairchild in 1970. In 1971, the LSI chips with 1024-bit dynamic random
access memory (RAM) and Universal Asynchronous Receiver Transmitter (UART) were developed.
4
■ 1 .2 4-BIT MICROPROCESSORS
The advent of microprocessors was accidental. Intel Corporation founded by Moore and Noyce in 1968
was initially focused on creating semiconductor memory (DRAMs and EPROMs) for digital comput¬
8 ers. In 1969, a Japanese calculator manufacturer - Busicom approached Intel with a design for a small
a> calculator, which required 12 custom chips. Ted Hoff, an Intel engineer felt that a general-purpose logic
o
sa device could replace the separate multiple components. This idea led to the development of the first
microprocessor. Microprocessors made a modest beginning as the drivers for calculators.
E
.2 Federico Faggin and Stanley Mazor realized Ted Hoff’s ideas into hardware at Intel. The result was
S the Intel 4000 family comprising the 4001 (2K ROM), the 4002 (320-bit RAM), the 4003 (10-bit I/O
in
00 shift-register) and the 4004, a 4-bit central processing unit (CPU). Intel introduced the 4004
s« microprocessor to the worldwide market on November 15, 1971. It was a 4-bit PMOS chip with 2,300
transistors. It was not truly a general-purpose microprocessor as it was basically designed for a calcu¬
H
lator. About the same time, Texas Instruments also developed the 4-bit microprocessor TMS 1000.
Texas Instruments is recognized as the inventor and owner of the microprocessor patent.
■ 1 .3 8-BIT MICROPROCESSORS
Federico Faggin and his team at Intel designed a chip for controlling a CRT display produced by
Computer Terminals Corporation, later called Datapoint. This chip did not meet Datapoint’s functional
requirement of speed and they decided not to use it. Intel introduced this chip as world’s first 8-bit
general-purpose microprocessor 8008 in 1972. The Intel 8008 was used in the famous Mark-8
computer kit. On realizing the potential of this product, Intel introduced the improved 8008, the 8080
microprocessor in 1974. The Intel 8080 really created the microprocessor market. The other notable
8-bit microprocessors include Motorola 6800, designed for use in automotive and industrial applications,
and Rockwell PPS-8, Signetics 2650 having innovative and powerful instruction set architecture.
With improvements in integration technology, Intel was able to integrate the additional chips
required by the 8080, that is, the 8224 clock generator and the 8228 system controller along with the
8080 microprocessor within a single chip - the Intel 8085. The other improved 8-bit microprocessors
include Motorola MC6809 designed for high performance, Zilog Z-80 and RCA COSMAC designed
for aerospace applications.
In 1975, Moore recalled that his prediction of exponential growth in the complexity of integrated
circuits was true. He also forecast a change for the next decade indicating that the pace of complex¬
ity increase would slow to a doubling every two years during the maturation of design capabilities.
With increase in processing power the microprocessors dominated as the CPU of digital comput¬
ers. Earlier to the arrival of microprocessors, CPU was realized from individual SSI chips. The
digital computer that uses a single chip microprocessor as its CPU is referred to as a microcomputer.
Intel introduced the 16-bit microprocessor 8086 (16-bit bus) in 1978 and 8088 (8-bit bus) in 1979. It
had 29,000 transistors. IBM selected the Intel 8088 for their personal computer (IBM-PC) introduced
in 1981. Intel released the 16-bit microprocessor 80286 (having 1,34,000 transistors) which was used
5
as CPU for the advanced technology personal computers (PC-AT) in 1982. It was called Intel 286 and
was the first Intel processor that could run all the software written for its predecessor Intel 8088. This
backward software compatibility was important for its commercial success. It is important to note that
this software compatibility remains a hallmark of Intel’s family of microprocessors.
<
2.
■ 1 .5 32-BIT MICROPROCESSORS
In 1985, Intel announced the 80386 a 32-bit microprocessor with 2,75,000 transistors. It supported ®
fl
I 8.
multitasking. Introduced in 1989, Intel 486 microprocessor was the first to offer a built-in math co- u
processor. It had 1.2 million transistors. o
In 1993, Intel Pentium microprocessor with 3.1 million transistors was introduced. It allowed *
computers to process real-world data like speech, sound, handwriting and photographic images. The
7.5-million transistor Intel Pentium II microprocessor, introduced in 1997, was designed specifically
to process audio, video and graphics data efficiently. Intel Celeron processors range designed for the
value PC market segment were released from 1999.
Intel Pentium III processors with 9.5 million transistors designed for advanced imaging, 3D,
streaming audio, video and speech recognition applications and Intel Pentium III Xeon processors for
workstation and server market segments were introduced in 1999. Intel Pentium IV processors with
more than 42 million transistors introduced from 2000 are used in the present PCs. Users can create
professional quality movies, deliver TV-like video via the internet, communicate with real-time video
and voice, render 3D graphics in real time, quickly encode music for MP3 players and simultaneously
run several multimedia applications while connected to the Internet.
Intel Xeon processors introduced from 2001 are targeted for high-performance and mid-range,
dual-processor workstations, dual and multiprocessor server configurations coming in the range.
■ 1 .6 RECENT MICROPROCESSORS
The Itanium processor is the first in a family of 64-bit products from Intel introduced in 2001. It is
well suited for the most demanding enterprise and high-performance computing applications like
e-Commerce security transactions, large databases, mechanical computer-aided engineering and
sophisticated scientific and engineering computing.
Introduced from 2003, the Intel Pentium M processor, the Intel 855 chipset family and the Intel
PRO/Wireless 2100 network connection are the three components of Intel Centrino mobile techno¬
logy. Intel Centrino mobile technology is designed specifically for portable computing, with built-in
wireless local area network (LAN) capability and breakthrough mobile performance. It enables
extended battery life and thinner, lighter, mobile computers.
A microcontroller is a highly integrated chip that contains all the components comprising a controller.
Typically, this includes a CPU, RAM, some form of read only memory (ROM), input/output (I/O)
6
ports, timers and so on. Unlike a general-purpose computer, which also includes all of these compo¬
nents, a microcontroller is designed for a very specific task - to control a particular system. As a result,
the number of parts can be reduced, which cuts down the cost. Microcontrollers are sometimes called
embedded microcontrollers, which just means that they are part of an embedded system - that is, one
part of a larger system. One of the popular 8-bit microcontrollers is Intel 8051.
o
M
Digital Signal Processor (DSP) is a special-purpose CPU used for digital signal processing appli-
0 cations. Once a signal is converted into digital data, its components can be isolated, analysed and
o rearranged more easily than in analogue form using various algorithms, such as Fast Fourier Trans-
form. It provides ultrafast instruction sequences, such as shift and add, and multiply and add, which
o are commonly used in math-intensive signal processing. DSP is used in many fields, including bio-
£ medicine, sonar, radar, seismology, audio, cell phones, sound cards, fax machines, modems, hard
in
disks, digital TV, speech and music processing, imaging and communications. It is also used to create
the concert hall and surround sound effects in stereo and home theatre equipments. One of the popu¬
« lar DSP chips is TMS320C54X from Texas instruments.
H
Microprocessors, microcontrollers and DSP chips are used in business machines, automotive
electronics, home appliances, electronic toys, variety of industrial applications and the like. Today,
there is no field in which microprocessors, microcontrollers or DSP chips have not made an impact.
Semiconductor Industries Association (SIA) has projected annual sales in the year 2006 for micro¬
processors, microcontrollers and DSPs as $36.4 billion, $12.3 billion and $9 billion, respectively.
2
Fundamentals
of a Computer
Calculator
■
■ Computer
• Input devices
•Output devices
•Memory and its classification
•Arithmetic Logic Unit (ALU)
•Control unit and CPU
• Input and output ports
■ Microcomputer
•Microprocessor
•Microcontroller
■ Computer languages
This chapter outlines in brief the various components of a computer and their major functions. In the
first half of the chapter we deal with the advantages of a computer over a calculator. The chapter ends
with a small note on microcomputers and a detailed discussion of the various computer languages
along with their merits and demerits.
■ 2.1 CALCULATOR
A calculator is a hand-held equipment using which arithmetic calculations are performed very easily.
Block diagram of a calculator is shown in Fig. 2.1.
It will have keypad as an input device. Values needed in a calculation are provided to the calculator
using this keypad. There will be an arithmetic unit, which is responsible for performing the needed
8
LCD Display
o
Arithmetic Unit
sE
Fig. 2.1
2 Block diagram of a
.2
S Keypad calculator
in
§
co
0
calculations. It is a semiconductor chip, which performs the calculations at electronic speeds.
H
Typically it can perform an addition operation in a microsecond.
In an arithmetic operation, say addition, the two inputs are entered using the keypad. The first value
that is input goes to the accumulator. The accumulator in turn, provides one input to the arithmetic
unit. Next the user inputs the operation to be performed by the arithmetic unit (e.g. *+’). Finally, the
user inputs the second value to the arithmetic unit using the keypad. Then the arithmetic unit performs
the add operation, under the control of the control unit, and displays the result on the LCD panel in
about a microsecond after the completion of the operation.
It takes the user about 3 seconds to get this result on the calculator assuming 1 second is needed
for inputting each of the values to be added, and 1 second is needed for inputing the operation to be
performed. However, the arithmetic unit utilized only about a microsecond for performing the calcu¬
lation. Thus a calculator becomes slow in the hands as one cannot input values to the calculator at elec¬
tronic speeds! This problem is overcome in a computer.
■ 2.2 COMPUTER
A computer is generally a desktop equipment, using which arithmetic calculations are performed at a
tremendous speed. Nowadays laptop computers as well as hand-held computers have emerged. The
blocks that make up a computer are shown in Fig. 2.2.
The input devices are used for supplying program and data to the memory. In other words, the com¬
puter system reads the program and the data from the input devices. Most common input devices are
the keyboard and mouse.
Output devices are used for displaying or recording the results computed by the computer. Most
common output devices are the CRT display, printer, and plotter.
9
Output Devices
Output Ports Tl
ZY l
0)
S
Control Unit
?£
3 S
■u
Arithmetic Logic Unit Main Memory
(ALU) 5
ZY
I
Input Ports
\7
Secondary Storage
Fig. 2.2
I
Input Devices
Blocks of a Von
Neumann computer
Compared to a calculator, the memory unit is an extra block. In the memory the entire set of opera¬
tions that are to be performed is written using the keyboard as a sequence of instructions. This
sequence of instructions is called program. Also, the data on which the program is to operate is entered
using the keyboard, and can be stored in memory. Memory can also be used for storing intermediate
and final results. This design of a stored program computer where program, data, and result reside in
memory is due to Von Neumann.
Memory can be classified into main memory and secondary memory. Secondary memory is also fre¬
quently termed as auxiliary memory. Main memory has the drawback of high-cost and low-capacity
storage. But its advantage is the high speed of data transfer. The control unit can directly communicate
with the main memory but not with the secondary memory. Main memory can be broadly classified
into random access memory (RAM) and read only memory (ROM).
Random access memory (RAM): RAM consists of a number of memory locations where in each
location typically 8 bits are stored. (A bit is a binary digit. Thus it can have only one of the two
values 0 or 1.) It is possible to read from a RAM location, as well as write to a RAM location. The
disadvantage of RAM is that it is volatile. What it means is that, the moment power supply to the
RAM is switched off, the information in the RAM will be lost.
Sequential Access: There are two types of accessing information. They are sequential access and ran¬
dom access. In sequential access it is necessary to access information strictly in an order. If there are
100 memory locations, it has to be accessed in the order of 1, 2, 3, ... 100. Thus, it takes least time to
access information from location 0 and maximum time to access information from location 100. An
example device that employs sequential access is the magnetic tape.
10
Random Access: In random access technique, it is possible to access a memory location in any order.
For example, one can read from the 100 locations in the order of 45, 34, 67, 28, 57, and so on.
Second, it takes the same time to read from a memory location irrespective of its position. In a RAM,
the method of access is random, and its name in fact is derived based on the method of access.
Read only memory (ROM): ROM also consists of a number of memory locations where in each loca¬
tion typically 8 bits are stored. A ROM also uses random access technique just like a RAM. The advan¬
s2 tage of ROM is that it is nonvolatile. What it means is that, even if the power supply to the ROM is
switched off, the information in the ROM will not be lost. However, it is only possible to read from a
o ROM location. Thus, in a computer ROM is used for storing information that should not be lost when
~ power is switched off. But first of all how to store information in a ROM? To answer this question, semi¬
w conductor manufacturers provide several versions of ROM whose characteristics are briefly discussed in
§ the following.
CO
Jj Mask-programmed ROM: It derives this name because the information is written to this type of
ROM at the time of manufacture using a suitable mask. Once the manufacturer writes this type of
ROM, it is not possible to change this information even by the manufacturer. The information entered
is permanent. It is cheap compared to the other types of ROMs, when cost per unit quantity is consid¬
ered. But the user has to place an order for a large number at a time. Thus, ROMs are used in equip¬
ments that are produced in large quantities. To give an analogy, mask-programmed ROMs are similar
to books that are printed in large volumes.
PROM: It stands for ‘programmable read only memory’. The user writes information to this type of
ROM using PROM programmer equipment. Once the user writes this type of ROM, it is not possible
to change this information anymore. The information entered is permanent. Although it is costlier
compared to a mask ROM, it is cheap compared to the other types of ROMs discussed successively
in this chapter. The advantage is that the user is free to buy even a single piece of PROM from a local
shop. Thus, PROMs are used in equipment that are produced in small quantities. To extend the anal¬
ogy PROMs are similar to blank notebooks that are written by a user making use of an ink pen.
EPROM: It stands for ‘erasable programmable read only memory’. Information is written to this type
of ROM by the user using a EPROM programmer equipment. After the user writes this type of ROM,
it is possible to change this information a number of times. Thus, although the information entered is
not lost when power is switched off, it is possible to erase it and then write new information. Erasing
of the EPROM is done by exposure of strong ultraviolet (UV) light from a very close distance on the
quartz window of the EPROM chip. Then the contents of the entire EPROM are lost. For this purpose
UV light eraser equipment are available. Although it is costlier compared to a mask ROM or a PROM,
it is very popular. The user is free to buy even a single piece of EPROM from a local shop. Thus,
EPROMs are used during the development phase of the equipment. They are also frequently used in
Type of ROM Info, entered Possible to Cost per chip Min. order Method of erasing Application
by change info? quantity info. area
Mask- Manufacturer No Cheapest Large Not possible Large-scale
programmed production
ROM
PROM User No Costlier One Not possible Medium-scale
production
EPROM User Yes Even more One Using strong R&D
costly UV source
EEPROM User Yes Costliest One Using electrical R&D
signals
11
equipments that are produced in small quantities. To extend the analogy writing to an EPROM is sim¬
ilar to writing on sand using a stick. If a strong breeze comes everything is erased!
EEPROM or EAPROM: It stands for ‘electrically erasable (or alterable) programmable read only
memory’. Information is written to this type of ROM by the user using a EPROM programmer equip¬
ment. After the user writes this type of ROM, it is possible to change this information a number of
Tl
times. Thus although the information entered is not lost when power is switched off, it is possible to
erase it and then write new information. However, erasing of the EEPROM is done by electrical sig¬ l
nals. Then the contents of the EEPROM can be selectively erased. It is costlier compared to the other 3.1
types of ROMs, and is gaining in popularity. The user is free to buy even a single piece of EEPROM
0)
s
from a local shop. Thus EEPROMs are used during the development phase of the equipment. They
3 S
?£
are also frequently used in equipments that are produced in small quantities. To extend the analogy ■u
EEPROMs are similar to blank notebooks that are written by a user making use of a soft pencil. The 5
information can be selectively rubbed out and new information can be written.
If an EPROM or an EEPROM can be used for reading as well as writing information, and is also
nonvolatile, what is the use of RAM? The problem with any type of ROM is that although reading
from the ROM is fast, the writing (if permitted) is very slow, and the erasing (if permitted) is
extremely slow. Also the writing of the EPROM is not possible when the program is in execution. To
give an example, some typical EPROM chips allow reading from a memory location in less than a
microsecond, write to a memory location in about 50 ms, and is erased in about 30 minutes.
Secondary memory: Sometimes there is a need to have more amount of information than is possible
to store in a main memory. In such cases it is stored in the secondary memory. This type of memory
is characterized by virtues of large capacity for storage and low cost per bit of storage. But its disad¬
vantage is its very low speed for access. The control unit does not directly access secondary storage.
Typical examples for secondary memory are the hard disk, floppy disk, and magnetic tape.
In a computer, there is an ALU, which is capable of performing logical operations (like AND, OR, Ex-
OR, Invert) in addition to the arithmetic operations. The control unit supplies the data required by the
ALU from memory, or from input devices, and directs the ALU to perform a specific operation based on
the instruction fetched from the memory. ALU is the ‘calculator’ portion of the computer.
The control unit fetches one instruction at a time from the main memory, and then executes it. In this
execution, it makes use of the ALU, if the instruction execution involves an arithmetic or logical oper¬
ation (like AND, OR, Ex-OR). This fetching and execution is done at electronic speeds, say in less
than a microsecond. Then the control unit fetches and executes the next instruction from the memory,
and so on, till the program is completed and the result is output using the output device. In many com¬
puters, the control unit and the ALU are integrated into a single block, and this single block is termed
as the central processing unit (CPU).
The point to be noted here is that the control unit is fetching the instructions from the main memory
at electronic speeds, rather than from a human being through a keypad at terribly low speeds. In a cal¬
culator we may indicate an operation to be performed by the arithmetic unit in 1 second. In a computer,
the control unit fetches and executes more than 1 million instructions at the same time. This is what
makes the computer much faster than a calculator. Is a calculator obsolete in view of the above virtues
12
of the computer? It is not. The reason is simple. If it is required to find the roots of a given quadratic
equation one needs only about 1 minute when using a calculator. But using a computer it may take as
much as a few hours to develop an error-free program for this problem. It depends on the ability of
the programmer. Once the program is developed it takes only a few microseconds to execute the pro¬
gram to obtain the roots of the quadratic equation. Thus a calculator is the choice if the calculation is
o required to be done only once. A computer is the choice if the calculations are required to be done a
number of times with different sets of data.
sE
2 2.2.6 INPUT AND OUTPUT PORTS
s
w CPU and the main memory are very fast compared to electromechanical input or output devices like
§
CO printers, etc. In such a case it is essential that the data lines of the computer is not kept engaged for a
« long time during communication with input/output (I/O) devices. Otherwise the overall speed of the
H computer system comes down drastically. So I/O devices are connected to a computer through I/O
ports.
For example, to communicate with a printer, the CPU loads the output port connected to the printer
at electronic speeds. This is slowly printed by the printer. When the printer has finished printing, the
output port requests the CPU for further data. This way, the CPU is allowed to work at its full speed
with no degradation in the overall speed of the computer system.
■ 2.3 MICROCOMPUTER
A microcomputer is a small sized, inexpensive, and limited capability computer. It has the same blocks
that are present in a computer. Present-day microcomputers are very small in size. They are of the size
of a notebook. In the days to come they are bound to become still smaller. They are very cheap so that
many individuals can possess them as their personal computers. Because of mass production they are
becoming still cheaper. Many early microcomputers were not very powerful. For example, they did not
have even a simple multiply instruction in their instruction set. Also, they could work only on unsigned
integer data. But present-day microcomputers have not only multiply and divide instructions on
unsigned and signed numbers, but are also capable of performing floating point arithmetic operations.
In fact they are more powerful than the mini computers and main computers of yesteryear.
2.3.1 MICROPROCESSOR
Microprocessor is the CPU part of a microcomputer, and is available as a single integrated circuit.
Thus a microprocessor will have the control unit and the ALU of a microcomputer. An example
is Intel 8085 microprocessor. In addition to the microprocessor, a microcomputer will have the
following:
2.3.2 MICROCONTROLLER
It is possible to integrate on a single chip all of the blocks that are needed in a microcomputer, except
the I/O devices. Such a chip is termed a microcontroller. An example is Intel 8751. A few of the blocks
on the 8751 are:
- 4K X 8 bits of EPROM;
- 128X8 bits of RAM;
-4 numbers of 8 bit I/O ports.
It also has timers and facility for serial communication. Microcontrollers are used in a variety of
instruments like washing machines, printer sharer, computer keyboards, etc. They are basically used
in equipment where the size and cost are required to be very small compared to a microcomputer, and
where lots of complex calculations are not needed.
In this section we describe the evolution of computer languages starting from the machine language
to the high-level languages. Their merits and demerits are discussed at length.
A program can be written using only Os and Is. The data can also be specified using only Os and Is.
Such a program is called machine language program. Machine language was the first in the
evolution of computer programming languages. Computer directly understands a program written in
the machine language. In fact, even to this day, basically computers understand only the Os and Is.
14
Disadvantages of machine language program: Writing a program in machine language has the
following drawbacks.
■ It is very tiresome to work with and highly error prone. While writing the program, a 1 and 0 can get
interchanged due to typographical error. But then it is very difficult to locate it for correction.
o ■ By a glance through the program, it is very difficult to visualize the function of the program. In
fact, it is very difficult to make out whether a particular bit sequence is an instruction in the pro¬
sE gram, or a data value, or the output result. This is because data, result, and an instruction are rep¬
resented using Os and Is in machine language.
2 ■ The same program does not work on another computer by a different manufacturer. This is
.2 because machine language is different for different computers. For example, on one computer the
S
w bit sequence 10100110 may mean ‘add two numbers’, and may mean ‘subtract two numbers’ on
§ another computer. In other words, a program written in machine language is said to be ‘not
CO
0 portable’.
H ■ To write a program in machine language one must be highly conversant with the organization and
architecture of the computer system being used.
Advantages of machine language program: The only advantages of writing in machine language are:
■ Machine language program is executed faster than a program written in a high-level language
(high-level language is discussed a little later).
■ A translator like compiler is not needed and so results in a cheaper computer system.
To conclude, machine language is rarely used nowadays, except where very high-speed execution is
required. It is also used in cheap microcomputer systems.
The next development in the evolution of computer languages was the assembly language. Assembly
language uses mnemonics in place of a sequence of Os and Is. For example, to add register A and B
in a particular computer, assembly language uses the mnemonic ‘ADD B’ in place of 10000000. Also,
assembly language uses symbolic names to denote addresses and data. A number of such examples are
dealt with in the successive chapters. Thus writing a program in assembly language has advantages
over writing the same in a machine language.
■ The same program does not work on another computer by a different manufacturer. This is because
assembly language is different for different computers. For example, on one computer the mnemonic
‘ADD E’ may mean ‘add contents of A register and E register’, and may have no meaning at all on
another computer. In other words, a program written in assembly language is also not portable.
■ To write a program in assembly language one must be highly conversant with the organization and
architecture of the computer system being used.
■ An assembler, which is a translator program, is needed for translating the assembly language pro¬
gram into machine code. But each assembly language instruction is translated into only one
15
instruction in the machine language. As such the assembler program is not a very huge one, and
so is quite cheap.
Advantages of assembly language program: The advantages of writing in assembly language are:
Tl
■ It is less tiresome to work with and much less error prone. While writing the program, if a typo¬
graphical error occurred due to oversight, it is much easier to locate it for correction. In most
I
2.1
■
cases, the assembler program detects it.
By a glance through the program it is much easier to visualize the function of the program.
0)
s
■ As far as speed of execution is concerned machine language and assembly language have equal
?£
3
speed of execution. Thus assembly language program is also executed faster than a program writ-
ten in a high-level language (high-level language is discussed next).
=2
To conclude, assembly language is rarely used nowadays except where very high-speed execution is
required. However, it is widely used in microcomputer systems. Both machine language and assem¬
bly language are termed as low-level languages.
The next development in the evolution of computer languages is the high-level language. Some exam¬
ples for high-level languages are as follows:
High-level languages use English-like language, but with less words and fewer ambiguities. For
example, in English the word ‘can’ can have different meanings based on the context. It is possible to
use it as a noun or a verb in a sentence. But high-level languages use only a few words, with each word
having a unique meaning.
■ A compiler, which is a translator program, is needed for translating the high-level language pro¬
gram into machine code. But each high-level language instruction is translated into more than one
instruction in the machine language. As such the compiler program is a huge one and so is quite
expensive.
■ The code generated by the compiler might not be as compact as written straightaway in low-level
language. Thus a program written in high-level language usually takes longer to execute.
Advantages of high-level languages: But writing a program in a high-level language has the
following advantages over writing the same in a low-level language.
16
■ It is very easy to learn a high-level language and is a pleasure to work with. While writing the
program, if a typographical error occurred due to oversight, it is much easier to locate it for
correction. Most of the times the compiler detects it.
■ By a glance through the program it is easy to visualize the function of the program.
■ The programmer is not required to be familiar with the organization or the architecture of the com¬
s ■
puter system being used. Thus even school children can work with high-level languages.
The same program works on any other computer, provided the other computer has a compiler for
a>
o the language in which the program is written. In other words the programs written in high-level
s languages are portable.
2 ■ Productivity is enormously increased.
.2
s
w To conclude, high-level languages are almost always used nowadays except where very high-speed
oo
s execution is required.
*
H
1. What is the difference between a calculator and a computer? What makes a computer faster
than a calculator?
2. Briefly explain the blocks that constitute a stored program computer.
3. Name a few input and output devices and their role in a computer.
4. Explain the terms ‘random access’ and ‘sequential access’. Identify the type of access in
the following:
a. RAM,
b. EPROM,
c. ROM,
d. Magnetic tape.
5. Distinguish between main memory and secondary memory. Identify the memory type for
the following:
a. Magnetic disk,
b. RAM,
c. Magnetic tape,
d. EEPROM.
6. Describe the features of various types of ROMs.
7. Describe the role of ALU and control unit in a computer.
8. What is the need for input ports and output ports?
9. Differentiate microprocessor, microcontroller, and microcomputer.
10. Give an overview of the evolution of computer languages, highlighting the merits and
demerits of each type of language.
3 Number
Representation
■
A computer is quite often used for performing computations on numbers. The types of numbers on
which the computer is required to perform calculations are: unsigned integers; signed integers; and
signed floating point numbers.
In a digital computer everything, whether it is some data, result, or an instruction, has to be rep¬
resented using only Os and Is. This is because the digital computer basically uses transistors that are
made to work in the ‘Off’ state or the ‘On’ state. The ‘Off’ state is generally represented as the 0 state
and the ‘On’ state is represented as 1 state. In computers Os and Is are called ‘bits’. A bit stands for
‘Binary digiT’.
—
Unsigned binary integers are numbers without any ‘+ ’ or ‘ ’ sign. Examples are: number of books
in a library, runs scored by a batsman in a cricket match, etc. Obviously they are unsigned integers
like 34,567 and 87. These numbers have to be represented in a computer using only binary notation
or bits. Numbers are represented in a computer using a fixed size, like 4, 8, 16, 32 bits, etc. If num¬
bers are represented in a computer using 8 bits, it is said that the computer uses 8-bit word size. Gen¬
erally, word sizes are a power of 2. In the early days of computing, word sizes were generally 4 bits.
These days, it is generally 32 bits or 64 bits. A tabular column of decimal numbers and their equiva¬
lent in unsigned binary is shown in the following, assuming a word size of 4 bits.
18
In other words, when the word size is only 4 bits, it is not possible to represent a number like 223.
The minimum word size has to be 8 bits to represent the number 223.
Signed integers are numbers with a ‘+ ’ or ‘-’ sign. An example is the list of temperatures (correct to
nearest digit) in various cities of the world. Obviously they are signed integers like +34, -15, -23,
and +17. These numbers along with their sign have to be represented in a computer using only binary
notation or bits.
There are various ways of representing signed numbers in a computer. The simplest way of repre¬
senting a signed number is the sign magnitude (SM) method.
In this method of representing signed numbers, the most significant bit (MSB) indicates the sign of
the number. The number is treated as positive, if the MSB is 0. It is negative if the MSB is 1. The other
bits indicate the magnitude of the number.
A tabular column of signed decimal numbers and their equivalent in SM notation follows assuming
a word size of 4 bits.
Number SM notation
+5 0 101
-5 1 101
+3 0011
-3 1 Oil
+7 0 111 Most positive number, which is +(23 - 1)
+0 0 000 Notation for +0
-0 1 000 Notation for -0
-7 1 111 Most negative number, which is -(23 - 1)
19
From this table, it is obvious that if the word size is n bits, the range of numbers that can be repre¬
sented is from —(2" 1 — 1) to + (2" 1 — 1). A table of word size and the range of SM numbers that
can be represented is shown in the following.
Notice that the bit sequence 1101 corresponds to the unsigned number 13, as well as the number —5 a
in SM notation. Its value depends only on the way the user or the programmer interprets the bit sequence. §
A number is represented inside a computer with the purpose of performing some calculation using
that number. The most basic arithmetic operation in a computer is the addition operation. The follow¬
ing examples show the result of adding two signed numbers that are represented inside the computer
in SM notation.
Example 1: Add the numbers ( + 5) and (—3) using a computer. The numbers are assumed to be
represented using 4-bit SM notation.
1111 <- Carries generated during addition
0101 <— (+5)
+ 1011 <- (-3)
Example 2: Add the numbers (—4) and (+2) using a computer. The numbers are assumed to be rep¬
resented using 4-bit SM notation.
0000 <- Carries generated during addition
1100 <-(-4)
+ 0010 <-(+2)
1110 +- Sum
The computer instead of giving the correct answer of -2 = 1010, has given the wrong answer of -6
= 1110! To give the correct answer the computer will have to manipulate the result of addition quite a lot.
Thus to conclude: SM notation is very simple to understand because it is very similar to the conven¬
tional way of representing signed numbers. But its disadvantages are:
■ There are two notations for 0 (0000 and 1000), which is very inconvenient when the computer
wants to test for a 0 result.
■ It is not convenient for the computer to perform arithmetic.
Hence, SM notation is generally not used to represent signed numbers inside a computer.
20
3 .2.2 1 's COMPLEMENT NOTATION
This is another method of representing signed integers in a computer. In this method also, the MSB
indicates the sign of the number. The number is treated as positive, if the MSB is 0. It is negative if
the MSB is 1. However, the other bits directly indicate the magnitude of the number only when the
number is positive. If the number is negative, the other bits signify the 1’s complement of the magni¬
o tude of the number.
0 A tabular column of signed decimal numbers and their equivalent in 1’s complement notation is
o shown below, assuming a word size of 4 bits.
a _
E Number 1’s complement notation
.2
S +5 0 101
10 -5 1 010
+3 0011
0 -3 1 100
H +7 0 111 Most positive number, which is +(23- 1)
+0 0 000 Notation for +0
-o 1111 Notation for -0
-7 1 000 Most negative number, which is — (23 - 1)
From the given table, it is obvious that if the word size is n bits, the range of numbers that can be
represented is from -(2" 1 - l)to +(2" 1 - 1). A table of word size and the range of l’s complement
numbers that can be represented is shown.
Word size Range for 1’s complement numbers
4 -7 to +7
8 -127 to +127
16 -32767 to +32767
32 -2147483647 to +2147483647
In other words, when the word size is only 4 bits, it is not possible to represent a number like -123.
The minimum word size has to be 8 bits to represent the number -123.
Notice that the bit sequence 1101 corresponds to the following values in different notations.
0010 = + 2
Example 2: Add the numbers (—4) and ( + 2) using a computer. The numbers are assumed to be rep¬
resented using 4-bit 1’s complement notation. z
0010 <— Carries generated during addition
3
1011 <-(-4) ?r
+ 0010 <—(+2) 3
a
3
1101 <- Sum
£
After the addition of the final carry, the result remains as 1101. This is —2, which is the correct □
answer. If it is not clear, as to how 1101 is —2, the following explanation should make it clear. In
1 101 the MSB is a 1. It means the number is negative. Then, the remaining bits do not provide the
magnitude directly. To solve this problem, just consider 1’s complement of 1 101. l’s complement of
1 101 is 0 010, which is +2. Thus, 1 101, which is l’s complement of 0 010 is -2.
Thus to conclude: 1’s complement notation is not very simple to understand because it is very much
different from the conventional way of representing signed numbers. The other disadvantage is that
there are two notations for 0 (0000 and 1111), which is very inconvenient when the computer wants
to test for a 0 result.
But, it is quite convenient for the computer to perform arithmetic. To get the correct answer after
addition, the result of addition and final carry has to be added up.
Hence, 1’s complement notation is also generally not used to represent signed numbers inside a
computer.
This is yet another method of representing signed integers in a computer. In this method also, the MSB
indicates the sign of the number. The number is treated as positive, if the MSB is 0. It is negative if
the MSB is 1. However, the other bits directly indicate the magnitude of the number only when the
number is positive. If the number is negative, the other bits signify the 2’s complement of the magni¬
tude of the number.
Thus a positive number has the same representation in SM, l’s complement, and 2’s complement
notations. Only negative numbers are represented differently in these notations.
+5 0 101
-5 1 Oil
+3 0011
-3 1 101
+7 0 111 Most positive number, which is +(23 - 1)
0 0 000 Notation for 0
-7 1 001
-8 1 000 Most negative number, which is -23
22
A tabular column of signed decimal numbers and their equivalent in 2’s complement notation is
shown below, assuming a word size of 4 bits.
Notice that there is a single notation for 0, immaterial of whether it is +0 or —0. One may feel that
0 000 is +0 only, as the MSB in this case is a 0. But then, the notation for —0 should be the 2’s com¬
plement of 0 000, which is 1111 + 1 = 0 000 ignoring the carry.
If it is not clear, as to how 1000 is —8, the following explanation should make it clear. In 1 000 the
o MSB is a 1. It means the number is negative. Then, the remaining bits do not provide the magnitude
0
directly. To solve this problem, just consider 2’s complement of 1 000. 2’s complement of 1 000 is
0 111 + 1 = 7 + 1 = +8. Thus, 1 000 is -8.
2a. An alternative way of arriving at the same conclusion is as follows. Shown in the following discus¬
E sion is the 2’s complement notations for +5, +6, +7 and —5, —6, —7. Then a question mark is placed
.2
s to indicate the value of 1 000. Using the rules of mathematical induction, 1 000 can be interpreted as
in
either +8 or —8. However, +8 is not correct, as the MSB is 1. Thus, 1 000 has the value —8 in 2’s
complement notation.
«
H
0 101 = +5 1 Oil = -5
0 110 = +6 2’s complement \ Q10 = —6
0 111 = +7 1 001 = -7
1 000 = ? 1 000 = ?
Thus, in 2’s complement notation an extra negative number can be represented compared with SM
or l’s complement notation. This is because, in 2’s complement notation, there is only a single nota¬
tion for zero, whereas in SM and 1’s complement notations there are two notations for 0.
From this, it is obvious that if the word size is n bits, the range of numbers that can be represented
is from -2"_1 to +(2',_1 - 1). A table of word size and the range of 2’s complement numbers that
can be represented is shown next.
—
In other words, when the word size is only 4 bits, it is not possible to represent a number like 123.
The minimum word size has to be 8 bits to represent the number 123. —
Notice that the bit sequence 1101 corresponds to the following values in different notations.
Its value depends only on the way the user or the programmer interprets the bit sequence.
Examples for addition of two signed numbers represented using 2’s complement notation are
shown next.
23
Example 1: Add the numbers ( + 5) and (—3) using a computer. The numbers are assumed to be rep¬
resented using 4-bit 2’s complement notation.
1101 (— Carries generated during addition
0101 V- (+5)
+ 1101 <— (—3)
z
0010 <- Sum 3
Notice that the computer straightaway gives the correct answer of +2 = 0010. ?r
TJ
Example 2: Add the numbers (—4) and ( + 2) using a computer. The numbers are assumed to be rep- ®
w
resented using 4-bit 2’s complement notation. 3
It may be necessary quite often to represent fractions. For example, it may be needed to represent
inside a computer a value like +0.875 or —0.875. This topic is discussed next.
—
Let us say we have the 4-bit number 1001. Then, what is its value? It has the value 9, 1, —6, or
—7 depending on whether it is unsigned, SM number, l’s complement number, or 2’s complement
number, respectively. Thus the value a given sequence of bits will have depends purely on the inter¬
pretation of the user.
So far, only integers were discussed. Thus, a number like 1000 was assumed to have the binary
point at the extreme right of the bit sequence. To represent signed fractions, it is necessary to assume
the binary point just after the MSB in the bit sequence.
The MS bit specifies the sign of a number. But then, how to specify the binary point inside a
computer? This problem is easily solved if all numbers are required to have the binary point at the
same position in the bit sequence. For example, if all numbers are required to have the binary point
24
just after the MS bit, it is just assumed that it exists there! Such numbers where the binary point is
assumed to be at a fixed position in the bit sequence are called fixed-point numbers.
Just as there are unsigned, SM, l’s complement, and 2’s complement integers, there are also
unsigned, SM, l’s complement, and 2’s complement fractions. Unsigned fractions will have the
assumed binary point at the extreme left. SM, 1’s complement, and 2’s complement fractions will have
this imaginary binary point just to the right of the MS bit.
o If the imaginary point is at the extreme right, then the number is an integer. If the imaginary binary
0
point is at the extreme left for an unsigned number, the number is an unsigned fraction. If the binary
§ point is to the immediate right of the MS bit, the number is a signed fraction. If the binary point is in
a the middle of a bit sequence, the number has an integer and a fractional part.
.2o Only 2’s complement fractions, which are the most common, are discussed next.
S
in
From the above, it is obvious that if the word size is n bits, the smallest magnitude non-zero
fraction that can be represented is 2-(«-0 A table of word size and the smallest magnitude fraction
that can be represented is shown in the following.
In other words, when the word size is only 4 bits, it is not possible to represent a number like
-0.0123 quite accurately. The minimum word size has to be 8 bits to represent the number -0.0123
somewhat accurately. The accuracy is very much improved if the word size is further increased.
25
■ 3.4 SIGNED FLOATING POINT NUMBERS
In our daily life, we work many times with real numbers, which will have an integer part and a frac¬
tional part. An example is 23.456. But this notation is not convenient for representing very large mag¬
—
nitude numbers, like 123456789.4567. This same number can be more conveniently represented in
scientific notation as -1.2345678 X 10+08. But this actually stands for -123456780. So there is an c
error of 9.4567, which forms a very small percentage error. §.
However, the advantage of this scientific notation is that a very much larger number than ? ®
-1.2345678 X 10+08 can be represented using same number of digits. For example, +9.9999999 X 5
10+" is the largest magnitude positive number using same number of digits.
Similarly, the way we represent real numbers in our daily life is not convenient for representing J
very small numbers, like +0.00000045678912. This same number can be more conveniently repre¬ 0
&
sented in scientific notation as +4.56789 X 10 07. But this actually stands for +0.000000456789. So
there is an error of 0.00000000000012, which forms a very small percentage error.
However, the advantage of this scientific notation is that a very much smaller magnitude number
than +4.56789 X 10-07 can be represented using same number of digits. For example, —1.00000 X
10_" is the smallest magnitude negative number using the same number of digits.
Using fixed-point notation, when the word size is as large as 32 bits, the largest magnitude signed
integer that can be represented is approximately ±2 X 10+09 only. And the smallest non-zero magni¬
tude signed fraction that can be represented is approximately ±0.5 X 10-09 only when the word size
is 32 bits.
So to represent very large, or very small signed numbers, with a word size of say, 32 bits, a com¬
puter uses floating point notation. This may result in small errors, but the error will be negligible, if
large number of digits is reserved for the fractional part. But the important thing is that much larger
range of numbers can be represented than is possible with fixed-point notation.
A floating-point number like +1.23 X 104 can be written in several equivalent ways as +12.3 X
103 or +0.123 X 105.
Similarly, there are several ways of representing floating point numbers in a computer also. As an
example, in the 32-bit floating point notation used in Intel 8087 numeric co-processor, the largest
magnitude number is approximately ± 5.12 X 10+38and the smallest magnitude number is approxi¬
mately ±8 X 10-39. The interested reader can refer to the book ‘Advanced Microprocessors and IBM-
PC Assembly Language Programming’ by Udaya kumar and Umashankar (Tata McGraw Hill Publi¬
cation) for more details.
1. Give examples for unsigned integers, signed integers, and floating point numbers.
2. Explain how unsigned numbers are represented in a digital computer. If such numbers are
represented using 16 bits, what will be the range?
3. Explain how signed numbers are represented in a digital computer using SM notation. If such
numbers are represented using 32 bits, what will be the range?
4. Explain how signed numbers are represented in a digital computer using l’s complement
notation. If such numbers are represented using 8 bits, what will be the range?
5. Explain how signed numbers are represented in a digital computer using 2’s complement
notation. If such numbers are represented using 64 bits, what will be the range?
26
6. Fill up the following table with the values of the bit sequences in unsigned integer, SM inte¬
ger, etc.
0110 1100 ? ? ? ? ?
11100110 ? ? ? ? ?
0 1011 0011 ? ? ? ? ?
0
7. What are the advantages of 2’s complement notation over SM and 1’s complement notations
2
a in representing signed numbers?
E 8. Flow can fractions be represented in a computer using fixed-point notation? Explain the 2’s
.2 complement method of representing fixed-point fractions.
S
in
«
H
4 Fundamentals of
Microprocessor
■ History
of microprocessors
■ Description of 8085 pins
•Vcc, and Vss pins
• AD7_Q pins
• A15_8 pins
• ALE pin
• IO/M* pin
m Programmer's view of 8085: Need for registers
•Meaning of programmer's view
■ Accumulator or register A
■ Registers B, C, D, E, H, and L
■ Questions
■ 4. 1 HISTORY OF MICROPROCESSORS
The history of microprocessors dates back to only the year 1971! By this time, the integrated circuits
technology was so much developed that it was possible to integrate on a single chip the logic of con¬
trol unit and ALU. Intel Corporation in the USA announced the first microprocessor in 1971. It was
the Intel 4004. It was a 4-bit processor intended for making programmable calculators.
A 4-bit microprocessor receives 4 bits of information from outside the microprocessor, performs the
necessary processing on it, and then sends out of the microprocessor a 4-bit result. It will have an ALU that
can perform operations on 4-bit numbers. It is said that a 4-bit microprocessor has a word size of 4 bits.
With only 4 bits as the word size, the 4004 could only represent signed numbers in the range
— 8 to +7, which is indeed very small. Hence, it was not really of practical use for arithmetic
calculations. However, it found applications in controlling devices. For example, it could be used to
switch on or switch off a motor. This was achieved by sending out logic 1 or logic 0 from the
microprocessor, and making use of a suitable driver circuitry.
Similarly, it could be used for checking if the temperature of a water bath is above or below a
certain desired temperature. This was done by the reading of the logic 1 or 0 value that was developed
28
by a temperature sensor after proper signal conditioning. The microprocessor can receive this infor¬
mation on a single pin to check the temperature.
Next in the evolution was the Intel 8008, the first 8-bit microprocessor. This was in the year 1972. This
was soon followed by Intel 8080, also an 8-bit microprocessor. Intel 8080 was the first commercially
popular 8 bit microprocessor. With 8 bits as the word size, it could represent signed numbers in the
range of — 128 to + 127. This is also not a good enough range for performing arithmetic calculations.
s Thus, the 8080 also was used only for control applications.
a>
o By this time, some other manufacturers of integrated circuits who were till then manufacturing only
s
a components like logic gates, flip flops, shift registers, etc. got fascinated by this new product, and they
2 also released to the market their designs of microprocessor. Notable among them was the 6800 from
.2
s Motorola and Z-80 from Zilog.
in
oo The 6800 design was based on the popular PDP-11 minicomputer of those days. This chip became
s quite popular for control applications. Z-80 was basically an improvement over the 8080, with a large
Jj number of new and powerful instructions. This chip also became very popular in the field of control
H
applications.
In the meanwhile, Intel came out with their improvement over the 8080. This was the Intel 8085.
This was only a hardware improvement over 8080. As far as instruction set was concerned, there was
basically no improvement. However, the 8085 also became very popular. The 6800, Z-80, and 8085
are all only 8-bit microprocessors, and they rule the 8-bit microprocessor world in control
applications.
Around the year 1974, Intel released Intel 8048 family, their first microcontroller family. This was
later followed by the Intel 8051 series of microcontrollers. Still later Intel released 8096 family of
microcontrollers. In the present-day world, a large number of microcontrollers are being used in a
variety of consumer products.
Around 1978, Intel released 8086, the first 16-bit microprocessor. With 16-bit word size, it was
possible to represent signed numbers in the range of —32,768 to +32,767, which is quite a decent
range for performing arithmetic calculations. As such, this processor became very popular not only for
control applications, but also for number crunching operations. Not to be outdone, Motorola came out
with 68000, their 16-bit processor. Zilog released Z-8000, again a 16-bit processor. These are the most
popular 16-bit processors.
Then IBM Corporation released the first personal computer (IBM-PC) using 8088 as the CPU
developed by Intel. The Intel 8088 has the same instruction set as the 8086. However, it can only
receive 8 bits at a time from outside, and send out 8 bits at a time. Internally it is capable of perform¬
ing 16-bit arithmetic. This chip was designed to make use of the processing power of a 16-bit ALU,
while being capable of communicating with the common 8 bit peripheral devices of those days. The
IBM-PC became extremely popular, and has now pervaded all walks of our lives. IBM released IBM-
PC/XT a little later, using the same Intel 8088. This computer had a hard disk drive in addition to the
floppy drives of the earlier PC.
From then on, the development of newer and more powerful processors has been very swift. Intel
released 80186, which was mainly used in embedded applications. Then Intel released 80286, which
was used in IBM-PC/AT. The processor could be used in multi-user, multi-tasking environment. How¬
ever, the 80186 and 80286 were still 16-bit processors. In the meanwhile Motorola released 68010,
also a 16-bit processor. Around this time Zilog lost the race to capture a major share in the market,
with Intel and Motorola emerging as the forerunners to the top spot.
In the early 80s, Intel released the 32-bit processor, the Intel 80386. With 32-bit word size, it was pos¬
sible to represent signed numbers in the range ± 2 X 109, which is quite a large range for performing
arithmetic calculations. If floating point notation is used, it can represent much larger numbers. As such,
this processor became very popular as the CPU in computers for number crunching operations. Not to
29
be outdone, Motorola came out with 68020, their 32-bit processor. Intel released 80486, which was basi¬
cally a 80386 processor and 80387 numeric co-processor on a single chip. Motorola released 68030. In
the early 90s Intel deviated from their naming tradition and released 80586 by the name Pentium proces¬
sor. It is extremely fast in performing arithmetic calculations and executing instructions. The Pentium 4
released in 2000 has 42 Million transistors worked with a clock frequency of 1.5 GHz and is rated for
Tl
1 500 MIPS (Million instructions per second)
To conclude, the present-day computers based on microprocessors are already faster than the mini l
computers and sometimes the main frame computers of yesteryear, and they are available at a small 5 3
fraction of the cost of such main frame computers. We can hope to see the release of still more g §
powerful and at the same time cheaper microprocessors and microcontrollers in the future.
In this book, Intel 8085 processor will be discussed at great length. Armed with that knowledge,
Zilog Z-80, Motorola 6800 processors, and Intel 8051 microcontroller will be discussed quite exhaus¬
?
o
—
•ISa
tively in a few chapters.
Intel 8085 is fabricated as a 40-pin DIP IC. DIP stands for ‘dual inline package’. It means the pack¬
age will have pins on only two sides, 20 on each side in this case.
Intel manufactures 8085 in several versions, like 8085A, 8085AH, 8085AH-2, and 8085AH-1. The
8085A is fabricated using NMOS technology. It is a variant of MOS (metal oxide semiconductor)
technology. It uses n channel silicon-gate process. The AH series are more expensive processors,
which use high-density MOS (HMOS) for fabrication. They typically consume 20% less power com¬
pared to the A series. The recommended internal clock frequency for the various types of 8085 is as
follows.
Basically they are same, the only difference being in the speed of operation, or the technology used
for fabrication. In this discussion, they are simply referred to as 8085. The pin diagram of 8085 is as
shown in fig. 4.1.
In fig. 4. 1, the pin number and its associated function is indicated for each of the 40 pins. For exam¬
ple, the diagram indicates that pin number 20 is the Vss pin, which should be connected to ground, and
40 is the Vcc pin, which should be connected to +5 V dc supply. A user definitely needs this informa¬
tion, when he is required to wire up a microprocessor in his circuit. However, for the purpose of under¬
standing the working of the processor, only the function of the various pins need to be known. There
is no need to know which pin number performs what function. For example, to understand the work¬
ing of 8085 microprocessor, the user should be aware that it needs a power supply of +5 V dc and
ground. It is not necessary to know the pin numbers to which +5 V dc and ground are to be connected.
A simplified diagram that does not indicate the pin numbers, but only indicates the function of the
various pins, can be called a functional pin diagram. Fig. 4.2 partly provides the functional pin
diagram of 8085. It shows only 22 pins. The remaining portion is explained later in the text.
30
Xi 1 40 Vcc
X2 2 39 Hold
Reset out 3 38 HLDA
SOD 4 37 Clk out
8M SID 5 36 Reset in
o Trap 6 35 Ready
S
a RST7.5 7 34 IO/M*
s RST6.5 8 33 SI
S
in
RST5.5 9 32 RD*
00
s« INTR
INTA*
10
11
8085
31
30
WR*
ALE
H 29
A DQ > 12 So
AD, 13 28 A15
AD2 14 27 A14
AD3 15 26 An
AD4 16 25 A12
AD5 17 24 A11
AD6 18 23 Aio
AD7 19 22 A9
Vss 20 21 a8 Fig. 4.1
Pin diagram of 8085
vco <ÿ>AD7-0
Gnd
A15-8
8085 ALE
RD*
Fig. 4.2
WR*
IO/M*
Functional pin diagram
of 8085 (incomplete)
Intel 8085 communicates with the outside world with 8 bits at a time. When the 8085 desires
to receive 8-bit information, it receives it on the eight pins AD7, AD6, ... ,AD0. These eight pins are
31
collectively called AD7.0. Their pin numbers are 19, 18, ... , 12, respectively. When the 8085 desires
to send out 8-bit information, it sends it out on these same pins.
Thus, these pins are used for receiving as well as sending out information. In other words, they are
bi-directional pins. These pins are used for:
■ Receiving a data byte (byte = 8 bits) from an input port or from memory. l
■ Sending out a byte to an output port or to memory Hi
n <t
How can the same lines be used for receiving information as well as sending out information? This I|
is not at all a problem, because at any instant of time, the processor is either receiving or sending out
information, but not both. They are decided by the signals sent out on RD* and WR* pins, which are •Iss.
0
discussed next.
RD* and WR* Pins : /?D*stands for ‘Read*’. A ‘*’ in this book after a signal name indicates that the
signal is an active low output. Thus RD* is an active low output signal. Another popular convention
is to denote RD* as RD.
RD* is pin number 32 of 8085. When the control unit in the 8085 sends out logic 0 on this pin, it
is indicating that it desires to read information. Then the information comes to the processor on the
AD7_O pins. In other words, when RD* = 0, AD7.0 pins are input pins. When RD* = 1, the 8085 is
not interested in reading information.
WR* stands for ‘Write*’. WR* is an active low output signal. WR* is pin number 31 of 8085.
When the control unit in the 8085 sends out logic 0 on this pin, it is indicating that it desires to write
information. Then the information is sent out by the processor on the AD7_0 pins. In other words, when
WR* = 0, AD7_0 pins are output pins. When WR* = 1, the 8085 is not interested in writing or send¬
ing out information.
Thus the action performed by the 8085 for various combinations of RD* and WR* are as follows.
If the control unit in the 8085 sends out logic 1 on both RD* and WR* simultaneously, it means
that the 8085 is not interested in reading or writing at that moment. As an example, when the 8085 is
busy with some internal processing it sends out logic 1 on both RD* and WR*.
The control unit will never send out logic 0 on both RD* and WR* simultaneously. If such a thing
ever happens, it means the processor has gone crazy, and you better throw it!
mi mi nil nil
The processor sends out the address in binary as shown previously. But, it is very inconvenient to
write down addresses using 16 bits, which is too long. Thus, it is preferable to write down address in
a shorter form. The same addresses in decimal could be written as 0, 1, 2, ... , 65,535. But converting
a binary address to decimal and vice versa is quite complex. Thus, it is preferred to write down the
same addresses in hexadecimal notation as 0000H, 0001H, 0002H, ... , FFFFH. In this book, the
addresses are generally shown in hex for convenience. But, the reader should always remember that
the processor sends out the address in binary on the 16 pins A15_8 and AD7_0.
Thus memory in a 8085-based microcomputer may be conceptualized as shown in Fig. 4.3.
If the processor is interested in reading information from memory location whose address in hex is
0002H, it sends out 00H on A15.8 and 02H on AD7_0. Then the processor sends out RD* as logic 0 and
WR* as logic 1. This results in 1DH, the contents of location 0002H, coming to the processor from
memory on AD7_0.
Similarly, if the processor is interested in writing the information 89H to memory location whose
address in hex is 4567H, it sends out 45H on A15.8 and 67H on AD7_0. A little later, the processor sends
33
Memory address
in hexadecimal
I
0000H
000 1H Tl
0002H 1DH
l
Hi
n n
4567H I|
•Is
89
a
w
out 89H on AD7.0 with RD* as logic 1 and WR* as logic 0. This results in 89H getting written to the
location 4567H.
Thus, it is seen that AD7_0 is used for sending out LS byte of address, as well as sending or receiv¬
ing 8 bits of data. In fact, AD in AD7_0 stands for ‘address data’. This multiplexing of LS byte of
address and data has been done in 8085 to save on the number of pins required for the chip.
If LS byte of address and data are multiplexed on AD7.0, how to know whether information on AD7_0
is address or data? This is solved by the ALE signal sent out by the 8085. ALE stands for ‘address
latch enable’.
At this moment, it is enough to understand that if this signal is at logic 1, 8085 is sending out address
information on AD7_0. In other words, when ALE = 1, AD7.0 pins act like address pins A7_0. If ALE is
at logic 0, the 8085 is sending out or receiving information. In other words, when ALE = 0, AD7.0 pins
act like data pins D7_0.
Information is received on AD7.0 when ALE = 0, if RD* = 0 and WR* = 1. Then AD7.0 pins act
like the input pins D7_0. Information is sent out on AD7.0 when ALE = 0, if WR* = 0 and RD* = 1.
Then AD7 0 pins act like the output pins D7_0.
We have seen that 8085 receives 8-bit information on AD7.0 from memory or an input port. When it
comes inside the microprocessor, where does it reside inside the processor? For this purpose, there is
what is called ‘register’ to store such information inside. A register is nothing but a group of flip-flops,
where each flip-flop can store a bit of information. The size of such a register in 8085 has to be 8 bits,
to store 8 bits of information.
It is necessary to have atleast one such register in any brand of microprocessor. The advantages of
a register over a memory location are as follows.
■ The contents of a register can be accessed much faster by the microprocessor, compared with the
contents of a memory location (by ‘access’ we mean ‘read or write’).
■ Instructions involving register operands will be shorter in length and are executed faster compared
with instructions involving memory operands.
However, the disadvantages of a register over a memory location are as follows.
■ During an interrupt service subroutine (ISS), the register values will have to be stored in an area
of memory called stack. Upon return from the ISS, the register values have to be restored from the
stack. This takes away the speed advantage that was mentioned earlier.
■ If there are too many registers, they occupy lot of real estate on the chip, thus reducing the real
estate available for the control unit and ALU.
■ In instructions involving register operands, the number of bits that are free to specify the opera¬
tion become reduced, thereby reducing the number of possible instructions.
The reader will be in a position to appreciate these advantages and disadvantages much better after
a study of a few of the successive chapters.
In view of the earlier discussion, most microprocessors provide only a few registers on the chip.
The registers provided on 8085 are A, B, C, D, E, H, and L. These are the general-purpose registers
35
(GPRs). In addition to these registers there are a few special purpose registers (SPRs). These are
discussed later.
A programmer just writes a program and executes it to solve a problem. He is not bothered to know lu
the pins of a processor, or its internal architectural details. To write a program, he needs to know only 3
a little about the processor. For example, he needs to know the registers that are available to the 3
programmer, and the instructions the processor understands to write his program. Thus the program¬ ■o ST
=
mer’s view of a processor, is the simplest description of the processor, without going through many
•Iss.
details. It is similar to the driver’s view of a car. From a driver’s viewpoint there are only a few parts w
in a car, like steering wheel, brake pedal, etc. He need not even know that there should be an engine! 5
He can still drive a car with so much of ignorance! But in reality a car will have probably a thousand
parts or more.
In this chapter, the programmer’s view of 8085 is discussed. To keep it simple, the complete pro¬
grammer’s view is not discussed. From the programmer’s view, 8085 just consists of a few registers,
as shown in Fig. 4.4. They are discussed next.
A
B C
D E
H L
8 bits 8 bits Fig. 4.4
Programmer’s view
of 8085 (incomplete)
Register A is a 8-bit register in 8085. Register A is quite often called as a Accumulator. Among the
8-bit GPRs of 8085, this is the most important. In an arithmetic operation involving two operands, one
operand has to be in this register. And the result of the arithmetic operation will be stored or accumu¬
lated in this register. This justifies the name ‘accumulator’ for this register. Similarly, in a logical
operation involving two operands, one operand has to be in the accumulator. Also, some operations,
like complementing and decimal adjustment, can be performed only on the accumulator.
There are more ways of putting information, as well as taking out information from accumulator
compared with the other registers. Technically speaking, there are more ways of addressing the
accumulator compared with the other registers.
36
■ 4.5 REGISTERS B, C, D, E, H, AND L
All these GPRS are 8-bits wide. They are less important than the accumulator. For example, there is
no instruction to add contents of D and H registers. Atleast one of the operands has to be in A. Thus
8 to add D and H registers, and to store the result in D register, the following have to be done.
2
o
2 ■ Move to A register the contents of D register.
a ■ Then add A and H registers. The result will be in A.
2 ■ Move this result from A register to D register.
.8
s
in
co It is possible to use these registers as pairs to store 16-bit information. Only BC, DE, and HL can
S form register pairs. The instructions of 8085, for example, cannot treat the contents of C and H regis¬
«j ters as a single 16-bit quantity. They are always treated as two separate 8-bit quantities.
The information in these register pairs can be interpreted as 16-bit data or 16-bit address of a mem¬
ory location, depending on the instruction being executed. For example, in the instruction LDAX B,
the contents of BC pair are treated as 16-bit memory address. In the instruction DAD B, the contents
of BC pair are treated as 16-bit data. These instructions are described in the next chapter.
When they are used as register pairs in an instruction, the left register is understood to have the MS
byte and the right register the LS byte. For example, in BC register pair, the content of B register is
treated as the MS byte, and the content of C register is treated as the LS byte.
After the names A, B, C, D, E, why it has become H and L, instead of F and G? The reason is as
follows.
As far as register pairs are concerned, HL pair is the most important. Although 8085 is a 8-bit
processor, the designers have provided for 16-bit addition in the instruction set. In such a 16-bit addi¬
tion, one of the operands has to be in the HL pair. Also, there are more ways of addressing the HL pair
compared with the other register pairs.
In some 8085 instructions there will be a memory operand, indicated as M in an instruction. An
example is ‘MOV C, M’. This instruction, to be discussed in a later chapter, stands for ‘move to C
register from memory’. In this instruction, the address of memory location is understood to be speci¬
fied as the contents of H and L registers. Thus, M in an instruction always stands for contents of a
memory location whose address is the contents of H and L. H will have the high part or MS byte of
address, and L will have the low part or LS byte of address. So, after A, B, C, D, E it was named as
H and L.
Now that we have an idea of the programmer’s view of 8085, let us see how it can be used to solve
problems for us. To explain this aspect, let us consider a very simple problem.
Let us say, we have some 8-bit number in a symbolic memory location X. We want to compute its 2’s
complement and store the result in symbolic memory location Y, overwriting the previous contents of
memory location Y.
39
5.1.1 APPROACH METHODOLOGY
We need to have memory locations X and Y in the RAM area of the kit. The RAM space in the kit is
F800H to FFFFH. So let us arbitrarily choose X as memory location F840FI, and Y as memory
location F850FI.
Let us say, we have in memory location F840FI the 8-bit number 12FI (i.e. 0001 0010). To find its 2!
2’s complement, first of all we have to find its l’s complement, and then add 1 to it. The l’s comple¬ I
ment is EDIT Thus the 2’s complement becomes EEFI. Finally this result is stored in memory loca- a »
tion F850FI. This is diagrammatically indicated in Fig. 5.1.
s3
LDAF840H
F840H 12H 12H A
CMA
EDH A
INRA
STAF850H
F850H EEH A
Fig. 5.1
Finding 2’s complement
of a number
5.1.2 PROGRAM
There is nothing like a unique program for solving a problem. However, there will exist a program
that is optimal considering one or more of the following factors: ease of developing the program; size
of program; and speed of execution of program.
In this book, the programs are written giving highest priority for ease of writing the program, fol¬
lowed by size of program, and finally speed of execution is considered. This is because, speed of exe¬
cution is most important only in real-time applications, like firing a missile. It is required to minimize
size of program, only when the program is too big for the computer’s memory. In the present-day
world, memory has become very cheap of late. Ease of developing program is very important, because
manpower is becoming increasingly expensive.
Program to solve this simple problem is as follows.
Line 1 —LDA F840H: Finding the l’s complement is done by changing Os to Is and Is to Os. This is
8 a logical invert or negate operation. In a microcomputer the logic unit of the ALU can only perform
a>
o this operation. The ALU is inside the microprocessor. So, first of all the number from memory loca¬
sa tion F840H has to be brought to the microprocessor. But then, which register in 8085 should receive
s
.2
it? As described earlier, Accumulator is the most important 8-bit register, and so it is desired to receive
S it in Accumulator. This is achieved by executing the instruction ‘LDA F840H’.
in Here, LDA stands for ‘LoaD the Accumulator from memory location’. Just LDA is meaningless.
00
s LDA must be followed by a 16-bit memory address to make sense. We say, ‘LDA’ is the mnemonic
for Toad the accumulator from memory location’. Such mnemonics are designed to help us memorize
Jj
H the actual expansion.
Notice that the first line, as well as other lines of the program, have some more information writ¬
ten after a semicolon. Whatever follows a semicolon is to be treated as a comment only. It is not part
of the instruction. It is optional. However, it makes the program easier to understand.
Thus, in the first line ‘(A) = 12H’ is to be treated as a comment. In this book ‘()’ stands for ‘con¬
tents of’. Thus, ‘(A) = 12H’ is to be read as ‘contents of A becomes 12H’. In other words, LDA F840H
results in A register content becoming 12H.
Line 2— CMA: Finding the 1’s complement of the number in Accumulator is achieved by executing
the CMA instruction. Here, CMA is the mnemonic for ‘CoMplement the Accumulator’. The result of
this operation will again be stored in the accumulator. Thus, Accumulator value changes to EDH, as
indicated in the comment part of this line. In 8085, only Accumulator contents can be complemented.
In fact, that was the reason for bringing the number from memory location F840H to Accumulator,
and not any other register.
—
Line 3 INR A: Adding 1 to the number in the accumulator is achieved by executing the INR A
instruction. Here, INR A is the mnemonic for ‘INcRement the Accumulator’. The result of this oper¬
ation will again be stored in the Accumulator. Thus, Accumulator value changes to EEH, as indicated
in the comment part of this line. Thus, we now have in the Accumulator, the 2’s complement of the
number at memory location F840H.
Line 4—STA F850H: This instruction stores the Accumulator contents in memory location F850H.
Here, STA is the mnemonic for ‘STore the Accumulator contents in memory location’. Just STA is
meaningless. STA must be followed by a 16-bit memory address, like F850H, to make sense. Thus,
contents of memory location F850H changes to EEH, as indicated in the comment part of this line.
Now the objective of the program is achieved.
Line 5—HLT: ‘HLT’ is the mnemonic for ‘Halt the processor’. Thus, when this instruction is executed,
the processor halts. The reader may be wondering as to why this instruction is necessary at all. The
reader may think that once the job is done the processor has to stop without being told so. But, a
microprocessor keeps on executing instruction after instruction, unless asked to halt! It is very much
unlike a human being, who unless ordered to work, is always whiling away his time!
41
■ 5.2 ABOUT THE MICROPROCESSOR KIT
To execute the previous program we need a microcomputer. A microprocessor kit is a tool to develop
and test user programs. It is quite often called ‘|xP kit’ or simply ‘kit’.
2!
3
5.2.1 MAJOR COMPONENTS OF A KIT r >
Do we store our program in EPROM or RAM? The advantage of storing it in EPROM would be
that the program will not be lost, when power supply fails. But the disadvantage is that we have to use
an EPROM programmer to enter the program. If RAM is used, the program can be easily entered, but
the disadvantage is that it will be lost if power supply fails. But we decide to store it in RAM for the
following reasons.
og EAR MIC o o
at
W
£o ft
H
u
W
Z o o 7
U-
717
CQ
7
t"
-J
u
&.
71K
o
cu U
a =>(B
o
a.
£2 Q
O o
oo Q Q 8
o
CN
X [SS
7
w £
u
77
<
77
SO (J
“
o-
77
CN
T1
to
-c
PH 3 S g
-J -J
p < s
Q PH
r
Q)
>
to
£ § s9
o 0 7 7 7
3
a s< CQ
NO >n
0 Q
<
i—
H
t
l
u oo X N- PM
X
o
0)
CN CN
oo
Dd
c
C3 3
oo -
o
o
o
o
o
o
U
w
z
0 <
a 7 77 UJ
77 77
o o Z NO a,? O' > -I tu- fe u
s -1
m o
QM o
oo
O
u X
z>
*n
>n 00
Q n
(N
n-
x <N
B
c_
O
S fe
UJ CQ LU
3
D X
oo
o o E
CN
x_ a -i
NO
p
o
o
o
o
C-
in
*aQ 0 0 in
—
o o >n NO
<o M
<N
cn U 3 a
<
0
CN 00 C in
o
NO
o -I
o
ua 7 W
X X
CN
D
p
x
r-
cn cn
NO (77
i
—i I°°I
p-
0 H
UJ
X
!_J
Gg
u
aX
at u
0 cn
o
u-
CN CN
C E
CN
&
c ■sf 2 o
oo -
U E- D X
—1
00 ON
cn cn
**
oo I
•
-r > o p A
oo
oo
oo
Z
z D£
S
0 [°~°1
OH—T §
oo o *Q
u
a
-t o o cn •—
0M
oo
oo E <
CN CN
CN o o
rj- 0 Z
D
>n
00
o o
5~j— j—
oo ft-
00
CN o o
00
Nfl
I- 1
§
cn 74LS04
oo
oo
in
'A, cn ON
oo
NO cn -J
O
—
CN 00 o o >n /1
oo X c/a
— l oo -l
a
i
p- p
o
CN
~20
o
f— < oo -t cn
at u -r ON -t -t
rI 7X 74HC03
< o VC X.
oo
oo
•r, CN
73 cn ~r rI
/
a§ u
D
— P
■
- 1
•n o o
-J
p
Z) -J
p —
7t
i —i
P
oe:
< Q m o o •'I-
a— 9 0 0 0 0
o o CN
oo N
0
c at
- cuf— o
o
cc
O o °S°S o xO
CN o q 2
■< 7 c
ss
•n UJ O -T ogioÿi o $
Z 7
5 z
OO
P4 c at
-
CN at
3 S
O
UJ z r-' cz
/ C O'
3 00 nj 2 s s;
H oo'
r
£ S3c4 £
p_
—
Z
-i
7 2
o.
u
zs
■
2gv
o5 o
OnOm o
in
x
-T
P cn U g < D §S OT? OÿO
>n ..
CN at g§ c>0
z z
z to
SZ
® CN
00
00 Q D ZR Oro O o m
Q o CU
5n
m
0 < o z Z 7 oJoS o m
•n
o
O[o°] £;
0 7 0 u
0 UJ
0 o?o5 O 5
°+few
>n o
>n Q >I0JLD3NN03 Q H
< H
o
U o <
t-
Awnixnv H01D3NN03 N0ISNVdX3 Sfl9
NO
in
ON
"ÿ*0000000000 .—
CN
i
O'
oooooooooooooooooo ooooooo
OOOOOOOOOOOOOOOOOOOOOOOOO OJ
2 O I CQ
z
a OOOOOOOOOO
CN >
O.
o Cu CL-
cn o
Connecting the power supply: The kit uses power supply module 85M-OPT-04, which provides Z!
+ 5 V dc at 1 A, and ± 12 V dc at 0.1 A. Connect power supply to the kit using the cable that is I
terminated with a nine-pin D-type connector at the kit end. Only two wires are required to be con- £ >
nected at this point. Generally orange, blue, or a white coloured wire is to be connected to the +5 V 5 %
dc terminal and black coloured wire is to be connected to the ground terminal, marked as ‘COM’ (for § §.
common) on the power supply unit. The other wires are to be left open at this stage. They are needed » <
to connect ± 12 V dc to the kit, when it is desired to connect the kit to a host computer. ■o
i
Sign-on message: Switch on the power supply. If the kit is working fine, it will display ‘-SdA 85’ on 3
the six seven-segment LEDs. The ‘-’in the beginning indicates that it is ready to accept user com¬
mands. It is generally called the system prompt. ‘-SdA 85’ is the sign on message that is displayed
whenever power is switched on, or whenever the ‘reset’ key is typed.
Checking contents of a memory location: Type the key ‘SUBST MEM’. It stands for ‘substitute
memory’. A dot is displayed at the right edge of the address field. Except for the dot, the address and
data fields will be blank. It indicates that the kit expects the user to type a four-hex digit address value.
If the user wants to see the contents of F820, he has to type the keys ‘F’, ‘8’, ‘2’, ‘0’ one after another.
The display in the address field after each of the above typing will be ‘000F.’, ‘00F8.’, ‘0F82.’, and
finally ‘F820.’.
Notice that the ‘.’ is still at the edge of the address field. It means that the user can still change the
address value if he desires, by typing the new address. Let us say, we are interested in contents of
F820H. Then type ‘next’ key. Then the display in the data field indicates the contents of memory loca¬
tion F820H. Suppose, memory location F820 is having the value 3E, the display after typing ‘next’
key will be ‘F820 3E.’. The typing needed to check contents of location F820H is indicated in
Fig. 5.4. In this Fig. ‘= ’ stands for a blank character.
key pressed addr field data field
SUBST MEM
F 000 F.
8 OOF 8.
2 OF 8 2. Fig. 5.4
0 F 8 2 0. Typing needed to check contents
NEXT F820 3 E. of location F820H
Changing the contents of a memory location: Notice that the ‘.’ is now at the edge of the data field.
It means that, if the user wants to change the data, he is free to do so. Suppose we want to change the
contents of F820H to 47H, then type ‘4’ and ‘7’. The display in the data field after each of the above
typing will be ‘04.’, and finally ‘47.’. Notice that the ‘.’ is still at the edge of the data field. It means
that the user can still change the data value if he desires, by typing the new data.
Let us say, the user decides to change the data to 3A. Then, type ‘3’ and ‘A’. The display in the data
field after each of the above typing will be ‘73.’, and finally ‘3A.’. Notice that the ‘.’ is still at the edge
of the data field. It means that the user can still change the data value if he desires, by typing the new
data. Let us say, the user is not interested in any more change. Then type ‘next’ key. Only now, the
46
monitor program stores in memory location F820H the value 3AH. Then the display changes to indi¬
cate the contents of the next memory location F821H. For example, if location F821FI is having the
value BEFI, the display will become ‘F821 bE’.
If the user wants to change the contents of F821FI to 40FI, he has to now type ‘4’, ‘O’, and finally
‘NEXT’. Then 40FI is entered into memory location F821FI, and the display indicates the contents of loca¬
8M tion F822FI. If the user wants to change the contents of F822FI to F8FI, he has to now type ‘F’, ‘8’, and
finally ‘NEXT’. Then F8FI is entered into memory location F822FI, and the display indicates the contents
o of location F823FI. If the user is not interested in entering any more information, he has to type ‘EXEC’
ft key, to terminate the process of storing information in memory. Power supply is switched off, when there
o is nothing else to be done on the kit. But then, the information stored in the RAM will be lost.
£ The typing required to first change the contents of F820FI to 47FI, and then to 3AFI, contents of
in
F821FI to 40FI, and contents of F822FI to F8FI is shown in Fig. 5.5. In this diagram ‘d d’ in the data
soo field indicates any arbitrary two-digit hex value, which is not of interest to us.
Jj
H
key pressed addr field data field
SUBST MEM
F 000 F.
8 OOF 8.
2 OF 8 2.
0 F 8 2 0.
NEXT F820 dd.
4 F820 0 4.
7 F820 4 7.
3 F820 7 3.
A F820 3 A.
NEXT F821 dd.
4 F821 0 4.
0 F821 4 0.
NEXT F822 dd.
F F822 OF.
8 F822 F 8. Fig. 5.5
NEXT F823 dd. Typing needed to modify contents of
EXEC locations F820H, F821H, and F822H
As an example, let us store the program in section 5.1.2 starting from F820H. The first instruction of
our program, LDA F840H, occupies 3 bytes in memory. Notice that in 8085 programs, a 16-bit mem¬
ory address like F840H, has to be stored in memory in byte reversal form. In this example, 40, which
is the LS byte has to be stored in the lower address F821H, and F8, which is the MS byte has to be
stored in the higher address F822H. Thus, LDA F840H has to be stored as 3A 40 F8 in the three
locations starting from F820H, as shown in Fig. 5.5. Here 3A is the opcode for LDA. There are micro¬
processors like Motorola 6800, where 16-bit addresses are not stored in memory in byte reversal form.
But somehow, in all Intel processors 16-bit addresses are required to be stored in byte reversal form.
This notation of storing addresses in memory is known as little endian notation. The notation used in
Motorola processors is known as big endian notation.
47
The second instruction in the program, CMA, occupies only 1 byte in memory. It is stored as 2F in
the memory location F823H. The third instruction in the program, INR A, also occupies only 1 byte
in memory. It is stored as 3C in the memory location F824FI. The fourth instruction in the program,
STA F850FI, occupies 3 bytes in memory. It is stored as 32 50 F8 starting from memory locations
F825FI. Finally, the last instruction in the program, FILT, occupies only 1 byte in memory. It is stored
as 76 in the memory location F828FI. Z!
This program stored in memory using opcodes is called the machine language program. The 3
machine language program is shown in the following. r>
The typing required on the keyboard to enter the program is shown in Fig. 5.6.
SUBST MEM
F820
NEXT
3A
NEXT
40
NEXT
F8
NEXT
2F
NEXT
3C
NEXT
32
NEXT
50
NEXT
F8
NEXT
76 Fig. 5.6
NEXT Typing required on the keyboard
EXEC to enter the program
48
5.2.7 ENTERING THE DATA
Now that the program is stored in memory, it is now necessary to store in memory the data on which
the program is to operate. As this program takes the data from memory location F840H, we store in
this location the data value. If we wish to compute 2’s complement of 12H, we store 12H in F840H
8 by typing the keys as shown in Fig. 5.7.
a>
o
2
a. SUBST MEM
2
« F840
S NEXT
in
00 12 Fig. 5.7
so NEXT Typing required on the keyboard
■c EXEC to enter the data
H
Now the program and data are in memory. To execute the program, we have to just type the keys as
shown in Fig. 5.8. In this diagram ‘a a a a’ in the address field indicates any arbitrary four-digit hex
value, which is not of interest to us. Similarly, ‘d d’ in the data field indicates any arbitrary two-digit
hex value.
Notice that once the program is executed, it displays ‘E\ Here, ‘E’ stands for ‘executed’. Of course,
many mistake it to mean ‘error’! Then to check the result of execution, we have to just look at the con¬
tents of memory location F850H. This is done by typing
RESET
SUBST MEM
F850
NEXT
As the data for the program is 12H, its 2’s complement is EEH. So, if we get EE displayed in the
data field now, we have got the correct answer.
In this example, we have run the entire program without a break. After this, it is a good practice to
check up the program with several different data. But every time we change the data, we have to run
the program again, and then only we should check the contents of F850H to see the result.
49
5.2.9 EXECUTING THE PROGRAM IN SINGLE STEP MODE
In the previous example, if the result is not EE, it means that the program has not produced correct 2’s
complement. This will be so, when the program is wrong. To locate the error, the facility provided in
a kit is the single-step feature.
Using single-step feature, the programmer can see the contents of memory locations, or registers, Z!
3
after the execution of each instruction in the program. If needed, he can even alter the contents of
memory locations or registers, after the execution of each instruction. If the user is not interested in a »
seeing the contents of memory locations or registers, he can simply proceed with the next instruction. 5|
To use this feature, the user should first of all note down the effect of execution of each of the
instruction in the comments field. This is called ‘tracing’ the program execution. For example, he is
should note that A register content should change to 12H, after the execution of LDA F840H. If after g
executing LDA F840H, register A has the value 34H, something is wrong with this instruction. Then g
the user should carefully check the coding of the instruction. A common mistake is coding LDA F840H 3
as ‘3A F8 40’, instead of the correct code, which is ‘3A 40 F8’. Other possible mistakes could be wrong
typing of code for LDA. The user by mistake may type ‘4A’ or ‘3B’, instead of the correct code ‘3A’.
If the tracing of the program gives wrong answer, it is definite that the program is incorrect. If the
tracing of the program gives correct answer, it is likely that the program works correctly for all possi¬
ble values of input data. The user should verify this by giving different sets of data in a judicious way.
To check the program in single step mode, type the keys as shown in Fig. 5.9 when the system
prompt appears in the address field.
In the single-step mode, after checking contents of a register, the other sequential registers can be
checked by just typing ‘NEXT’ every time, finally terminating the command by typing ‘EXEC’. For
example, if the user is only interested in checking contents of H and L registers at the end of execu¬
tion of an instruction, he has to type the following.
EXAM REG
H (displays H register contents in the data field)
NEXT (displays L register contents in the data field)
EXEC (terminates the ‘EXAM REG’ command)
In this, notice that to display contents of H register the key on which both H and 8 are printed has
to be typed. After typing ‘EXAM REG’, if this key is typed, it is interpreted as H. Similarly, after
typing ‘SUBST MEM’, if this key is typed, it is interpreted as 8. There are some more keys on the
keyboard, which have two values engraved.
Similarly, in the single-step mode, after checking contents of a memory location, the other sequen¬
tial locations can be checked by just typing ‘NEXT’ every time, finally terminating the command by
typing ‘EXEC’. For example, if the user is only interested in checking contents of memory locations
F850H, F851H, and F852H at the end of execution of an instruction, he has to type the following.
SUBST MEM
F 8 5 0 (displays F850 in the address field)
NEXT (displays contents of F850 in the data field)
NEXT (displays contents of F851 in the data field)
NEXT (displays contents of F852 in the data field)
EXEC (terminates the ‘SUBST MEM’ command)
50
key pressed addr field data field
SINGLE STEP a a a a. dd
F 000 F.
8 OOF 8.
2 OF 8 2.
oM 0 F 8 2 0.
M
o NEXT F 8 2 0. 3A
u
o NEXT F 8 2 3. 2F
a EXEC
o
S EXAM REG
in
00 A = =A 1 2.
o
00
ti
EXEC
x
SINGLE STEP F 8 2 3. 2F
NEXT F 8 2 4. 3C
EXEC
EXAM REG
A = =A Ed.
EXEC
EXAM REG
A = =A EE.
EXEC
SINGLE STEP F 8 2 5. 32
NEXT F 8 2 8. 76
EXEC
SUBST MEM
F 000 F.
8 OOF 8.
5 OF 8 5.
0 F 8 5 0.
NEXT F850 EE.
EXEC
SINGLE STEP F 8 2 8. 76
EXEC
RESET - S dA 85
Fig. 5.9 Typing needed to trace the program in single step mode
51
5.2.1 0 USING A KIT OTHER THAN ALS-SDA-85M
When the user has access to a kit by some other manufacturer, there are likely be to some differences
in the features compared with the ALS-SDA-85M kit. The differences are likely to be in the follow¬
ing details.
3!
3
■ EPROM capacity and address range,
■ RAM capacity and address range, r
U
>
(A
■
■
Number of I/O ports and their address,
Utility routines and their addresses,
f§
■ Availability of special peripheral chips and their addresses, and !i
■o
■ Differences in naming of some keys.
I3
The user is required to go through the manual for the kit to get the necessary information. 3
So far we have discussed the use of the kit in the keyboard mode. In this case, the user has to trans¬
late the assembly language program into machine code, and enter the program in hexadecimal using
the keyboard. This translation is quite monotonous, especially when the program is quite long.
If we have a computer with an ASCII keyboard, the program can be entered into computer mem¬
ory straightaway as an assembly language program. Then, the assembler program in the computer can
translate this assembly language program to machine code, relieving the user from this monotonous
task. The machine code can finally be downloaded from the computer to the kit using RS-232C serial
link. The user can then execute the downloaded program on the kit.
Also, the user can upload a machine language program from the memory of the kit to the hard disk
or the floppy disk on the computer. The hard disk or floppy disk is non-volatile, which means it does
not lose the information even after power is switched off and then turned on. Thus the user programs
can be stored as disk files on the computer, for downloading later to the kit for execution. However, the
cost goes up in serial mode, as a computer is a must in addition to the kit. Also, there must be assem¬
bler software on the computer. The circuitry and the monitor software on the kit also is increased.
The actual procedure for using the kit in serial mode is explained in a later chapter.
•Register codes
•Immediate addressing mode
•Register addressing mode
•Absolute addressing mode
•Register indirect addressing mode
•Implied addressing mode
■ Instruction type LDAX rp
■ Instruction type STAX rp
■ Questions
In this chapter a total of 13 instruction types covering 83 instructions are explained. In the previous
chapter a very simple program to compute the 2’s complement of a number was discussed. The
program was as follows.
53
LDA F840H (A) = 12H
CMA (A) = EDH
INR A (A) = EEH
STA F850H (F850 ) = EEH
HLT
In this program, LDA F840H execution results in movement of information from F840H to Accu¬ s
mulator. Thus it is an example for data transfer group of instructions. CMA execution results in com-
plementing Accumulator contents. It is a logical invert operation. Thus it is an example for logical
group of instructions. INR A execution results in incrementing Accumulator contents. It adds 1 to the
Accumulator contents. Thus it is an example for arithmetic group of instructions. STA F850H execu-
tion results in movement of information from Accumulator to memory location F850H. Thus it is one
—
o
§
“
% 2.
H
5' 3
more example for data transfer group of instructions. HLT execution results in halting the processor, w ■§
It is an example for machine control group of instructions, as this instruction controls the machine,
which is the microcomputer here.
From the previous discussion, we find that 8085 microprocessor executes a variety of instructions,
which can be broadly classified as follows.
Of course, in the earlier simple program, we have not come across some groups of instructions, like
the branch group. But, in many programs we frequently come across such instructions. To write a
program, we need to first of all know all the instructions that the 8085 can execute.
Towards this objective, in this chapter we describe the data transfer group of instructions, and in
the successive few chapters we describe the other groups of instructions.
As we have already seen from the previous program, although instruction may be as large as 3 bytes,
the opcode is always 1 byte in length. With 8 bits for the opcode, 28 = 256 distinct opcodes are pos¬
sible. In hexadecimal, the opcodes can be from 00H to FFH. Each opcode corresponds to an instruc¬
tion. Thus theoretically, 256 instructions are possible in the instruction set of 8085. However, only 246
54
opcodes are implemented in 8085. They can be discussed under 66 types, which are broadly classified
into the six groups listed earlier. Of the 246 opcodes we have:
- 202 opcodes that are 1-byte long,
- 18 opcodes that are 2-byte long, and
- 26 opcodes that are 3-byte long.
8 In data transfer group, we have 83 opcodes, which will be discussed in this chapter under 13 dif¬
a>
o ferent types.
2
a
2
.2
s ■ 6.2 INSTRUCTION TYPE MVI r, d8
U)
00
s
Jj
H MVI is a mnemonic, which stands for ‘MoVe Immediate’. This is an instruction to load a register with
an 8-bit value. This instruction uses immediate addressing for specifying the data. We will discuss
about addressing modes in a later section. In this type, ‘d8’ stands for any 8-bit data, and ‘r’ stands for
any of the following registers.
r = A, B, C, D, E, H, or L
As ‘r’ can have any of the seven values, there are seven opcodes for this type of instruction. It
occupies 2 bytes in the memory. MVI E, 8DH is an example instruction of this type. It is a 2-byte
instruction, with opcode for MVI E using up one byte, and 8DH using up one more byte. Suppose
E register content is 45H. When the 8085 executes this instruction, the contents of E register will
change to 8DH. This is shown below in an easy-to-understand manner.
Before After
(E) 45 8D
As summary for the above discussed instruction type, we provide within brackets the size of the
instruction, a representative instruction, and the number of opcodes for this type of instruction, as follows.
MVI r, d8 (2 byte; MVI E, 8EH; 7 opcodes)
In fact, from now on, for each instruction this type of summary will be provided.
MOV is a mnemonic, which stands for ‘MOVe’. This is an instruction to load register rl with the
8-bit value in register r2. Notice that in 8085 instructions, the first operand specifies the destination,
and the second the source. This instruction uses register addressing for specifying the data. In this
type, ‘rl’ and ‘r2’ stand for any of the following registers.
rl, r2 = A, B, C, D, E, H, or L
As rl can have any of the seven values, and r2 can have any of the seven values, there are 7 X 7 = 49
opcodes for this type of instruction. It occupies only 1 byte in memory. MOV E, H is an example instruc¬
tion of this type. It is a 1-byte instruction. Suppose E register content is 45 H, and H register content is
55
8DH. When the 8085 executes this instruction, the contents of E register will change to 8DH. This is
shown as follows.
Before After
(E) 45 8D
(H) 8D 8D
Notice that H register content is not altered at all. Although Intel has called it a ‘move’ instruction, o
s
it is in reality a ‘copy’ instruction. 23
Another thing to note is that there are instructions like ‘MOV D, D’. This instruction moves con- 3 3
®
tents of D register to D register itself. It is of no use to anybody! However, such useless instructions S
are also provided in the instruction set of 8085, and some very useful instructions that a programmer a
§ §
£
looks for are not provided! » ■u
Summary: MOV rl, r2 (1 byte; MOV E, H; 49 opcodes)
This is an instruction to load register r with the 8-bit value in memory location. But from which mem¬
ory location? The address of the memory location is understood to be provided in HL register pair.
This instruction uses register-indirect addressing for specifying the data.
As r can have any of the seven values, there are seven opcodes for this type of instruction. It occu¬
pies only 1 byte in memory. MOV E, M is an example instruction of this type. It is a 1-byte instruc¬
tion. Suppose E register content is 45H, H register content is F8H, and L register content is 50H. Let
us say location F850H has the data value 8DH. When the 8085 executes this instruction, the contents
of E register will change to 8DH, as shown below.
Before After
(E) 45 8D
(HL) F850 F850
(F850) 8D 8D
Henceforth, for simplicity, only values that have changed will be shown in the ‘After’ column.
Thus the simplified diagram for the execution of MOV E, M will be as follows.
Before After
(E) 45 8D
(HL) F850
(F850) 8D
Notice that there are instructions like ‘MOV H, M’. This instruction moves contents of a memory
location pointed by HL register pair to H register. Generally, it is of no use to anybody! However, such
useless instructions are also provided in the instruction set of 8085.
This is an instruction to load a memory location with the 8-bit value in register ‘r\ But which mem¬
ory location? The address of the memory location is understood to be provided in HL register pair.
8 This instruction uses register addressing for specifying the data.
a> As ‘r’ can have any of the seven values, there are seven opcodes for this type of instruction. It
o
2 occupies only one byte in memory. MOV M, E is an example instruction of this type. It is a 1-byte
a instruction. The result of execution of this instruction is shown with an example below.
2
.2 Before After
2
in
00
(E) 45
s« (HL)
(F850)
F850
8D 45
H
Notice that there are instructions like ‘MOV M, H’. This instruction moves contents of register H
to memory location pointed by HL register pair . Generally, it is of no use to anybody! However, such
useless instructions are also provided in the instruction set of 8085.
So far we have seen that whenever M is used in an instruction mnemonic, we are referring to the con¬
tents of a memory location whose address is given in HL register pair. Thus, if MOV E, M is executed,
register E receives contents of memory pointed by HL. Suppose we want E to receive contents of
memory location FF00H. Then we have to make sure that HL will have FF00H, before MOV E, M is
executed. Thus any instruction involving M, like ‘MOV E, M’, should generally be preceded by
instruction/s to load H and L registers with the desired memory address.
How to ensure that HL pair will have the value FF00H? One method is to use the following two
instructions: MVI H, FFH and MVI L, 00H.
These two instructions occupy a total of 4 bytes in memory. A simpler alternative is to use the
3-byte instruction ‘LXI H, FF00H’, of the type ‘LXI rp, dl6\
LXI is a mnemonic that stands for ‘Load extended register Immediate’. Here, a register pair is
termed as extended register. It is an instruction that loads register pair ‘rp’ with the 16-bit data denoted
as dl6. This instruction uses immediate addressing for specifying the data. In this type, ‘rp’ stands for
any of the following register pairs.
rp = BC, DE, or HL
As ‘rp’ can have any of the three values, there are three opcodes for this type of instruction. It occu¬
pies 3 bytes in memory. First byte specifies the opcode, and the next two bytes provide the 16-bit data.
LXI H, F850H is an example instruction of this type. It is a 3-byte instruction. The result of execution
of this instruction is shown below with an example.
Before After
(HL) FABCH F850H
57
Notice that when specifying the register pair, we specify only the MS register in the instruction
mnemonic. The ‘X’ in the mnemonic indicates that it is a register pair, and so if the MS register is
specified the register pair is identified. Thus, if our interest is to load HL with F850H, we have to write
the instruction as ‘LXI H, F850H’ and not as ‘LXI HL, F850H’ or ‘LXI L, F850H’. LXI H, F850H is
stored in memory with F850 stored in byte reversal form as shown below.
Code for LXI H
50 O H
s
F8 23
5W 3
The other instructions of this type are LXI B, dl6 and LXI D, dl6. 3 $
Summary: LXI rp, dl6 (3 bytes; LXI H, F850H; 3 opcodes)
ao
ilu
«1
This is an instruction to load a memory location pointed by HL pair with an 8-bit value. This instruc¬
tion uses immediate addressing for specifying the data. It occupies 2 bytes in memory.
MVI M, 8DH is an example instruction of this type. It is a 2-byte instruction, with opcode for MVI
M using up one byte, and 8DH using up one more byte. The result of execution of this instruction is
shown below with an example.
Before After
(HL) F845
(F845) AC 8D
LDA is a mnemonic that stands for LoaD Accumulator contents from memory. This is an instruction
to load Accumulator with the contents of a memory location whose 16-bit address is indicated in the
instruction as a16. This instruction uses absolute addressing for specifying the data. It occupies 3 bytes
in memory. First byte specifies the opcode, and the successive 2 bytes provide the 16-bit address.
LDA F850H is an example instruction of this type. It is a 3-byte instruction. The result of execu¬
tion of this instruction is shown below with an example.
Before After
(F850) BCH
(A) 12H BCH
LDA F850H is stored in memory with F850 stored in byte reversal form as shown below.
8
a>
o
2a ■ 6.9 INSTRUCTION TYPE STA al 6
2
.2
s STA is a mnemonic that stands for STore Accumulator contents in memory. This is an instruction
in
00 to store Accumulator contents in a memory location whose 16-bit address is indicated in the instruc¬
s tion as al6. This instruction uses absolute addressing for specifying the destination. It occupies 3
Jj bytes in memory. First byte specifies the opcode, and the successive 2 bytes provide the 16-bit
H
address.
STA F850FI is an example instruction of this type. It is a 3-byte instruction. The result of execu¬
tion of this instruction is shown below with an example.
Before After
(A) 12H
(F850) BCH 12H
STA F850FI is stored in memory with F850 stored in byte reversal form as shown below.
Code for STA
50
F8
There are no instructions in 8085 like STB al6, STD al6, etc. As was stated earlier, Accumulator
is the most important 8-bit register, whose contents can be stored in memory in more ways than any
other 8-bit register.
Summary: STA al6 (3 bytes; STA F850H; 1 opcode)
XCHG is a mnemonic, which stands for eXCHanGe. This is an instruction to exchange contents of
HL pair with DE pair. This instruction uses implied addressing. It occupies only 1 byte in memory.
The result of execution of this instruction is shown below with an example.
Before After
(HL) 1234H 5678H
(DE) 5678H 1234H
Using XCHG instruction, only HL and DE contents can be exchanged. There are no instructions in
8085 to exchange contents of HL and BC or to exchange contents of DE and BC.
Summary: XCHG (1 byte; XCHG; 1 opcode)
59
■ 6.11 ADDRESSING MODES OF 8085
Shown in the following are the sizes of a few of the instruction types.
MOV rl, r2 1 byte
MOV r, M 1 byte
MVI r, d8 2 bytes
O H
Let us see the reason for this before we discuss the addressing modes. 23
5arr 3
«ÿ
2 $
ao
Register
B
Register code
000
il
«1 JS
C 001
D 010
E 011 Fig. 6.1
H 100 Three-bit register codes
L 101 for 8085 registers
M 110
A 111
The 8085 sends out 16-bit address to access one of the 216 = 64K locations. But, for convenience, we
indicate memory address using four hexadecimal digits. Similarly, for convenience, we indicate reg¬
isters as A, B, C, etc. But in reality, the microprocessor has to specify these registers using 0s and Is
only. With only seven registers in 8085, 3 bits are enough to specify a register. The 3-bit register codes
for the registers of 8085 are tabulated in Fig. 6.1.
With 3-bit register code, eight registers can be specified. But 8085 has only seven registers. So
notice that register code 110 does not specify any register.
Fortunately, the user is not required to memorize these register codes. It is enough if the user real¬
izes that 3 bits are needed to specify a register.
Opcode for MOV E, H: Now let us see how Intel has arrived at the opcode for MOV E, H. Six bits
are needed to specify the two registers E and H. Still 2 bits are left in a byte. These 2 bits indicate the
code for ‘MOV’. The template chosen by Intel for instruction type MOV rl, r2 is shown in the
following. Intel chose 0 1 as the code for MOV.
01 rl code r2 code
From the previous register code table, the opcode for MOV E, H will be
01 Oil 100 = 5CH
This can be verified from the opcode chart given in the previous chapter. Similarly, the code for
MOV A, B will be: 0 1 111 0 00, which is 78H.
Opcode forMOV E, M: Now let us see how Intel has arrived at the opcode for MOV E, M.
How to specify M? Intel solved this problem by choosing 110, which was not used to specify any reg¬
ister, as the code for M. Thus, although M stands for the contents of a memory location pointed by HL,
it has 110 as the ‘register code’. Six bits are needed to specify E and M. The remaining 2 bits in the byte
indicate the code for ‘MOV’. From the previous register code table, the opcode for MOV E, M will be
60
01 Oil 110 = 5EH
This can be verified from the opcode chart given in the previous chapter. Similarly, the code for
MOV A, M will be: 0 1 111 110, which is 7EH.
Opcode for MVI E, 34H: Now let us see how Intel has arrived at the opcode for MVI E, 34H. It will
8 be a 2-byte instruction, with 34H as the second byte. In the first byte, 3 bits are needed to specify the
u registers E. Still 5 bits are left in the first byte. These 5 bits indicate the code for ‘MVI’. The template
o chosen by Intel for instruction type MVI r, d8 is shown as follows.
a
s
.2
00 r code 110
s From the previous register code table, the opcode for MVI E will be
in
oo
s 00 Oil 1 1 0 = 1EH
J! This can be verified from the opcode chart given in the previous chapter. Similarly, the code for
MVI M will be: 0 0 110 110, which is 36H.
Need for addressing modes: A user would like to have the ability to access data in different ways
for reasons of convenience. These different ways of accessing data is called the ‘addressing modes’.
It is something similar to the way a teacher in a classroom would refer to a particular student in one
of the following ways.
- By mentioning the name of the student;
- By indicating the position of his seat in the class;
- By indicating the features of the student, etc.
Consider the execution of the following four instructions. In each of these instructions we move
25H to the accumulator, using different addressing modes.
1. MVI A, 25H
Before After
(A) 12H 25H
2. MOV A, B
Before After
(B) 25H
(A) 12H 25H
3. LDAF850H
Before After
(F850) 25H
(A) 12H 25H
4. MOV A, M
Before After
(HL) F850H
(F850) 25H
(A) 12H 25H
61
6.11.2 IMMEDIATE ADDRESSING MODE
In the previous examples, only MVI A, 25H clearly indicates that 25H has to be moved to the A reg¬
ister. This instruction occupies 2 bytes in memory as follows.
Opcode for MVI A
25H
s
As the data to be moved is immediately after the opcode in the instruction, this kind of addressing o H
is called immediate addressing mode. Z
||
z$
6.1 1 .3 REGISTER ADDRESSING MODE
ao
§i
»■u
In the previous examples, MOV A, B indicates that contents of the B register have to be moved
to the A register. It does not directly say that 25H has to be moved. The instruction only provides
the address of the data. In this case, the address is register B, provided in the opcode using
3 bits. This kind of addressing where the data is specified in a register is called Register address¬
ing mode.
In the previous examples, LDA F850H indicates that contents of the memory location F850H have to
be moved to the A register. It does not directly say that 25H has to be moved. The instruction only pro¬
vides the address of the data. In this case, the memory address is F850H, provided in the instruction
using 2 bytes. This kind of addressing where the data is specified in a memory location is called
absolute addressing mode. It is called absolute addressing because a full 16-bit memory address is
given as part of the instruction, in comparison with register addressing where only a 3-bit register
address is provided as part of the instruction.
This type of addressing is sometimes called direct addressing.
In the previous examples, MOV A, M indicates that contents of the memory location pointed by HL
register pair has to be moved to A register. It does not directly say that 25H has to be moved.
If HL content is F850H, we are not moving F850H to Accumulator. We have to take F850H as the
memory address, and so move contents of F850H. This is an indirect way of using register pair HL.
Hence, this instruction is said to use Register indirect addressing.
Consider the instruction XCHG. It exchanges the contents of HL pair with DE pair. But the instruc¬
tion mnemonic does not explicitly specify that HL and DE are to be exchanged. In other words, we
do not write the instruction as ‘XCHG HL, DE’. It is just implied that we have to exchange HL and
DE. So this type of addressing is called implied addressing mode. It is also called implicit addressing
mode, sometimes.
62
These are the only addressing modes available in 8085. Some other important addressing modes,
like Indexed addressing, are not provided in 8085.
STAX is a mnemonic that stands for STore Accumulator contents in memory pointed by extended
register denoted as ‘rp’. This instruction uses register indirect addressing for specifying the destina¬
tion. It occupies only 1 byte in memory.
STAX B is an example instruction of this type. It is a 1-byte instruction. The result of execution of
this instruction is shown below with an example.
Before After
(BC) F2BCH
(A) 12H
(F2BC) DCH 12H
Only other instruction of this type is STAX D. Note that STAX H is not provided in 8085. This is
because, STAX H is the same as MOV M, A in its function.
63
Also note that there are no instructions in 8085 like STBX rp, STCX rp, etc. As was stated earlier,
Accumulator is the most important 8-bit register, whose contents can be stored in memory in more
ways than any other 8-bit register.
Summary: STAX rp (1 byte; STAX B; 2 opcodes)
s
0 H
23
6.14 INSTRUCTION TYPE LHLD ol6
■
a3 3
2 $
ao
LHLD is a mnemonic that stands for Load HL pair using Direct addressing from memory location |®
whose 16-bit address is denoted as al6. As HL pair has to be loaded, the data comes from two w -o
consecutive locations starting at the address a16. This instruction uses absolute addressing for speci¬
fying the data. It occupies 3 bytes in memory.
LHLD F2BCH is an example instruction of this type. It is a 3-byte instruction. The result of exe¬
cution of this instruction is shown below with an example.
Before After
(F2BC) DCH
(F2BD) ABH
(H) 12H ABH
(L) 34H DCH
LHLD F2BCH is stored in memory with F2BC stored in byte reversal form as shown below.
Note that there are no instructions in 8085 like LBCD al6 and LDED al6. As was stated earlier,
HL pair is the most important register pair, whose contents can be loaded in more ways than any other
register pair.
SHLD is a mnemonic, which stands for Store HLpair using Direct addressing in memory location
whose 16-bit address is denoted as a16. As HL pair has to be stored, it has to be stored in two consec¬
utive locations starting at the address al6. This instruction uses absolute addressing for specifying the
destination. It occupies 3 bytes in memory.
64
SHLD F2BCH is an example instruction of this type. It is a 3-byte instruction. The result of exe¬
cution of this instruction is pictorially shown below with an example.
Before After
(H) 12H
(L) 34H
8 (F2BC) DCH 34H
2
o (F2BD) ABH 12H
2
a SHLD F2BCH is stored in memory with F2BC stored in byte reversal form as shown below.
2
.8 Code for SHLD
S
in
co
BC
S F2
«j
Note that there are no instructions in 8085 like SBCD al6 and SDED al6. As was stated earlier,
HL pair is the most important register pair, whose contents can be stored in memory in more ways
than any other register pair.
1. Classify the different types of instructions available in 8085, giving an example for each type.
2. How many different instructions are implemented in 8085?
3. Distinguish between the following pairs of instructions:
a. LXI H, 1234H and LHLD 1234H;
b. LDA F900H and STA F900H;
c. MVI M, 8DH and LXI H, 008DH;
d. LHLD FA00H and SHLD FA00H.
4. What do you mean by addressing mode? Explain the different types of addressing modes
available in 8085, with an example for each.
5. Using the register code table, derive opcodes for the following instmctions:
i) MOV B, C, ii) MOV M, D, iii) MOV A, M.
7
m \
P:
Arithmetic
Group of
Instructions
■ Instructions to perform addition
• Instruction type ADD R
•Flags register
• Instruction type ADI d8
• Instruction type INR R
• Instruction type ADC R
• Instruction type ACI d8
■ Instructions to perform subtraction
• Instruction type SUB R
• Instruction type SUI d8
• Instruction type DCR R
• Instruction type SBB R
• Instruction type SBI d8
■ Instruction type INX rp
■ Instruction type DCX rp
• BCD numbers
• DAA instruction
■ Questions
A total of 14 instruction types covering 62 instructions will be explained in this chapter. The various
instruction types to perform addition and subtraction operations are dealt with in detail in this chapter.
Furthermore, the instruction types INX rp, DCX rp, and DAD rp are also discussed briefly in the
latter portion of the chapter.
66
■ 7. 1 INSTRUCTIONS TO PERFORM ADDITION
Intel 8085 is a primitive microprocessor. In its arithmetic group of instructions, it has only add and
subtract instructions. It does not have instructions to multiply or divide numbers. In this section we
8 discuss the instructions to perform addition.
a> In the addition of two numbers, 8085 imposes the restriction that one of the operands must be in
o
sa the Accumulator. The other operand can be one of the following.
E
— Contents of an 8-bit register,
S — Contents of memory location pointed by HL pair,
in
00 — Eight-bit immediate data.
s«
H
7.1.1 INSTRUCTION TYPE ADD R
ADD is a mnemonic that stands for ‘Add contents of R to accumulator’. In this case, ‘R’ stands for
any of the following registers or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to add contents of R and accumulator. The result of the addition will be
stored in the accumulator.
As R can have any of the eight values, there are eight opcodes for this type of instruction. It
occupies only 1 byte in memory.
‘ADD E’ is an example instruction of this type. It is a 1-byte instruction. The result of execution of
this instruction is shown below with an example.
Before After
(E) F3H
(A) 45H 38H
In the previous example, the result of addition of 45H and F3H turns out to be 38H, which is less than
the original value of 45H present in the accumulator! Obviously, the user will not be happy with such
state of affairs. The correct answer is 38H with carry of 1. This carry information is stored in a
special 8-bit register called the flags register, F for short. A flag can be in the hoisted state, or it is not
hoisted at all! Thus a flag can be represented by 1 bit of information. Thus the flags register can have
a total of eight flags. But only five flags are implemented in 8085. They are:
These individual flags are either hoisted (set to 1), or not hoisted (reset to 0) depending on the result
of execution of an arithmetic or logical instruction. But in a few arithmetic and logical instructions,
some or none of these flags are affected.
However, in any data transfer instruction, none of the flags are affected. Hence the topic of flags
register was not discussed at all while explaining data transfer instructions in the previous chapter.
Carry flag (Cy): Notice that in the addition of any two 8-bit numbers, the carry generated can be
either 0 or 1. Thus to store the carry information 1-bit storage is enough. The Cy flag is stored in the
LS bit position in the flags register. Instructions that use the Cy flag are widely used in the user
programs.
Example 1: In the addition of 45H and F3H, the result is 38H with Cy flag = 1, as shown below.
Cy AC
10
4 5H
+ F3H
3 8H = 0011 1000
Example 2: In the addition of 85H and 1EH, the result is A3H with Cy = 0, as shown below.
Cy AC
01
8 5H
+ 1 EH
A 3H = 1010 0011
68
Auxiliary carry flag (AC): In the addition of any two 8-bit (2-hex digit) numbers, a carry may be
generated when we add the LS hex digits of the two numbers. Such a carry is called intermediate carry,
half carry, or auxiliary carry (AC). Intel prefers to call it AC. In Ex. 1, AC is not generated. In Ex. 2,
AC is generated.
As this is only an intermediate carry, we are not interested in storing this information. But 8085
8 still stores this AC information in bit position 4 of the flags register. The result of execution of DAA
a> instruction, to be described later, is affected by the status of this flag. However, 8085 instruction set
o
sa does not provide any instruction, which explicitly uses the AC flag.
E
Sign flag (S): The S flag is set to 1, if the result of an arithmetic operation is negative, indicated by
S MS bit of 8-bit result being 1. It is reset to 0 if the result is positive, indicated by MS bit of 8-bit result
in
00 being 0.
s« Thus, the value of S flag is essentially the value of the MS bit of the 8-bit result. In Ex. 1, as the
8-bit result is 38H = 0011 1000, the sign flag is reset to 0. Note that we are not considering here the
H
9-bit result including the carry, to decide the S flag value. In Ex. 2, as the 8-bit result is A3H = 1 010
0011, the sign flag is set to 1.
If we are working with unsigned numbers, we simply ignore the S flag. For example, if we are
treating 85H and 1EH as unsigned numbers, their sum will be the unsigned number A3H. In this case,
S flag becomes 1, but we do not care for the value of the S flag.
Instructions that use the S flag are quite often used in the user programs.
Parity flag (P): The P flag is set to 1, if the 8-bit result of an arithmetic operation has an even number
of 1’s in it. If there are odd number of 1’s in the 8-bit result, the P flag is reset to 0.
In Ex. 1, as the 8-bit result 38H = 0011 1000 has three numbers of l’s (an odd number), the par¬
ity flag is reset to 0. In Ex. 2, as the 8-bit result A3H = 1010 0011 has four numbers of 1’s (an even
number), the parity flag is set to 1.
As the user does not really care for the number of 1’s present in the result after an arithmetic oper¬
ation, this flag is not of much use practically.
Zero flag (Zj: The Z flag is set to 1, if the 8-bit result of an arithmetic operation is 00H. If the 8-bit result
is not equal to 00H, the Z flag is reset to 0. Thus the Z flag is hoisted to indicate that the result is 0.
In Ex. 1, as the 8-bit result is 38H and is non-zero, the Z flag is reset to 0. Also in Ex. 2, as the 8-
bit result is A3H, the Z flag is reset to 0. Instructions that use the Z flag are widely used in the user
programs.
ADI is a mnemonic, which stands for ‘ADd Immediate to Accumulator’ and ‘d8’ stands for any 8-bit
data. This instruction is used to add 8-bit immediate data to the Accumulator. The result of addition
will be stored in the accumulator. It occupies 2-bytes in memory. The flags are affected based on the
result.
‘ADI F3H’ is an example instruction of this type. It is a 2-byte instruction. The result of execution
of this instruction is shown below with an example.
Before After
(A) 45H 38H
(F) any values Cy = 1, AC= 0, S = 0, P = 0, Z = 0
Summary: ADI d8 (2 bytes; ADI F3H; 1 opcode)
69
7.1.4 INSTRUCTION TYPE INR R
INR is a mnemonic that stands for ‘INcRement’ and ‘R’ stands for any of the following registers or
memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M >
This instruction is used to add 1 to the contents of R. The result of increment will be stored in R. S
All flags, except Cy flag, are affected depending on the result. Many times a register content is used 8. f
as a counter. If Cy flag were to be affected during increment of a counter, it causes problems in many jj o'
cases, as will be seen in program examples later. So by design, Cy flag is not affected by execution of j ®
this instruction. 2. c
As R can have any of the eight values, there are eight opcodes for this type of instruction. It occu- §
pies only 1 byte in memory. ‘INR M’ is an example instruction of this type. It is a 1-byte instruction. "
The result of execution of this instruction is shown below with an example.
Before After
(HL) F850H
(F850) 45H 46H
(F) any values Cy = no change, Ac = 0, S = 0, P = 0, Z = 0
If contents of F850H were FFH, it becomes 00H after execution of INR M.
Summary: INR R (1 byte; INR M; 8 opcodes)
There are times when a user is required to add two numbers each of which is several bytes in size. For
example, let us say it is needed to add the following two 16-bit numbers.
I
34 56H
+A2 F2H
D7 48H
In this example, the addition of 56H and F2H results in a sum of 48H with a carry of 1. Next, we
have to add 34H and A2H along with this carry value of 1. To facilitate such an operation, 8085 pro¬
vides instructions to add two numbers along with carry value.
ADC is a mnemonic that stands for ‘ADd with Carry’ and ‘R’ stands for any of the following reg¬
isters, or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to add contents of R and Accumulator along with the carry value. The result
of the addition will be stored in the Accumulator. As R can have any of the eight values, there are eight
opcodes for this type of instruction. It occupies only 1 byte in memory. ‘ADC E’ is an example instruc¬
tion of this type. It is a 1-byte instruction. The result of execution of this instruction is shown below
with an example.
Before After
(E) 45H
(A) 33H 79H
(F) Cy = 1 Cy = 0, AC= 0, Z = 0, P = 0, S = 0
others = any values
Summary: ADC R (1 byte; ADC E; 8 opcodes)
70
7.1 .6 INSTRUCTION TYPE ACI d8
ACI is a mnemonic that stands for ‘Add with Carry Immediate to accumulator’ and ‘d8’ stands for
any 8-bit data. This instruction is used to add 8-bit immediate data to the accumulator along with the
carry value. The result of the addition will be stored in the accumulator. The flags are affected based
8 on the result. It occupies 2 bytes in memory.
a> ‘ACI F3H’ is an example instruction of this type. It is a 2-byte instruction. The result of execution
o
sa of this instruction is shown below with an example.
E Before After
S (A) 45H 38H
in
00
(F) Cy= 0 Cy = 1, AC= 0, S = 0, P = 0, Z = 0
s« others = any values
In this section we discuss the instructions to perform subtraction. In the subtraction of two numbers,
8085 imposes the restriction that Accumulator will have one operand from which the other operand
specified by one of the following will be subtracted.
SUB is a mnemonic that stands for ‘SUBtract contents of R from Accumulator’. ‘R’ stands for any of
the following registers, or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to subtract contents of R from Accumulator. The result of the subtraction
will be stored in the Accumulator. As R can have any of the eight values, there are eight opcodes for
this type of instruction. It occupies only 1 byte in memory. ‘SUB E’ is an example instruction of this
type. It is a 1-byte instruction. The result of execution of this instruction is shown below with examples.
Example 1:
Before After
(A) F3H AEH
(E) 45H
(F) any values Cy = 0, AC= 0, S = 1, P = 0, Z = 0
Internally, 8085 performs this subtraction by adding the 2’s complement of 45H to F3H, as shown
below.
71
Cy Ac
10
F3H
+ B BH <— 2’s carry element of 45H
A EH >
The carry generated in this addition is complemented and stored as the Cy flag value. Thus the Cy
flag value after the subtraction will be 0. Note that even though it is a subtract operation, Intel prefers
to use the terms carry and auxiliary carry in place of borrow and auxiliary borrow.
—
0
"* it
3j
i
§
“
»'
Example 2 a\o fg
Before After u
(A) 3OH 10H
(E) 20H
(F) any values Cy = 0, AC= 1, S = 1, P = 0, Z = 0
Internally, 8085 performs this subtraction by adding the 2’s complement of 20H to 30H, as shown
in the following.
Cy Ac
11
3 OH
+ E OH
1 OH
The carry generated in this addition is complemented and stored as the Cy flag value. Thus the Cy
flag value after the subtraction will be 0.
Summary : SUB R (1 byte; SUB E; 8 opcodes)
SUI is a mnemonic that stands for ‘Subtract Immediate from Accumulator’ and ‘d8’ stands for any
8-bit data. This instruction is used to subtract 8-bit immediate data from the Accumulator. The result
of the subtraction will be stored in the Accumulator. The flags are affected based on the result. It occu¬
pies 2 bytes in memory. ‘SUI F3H’ is an example instruction of this type. It is a 2-byte instruction.
The result of execution of this instruction is shown below with an example.
Before After
(A) 45H 52H
(F) any values Cy = 1, AC = 1, S = 0, P = 0, Z = 0
Cy Ac
01
45 H
+ 0 DH
5 2H
Summary: SUI d8 (2 bytes; SUI F3H; 1 opcode)
72
7.2.3 INSTRUCTION TYPE DCR R
DCR is a mnemonic, which stands for ‘DeCRement’ and ‘R’ stands for any of the following registers,
or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
8
M
o This instruction is used to subtract 1 from the contents of R. The result of the increment will be
S
a stored in R. All flags, except Cy flag, are affected depending on the result. Many times a register con¬
E tent is used as a down counter. If Cy flag were to be affected during decrement of a counter, it causes
.2 problems in many cases, as will be seen in program examples later. So by design, Cy flag is not
S
in affected by the execution of this instruction.
00
s« As R can have any of the eight values, there are eight opcodes for this type of instruction. It occu¬
pies only 1 byte in memory. ‘DCR M’ is an example instruction of this type. It is a 1-byte instruction.
H The result of execution of this instruction is shown in the following with an example.
Before After
(HL) F850H
(F850) 45H 44H
(F) any values Cy = no change, AC= 1, S = 0, P = 1, Z = 0
Internally, 8085 performs this decrement operation by adding the 2’s complement of 01H to 45H.
If contents of F850H were 00H, it becomes FFH after execution of DCR M.
Summary. DCR R (1 byte; DCR M; 8 opcodes)
There are times when a user is required to subtract two numbers each of which is several bytes in size.
For example, let us say it is needed to perform the following subtraction.
-i
34 56H
-12F2H
21 64H
In this example, the subtraction of 56H and F2H results in 64H with a borrow of 1. Next, we have
to subtract 34H and 12H along with this borrow value of 1. To facilitate such an operation, 8085
provides instructions to subtract two numbers along with the borrow value.
SBB is a mnemonic that stands for ‘SuBtract with Borrow’ and ‘R’ stands for any of the following
registers, or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to subtract contents of R from accumulator, along with the carry value. The
result of the subtraction will be stored in the Accumulator. As R can have any of the eight values, there
are eight opcodes for this type of instruction. It occupies only 1 byte in memory. ‘SBB E’ is an exam¬
ple instruction of this type. It is a 1-byte instruction. The result of execution of this instruction is
shown below with an example.
73
Before After
(E) 45H
(A) 33H EDH
(F) Cy = 1 Cy = 1, AC= 0, Z = 0, P = 1, S = 1
others = any values
>
Summary. SBB R (1 byte; SBB E; 8 opcodes)
I—
o 3
it
SBI is a mnemonic that stands for ‘Subtract with Borrow Immediate from Accumulator’ and ‘d8’
if£
stands for any 8-bit data. This instruction is used to subtract 8-bit immediate data from the Accumu- g
lator along with the carry value. The result of subtraction will be stored in the Accumulator. The flags
are affected based on the result. It occupies 2 bytes in memory.
‘SBI F3FT is an example instruction of this type. It is a 2-byte instruction. The result of execution
of this instruction is shown below with an example.
Before After
(A) 45H 52H
(F) Cy = 0 Cy = 1, AC = 1, S = 0, P = 0, Z = 0
others = any values
Summary. SBI d8 (2 bytes; SBI F3H; 1 opcode)
INX is a mnemonic that stands for ‘INcrement extended register’ and ‘rp’ stands for any of the fol¬
lowing register pairs.
rp = BC, DE, or HL
This instruction is used to add 1 to the contents of rp. The result of the increment will be stored in
rp. Note that flags are not at all affected by the execution of this instruction. A register pair is gener¬
ally used to store a memory address. If flags were to be affected during increment of a memory
address, it causes problems in many cases, as will be seen in program examples later. So by design,
flags are not affected by execution of this instruction.
As rp can have any of the three values, there are three opcodes for this type of instruction. It occu¬
pies only 1 byte in memory. ‘INX B’ is an example instruction of this type. It is a 1-byte instruction.
The result of execution of this instruction is shown below with an example.
Before After
(BC) F850H F851H
It may appear that INX B is functionally same as INR C in this example. But, if content of BC was
F8FFH, it becomes F900H after execution of INX B. If content of BC was F8FFH, it becomes F800H
after execution of INR C. Thus, basically, INX instruction increments a 16-bit quantity, whereas INR
increments a 8-bit quantity.
Summary: INX rp (1 byte; INX B; 3 opcodes)
74
■ 7.4 INSTRUCTION TYPE DCX rp
DCX is a mnemonic, which stands for ‘Decrement extended register’ and ‘rp’ stands for any of the
following register pairs.
8
M
rp = BC, DE, or HL
o This instruction is used to subtract 1 from the contents of rp. The result of the decrement will be
S stored in rp. Note that flags are not at all affected by the execution of this instruction. A register pair
a
E is generally used to store a memory address. If flags were to be affected during decrement of a
.2 memory address, it causes problems in many cases, as will be seen in program examples later. So by
S
in
00
design, flags are not affected by execution of this instruction.
s« As rp can have any of the three values, there are three opcodes for this type of instruction. It occu¬
pies only 1 byte in memory. ‘DCX B’ is an example instruction of this type. It is a 1-byte instruction.
H The result of execution of this instruction is shown below with an example.
Before After
(BC) F850H F84FH
It may appear that DCX B is functionally same as DCR C in this example. But, if content of BC
was F900H, it becomes F8FFH after execution of DCX B. If content of BC was F900H, it becomes
F9FFH after execution of DCR C. Thus, basically, DCX instruction decrements a 16-bit quantity,
whereas DCR decrements a 8-bit quantity.
Summary. DCX rp (1 byte; DCX B; 3 opcodes)
Intel 8085 is basically an 8-bit microprocessor. But the designers have provided instructions to
perform 16-bit additions also. As the internal architecture is only 8 bits, this instruction easily takes
double the time needed to add two 8-bit numbers.
DAD is a mnemonic, which stands for ‘Double ADd’ and ‘rp’ stands for any of the following
register pairs.
rp = BC, DE, or HL
This instruction is used to add contents of rp to HL. The result of the addition will be stored in HL.
Thus in this instruction, HL is used as a 16-bit accumulator. Only Cy flag is affected depending on the
result.
If other flags were to be affected during execution of this instruction, it causes problems in many
cases, as will be seen in program examples later. So by design, flags other than Cy, are not affected
by execution of this instruction.
As rp can have any of the three values, there are three opcodes for this type of instruction. It occu¬
pies only 1 byte in memory. ‘DAD B’ is an example instruction of this type. It is a 1-byte instruction.
The result of execution of this instruction is shown below with an example.
Before After
(HL) 1234H 0A84H
(BC) F850H
(F) any values Cy = 1, no change in other flags
Summary: DAD rp (1 byte; DAD B; 3 opcodes)
75
■ 7.6 DECIMAL ADDITION IN 8085
In a digital computer everything is represented using Os and Is only. For example, an instruction will
have a code using only Os and Is. Data is also represented using Os and Is. Data can be of different
types like unsigned numbers, signed numbers, floating point numbers, binary coded decimal (BCD) >
numbers, etc. Thus, a series of Os and Is will acquire a value based on the interpretation.
o 3
3 *
7.6.1 BCD NUMBERS
Many a time, we are required to represent decimal numbers in a computer, and perform arithmetic
I!
o
<i>
on these numbers. For example, we may be required to total the marks a student has obtained in five
different subjects, where obviously, the marks are awarded in decimal notation.
For this purpose BCD code is extensively used. In BCD notation, 4 bits are used to code a digit,
and so two digits of information is stored in a byte. For example, decimal 39 is represented in BCD
as 0011 1001. Codes 1010, 1011, 1100, 1101, 1110, and 1111 are illegal in BCD notation. Similarly
decimal 1,024 is represented in BCD as 0001 0000 0010 0100. Same decimal 1,024 is represented in
binary as 0000 0100 0000 0000. As stated earlier, the value of a series of 0s and Is depends on the
interpretation.
Suppose we want to add the two decimal numbers 38 and 45. They will be represented in BCD as 0011
1000 and 0100 0101. The addition results in 0111 1101. If we interpret this result as a BCD number,
the answer is incorrect as well as illegal. This is because, 1101 is an illegal BCD number. This is where
the DAA instruction proves its usefulness. All that is required to be done is to add the BCD numbers
and store the result in A, and then execute the DAA instruction.
The working of DAA instruction depends on the contents of the AL register, Cy, and AC flags. In
effect, it adds 00H, 06H, 60H, or 66H to accumulator so as to get the correct BCD answer in the
Accumulator.
If the LS hex digit in A is <= 9 and AC flag is 0, the LS hex digit value will not be altered.
If the LS hex digit is > 9, or if AC flag is set to 1, it adds 6 to the LS hex digit of A. It increments
the MS hex digit if this addition resulted in a carry to the MS digit position. In this process,
the Cy flag will be set to 1 if the MS hex digit was incremented from F to 0.
Now, if the MS hex digit is <= 9 and Cy flag is 0, the MS hex digit will not be altered, and Cy
flag is reset to 0.
If the MS hex digit is > 9, or if Cy flag is set to 1, it adds 6 to the MS hex digit of A and sets Cy
flag to 1.
Note that DAA instruction cannot be used for decimal subtraction. Intel 8085 does not provide an
instruction for decimal subtraction. So a series of instructions are to be executed to perform decimal
subtraction.
A number of examples are provided here to explain decimal addition using DAA instruction.
Example 1: Let us say, we add 45 BCD and 38 BCD, and store the result 7DH in A. In this case, Cy
flag = 0 and AC flag = 0. But as D is an invalid BCD code, the DAA instruction adds 06H to A. Thus,
we get 83H in A, which will now be interpreted as 83 BCD, the correct sum of 45 and 38.
76
Example 2\ Let us say, we add 63 BCD and 88 BCD, and store the result EBH in A. In this case, Cy
flag = 0 and AC flag = 0. But as both E and B are invalid BCD codes, the DAA instruction adds 66H
to A. Thus, we get 51H in A, and Cy flag will be set to 1. This will now be interpreted as 5 1 BCD with
Cy flag = 1, the correct sum of 63 and 88.
„ Example 3: Let us say, we add 53 BCD and 36 BCD, and store the result 89H in A. In this case, Cy
5 flag = 0 and AC flag = 0. Since both 8 and 9 are valid BCD codes, the DAA instruction does not add
g anything to A. In other words, it is same as adding 00 to AL. The result in A will now be interpreted
g as 89 BCD, the correct sum of 53 and 36.
M
j o
o 2 Example 4: Let us say, we add 99 BCD and 88 BCD, and store the result 21H in A. In this case, Cy
jj jjj flag = 1 and AC flag = 1. So, although both 2 and 1 are valid BCD codes, the DAA instruction adds
o ■o 66H to A. Thus, we get 87H in A, and Cy flag will be set to 1. The result in A will now be interpreted
aB as 87 BCD with Cy flag = 1, the correct sum of 99 and 88.
S
Example 5: Let us say, we add 63 BCD and 42 BCD, and store the result A5H in A. In this case, Cy
flag = 0 and AC flag = 0. But as A is an invalid BCD code, the DAA instruction adds 60H to AL.
Thus we get 05H in A, with Cy flag = 1, which will now be interpreted as 05 BCD with Cy flag = 1,
the correct sum of 63 and 42.
P:
Logical Group of
Instructions
A total of 1 5 instruction types covering 43 instructions will be explained in this chapter. These include
the various instruction types to perform AND, OR, exclusive OR, complement Accumulator, comple¬
ment = set Cy flag, compare and rotate Accumulator operations.
78
■ 8.1 INSTRUCTIONS TO PERFORM 'AND' OPERATION
In the logical group of instructions, 8085 has instructions to perform AND, OR, Ex-OR, and NOT
operations. It does not have instructions to perform NAND or NOR operations. This is because in
8 controlling of peripherals, it is generally not required to perform NAND or NOR operations. In this
So section we discuss the instructions to perform AND operation.
£ In operations like AND, which need two operands, 8085 imposes the restriction that one of the
a operands must be in the Accumulator. The other operand can be one of the following.
£
£
2 - Contents of an 8-bit register;
in
«9 - Contents of memory location pointed by HL pair;
s - Eight-bit immediate data.
Jj
The AND operation performs bit-wise AND of the two operands. If X is a bit of Accumulator, and
Y is a bit of the other operand in the same bit position, the AND operation is performed as per the
following truth table.
0 0 0
0 1 0
1 0 0
1 1 1
It can be noticed that X AND 1 = X, and X AND 0 = 0. Thus, AND operation is used for selec¬
tively resetting to 0 some bits of the Accumulator. Bits of the Accumulator that are ANDed with Os
are reset to 0, and bits of the Accumulator, which are ANDed with Is are not changed. Thus, if it is
desired to reset MS 4 bits of Accumulator, AND Accumulator contents with 0FH.
The AND instruction affects the flags as follows.
ANA is a mnemonic, which stands for ‘ANd Accumulator’ and ‘R’ stands for any of the following
registers, or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to AND contents of R with Accumulator. The result of AND operation will
be stored in the Accumulator. As R can have any of the eight values, there are eight opcodes for this
type of instruction. It occupies only 1 byte in memory. ‘ANA E’ is an example instruction of this type.
It is a 1-byte instruction. The result of execution of this instruction is shown below with an example.
79
Before After
(E) 45H
(A) 33H 01H
(F) any values Cy = 0, AC =1,Z=0, P=0, S=0
(A) = 33H = 0 0 1 1 0 0 1 1 £
AND <£.
(E) = 45H = 0 1 000 1 0 1
a o“
3
00000001 = 01H
si
c. v
Summary. ANA R (1 byte; ANA E; 8 opcodes)
ANI is a mnemonic, which stands for ‘ANd Immediate with Accumulator’ and ‘d8’ stands for any 8-
bit data. This instruction is used to AND 8-bit immediate data with the Accumulator. The result of
ANDing will be stored in the Accumulator. The S, P, and Z flags are affected based on the result. Cy
is reset to 0, and AC is set to 1. It occupies 2 bytes in memory.
‘ANI F3H’ is an example instruction of this type. It is a 2-byte instruction. The result of execution
of this instruction is shown below with an example.
Before After
(A) 45H 41H
(F) any values Cy = 0, AC= 1,S=0, P = 1,Z = 0
Summary: ANI d8 (2 bytes; ANI F3H; 1 opcode)
The OR operation performs bit-wise OR of the two operands. If X is a bit of Accumulator, and Y is a
bit of the other operand in the same bit position, the OR operation is performed as per the following
truth table.
Before After
(E) 45H
(A) 33H 77H
(F) any values Cy =0, Ac = 0, Z = 0, P = 1, S = 0
(A) = 33H = 0 0 1 1 0 0 1 1
OR
(E) = 45H = 0 1 0 0 0 1 0 1
0 1 1 1 0 1 1 1 = 77H
Summary. ORA R (1 byte; ORA E; 8 opcodes)
ORI is a mnemonic that stands for ‘OR Immediate with Accumulator’ and ‘d8’ stands for any 8-bit
data. This instruction is used to OR 8-bit immediate data with the Accumulator. The result of ORing
will be stored in the Accumulator. The S, P, and Z flags are affected based on the result. Cy and AC
are reset to 0. It occupies 2 bytes in memory. ‘ORI F3H’ is an example instruction of this type. It is a
2-byte instruction. The result of execution of this instruction is shown below with an example.
Before After
(A) 45H F7H
(F) any values Cy = 0, AC = 0, S = 1, P = 0, Z = 0
Summary: ORI d8 (2 bytes; ORI F3H; 1 opcode)
The Ex-OR operation performs bit-wise Ex-OR of the two operands. If X is a bit of Accumulator, and
Y is a bit of the other operand in the same bit position, the Ex-OR operation is performed as per the
following truth table.
81
0 0 0
0 1 1
1
1
0
1
1
0
r
<s.
3 ID
It can be noticed that X Ex-OR 1 = X*, and X Ex-OR 0 = X. Thus, Ex-OR operation is used for «ÿ
selectively complementing some bits of the Accumulator. Bits of the Accumulator, which are Ex-ORed § §
with Is are complemented, and bits of the Accumulator, which are Ex-ORed with Os are not changed. 5'
Thus, if it is desired to complement MS 4 bits of Accumulator, Ex-OR Accumulator contents with FOH. 3L 2.
The Ex-OR instruction affects the flags as follows.
- S, P, and Z flags are updated based on the result;
- Cy and AC flags are reset to 0.
XRA is a mnemonic that stands for ‘exclusive OR accumulator’ and ‘R’ stands for any of the follow¬
ing registers, or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to Ex-OR contents of R with the Accumulator. The result of Ex-OR oper¬
ation will be stored in the Accumulator. As R can have any of the eight values, there are eight opcodes
for this type of instruction. It occupies only 1 byte in memory. ‘XRA E’ is an example instruction of
this type. It is a 1-byte instruction. The result of execution of this instruction is shown below with an
example.
Before After
(E) 45 H
(A) 33H 76H
(F) any values Cy = 0, AC = 0, Z = 0, P = 0, S = 0
(A) = 33H = 0 0 1 1 0 0 1 1
Ex-OR
(E) = 45H = 0 1 0 0 0 1 0 1
0 1 1 1 0 1 1 0 = 76H
Summary: XRA R (1 byte; XRA E; 8 opcodes)
XRI is a mnemonic that stands for ‘exclusive OR Immediate with Accumulator’ and ‘d8’ stands for any
8-bit data. This instruction is used to Ex-OR 8-bit immediate data with the Accumulator. The result of Ex-
ORing will be stored in the Accumulator. The S, P, and Z flags are affected based on the result. Cy and
AC are reset to 0. It occupies 2 bytes in memory. ‘XRI F3H’ is an example instruction of this type. It is a
2-byte instruction. The result of execution of this instruction is shown below with an example.
82
Before After
(A) 45H B6H
(F) any values Cy = 0, AC =0, S = 1,P=0, Z=0
Summary. XRI d8 (2 bytes; XRI F3H; 1 opcode)
8
S
§ ■ 8.4 INSTRUCTION TO COMPLEMENT ACCUMULATOR
!
.2
s The complement instruction in 8085 has the mnemonic CMA. It stands for ‘CoMplement the Accu¬
in
«9 mulator’. It performs l’s complement operation on the contents of Accumulator, and the result is
s stored back in the Accumulator. Notice that only Accumulator contents can be complemented and not
Jj any other register. Flags are not affected by the execution of this instruction. It occupies only 1 byte
in memory. The result of execution of this instruction is shown below with an example.
Before After
(A) 45H BAH
Summary : CMA(1 byte; CMA; 1 opcode)
Intel 8085 also provides instructions to complement the Cy flag, and set the Cy flag to the 1 state. But
it does not have an instruction to reset the Cy flag to 0. If it is desired to reset Cy flag to 0, the method
is to set it to 1 and then complement it. Notice that no other flag can be set or complemented.
CMC stands for ‘CoMplement the Carry flag’. It performs complement operation on the Cy flag, and
the result is stored back in the Cy flag. The result of execution of this instruction is shown below with
an example.
Before After
(Cy) 1 0
Summary: CMC (1 byte; CMC; 1 opcode)
STC stands for ‘SeT the Carry flag’. It sets the Cy flag to the 1 state, immaterial of its earlier value.
The result of execution of this instruction is shown below with examples.
Example 1
Before After
(Cy) 1 1
83
Example 2
Before After
(Cy) 0 1
Summary: STC (1 bytes; STC; 1 opcode)
r
<s.
3
%
ID
■ 8.6 INSTRUCTIONS TO PERFORM COMPARE OPERATION
II■o
A compare instruction compares two operands, and affects the status flags’ values depending on the
result of the comparison. In this operation, 8085 imposes the restriction that one of the operands must
be in the Accumulator. The other operand can be one of the following.
The compare instruction actually computes the value of the Accumulator contents minus other
operand. The original values of the operands are not changed. The result is stored in a register that is
not accessible to the programmer. Based on the result, all the flags are affected.
It is similar to comparing the heights of two people person 1 and person 2. After the comparison,
the heights remain unaltered. But, we would have come to one of the following conclusions.
CMP is a mnemonic that stands for ‘CoMPare Accumulator’ and ‘R’ stands for any of the following
registers, or memory location M pointed by HL pair.
R = A, B, C, D, E, H, L, or M
This instruction is used to compare contents of the Accumulator with R. The result of compare
operation will be stored in the Temp register. Temp is an internal register that is not accessible to the
programmer. As R can have any of the eight values, there are eight opcodes for this type of instruc¬
tion. It occupies only 1 byte in memory. ‘CMP E’ is an example instruction of this type. It is a 1-byte
instruction. The result of execution of this instruction is shown below with examples.
Example 1
Before After
(A) F5H
(E) 25H
(Temp) 45 H DOH
(F) any values Cy= 0, AC= 1, Z= 0, P= 0, S= 1
84
Example 2
Before After
(A) 50H
(E) 70H
(Temp) 45H EOH
8 (F) any values Cy = 1, AC = 1, Z =0, P =0, S = 1
So
° Example 3
!
.2 Before After
2 (A) 50H
in
«9 (E) 50H
s (Temp) 45H 00H
jj (F) any values Cy = 0, AC = 1, Z = 1, P = 1, S = 0
Example 4
Before After
(A) F5H
(E) D5H
(Temp) 45H 20H
(F) any values Cy = 0, AC = 1, Z = 0, P = 0, S = 0
Example 5
Before After
(A) 25H
(E) F5H
(Temp) 45H 30H
(F) any values Cy = 1, AC = 1,Z = 0, P = 1,S = 0
Summary: CMP R (1 byte; CMP E; 8 opcodes)
CPI is a mnemonic that stands for ‘ComPare Immediate with Accumulator’ and ‘d8’ stands for any
8-bit data. This instruction is used to compare Accumulator with 8 bit immediate data. The result of
the comparison will be stored in an internal register not accessible to the programmer. All the flags are
affected based on the result. It occupies 2 bytes in memory. ‘CPI F5H’ is an example instruction of
this type. It is a 2-byte instruction. The result of execution of this instruction is shown below with an
example.
Before After
(A) 25H
(Temp) 45H 30H
(F) any values Cy = 1, AC = 1,Z = 0, P = 1,S=0
Intel 8085 provides instructions to rotate Accumulator contents left or right. It is to be noted here that
rotate operation can be performed only on Accumulator contents. These instructions are explained as
follows. r
<s.
3
%
ID
8.7.1 INSTRUCTION TYPE RLC
II■i
RLC stands for ‘Rotate Left Accumulator’. It rotates the Accumulator contents to the left by 1-bit 3 2.
position. Fig. 8.1 illustrates this operation.
Cy Accumulator
Fig. 8.1
o
b Rotate left without
involving Cy in rotation
As can be seen, after rotate left operation, the bit that moves out from the MS bit position goes to
the vacancy created in the LS bit position. Also, Cy flag gets a copy of the bit moved out from the MS
bit position. Notice that Cy flag is not involved in the rotation, and it is only 8-bit rotation of accumu¬
lator contents. Only Cy flag is affected by this instruction execution.
The instruction is useful in the following ways.
■ To check the value of the MS bit of Accumulator, perform rotate left, and note the cy flag value.
■ To perform multiplication by 2, rotate the Accumulator to left. It works correctly for unsigned
numbers, as long as the MS bit of Accumulator is a 0 before rotation. For multiplication by 2",
perform rotate left n times.
Example 1
Before After
(A) 24H 48H
(Cy) 1 0
Example 2
Before After
(A) 84H 09H
(Cy) 0 1
Note that accumulator value is not doubled in this case because MS bit of accumulator was a 1
before rotation.
86
8.7.2 INSTRUCTION TYPE RAL
RAL stands for ‘Rotate Accumulator Left involving Cy flag in rotation’. It rotates the Accumulator
contents to the left by 1-bit position. Fig. 8.2 illustrates this operation.
8 Cy Accumulator
Fig. 8.2
Su Rotate left involving
£ carry in rotation
!
.2 As can be seen, after rotate left operation, the bit moves out from the MS bit position and goes to
2
in the Cy flag, and in the process moves out the earlier carry bit to the vacancy created in the LS bit posi¬
«9
s tion. Notice that Cy flag is involved in the rotation, and it is 9-bit rotation of Accumulator and Cy con¬
tents. Only Cy flag is affected by this instruction execution.
Jj
The instruction is useful in the following ways.
■ To check the value of the MS bit of Accumulator, perform rotate left, and note the Cy flag value.
■ To perform multiplication by 2, rotate the Accumulator to the left. It works correctly for unsigned
numbers, as long as the MS bit of Accumulator and Cy flag are 0 before rotation. For multiplica¬
tion by 2", perform rotate left n times.
■ To introduce a new bit value to the LS bit position, put this bit value in the Cy flag and then
execute this instruction.
RRC stands for ‘Rotate Right Accumulator’. It rotates the Accumulator contents to the right by 1-bit
position. Fig. 8.3 illustrates this operation.
87
Accumulator Cy
Figure. 8.3
Rotate right without involving
carry in rotation
As can be seen, after rotate right operation, the bit moves out from the LS bit position and goes to
the vacancy created in the MS bit position. Also, Cy flag gets a copy of the bit moved out from the
_r
a
g
LS bit position. Notice that the Cy flag is not involved in the rotation, and it is only 8-bit rotation of % “O
Accumulator contents. Only Cy flag is affected by this instruction execution.
The instruction is useful in the following ways.
II
■o
■ To check the value of the LS bit of Accumulator, perform rotate right, and note the Cy flag value.
■ To perform division by 2, rotate the Accumulator to the right. It works correctly for unsigned
numbers, as long as the LS bit of Accumulator is a 0 before rotation. For division by 2", perform
rotate right n times.
RAR stands for ‘Rotate Accumulator Right involving Cy flag in rotation’. It rotates the Accumulator
contents to the right by 1-bit position. Fig. 8.4 illustrates this operation.
Accumulator Cy
Figure. 8.4
Rotate right involving
carry in rotation
As can be seen, after rotate right operation, the bit moves out from the LS bit position and goes to
the Cy flag, and in the process moves out the earlier carry bit to the vacancy created in the MS bit
position. Notice that Cy flag is involved in the rotation, and it is 9-bit rotation of Accumulator and Cy
contents. Only Cy flag is affected by this instruction execution.
88
The instruction is useful in the following ways.
■ To check the value of the LS bit of Accumulator, perform rotate right, and note the Cy flag value.
■ To perform division by 2, rotate the Accumulator to the right. It works correctly for unsigned num¬
bers, as long as the LS bit of Accumulator and Cy flag are 0 before rotation. For division by 2",
o perform rotate right n times.
0
■ To introduce a new bit value to the MS bit position, put this bit value in the Cy flag and then
execute this instruction.
2
a
S The result of execution of this instruction is shown below with examples.
.2
® Example 1
U)
Before After
o (A) 48H 24H
(Cy) 0 0
Note that Accumulator value is halved.
Example 2
Before After
(A) 49H 24H
(Cy) 0 1
Note that Accumulator value is not halved in this case because LS bit of Accumulator was a 1
before rotation.
Example 3
Before After
(A) 48H A4H
(Cy) 1 0
Note that Accumulator value is not halved in this case because Cy flag was a 1 before rotation.
1 . Write an 8085 assembly language program, which takes the data from memory location X, and
sets to 1 all the odd numbered bits of this byte, and stores the result at memory location Y.
2. Write an 8085 assembly language program, which takes the data from memory location X,
and resets to 0 all the even numbered bits of this byte, and stores the result at memory
location Y.
3. Write an 8085 assembly language program that takes the data from memory location X, and
complements the 4 bits in the middle of this byte, and stores the result at memory location Y.
4. Write an 8085 assembly language program, which takes the data from memory location X,
and sets to 1 the MS 2 bits, complements the 4 bits in the middle of this byte, and resets to
0 the LS 2 bits, and stores the result at memory location Y.
5. Write an 8085 assembly language program, which takes the data from memory location
X, and multiplies this byte by 4, and stores the result at memory location Y.
89
6. Write an 8085 assembly language program, which takes the data from memory location X,
and divides this byte by 8, and stores the result at memory location Y.
7. Write an 8085 assembly language program, which takes the data from memory location
X, and multiplies this byte by 10, and stores the result at memory location Y.
8. Assume that before the execution of any instruction we have (A) = 65H, (B) = B2H, (H) =
F9H, (L) = 50H, Cy flag = 1, and content of memory location F950H is 38H. What is the
value of A register and value of different flags after the execution of each of the following.
r
<s.
a. CMP B
3
%
ID
b.
c.
CMPM
CPI55H
II■o
d. RAL
e. RRC
f. CMA
g. CMC
9. Distinguish between the following pairs of instructions.
a. RAL and RLC
b. RRC and RLC
c. CMA and CMC
d. XRAM and ORAM
10. Explain the working of CMP instruction with examples.
'•>>>,
9
NOP and Stack
mi;P: v
Group of
Instructions
■ Questions
A total of nine instruction types covering 15 instructions will be explained in this chapter.
The NOP and stack group of instructions are discussed in depth in this chapter. In the first half, the
role of the stack pointer (SP) is explained and the various instruction types are dealt with in the
remaining part of the chapter.
Stack is a LIFO (last in, first out) data structure implemented in the RAM area and is used to store
addresses and data, when the microprocessor branches to a subroutine. Subroutines are discussed in
the next chapter.
In the programmer’s view of 8085, only the general purpose registers A, B, C, D, E, H, and L, and
the Flags registers were discussed so far. But in the complete programmer’s view of 8085, there are
two more special purpose registers, each of 16-bit width. They are the stack pointer, SP, and the pro¬
gram counter, PC. The role of PC will be explained in the next chapter. The complete programmer’s
view of 8085 is shown in Fig. 9.1.
91
8 bits 8 bits
A F
B
D[ ]E
H L
PC [
f
Fig. 9.1 •B
SP Programmer’s view
■<— 16 bits —► of 8085
r«2
SP is a special purpose 16-bit register. It contains a memory address. Suppose SP contents are 2. 0 H
FC78H, then the 8085 interprets it as follows. § 3
Memory locations FC78H, FC79FI, ... , FFFFFI are having useful information. In other words, these " ■§
locations are treated as filled locations. Memory locations FC77FI, FC76FI, ... , 0000FI are not having
any useful information. In other words, these locations are treated as empty locations.
Thus, the contents of SP specify the top most useful location in the stack. In other words, it indi¬
cates the memory location with the smallest address having useful information. This is pictorially rep¬
resented in Fig. 9.2.
FC75H
FC76H
SP |FC78 |
FC77H |Empty
FC78H
FC79H
1 Full
FC7AH
Fig. 9.2
Interpretation of SP
contents
Let us say SP contents are FC78H, and we want to read information from a stack location. In this case,
we are not interested in reading from a location whose address is less than the memory address
present in SP. This is because 8085 interprets them as useless information. For example, there is no
point in reading useless information from memory location FC75H.
Right now, memory locations FC78H, FC79H, ... , FFFFH are all interpreted by 8085 to have use¬
ful information. Then, which one of these useful information to read? The 8085 reads from the top of
the stack of useful information. The 8085 is designed to read always 2 bytes useful information from
the top of stack. As such, in this case it reads from locations FC78H and FC79H. In 8085, this infor¬
mation, which is read can only be loaded into a register pair. This operation of loading a register pair
by reading information from the stack top is called a POP operation.
Suppose we load register pair BC when SP contents was FC78H. Then, information from memory
locations FC78H and FC79H are copied to BC pair. But after this pop operation, the contents of FC78H
and FC79H are treated as useless by the 8085. This is because, a copy is there in the register pair BC
92
anyway! Thus only FC7AH, FC7BH, ... , FFFFH are useful locations. To indicate this, the SP contents
are changed to FC7AFI. This is done automatically by 8085 by incrementing SP contents by 2.
8 Let us say SP contents are FC7AFI, and we want to write information to a stack location. In this case,
2
0 we are not interested in writing to a location whose address is equal or greater than the memory
2
a address present in SP. This is because the 8085 interprets them as having useful information, which
2 should not be destroyed! For example, there is no point in overwriting and destroying useful informa¬
.2 tion at memory location FD7AFI. We should be writing into a location where there is presently use¬
£
IA less information, and make it useful!
a
s Right now, memory locations FC79FI, FC78FI, ... , 0000FI are all interpreted by 8085 to have useless
information. Then, which one of these useless locations to overwrite? The 8085 writes above the top of
4j
the stack of useful information. The 8085 is designed to write always into two locations of useless infor¬
mation just above the top of stack. As such, in this case it writes to locations FC79H and FC78H. In 8085,
this information, which is written can only be coming from a register pair. This operation of storing a
register pair by writing information above the stack top is called a PUSH operation.
Suppose we store register pair BC when SP contents were FC7AH, then, information from BC pair
is stored in memory locations FC79H and FC78H. But after this push operation, the contents of
FC79H and FC78H are treated as useful by the 8085. This is because nobody stores useless informa¬
tion! Thus FC78H, FC79H, ... , FFFFH are all useful locations. To indicate this, the SP contents are
changed to FC78H. This is done automatically by 8085 by decrementing SP contents by 2.
This instruction loads register pair rp by popping out 2 bytes from the top of the stack. In previous
chapters, ‘rp’ stood for any of the register pairs BC, DE, or HL. But as per the programmer’s view in
Fig. 9. 1, we can treat combination of Accumulator and flags as one more register pair. This pair is gen¬
erally called PSW, which stands for ‘processor status word’. In the PSW, Accumulator is the MS byte,
and Flags register is the LS byte.
Two bits are used in an opcode to specify a register pair. Thus actually four register pairs can be
specified using 2-bit code for ‘rp’. In fact, even SP can be treated as a register pair. Thus in all there
are five register pairs. One may think that to specify one of them, 3 bits are needed. But 8085 opcodes
use only 2 bits to specify a register pair as shown below. The ‘rp’ code of 11 specifies either SP or
PSW, but not both. For example, in POP rp instruction, rp can be BC, DE, HL, or PSW. There is no
instruction like POP SP. Similarly, in LXI rp instruction, rp can be BC, DE, HL, or SP. There is no
instruction like LXI PSW.
rp code register pair
00 BC
01 DE
10 HL
11 SP or PSW, but never both.
Thus, in POP rp instruction ‘rp’ stands for one of the following register pairs.
rp = BC, DE, HL, or PSW
93
As rp can have any of the four values, there are four opcodes for this type of instruction. It occu¬
pies only 1 byte in memory. ‘POP PSW’ is an example instruction of this type. It is an 1-byte instruc¬
tion. The result of execution of this instruction is shown below with an example.
Before After
(SP) FE06H FE08H f
•B
(FE06) 66H
(FE07) 55H
(A)
(F)
33H
44FI
55H
66H
r«2
<A
II
o
-
*
Note that POP PSW instruction is useful in loading the flags register with any desired value. Suppose o jj
it is desired to load flags register with the value 66FI, the following instructions have to be executed. » c
MVI C, 66H
PUSHB
POP PSW
Summary. POP rp (1 byte; POP PSW; 4 opcodes)
This instruction stores contents of register pair rp by pushing it into two locations above the top of the
stack, rp stands for one of the following register pairs.
Let us say, SP contents is FC00H and it is desired to pop contents of FC06H and FC07H to BC
register pair. One way of achieving this objective is by executing the following sequence of
instructions.
However, this is a laborious process. Loading SP with FC06H, and then popping to BC pair can
achieve the same very efficiently. When SP is loaded with FC06H, 8085 interprets that FC06H,
FC07H, ... , FFFFH are all filled locations. Then POP B instruction execution results in popping con¬
tents of FC06H and FC07H to BC pair.
LXI SP, dl6 instruction is a special case of LXI rp, dl6 which was discussed in the chapter on data
transfer group of instructions. Thus LXI SP instruction is used to load 16-bit immediate data to the SP. It
occupies 3 bytes in memory. ‘LXI SP FC06H’ is an example instruction of this type. It is a 3-byte instruc¬
tion. The result of execution of this instruction is shown in the following example.
95
Before After
(SP) FCOOH FC06H
Summary. LXI SP dl6 (3 bytes; LXI SP, FC06H; 1 opcode)
f
■o
m 9.5 INSTRUCTION TYPE SPHL
3 </>
W
2 0
This instruction loads the stack pointer with the contents of register pair FIL. It is an indirect way of *> *
loading the stack pointer, and as such, it is not quite commonly used. It occupies only 1 byte in |g
memory, compared to LXI SP instruction, which is 3 bytes long. Because of this advantage, SPFIL can w £
be useful when SP is required to be initialized to a specific value a number of times in a program.
The result of execution of this instruction is shown below with an example.
Before After
(HL) FB50H
(SP) F900H FB50H
Summary: SPFIL (1 byte; SPFIL; 1 opcode)
XTHL is a mnemonic that stands for ‘exchange Top of stack with HL’. This instruction exchanges
the contents of the top two locations of the stack with the contents of register pair HL. Note that it is
not exchange of SP with HL.
It occupies only 1 byte in memory. The result of execution of this instruction is shown below with
an example. Note that SP contents remain unchanged. It is neither decremented nor incremented.
Before After
(SP) F900H
(HL) FB50H 3525H
(F900) 25H 50H
(F901) 35H FBH
Summary : XTHL (1 byte; XTHL; 1 opcode)
INX SP instruction is a special case of INX rp that was discussed in the chapter on arithmetic group
of instructions. Thus INX SP instruction is used to increment the SP contents by 1. It occupies only 1
byte in memory. The result of execution of this is shown in the following example.
96
Before After
(SP) FCOOH FCOIH
Summary: INX SP (1 byte; INX SP; 1 opcode)
8
o ■ 9.8 INSTRUCTION TYPE DCX SP
2
a
2
.2 DCX SP instruction is a special case of DCX rp that was discussed in the chapter on arithmetic group
g
in of instructions. Thus DCX SP instruction is used to decrement the SP contents by 1. It occupies only
sco 1 byte in memory. The result of execution of this instruction is shown below with an example.
H
Before After
(SP) FCOOH FBFFH
Summary: DCX SP (1 byte; DCX SP; 1 opcode)
DAD SP instruction is a special case of DAD rp, which was discussed in the chapter on arithmetic
group of instructions. Thus DAD SP instruction is used to add SP contents to HL contents, and store
the result in HL. It occupies only 1 byte in memory. The result of execution of this instruction is shown
below with an example. Only carry flag is affected depending on the result.
Before After
(HL) 0234H FA84H
(SP) F850H
(F) any values Cy = 0, no change in other flags
Summary: DAD SP (1 byte; DAD SP; 1 opcode)
NOP is an instruction that cannot be classified under any group discussed so far. For convenience, we
discuss this instruction in this chapter.
NOP is a mnemonic that stands for ‘No OPeration’. This instruction does not do anything! Still, it
is quite useful. It occupies only 1 byte in memory. It has the opcode 00H. It is useful in the following
cases.
1. NOP instruction is very useful for generating small-time delays of the order of a few microsec¬
onds. Let us say, the 8085 has to send information to two peripherals with a gap of approximately
4 |xs. In such a case, after sending information to the first peripheral, it has to wait for 4 |xs. This
97
is where an NOP instruction is very useful. Every instruction needs a certain time for instruction
fetch and execution. These details are provided in a later chapter on 8085 architecture. NOP
instruction uses up about 1.3 p,s for the instruction fetch and execution. If there are three
NOP instructions, then about 4 p,s delay is generated. After these NOP instructions, information
can be sent to the second peripheral.
2. NOP instruction is very useful when we are required to delete a few instructions in our f
program. Let us say we have a long program as shown in the following. ■o
Memory Contents 3 (A
address
F800H LDA FEOOH II
0
?
*
F803H MOV C, A § ° 3
</> c
F804H MOV D, A
F805H MOV E, A
FD56H HLT
When the program is executed, let us say we do not get the desired result, because the program
is wrong. Then we go about debugging the program in single step mode. Let us say, we come to
the conclusion that the instruction ‘MOV D, A’ at location F804FI should be removed. If we
remove the instruction ‘MOV D, A’, then the program portion from memory location F805H to
FD56H should be moved up by one position. This amounts to almost rewriting the whole pro¬
gram, as the error has occurred in the beginning of the program. Anyway, we do not have to lose
heart, as NOP instruction is there to our rescue. We just have to replace ‘MOV D, A’ with NOP
instruction! Then there is no need for moving up by one position the program portion from F805H
to FD56H.
3. NOP instruction is very useful when we are required to insert a few instructions in our
program. Let us say we have a long program as shown in the following.
Memory Contents
address
F800H LDA FEOOH
F803H MOV B, A
F804H MOV D, A
F810H ADD E
F811H NOP
FD56H HLT
When the program is executed, let us say we do not get the desired result, because the
program is wrong. Then we go about debugging the program in single step mode. Let us say, we
come to the conclusion that in between MOV B, A and MOV D, A there should have been MOV
C, A also. If we insert the instruction ‘MOV C, A’ at F804H, then the original program portion
from memory location F804H to FD56H should be moved down by one position. This amounts
98
to almost rewriting the whole program, as the error has occurred in the beginning of the program.
Anyway, we do not have to lose heart, if we have NOP instructions at several places in our
program. Suppose we have a NOP instruction at F811H, then we have to move down only a
small portion of the program from F804H to F810H by one location. Now the earlier NOP
instruction is replaced with ADD E.
s Thus, it is a good programming practice to have a few NOP instructions in the program at
u regular intervals, especially during program development. Alternatively, the user can make use
of ‘insert’ and ‘delete’ keys provided on the kit, which help in inserting new instructions to the
£
a program, or delete existing instructions.
E
.2 Summary. NOP (1 byte; NOP; 1 opcode)
S
in
jj
The way 8085 executes a simple program (without ‘jump’ instructions) is as follows. It fetches the
first instruction from memory and executes it. Then it fetches the next instruction from memory and
executes it. It continues this way till the halt instruction is fetched and executed.
For example, to compute the 2’s complement of the number at location F840H and store the result
in location F850H, the program shown in Fig. 10.1 has to be executed.
LDA F840H
CMA
Fig. 10.1
INR A
Assembly language program
STA F850H
to compute 2’s complement
HLT
Let us say, the machine language program is stored starting from memory location F820H as shown
in Fig. 10.2.
Memory Memory
address contents
F820H 3AH
F821H 40H
F822H F8H
F823H 2FH
F824H 3CH
F825H 32H
Fig. 10.2
F826H 5OH
Machine language
F827H F8H
program to compute
F828H 76H 2’s complement
101
To execute the program, 8085 will have to send out the address F820H and fetch the opcode 3AH
for the first instruction ‘LDA F840H’ from memory for execution. In such a case, from which regis¬
ter in 8085 the address value F820FI is sent out? The address is sent out from a special purpose
register called program counter (PC for short).
n
3
10.1.1 PC— PROGRAM COUNTER 3
a =ÿ
PC is a 16-bit register. It contains a memory address. Suppose the PC contents are F820FI, then it means 2 2
ao
that the 8085 desires to fetch the instruction byte at F820FI. After fetching the byte at F820FI, the PC oS. vs
is automatically incremented by 1. This way 8085 becomes ready to fetch the next byte of the instruc¬ sa
tion (in case instruction fetch is incomplete), or fetch the next opcode (in case instruction fetch is over).
In the previous program example, first of all PC is loaded with the value F820H. This is done by
typing the ‘Go’ key, then typing ‘F820’, and finally typing the ‘Exec’ key. Then the 8085 performs the
following action. It sends out F820H the address, which is the content of the PC. From location F820H
it receives 3AH, the opcode for LDA. It is received in an 8-bit register called instruction register (IR
for short), as shown in Fig. 10.3.
IR is a special purpose register, which is used to receive the 8-bit opcode portion of an instruction.
It is not accessible to the programmer. What it means is that there are no instructions by which the
programmer can load it with values of his choice. For example, instructions like ‘MOV IR, D’ or
‘MVI IR, 45H’ are not present in the instruction set of 8085. Thus, IR register is not shown in the
programmer’s view of 8085.
After the opcode is fetched from location F820H, the PC is incremented to F821H and is sent out as
address. Then 8085 receives 40H, the LS byte of data address F840H. It is received in an 8-bit regis¬
ter called Z register. Z is a register, which is used to receive the LS byte portion of the 16-bit address
in a 3-byte instruction. It is not accessible to the programmer. Thus, Z register is not shown in the
programmer’s view of 8085.
Next, PC is incremented to F822H and it receives F8H, the MS byte of data address F840H. It is
received in an 8-bit register called W register. W is a register that is used to receive the MS byte
portion of a 16-bit address in an instruction. It is not accessible to the programmer.
102
Now PC is incremented to F823H, but the next instruction fetch does not start as yet. This is
because, so far, 8085 has only completely fetched the instruction code for LDA F840H, but it has not
yet executed it!
The 8085 executes the instruction LDA F840FI, by sending out the address F840FI from W and Z
registers, and loading the accumulator with the contents of memory location F840FI.
Only after the 8085 executes ‘LDA F840FF instruction, it sends out the address F823FI, which is
8
<2 the content of PC, and receives 2FFI, the opcode for CMA. It is received in the IR register. Meanwhile
0
the PC is incremented to F824FI. As the complete instruction is received by the 8085, it then executes
2
a it using the ALU for the complement operation.
2 Only after CMA instruction is executed by the 8085, it sends out the address F824FI, which is the
.2
£ content of PC, and receives 3CFI, the opcode for INR A. It is received in the IR register. Meanwhile
in
a the PC is incremented to F825H. As the complete instruction is received by the 8085, it then executes
s« it using the ALU for the increment operation.
Only after INR A instruction is executed by the 8085, it sends out the address F825H, which is the
content of PC, and receives 32H, the opcode for STA. It is received in the IR register. Meanwhile the
PC is incremented to F826H.
Then F826H from PC is sent out as address and 8085 receives 50H, the LS byte of address F850H.
It is received in Z register. Meanwhile PC is incremented to F827H. Then F827H from PC is sent out as
address and 8085 receives F8H, the MS byte of address F850H. It is received in W register. Meanwhile
PC is incremented to F828H. But the next instruction fetch at F828H does not start as yet. This is
because, so far the 8085 has only completely fetched the instruction code for STA F850H, but it has not
yet executed it! The 8085 executes the instruction STA F850H, by sending out the address F850H from
W and Z registers, and storing the accumulator contents in memory location F850H.
Only after STA F850H instruction is executed by the 8085, it sends out the address F828H, which
is the content of PC, and receives 76H, the opcode for HLT. It is received in the IR register. Mean¬
while the PC is incremented to F829H. As the complete instruction is received by the 8085, it then
executes it, which results in halting the processor.
The role of PC in 8085 for simple programs (without jump instructions) can be summarized as
follows:
PC is a 16-bit register. It contains a memory address. Suppose the PC contents are F820H, then it
means that the 8085 desires to fetch and execute the instruction starting at F820H. After fetching
the instruction starting at F820H, the PC is automatically incremented by 1, 2, or 3 depending on
the instruction length. Then the instruction is executed, during which the PC value is not changed.
This way PC will be pointing to the next instruction by the time the current instruction is executed.
In simple programs there is a linear program flow from the first instruction to the last instruction in
the program. But the real power of a computer exists in its ability to change program flow, and the
programs should be written to take advantage of this ability.
Change in program flow is needed when a sequence of instructions is to be executed repeatedly. It
is also needed when it is required to choose between two or more sequences of actions based on some
conditions. The branch group of instructions in 8085 effects such a change in program flow. The
branch instructions can be used for effecting a forward branch or a backward branch.
103
An important subgroup of the branch group of instructions is the jump instructions. The jump
instructions are classified into:
Memory Instruction
address
F820H JMPF950H-
F823H MOV B, C
In the previous example, after 8085 fetches ‘JMP F950H’ the PC value would have been automat¬
ically incremented by 3 to F823H. Functionally, JMP F950H can be treated as:
JMP F950H = LXI PC, F950H
Thus, execution of JMP F950H results in loading of PC with the value F950H, overwriting the
earlier value of F823H. Hence, after execution of JMP F950H, the instruction MOV C, B at mem¬
ory location F950H will be fetched and executed. In this example, a forward jump was effected.
JMP F805H instruction at location F820H, effects a backward jump.
In the instruction set of 8085, ‘JMP’ is used instead of ‘LXI PC’, because JMP very explicitly indi¬
cates a change in program flow. But, LXI PC just indicates that PC value is changed, but its implied
change in program flow may go unnoticed by the programmer.
The result of execution of ‘JMP F950H’ can be indicated in terms of change in PC value as follows.
Before After
(PC) F820H F950H
Summary: JMP al6 (3 bytes; JMP F950H; 1 opcode)
PCHL is a mnemonic, which stands for ‘Load PC with contents of HL\ This instruction is used to
jump to the address provided in HL register pair. Thus it is an unconditional indirect jump instruction.
Because of its indirect jump feature, it is not very commonly used.
It is a 1-byte instruction compared with the direct jump instruction, which is 3-bytes long. Because
of this size advantage, it can be useful for jumping to a frequently used portion of the program.
The result of execution of this instruction is shown below with an example.
104
Memory Instruction
address
F820H LXI H, F950H
F823H PCHL -
F824H MOV B, C
8
<2
0
2
a F950H MOV C, B; This is executed next after PCHL
2
.2
£ In the previous example, after 8085 fetches ‘PCHL’ the PC value would have been automatically
in
a incremented by 1 to F824H.
s« But execution of PCHL results in loading of PC with the value F950H, which is the content of HL,
overwriting the earlier value of F824H. Hence, after execution of PCHL, the instruction ‘MOV C, B’
at memory location F950H will be fetched and executed.
The result of execution of ‘PCHL’ can be indicated in terms of change in PC value as follows.
Before After
(HL) F950H
(PC) F823H F950H
Summary. PCHL (1 byte; PCHL; 1 opcode)
The conditional jump instructions of 8085 perform a jump based on the value of a single flag. The
jump takes place based on the value of carry flag, zero flag, parity flag, or sign flag. There is no jump
instruction based on the value of auxiliary flag. This is because, generally no one is interested in
performing a jump based on this flag!
Memory Instruction
address
F820H JNC F950H -
F823H MOV B, C; This is executed next after JNC F950H if Cy=l
aD
3 s3
0
10.3.2 JC a 1 6 —JUMP IF CARRY 2. V
S°
JC is a mnemonic, which stands for ‘Jump if Carry’. This instruction is used to jump to the address
al6 provided in the instruction, only if carry flag value is 1. If carry flag value is 0, program flow con¬
tinues sequentially.
‘JC F950H’ is an example instruction of this type. It is a 3-byte instruction. The result of execution
of this instruction is shown below with an example.
Memory Instruction
address
F820H JC F950H -
F823H MOV B, C; This is executed next after JC F950H if Cy =0
Memory Instruction
address
F820H JNZF950H -
F823H MOV B, C; This is executed next after JNZ F950H if Z=1
JZ is a mnemonic, which stands for ‘Jump if Zero result’. This instruction is used to jump to
the address al6 provided in the instruction, only if result is zero, indicated by Z flag value of 1. If
Z flag value is 0, program flow continues sequentially. ‘JZ F950H’ is an example instruction of
o this type. It is a 3-byte instruction. The result of execution of this instruction is shown below with
an example.
0
2a.
s
s
U) Memory Instruction
CO address
s F820H JZ F950H -
Jj F823H MOV B, C; This is executed next after JZ F950H if Z=0
H
JPO is a mnemonic, which stands for ‘Jump if Parity Odd’. This instruction is used to jump to the address
al6 provided in the instruction, only if parity flag value is 0. If parity flag value is 1, program flow
continues sequentially. ‘JPO F950H’ is an example instruction of this type. It is a 3-byte instruction.
Summary: JPO al6 (3 bytes; JPO F950H; 1 opcode)
JPE is a mnemonic, which stands for ‘Jump if Parity Even’. This instruction is used to jump to the
address al6 provided in the instruction, only if parity flag value is 1. If parity flag value is 0, program
flow continues sequentially.
Summary: JPE al6 (3 bytes; JPE F950H; 1 opcode)
JP is a mnemonic, which stands for ‘Jump if Positive’. This instruction is used to jump to the address
al6 provided in the instruction, only if sign flag value is 0. If sign flag value is 1, program flow
continues sequentially.
Summary: JP al6 (3 bytes; JP F950H; 1 opcode)
107
10.3.8 JM a 1 6— JUMP IF MINUS
JM is a mnemonic, which stands for ‘Jump if Minus’. This instruction is used to jump to the address
al6 provided in the instruction, only if sign flag value is 1. If sign flag value is 0, program flow
continues sequentially.
n
Summary: JM al6 (3 bytes; JM F950H; 1 opcode) 3
3
a =ÿ
aD
3 3
o s
av
■ 1 0.4 UNCONDITIONAL CALL AND RETURN INSTRUCTIONS S 2.
Very often a particular sequence of instructions have to be used at several points in the program.
Writing the same sequence of instructions at these various points can be avoided by writing this series
of instructions as a subprogram. Such subprograms are also variously termed as subroutines, or pro¬
cedures.
Whenever the instructions in a subroutine are required to be executed, we branch to the subroutine
using the CALL instruction. It is a 3-byte instruction, with 1 byte for the opcode, and 2 bytes for the
address of subroutine. CALL stands for ‘call a subroutine’. After executing the instructions in the
subroutine we return to the next instruction after the CALL instruction in the main program. This is
achieved by the execution of the RET (stands for RETum from subroutine) instruction in the subrou¬
tine. RET is a 1-byte instruction.
The difference between a JMP instruction and a CALL instruction is as follows. If a JMP instruction
is executed, we jump to the destination location, and the execution carries on from there, without both¬
ering to come back later to the instruction after the JMP.
If a CALL instruction is executed, we jump to the subroutine, and the execution carries on from
there till the RET instruction is executed in the subroutine, and then we come back to the instruction
after the CALL in the main program.
The address of the next instruction after the CALL instruction is called the return address. This is
the address to which the program flow returns when the RET instruction is executed by the 8085. By
the time the 8085 fetches the call instruction, the PC would have been incremented by 3, and will be
pointing to the next instruction after call. In other words, PC will have the return address by the time
the call instruction is fetched.
In order to facilitate such a return, the CALL instruction will first of all store above the top of stack,
the return address. Only then the branch to the subroutine takes place.
Thus a CALL instruction can be visualized as push PC value on the stack, and then branch to the
subroutine. Thus CALL F900H can be treated as:
Note that there is no direct instruction like PUSH PC in 8085. As part of the execution of CALL
instruction, PUSH PC operation is performed.
108
The RET instruction, which is at the end of a subroutine, loads the PC with the return address
popped out from the top of the stack, so that a branch back to the calling program at the next instruc¬
tion after CALL is achieved. Thus RET can be treated as:
RET = POP PC
In the instruction set of 8085, ‘RET’ is used instead of ‘POP PC’, because RET very explicitly indi¬
s cates a return back to the calling program. But, POP PC just indicates that PC value is changed, but
a>
o its implied return to the calling program may go unnoticed by the programmer.
sa The use of CALL F950H to branch to the subroutine at F950H, and the RET instruction in the sub¬
2 routine to get back to the main program are illustrated in Fig. 10.4.
.2
s
in Memory Instruction
oo
s address
Jj
H F820H -
CALL F950H
F823H MOV B, C; Executed after RET at location F995H
F890H HLT
—
1 ► F950H MOV C, B; Executed next after CALL F950H
Fig. 10.4
F995H RET Usage of CALL and RET
instructions
The way stack contents get affected due to execution of ‘CALL F950H’ is shown below.
Before After
(PC) F820H F950H
(SP) FC08H FC06H
(FC07) 60H F8H
(FC06) 70H 23H
Note that if the CALL instruction is at location F820H, the return address stored on the top of the
stack is F823H.
Summary. CALL al6 (3 bytes; CALL F950H; 1 opcode)
The way in which the stack contents get affected due to execution of RET is shown below.
Before After
(PC) F995H F823H
(SP) FC06H FC08H
(FC06) 23H
(FC07) F8H
Summary: RET (1 byte; RET; 1 opcode)
Using this approach, we can branch any number of times to the subroutine, which is written only
once. This saves a lot of memory space for the complete program.
Also usage of subroutines helps in modular design for solving a programming problem. It makes
the main program look very simple and compact.
109
■ 1 0.5 CONDITIONAL CALL INSTRUCTIONS
The conditional call instructions of 8085 branch to a subroutine based on the value of a single flag.
The branch takes place based on the value of Cy flag, Z flag, P flag, or S flag. There is no call instruc¬
tion based on the value of auxiliary flag. This is because, generally no one is interested in branching
n
3
to a subroutine based on this flag! The conditional call instructions are 3 bytes in length, 1 byte for 3
the opcode, and another 2 bytes for the subroutine address. a =ÿ
aD
3 3
0 s
av
S 2.
10.5.1 CNC a 1 6— CALL IF NOT CARRY
CNC is a mnemonic, which stands for ‘Call if Not Carry’. This instruction is used to branch to the
subroutine whose 16-bit address is provided in the instruction, only if Cy flag value is 0. If Cy flag
value is 1, program flow continues in the main program sequentially. ‘CNC F950H’ is an example
instruction of this type. It is a 3-byte instruction. The result of execution of this instruction is shown
below with an example.
Memory Instruction
address
F880H HLT
F980H RET
In the previous example, after 8085 fetches ‘CNC F950FT the PC value would have been automat¬
ically incremented by 3 to F823H. But CNC F950H instruction execution results in saving PC value
on the stack and loading of PC with the value F950H, only if Cy flag value is 0.
Hence, after execution of CNC F950H, branch to subroutine at memory location F950H will take
place only if Cy flag value is 0. After executing the RET instruction in the subroutine, the program
flow will continue with the instruction at F823H in the main program.
If Cy flag value is 1, the PC value will remain as F823H, and so the program flow continues with
the instruction MOV B, C in the main program.
Summary: CNC al6 (3 bytes; CNC F950H; 1 opcode)
no
10.5.2 CC a 16— CALL IF CARRY
CC is a mnemonic, which stands for ‘Call if Carry’. This instruction is used to branch to the subrou¬
tine whose 16-bit address is provided in the instruction, only if Cy flag value is 1. If Cy flag value is 0,
program flow continues in the main program sequentially.
s Summary : CC al6 (3 bytes; CC F950H; 1 opcode)
a>
o
sa
o 10.5.3 CNZ a 16— CALL IF NOT ZERO RESULT
s CNZ is a mnemonic, which stands for ‘Call if Not Zero result’. This instruction is used to branch to
in
00
s the subroutine whose 16-bit address is provided in the instruction, only if result is not zero, indicated
by Z flag value of 0. If Z flag value is 1, program flow continues in the main program sequentially.
Jj
H Summary. CNZ al6 (3 bytes; CNZ F950H; 1 opcode)
CZ is a mnemonic, which stands for ‘Call if Zero result’. This instruction is used to branch to the sub¬
routine whose 16-bit address is provided in the instruction, only if result is zero, indicated by Z flag
value of 1. If Z flag value is 0, program flow continues in the main program sequentially.
Summary. CZ al6 (3 bytes; CZ F950FI; 1 opcode)
CPO is a mnemonic, which stands for ‘Call if Parity Odd’. This instruction is used to branch to the
subroutine whose 16-bit address is provided in the instruction, only if P flag value is 0. If P flag value
is 1, program flow continues in the main program sequentially.
Summary : CPO al6 (3 bytes; CPO F950H; 1 opcode)
CPE is a mnemonic, which stands for ‘Call if Parity Even’. This instruction is used to branch to the
subroutine whose 16-bit address is provided in the instruction, only if P flag value is 1. If P flag value
is 0, program flow continues in the main program sequentially.
Summary: CPE al6 (3 bytes; CPE F950H; 1 opcode)
CP is a mnemonic, which stands for ‘Call if Positive’. This instruction is used to branch to the
subroutine whose 16-bit address is provided in the instruction, only if S flag value is 0. If S flag value
is 1, program flow continues in the main program sequentially.
Summary: CP al6 (3 bytes; CP F950H; 1 opcode)
Ill
10.5.8 CM a 1 6— CALL IF MINUS
CM is a mnemonic, which stands for ‘Call if Minus’. This instruction is used to branch to the subrou¬
tine whose 16-bit address is provided in the instruction, only if S flag value is 1. If S flag value is 0,
program flow continues in the main program sequentially.
n
Summary: CM al6 (3 bytes; CM F950H; 1 opcode) 3
3
a =ÿ
aD
3 3
o s
av
■ 1 0.6 CONDITIONAL RETURN INSTRUCTIONS S 2.
The conditional return instructions of 8085 effect a return to the main program based on the value of
a single flag. The return takes place based on the value of Cy flag, Z flag, P flag, or S flag. There is
no return instruction based on the value of AC flag. This is because, generally no one is interested in
returning to the main program based on this flag! The conditional return instructions are 1 byte
in length.
RNC is a mnemonic, which stands for ‘Return if Not Carry’. This instruction is used to return to the
main program, only if Cy flag value is 0. If Cy flag value is 1, program flow continues in the subrou¬
tine sequentially. It is a 1-byte instruction. The result of execution of this instruction is shown below
with an example.
Memory Instruction
address
F820H -
CALL F950H
> F823H MOV B, C; Executed next after RNC at F960H if Cy=0
; Executed next after RET at F980H if Cy=l
F880H HLT
If Cy=0
F960H RNC
F961H MOV D, E; Executed next after RNC if Cy=l
F980H RET
112
In the previous example, when 8085 executes CALL F950H instruction, it saves PC value of
F823H on the top of the stack, and branches to the subroutine at F950H.
In the subroutine, after 8085 fetches ‘RNC’ the PC value would have been automatically incre¬
mented by 1 to F961H. But RNC instruction execution results in loading PC value with the return
address from the stack top, only if Cy flag value is 0.
Hence, after execution of RNC, return to the main program at memory location F823H will take
s place only if Cy flag value is 0.
a>
o If Cy flag value is 1, the PC value will remain as F961H, and so the program flow continues with
sa the instruction MOV D, E in the subroutine. Finally when the 8085 executes the RET instruction in
2 the subroutine, return to the main program at location F823H takes place.
.2
s
in Summary. RNC (1 byte; RNC; 1 opcode)
00
s
Jj
H
1 0.6.2 RC — RETURN IF CARRY
RC is a mnemonic, which stands for ‘Return if Carry’. This instruction is used to return to the main
program, only if Cy flag value is 1. If Cy flag value is 0, program flow continues in the subroutine
sequentially.
Summary. RC (1 byte; RC; 1 opcode)
RNZ is a mnemonic, which stands for ‘Return if Not Zero result’. This instruction is used to return to
the main program, only if result is not zero, indicated by Z flag value of 0. If Z flag value is 1, program
flow continues in the subroutine sequentially.
Summary. RNZ (1 byte; RNZ; 1 opcode)
RZ is a mnemonic, which stands for ‘Return if Zero result’. This instruction is used to return to the
main program, only if result is 0, indicated by Z flag value of 1. If Z flag value is 0, program flow
continues in the subroutine sequentially.
Summary: RZ (1 byte; RZ; 1 opcode)
RPO is a mnemonic, which stands for ‘Return if Parity is Odd’. This instruction is used to return to
the main program, only if P flag value is 0. If P flag value is 1, program flow continues in the subrou¬
tine sequentially.
Summary. RPO (1 byte; RPO; 1 opcode)
113
1 0.6.6 RPE— RETURN IF PARITY EVEN
RPE is a mnemonic, which stands for ‘Return if Parity is Even’. This instruction is used to return to
the main program, only if P flag value is 1. If P flag value is 0, program flow continues in the subrou¬
tine sequentially.
n
Summary: RPE (1 byte; RPE; 1 opcode) 3
3
a =ÿ
aD
3 3
o s
10.6.7 RP— RETURN IF POSITIVE av
S 2.
RP is a mnemonic, which stands for ‘Return if Positive’. This instruction is used to return to the main
program, only if S flag value is 0. If S flag value is 1, program flow continues in the subroutine
sequentially.
Summary : RP (1 byte; RP; 1 opcode)
In this case n has a value from 0 to 7 only. Thus the eight possible RST instructions are RST 0,
RST 1, ... , RST 7. These are basically single-byte call instructions. Functionally RST n instruction is:
RST n = CALL « * 8
For example, RST 2 is functionally equivalent to CALL 2*8 = CALL 001OH. The advantage of
RST 2 is that it is only 1 byte, whereas CALL 001OH is 3-byte long. Thus RST instructions are use¬
ful for branching to frequently used subroutines.
The reader may wonder how RST n is only 1-byte long, whereas MVI B, d8 is 2-byte long. The
reason is that n value is restricted to the range 0-7. So only 3 bits are needed to denote n value, and
the other 5 bits in the byte provide the code for RST.
RST 2 is an example instruction of this type. It is a 1-byte instruction. It is functionally same as
CALL 001OH = PUSH PC + JMP 001OH. It causes a branch to subroutine at 001OH. Similarly, RST 3
causes a branch to subroutine at 3 * 8 = 0018H. Thus, the subroutine, which starts at location 001OH
should not go beyond memory location 0017H. So at the most only eight locations are available for
the subroutine, which is too small in general. This limitation is overcome by branching to a subroutine
at some other memory location, like F950H. It is achieved by the combination of RST 2 instruction,
and JMP F950H instruction at memory location 0010H, as shown in Fig. 10.5.
114
Memory Instruction
address ©
001OH JMPF950H
8M ©
0
F820H RST 2 -
S F821H MOV B, C; Executed after RET at location F995H
a
2
.2
s
in
co F890H HLT
s
jj F950H MOV C, B; Executed next after RST 2 and JMP F950H
H
Fig. 10.5
Use of RST 2 to branch to
F995H RET subroutine at F950H
©
The way in which the stack contents get affected due to execution of RST 2 is shown below.
Before After
(PC) F820H 001OH
(SP) FC08H FC06H
(FC07) 60H F8H
(FC06) 70H 21H
Note that if the RST 2 instruction is at location F820H, the return address stored on the top of the
stack is F821H. Thus PC value is stored on the top of the stack, and a branch to 001OH takes place.
But then, because of the JMP F950H at location 001OH, branch to subroutine at F950H finally takes
place.
Many times, an I/O port supplies an RST instruction to 8085, when the 8085 is interrupted on INTR
pin. Interrupts are discussed in a later chapter.
‘RST’ stands for ‘restart’. They are called restart instructions because, generally one of these
instructions is going to transfer control to the monitor program in the microprocessor kit. When a
microprocessor kit is switched off, and then switched on, the control is transferred to the monitor
program. Thus the action performed by an RST instruction is similar to restarting the kit, and hence
the name ‘restart’ instruction. On the ALS-SDA-85M kit RST 1 instruction transfers control to the
monitor program.
Summary: RST n (1 byte; RST 5; 8 opcodes)
The complete role of PC in 8085 can be summarized as follows:
PC is a 16-bit register. It contains a memory address. Suppose the PC contents are F820H. Then
it means that the 8085 desires to fetch and execute the instruction starting at F820H. After fetch¬
ing the instruction starting at F820H, the PC is automatically incremented by 1, 2, or 3 depend¬
ing on the instruction length. Then the instruction is executed, during which the PC value is not
115
changed, if the instruction did not belong to the branch group. This way PC will be pointing to
the next instruction by the time the current instruction is executed.
If the instruction belonged to the branch group the following actions take place.
n
■ If the current instruction is an unconditional direct jump or call instruction, PC is loaded with the 3
value provided in the JMP or CALL instruction. Thus, a jump is effected. S
■ If the current instruction is PCHL, PC is loaded with the value present in HL register pair. Thus, 2 O =
3-
a jump is effected.
■ If the current instruction is an RST n instruction, PC is loaded with the value n* 8. Thus, a jump §
II
is effected.
ato
■ Questions
In the previous chapters a total of 59 instruction types had been discussed. Only seven more types are
still to be discussed. However, let us have a digression and discuss about the concept of chip selection.
In a microcomputer system, the microprocessor is the master, which controls all the operations of the
computer. This is because, it is the microprocessor in which the control unit is embedded. In addition
to the processor, there may be a number of RAM chips, EPROM chips, I/O port chips, and other
special peripheral chips like 8253 timer, etc.
But at any instant of time, the processor can communicate with only one chip. The processor selects
the chip with which it would like to communicate by activating chip select pin/pins of the chip. Thus,
generally all chips, other than the processor itself, will have one or more chip select input pins. A chip
select pin can be active low or active high. It is the choice of the chip designer. User has no choice in
this respect. Only when the chip select pins of the chip are activated, the chip is selected for com¬
munication with the processor. A chip select line is also variously called as ‘device select line’, ‘chip
enable line’, or ‘device enable line’.
For example, 74138 has three chip select pins as described in a later section. The 8255 PPI chip to
be discussed in a later chapter has only one chip select line. It is advantageous sometimes to have more
than one chip select pin, as explained in a later section.
118
■ 1 1.2 RAM CHIP-PIN DETAILS AND ADDRESS RANGE
Let us say, we have a 2K X 8 RAM chip. Here, 2K X 8 means that there are 2K (= 2 X 1,024 = 2,048)
memory locations, with each location having 8 bits of information. It is available as a 24-pin DIP IC.
s Its functional pin diagram is as shown in Fig. 11.1.
a>
o
sa Vcc <ÿ=>D7-0
2 Gnd
.2 A lo-o 2KX8
s RAM
in cs* Chip
00 Fig. 11.1
s
0
RD*
WR* Functional pin diagram of
H
2K X 8 RAM chip
Note that there are 11 address input pins to select one of the 2K = 211 memory locations in the chip.
There are eight bi-directional pins for data transfer with the processor. The action desired by the
processor is indicated by the RD* and WR* input pins to the RAM chip. There are two pins meant for
power supply connection of +5 V DC and Ground. Then there is an active low chip select pin.
Now the question is what is the starting address for this chip? Does it have to always start from
0000H, or can it start at say, C000H? If we think that the address should always start from 0000H, it
is not convenient for the following reason. Suppose we had a 2K X 8 RAM chip in our system, with
the address starting at 0000H. Sometime later, we decide to increase the RAM capacity in the system
by another 2K X 8. If this added memory also starts with the address 0000H, effectively we have not
made any addition to the RAM capacity at all! To be useful, the added memory address range should
not overlap with the existing address range. Thus, it should be possible for the designer of the com¬
puter system to place the memory in the address range of his choice. Of course, in 8085-based system
it should be within the address range 0000H-FFFFH.
Let us say it is desired to place the 2K X 8 RAM such that its starting address is C000H. One pos¬
sible circuit connection is as shown in Fig. 11.2.
A] 5 1
A]4 1 Fig. 11.2
Chip selection
AU — Mx> NAND SL To CS*of 2KX8 RAM chip circuit to provide
A]2' 2K X 8 RAM with
0_ starting address
An as C000H
jL
IO/M*-
Note that there are 11 address input pins to select one of the 2K = 2 11 memory locations in the
2K X 8 RAM chip. But 8085 sends out a 16-bit address. So what happens to the remaining five lines
of address information? These address lines are used for selecting the chip such that the starting
address for the RAM becomes C000H as shown in the following.
A15_U selects RAM A10_0 selects a location in RAM
11000 000 0000 0000 = C000H
119
All of the the NAND gate inputs become 1, when A15 A14 A13 A12 An IO/M* = 1 1 0 0 0 0. So the
NAND gate output becomes 0, thus activating the chip select pin of the RAM. Thus location 0 in the
RAM is addressed by the 8085 as location C000H. Location 1 in this RAM will be addressed as
C001H. The last location in this RAM is selected when the LS 11 bits are 111 1111 1111, and as the
RAM has to be selected, the MS 5 bits have to be 11000. Thus the last address in this RAM is 11000
111 1111 1111= C7FFH. Flence, for the chip select circuit shown in Fig. 11.2 the address range will O
be from C000H-C7FFH. *</>
If the 8085 sends out an address that is outside the range of C000FI-C7FFFI, the chip does not get
selected at all. Suppose the address sent out by 8085 is BFFFFI, then the MS 5 bits of address will be a
10 1 1 1. Flence, the NAND gate output will be 1, and so the chip is not selected. Similarly, suppose
the address sent out by 8085 is C800FI, then the MS 5 bits of address will be 1 10 0 1. Flence, the <2
NAND gate output will be 1, and so the chip is not selected. n
It can be easily seen that C000FI-C7FFFI is actually 2K locations, as follows. If we have memory
from C000FI to C003FI, the memory locations are C000FI, COO1 FI, C002FI, and C003FI, a total of four
memory locations. Thus in general, if memory address range is from X to Y, number of memory loca¬
tions = (Y-X) + 1. Flence number of memory locations in the address range C000FI-C7FFFI is 7FFFI
+ 1 = 800H = 2048 = 2K.
Note that there is nothing like a unique chip select circuit. The chip select circuit shown in Fig. 11.3
also serves to give the same address range, C000FI-C7FFFI.
A 14
All the OR gate inputs become 0, when A15 A14 A13 A12 An IO/M* = 1 1 0 0 0 0. So the OR gate
output becomes 0, thus activating the chip select pin of the RAM.
Another thing to note is that the starting address should be exactly divisible by the size of the mem¬
ory chip. For example, a 2K X 8 RAM chip can have any starting address that is divisible by 800H =
2K. Thus, possible starting addresses for a 2K X 8 RAM are 0000H, 0800H, 1000H, F800H. Let
us see why it is not possible to have the starting address as, say, C400H for a 2K X 8 RAM.
A15_n selects RAM A10_0 selects a location in RAM
11000 100 0000 0000 = C400H
In this case, the RAM chip is selected, but when address C400H is sent out, location 400H within
the chip is selected. Thus, the starting address for a 2K X 8 RAM cannot be C400H.
Suppose we have the chip select circuit as shown in Fig. 11.4, then what is the address range for the RAM?
120
A15 i
000 C000H-C7FFH
00 1 C800H-CFFFH
010 D000H-D7FFH
011 D800H-DFFFH
100 E000H-E7FFH
101 E800H-EFFFH
110 F000H-F7FFH
11 1 F800H-FFFFH
Thus location 0 in this 2K X 8 RAM can be addressed by the 8085 as any of the eight memory loca¬
tions C000H, C800H, D000H, D800H, E000H, E800H, F000H, F800H. In other words, every
location has multiple addresses. This kind of chip selection where we have some don’t care values for
address lines is called partially decoded addressing.
Its disadvantage is that just 2K locations of physical memory have occupied 16K locations of
address space. In the previous example, the RAM has occupied locations C000H-FFFFH, a total of
16K locations.
But the advantage of partially decoded addressing is that the chip select circuit is quite simple.
If all the address lines are used for selecting a memory chip, and for selecting a location in that
memory chip, it is called ‘fully decoded addressing’. Its advantage is that 2K locations of physical
memory occupy only 2K locations of address space. For the chip select circuit of Figs. 11.2 or 11.3,
the RAM occupies locations C000H-C7FFH, a total of 2K locations. But the disadvantage of fully
decoded addressing is that the chip select circuit is quite complex.
As an example of decoder IC, let us consider the popular 3 to 8 decoder IC 74138. It is a 16-pin IC,
with its pin out as shown in Fig. 11.5 and functional pin diagram as shown in Fig. 11.6.
As shown in Fig. 11.6, it has three active high input pins denoted as I2, 1], and I0. It has eight active
low outputs denoted as 07*, 06*, ... , O0*. The chip needs a power supply of +5 V DC and Ground.
121
;LS) i0 1 16 Vcc
Input
pins
It 2 15 O0*
IMS) h 3 74138 14 Of
-Ei* 3 to 8
4 13 o2*
Chip decoder
select pins E2* O3*
5
16 pin O
-E3 6 DIP II O4*
07* 7 10 05* Fig. 11.5 </>
Gnd 8 9 06* Pin diagram of 74138
s
Vcc 07* &
Gnd o6* 5'
h O5*
Inputs Ii O4*
Io O3*
Chip Ei* o2*
Fig. 11.6
select E2* OI*
inputs Functional pin diagram
E3 O0*
of 74138
Depending on the inputs I2, Ij, and I0, only one of the output lines is activated. For example, if
I2 Ii Io = 1 1 1, then 07* output line becomes 0, or technically speaking, 07* line is activated. If
I2 Ij Io = 0 1 0, then 02* output line becomes 0. However, an output line is activated depending on
the input, only if the 74138 chip is selected. The 74138 chip is selected when E]* = 0, E2* = 0, and
E3 = 1. If this condition is not satisfied then all the output lines will be in the 1 state. In other words, if
the chip is selected, then depending on the inputs I2, Ib and I0, one of the output lines will become 0.
Chip 74138 can be used to generate chip select signals for upto eight chips in a microcomputer
system. As an example, suppose we have eight numbers of IK X 8 EPROM chips, and we want the
starting address for these chips as 2000H, 2400H, 2800H, ... , 3C00H, respectively. Then the simple
interface circuit shown in Fig. 11.7 is adequate.
In Fig. 11.7, 74138 is selected when A15 A14 A13 = 001. EPROM-O is selected when 74138 is
selected and A12 Au A10 = 0 00. The address lines A9_0 select a location in the EPROM-O after 74138
and EPROM-O are selected. Thus the starting address for EPROM-O is:
A9-0
EPROM-7
Al2 I2 o7* 1KX8
06* D7-0
An II
A10 Io 05*
04*
74138
03*
0 A15 EI* 02*
0 A[4 E2* OI* Fig. 11.7
1 A13 E3 Oo* CSt EPROM-O - 1\
Use of 74138 to generate
A9-0 chip select signals
122
A12-10 selects A9_0 selects
selects 74138 an EPROM a location in EPROM
001 000 0000000000 = 2000H
Similarly, EPROM-7 is selected when 74138 is selected and A12 An A10 =111. The address lines
A9.0 select a location in the EPROM-7 after 74138 and EPROM-7 are selected. Thus the starting
8
M
address for EPROM-7 is:
0
2 A15-13 A12-10 selects A9_0 selects
a selects 74138 an EPROM a location in EPROM
s
.2 001 1 1 1 0000000000 = 3C00H
S
in If we do not use 74138, then we must have separate chip select circuits for each of the eight chips,
00
s which will be more expensive as well as more complex.
Jj
H
1 1 .5.1 ADVANTAGE OF MULTIPLE CHIP SELECT LINES
Suppose we want the EPROMs to have the starting addresses as 4000H, 4400H, ... , 5C00H. 4000H in
binary is 010 000 000000000 0. In such a case, the 74138 has to be selected for the condition A15
A14 A13 = 010. This can be achieved by the connection shown in Fig. 11.8, which uses two invert gates.
Flowever, the chip selection of 74 1 38 for the condition A15 A14 A13 = 0 1 0 can be done without using
any gates as shown in Fig. 11.9. Flere, advantage is taken of the multiple chip select pins of 74138 to
simplify its chip selection. Thus, wherever possible chip designers provide multiple chip select lines.
0 A]5 El
1 A 14 -t» E2 74138
Fig. 11.8
Selection of 74138 when
0 A13 — |>O- E3
A15 A14 A13 = 0 1 0
0 A,5 V
0 A ]3 E2* 74138
Fig. 1 1.9
1 A]4 E3 Simpler selection of
74138
The ALS-SDA-85M kit has a 27128 EPROM of size 16K X 8, and 6116 RAM of size 2K X 8. Also
empty sockets are provided in the kit for an expansion EPROM and an expansion RAM. All these four
chips are selected using a 74138 IC, as shown in Fig. 11.10.
123
1 E3 OS CS* EPROM 8
A13-0
&
0
In this case, the 74138 is selected whenever IO/M* is 0. The 27128 EPROM is selected when A15
AJ4 = 0 0. A location within this 16K X 8 EPROM is selected by the remaining address pins A13_0. As
such, the lowest and highest address for the 27128 will be as follows.
Lowest address: 00 00000000000000 = OOOOH
Highest address: 00 11111111111111= 3FFFH
The 6116 RAM is selected when A15 A14 = 1 1. A location within this 2KX 8 RAM is selected by
the 11 address pins A10_0. The remaining address pins A13.n have no role to play, and they are treated
as don’t cares. As such, the lowest and highest address for the 6116 will be as follows.
Lowest address: 11 xxxOOOOOOOOOOO
Highest address: 11 xxx 1 1 1 1 1 1 1 1 1 1 1
If the don’t cares are taken as Os, the address range will be C000H-C7FFH. If the don’t cares are
taken as Is, the address range will be F800H-FFFFH. The other equivalent starting addresses for the
RAM are C800H, DOOOH, D800H, EOOOH, E800H, and FOOOH. It was possible to connect a RAM
chip with a maximum capacity of 16K X 8 using 03 output of 74138. For reasons of economy, only a
2K X 8 RAM chip is used in the ALS kit.
Each of the expansion EPROM/RAM that can be connected using O* and 0*2 outputs of 74138 can
have a maximum size of 16K X 8.
There is also one more 74138 IC on the kit, which is used for generating chip select signals for
peripheral ICs like 8255, 8253, etc.
A,7,:;
2732
4KX8
EPROM
A;:
8 &
a> IO/M*
0
sa 5. For the circuit shown below identify the address range for the RAM. In this case, do we have
s
.2
multiple memory address range? If so, identify the different equivalent address ranges.
S Dv_0<
U)
00
s« 6116
2KX8
RAM
H Ai4ÿ)£S,
ay
10/M*—
6. What are the merits and demerits of having more than one chip select pin?
7. What are the merits and demerits of having multiple address ranges for a memory chip?
8. Explain the working of 74138 IC with a functional diagram. What is its application in a
microcomputer system?
9. For the circuit shown below identify the address range for the RAMs and the EPROMs. In
this case, do you have multiple memory address range for any of the memory chips? If so,
identify the different equivalent address ranges.
A12-0:
RAMI
8KX8
CS*
07*
»: Aii-o =
< RAMO
A,4—1—
A15- s z-
A13-1 z"
74138
0.1 cs«
4K.X8
6 — i E; n*
All-0;
o— > Bj «; EPRON
4KX8
E3 o;:
cs»
EPROMO
2KX8
CS*
10. With a neat diagram explain the interfacing circuit using 74138 needed to connect the
following.
a. 2K X 8 RAM chip in the address space C000H-C7FFH,
b. 4K X 8 RAM chip in the address space D000H-DFFFH,
c. 2K X 8 EPROM chip in the address space 0000H-07FFH,
d. 8K X 8 EPROM chip in the address space 2000H-3FFFH.
In this case, do you have multiple memory address ranges for any of the memory chips?
If so, identify the different equivalent address ranges.
12 Addressing
m \
P: I/O Ports
of
This chapter reverts to the description of instruction types. Two of the remaining 7 instruction types
are explained here. This chapter discusses in detail the importance and need for I/O ports and also
describes memory-mapped I/O and I/O-mapped I/O.
CPU and main memory are very fast compared with electromechanical input or output devices like print¬
ers, etc. In such a case, it is essential that the data lines of the computer are not kept engaged for a long
time during communication with input/output (I/O) devices. Otherwise, the overall speed of the
computer system comes down drastically. So I/O devices are connected to a computer through I/O ports.
For example, to communicate with a printer, the CPU loads the output port connected to the printer
at electronic speeds. The printer slowly prints this. When the printer has finished printing, the output
port requests the CPU for further data. This way, the CPU is allowed to work at its full speed, with no
degradation in the overall speed of the computer system.
126
In fact, to further improve the speed, a printer will have printer buffer. For example, the popular
LX-800 Epson dot matrix printer has a printer buffer size of 3 KB. The microcomputer fills this buffer
in a fraction of a second. Then as far as the computer is concerned the printing is over! But, the user
would not have witnessed the printing of a single character as yet! The printer prints this entire infor¬
mation in about 15-20 s. Then the printer requests for further data to be printed, from the computer.
Thus I/O devices are never directly connected to a computer. Computer and an I/O device always
8 communicate with an I/O port as the middleman.
a>
o
sa
g 12.1.1 COMPARISON OF I/O PORT CHIPS AND MEMORY CHIPS
s
in An I/O port chip also stores information like a memory chip. But, an I/O port chip generally stores
oo
s only 1 byte of information, and some I/O port chips store a few bytes of information. For example,
Intel 8212 I/O port chip stores only 1 byte of information, but Intel 8255 chip can be used to store
Jj
H 3 bytes of information. However, memory chips contain a large number of memory locations like IK,
4K, 8K, etc. A location within a memory chip is selected by the address pins A„.0.
Second, an I/O port chip will have eight lines for communication with the microprocessor, and
another eight lines for communication with the I/O device. An I/O port only acts as a buffer between
the microprocessor and the I/O device. But, a memory chip will have only eight lines for communi¬
cation with the microprocessor.
Third, many I/O port chips are programmable. That is, the user can decide the way the I/O port should
work in his system. An example for a programmable I/O port chip is Intel 8255. In this case, the user can
program a port as an input port or an output port. Also, the user can program a port to work in one of the
several modes like basic I/O, handshake I/O, etc. However, there are I/O port chips, which are non¬
programmable. An example is Intel 8212. Obviously, programmable I/O ports are more popular than
non-programmable ports. But memory chips are only storage chips, and as such are not programmable.
Fig. 12.1a, b, and c indicate the essential pins needed in an output port, an input port, and a mem¬
ory chip. For simplicity, I/O port chip with a single port is shown.
Vcc Gnd
1 1 Output
CS*
device
WR*
T>7-(l
from P7-0
8085
Fig. 12.1a
Pins for an output port chip
Vc0 Gnd
1 1
CS* -► Input device
RD*
D7.0
to 8085
Fig. 12.1b
Pins for an input port chip
127
Vcc Gnd
1 I
CS*
RD*
A„-o
WR* ≥
a
u1o
5
070 Fig. 12.1c
Pins for a memory chip Z
V)
There are two instructions in 8085 for communication with I/O ports. They are the IN and OUT
instructions. The IN or OUT instruction mnemonic should be followed by an 8-bit port address. Thus
28 = 256 input ports and 256 output ports are possible in a 8085-based microcomputer.
12.2.1 IN a8 INSTRUCTION
IN is a mnemonic that stands for INput to Accumulator from the contents of input port whose 8-bit
address is indicated in the instruction as a8. It occupies 2 bytes in memory. The first byte specifies the
opcode, and the next byte provides the 8-bit port address. IN EFH is an example instruction of this
type. The result of execution of this instruction is shown below with an example.
Before After
Input port no. EFH BCH
Accumulator contents 12H BCH
IN instruction is the only instruction using which the Accumulator can be loaded with the contents
of an input port. A possible chip select circuit to connect an input port with an address as EFH is as
shown in Fig. 12.2.
1 A7
1 A6
1 A5
0 A4
1 A3 NAND To CS* of input port chip
1 A2
1 A1
1 A0 Fig. 12.2
0
l
RD*— c>o
IO/M*
— ► Chip select circuit for input
port EFFI
OUT is a mnemonic that stands for OUTput Accumulator contents to an output port whose 8-bit
address is indicated in the instruction as a8. It occupies 2 bytes in memory. First byte specifies the
opcode, and the next byte provides the 8-bit port address. OUT EFFI is an example instruction of this
8 type. The result of execution of this instruction is shown below with an example.
M
o Before After
S Accumulator contents 12H
a
s
.2
Output port no. EFFI BCFI 12H
S OUT instruction is the only instruction using which Accumulator contents can be sent out to an out¬
in
00 put port. A possible chip select circuit to connect an output port with an address as EFFI is as shown
s in Fig. 12.3.
Jj
H
1 A7
1 A6
1 A5
0 A4
1 A3 NAND To CS* of output port chip
1 A2
1 A,
1 Ao- Fig. 12.3
0 WR*— cx> — ►
Chip select circuit for output
1 IO/M* port EFH
It is possible to address an I/O port as if it were a memory location. For example, let us say, the chip
select pin of an I/O port chip is activated when address = FFF0H, IO/M* = 0, and RD* = 0. This is
shown in Fig. 12.4.
In this case, the I/O port chip is selected when the 8085 is thinking that it is addressing memory
location FFF0H for a read operation. Note that 8085 thinks that it is addressing a memory location
because it has sent out IO/M* as a logic 0. But in reality, an input port has been selected, and the input
port supplies information to the 8085. Such I/O ports that are addressed by the processor as if they
were memory locations are called memory-mapped I/O ports.
129
A15
A14
A13
A12 '
A11
A10 ≥
Q.
u1o
A9 '
A8 ’
5
A7
Z
A6 » -»
NAND To CS* of input port chip
A5
A4
0 A3— Oo — ►
0 A2— Oo — ►
0 Ai— >0— ►
0 AQ — to— ►
Fig. 12.4
0
0
IO/M* —
—[>6
RD*— to— »
> Memory-mapped input port
with address FFFOH
Generally, a processor like 8085 addresses an I/O port by sending out 8-bit port address and IO/M* = 1.
For example, let us say, the chip select pin of an I/O port chip is activated when 8-bit address = FOH,
IO/M* = 1, and RD* = 0. This is shown in Fig. 12.5.
1 A7
1 A6
1 A5
1 A4 To CS* of input port chip
0 A3— to— ►
0
0
A2— to—
A] —to— *
►
Such I/O ports, which are addressed by the processor by sending out IO/M* as logic 1 are called
I/O-mapped I/O ports. Our discussion about I/O ports in Sect. 12.2 of this chapter was confined to
only such I/O-mapped I/O ports.
In this section the merits and demerits of these two schemes of addressing of I/O ports is discussed.
130
1 2.5.1 MERITS OF I/O-MAPPED I/O AND DEMERITS OF MEMORY-MAPPED I/O
1. IN and OUT instructions are used for addressing I/O-mapped I/O ports. The mnemonics of
these instructions clearly indicate that the processor communicates with an I/O port. To load
8 accumulator from memory-mapped input port with address FFFOH, we have to execute LDA
a> FFFOH instruction. The mnemonic of this instruction gives the impression that the accumula¬
o
sa tor is being loaded from memory location FFFOH!
2. Some microprocessors have a control pin to select a memory location or an I/O port. For
s
.2 example, in 8085, IO/M* pin is used to select an I/O port or a memory location. The address
S sent out on the address pins selects an I/O port or a memory location based on the value sent
in
00 out on IO/M*. In other words, there are two separate address spaces— one for I/O ports and
s another for memory, as shown in Fig. 12.6.
Jj
H
Memory address space I/O address space
selected when IO/M*=0 selected when IO/M*= 1
0000H 00H
000 1H
67H
0067H FFIT
Fig. 12.6
Memory address space
and I/O address space for
FFFFH
8085
3. The I/O address space has only 256 locations, as I/O port addresses are only 8 bits in size in
8085. The memory address space has 64K locations, as memory addresses are 16-bits long in
8085. Thus it is possible to have full 64K space exclusively for memory, if we prefer to use
I/O-mapped I/O scheme for addressing I/O ports. It is possible to have an I/O-mapped I/O port
with the address 67H, and also have a memory location with the address 0067H.
4. If we connect I/O ports as memory-mapped ports with addresses in the range of FF00H-
FFF0H, then we should not allot this address range to any memory chip. If memory and a
memory-mapped I/O port have the same address, both get selected simultaneously, and this
can damage the processor, the memory chip, and the I/O port chip. Thus with memory-
mapped I/O, the entire 64K address space (in case of 8085) is not available for memory. This
is because some address space is lost to memory-mapped I/O ports.
1. Only IN and OUT instructions are used for addressing I/O-mapped I/O ports. But a large num¬
ber of instructions can be used for communication with a memory-mapped I/O port. In fact,
all the instructions that can be used to access a memory location can be used to access a
131
memory-mapped I/O port. Suppose we have a memory-mapped input port with the address
FFFOH, then the effect of executing the following instructions is shown alongside in the
comments field.
ANAM
ADD M
Perform AND of accumulator and input port FFFOH
Add accumulator contents and input port FFFOH 5 1
J3
2. The designer of a microprocessor is required to produce a chip design with minimum possi- « 2.
ble pins. That is the reason we find multiplexed address and data pins, AD7_0. If I/O-mapped
I/O facility is needed, then there must be a pin of the kind IO/M*, to distinguish between I/O
addressing and memory addressing. However, if it is decided by the designer of the processor
chip that memory and I/O ports will be addressed in the same way, then there is no need of
IO/M* pin at all. In Motorola microprocessor chips, there is no IO/M* pin because they use
memory-mapped I/O ports. In Motorola 6800 there will be a single address space in which
space allocation is made for memory and I/O ports, as shown in Fig. 12.7. The ports are
always memory-mapped in Motorola 6800.
3. Using I/O-mapped I/O, only accumulator can communicate with an I/O port. But using
memory-mapped I/O any register can communicate with a memory-mapped I/O port. For
example, ‘MOV C, M’ can be used to load C register with contents of a memory-mapped I/O
port, whose address is provided in HL pair.
4. Using I/O-mapped I/O, only data transfer operation is possible between accumulator and an
I/O port. They are done using the IN and OUT instructions. But using memory mapped I/O,
even arithmetic and logical operations can be performed. For example, ADD M can be used
to add accumulator and contents of a memory-mapped I/O port, whose address is provided in
HL pair. Similarly, ANA M can be used perform AND operation on accumulator and contents
of a memory-mapped I/O port, whose address is provided in HL pair.
0000H
Space for
0001H
memory
Space for
I/O ports
Space for
memory Fig. 12.7
6800 address space for
FFFFH
memory and I/O ports
132
5. Using I/O-mapped I/O, only 256 input ports and 256 output ports could be addressed in a 8085
system, as an I/O port address is only 8-bits wide. But, using memory-mapped I/O, even upto
64K I/O ports can be addressed in a 8085 system, as memory addresses are 16-bits wide.
However, this will be at a cost of corresponding reduction in memory addressing capacity.
Shown in Fig. 13.1 is the architecture of 8085. As we are already aware, it has 8-bit ALU, control unit,
the general purpose registers A, B, C, D, E, H, and L, and the special purpose SP, PC, and Flags registers.
It basically performs 8-bit arithmetic and logical operations. Accumulator and Temp registers provide
the two operands needed in the operations like addition or logical AND operations. The results will
be stored in the accumulator. This is done by sending the ALU output to the accumulator via the inter¬
nal bus. Also, the Flags register is affected based on the result. In an instruction like ADC B, Temp
register receives B register value, and Cy flag is also input to the ALU.
In ‘DAD B’ instruction, it is required to add HL and BC contents, and store the result in HL. This
is also performed by the 8-bit ALU, by adding the LS bytes first, and then adding the MS bytes along
with any carry generated.
Even increment or decrement of 16-bit registers is done by this ALU. But in Fig. 13.1 incre-
menter/decrementer unit is separately shown just for convenience.
This unit is responsible for generating timing signals and control signals. This unit controls all the
activities inside the 8085, as well as outside the 8085.
XI, X2, and Clk out pins: To facilitate timing operations in the microcomputer system, there is a
clock generator in the control unit of 8085. The complete oscillator circuit, except the quartz crystal
is within the chip. Two pins XI and X2 are brought out of the chip to provide for the external crystal
connection. This is shown in Fig. 13.1. Generally a capacitor of about 20pF value has to be connected
between X2 and ground to ensure proper starting up of the crystal. There is a divide by 2 counter in
the control unit, which divides the crystal frequency by 2. The 8085A can work at an approximate
135
8085
Internal 8-bit bus
l
Ey zy zy
\7 >
A Temp IR Mux/Demux 3
EL IE EE
M| i/ps w z
\7 Is _B C §?
Instruction D E
Flags 8 bit ALU
decoder I H L 53
E
ei
SP
256 o/ps PC
Timing & control unit [£Z
1 Incr. / Deer.
Oscillator H-2 3 MHz Addr. latch
Internal clock
1e
Address
\z
Addr. / data
buffer buffer
zy
xtHDH X2 RD*
\z
WR* Al5-8 AD7-0
JOpF
IO/M*
So Reset in* \Z
Si Reset out Address
latch
ALE
Hlda A7-0
Hold
RD* Memory
Ready
WR* F>7-Q
Clk out
maximum clock frequency of 3 MHz internally. Thus typically a 6-MHz crystal is connected between
XI and X2. All operations in a 8085 system take place in synchronization with this clock. This clock
signal is brought out on ‘clock out’ pin of 8085. Peripheral chips, like 8251 USART, which need a
clock signal for their operation, use it.
It is also possible to connect a LC-tuned circuit between XI and X2, instead of a crystal. In very
low-cost systems, a resistor can be connected between XI and X2, and a capacitor connected between
XI and ground to generate the oscillations. However, for good stability of oscillations, it is preferred
to use a quartz crystal.
With internal frequency as 3 MHz, the clock period will be 333 nS. In other words, the 8085 clock
ticks once every 333 nS, whereas our watch is ticking once every second! A clock cycle of 8085 is also
called as a T state, T standing for ‘timing’.
136
The minimum internal frequency of operation of 8085 (any version) is 500 KHz. In other words, the
minimum crystal frequency should be 1 MHz. At lesser frequencies, the information in the registers
will be lost. This is because, the registers are basically dynamic RAM cells, which have to be continu¬
ously refreshed.
8 Status signals IO/M *, S 1 and SO: An instruction cycle (to be discussed later) requires one to five
a> machine cycles, depending on the instruction. Each machine cycle performs a specific operation, like
o read from memory. A machine cycle needs a fixed number of clock cycles, the minimum being three
2
a clock cycles. The first clock cycle is termed as T1 (T for time period), the second clock cycle as T2, etc.
2 The type of machine cycle that 8085 is going to execute is indicated by the status signals IO/M*,
.2
s SI, and SO. This is shown in the following table. These status signals are emitted by the 8085 during
in
T1 of a machine cycle.
soo
Jj IO/M* SI SO Machine cycle
H
0 0 1 Memory write (MW)
0 1 0 Memory read (MR)*
0 1 1 Opcode fetch (OF)
1 0 1 I/O write (IOW)
1 1 0 I/O read (IOR)
1 1 1 Interrupt acknowledge (INA)**
*It is bus idle (BI) in the case of DAD instruction.
**It is BI in the case of acknowledge for vectored interrupts.
Generally in a 8085 system, only IO/M* signal is made use of in selecting a memory or an I/O port
chip. SI and SO signals are not made use of in most systems. For example, in ALS-SDA-85M kit, SI
and SO signals are unused.
In this chapter, OF, MR, MW, IOR, and IOW machine cycles will be discussed. BI and INA
machine cycles will be discussed in the chapter on 8085 interrupts.
Control signals RD*, WR*, and INTA*: During T1, the status signals and address are sent out. Only
after the address has become stable, the control signals are emitted by the 8085 during T2 of a machine
cycle. The values of the control signals for the various machine cycles are shown in the following
table. INTA* signal will be discussed in detail in the chapter on 8085 interrupts.
During T2 of the halt machine cycle, 8085 tristates RD* and WR*, and emits 1 on INTA.
Address latch enable signal (ALE): As discussed in the previous chapter, 8085 has multiplexed address
data pins. So LS byte of address will have to be sent out for only a short time in a machine cycle.
During the rest of the machine cycle, the same pins are to be used for data transmission or reception.
Some specialized chips like Intel 8155 have an address latch within the chip, as shown in Fig. 13.2.
During T1 of a machine cycle, 8085 sends out address on AD7.0 and sends out logic 1 on ALE. The
address latch in 8155 latches on to the address using this signal. During T2, 8085 stops sending the
137
RD*
WR*
ll
XT
ALE
Addr. 256X8
AD7-0 latch A7-0 RAM >
from 80856
-N./I-1\ «5
V\|VW
3;
~Z\ hi
D7-0
Fig. 13.2 53
Details of RAM and
address latch in Intel 8155
address on AD7_0 and sends out logic 0 on ALE. But the latched address is available to the memory por¬
tion of 81 55 for the complete machine cycle. From T2 onwards, AD7_0 is used as D7_0 for receiving or
transmitting data.
If we are using conventional memory chips, like Intel 6116 RAM chip, they do not have an address
latch inside. In such a case, we should have an external address latch as shown in Fig. 13.3.
A15-11 ~j cs*
Chip select
IO/M* circuit
AIO-8
ALE
AD 7.0
from
8085
< 74LS373
latch A7_O
6116 RAM
2K.X8
D7-0
RD* Fig. 13.3
WR* Use of an address latch
with a memory chip
During T 1 of a machine cycle, 8085 sends out address on AD7_0 and sends out logic 1 on ALE. The
74LS373 latches onto the address using this signal. During T2, 8085 stops sending the address on
AD7_0 and sends out logic 0 on ALE. But the latched address is available to the memory chip for the
complete machine cycle. From T2 onwards, AD7_0 is used as D7_0 for receiving or transmitting data.
Thus ALE signal is used to enable an address latch to perform the latching operation. ALE stands
for ‘address latch enable’, justifying its name. Practical use of 74LS373 is shown in Fig. 13.19, later
in the chapter.
Ready input pin: This pin is sensed by the 8085 during T2 of a machine cycle. If it is logic 0, the
8085 understands that the memory or peripheral is not yet ready to send or receive the data. As such,
the 8085 will wait the required number of clock cycles for the ready signal to go to logic 1. Only then
the 8085 enters T3 state of a machine cycle. Using the Ready signal, it is easy to interface to very slow
memory or peripheral chips.
138
1 3.1 .3 INSTRUCTION REGISTER (IR)
This is a 8-bit register. It is used to receive the 8-bit opcode of an instruction from memory. It may be noted
that opcode of an instruction is only 8 bits, even though the instruction may be 3-bytes long. IR register is
not accessible to the programmer. This means there are no instructions by which a programmer can load a
8 value of his choice into this register. There are no instructions like ‘MVI IR, 35H’, or ‘MOV IR, C’.
a>
o
2 Instruction decoder: The instruction decoder receives the input from the IR register. It is an 8-input
a to 256-output decoder. Depending on the input, only one of the 256 output lines will be activated. The
2
.2 instruction decoder output drives the control unit. The control unit then generates the appropriate con¬
S trol signals to execute the instruction.
in
00
s« 1 3.1 .4 W AND Z REGISTERS
H
These are 8-bit registers. They are not accessible to the programmer. They are used for temporarily
storing inside the 8085, the 16-bit address operand of an instruction. For example, when ‘LDA
C234H, instruction is fetched, IR register will receive the opcode for LDA, and W and Z registers will
receive C2H and 34H, respectively.
This is an 8-bit register. It is not accessible to the programmer. It temporarily stores inside 8085, the
8-bit operand in an instruction. For example, when ‘MVI M, 34H’ instruction is fetched, IR register
will receive the opcode for MVI M, and Temp register will receive 34H.
In arithmetic and logical operations that involve two operands, the accumulator provides one
operand, and the other is provided by the Temp register. For example, in ADD B instruction, B regis¬
ter contents are moved to the Temp register, and then addition of A and Temp register contents is per¬
formed by the ALU.
13.1.6 MULTIPLEXER/DEMULTIPLEXER
Consider the execution of the instruction ‘MOV A, C\ In this case, the 8-bit value in the C register
has to be moved to the A register. The registers B, C, D, E, H, and L are connected to the internal bus
through a multiplexer/demultiplexer. The register select unit sends the appropriate code to the multi¬
plexer so that C register contents are sent out of the multiplexer to the internal bus. Then Accumulator
receives the data from the internal bus.
Similarly, consider the execution of the instruction ‘MOV D, A’. In this case, the 8-bit value in the
Accumulator has to be moved to the D register. Accumulator sends out the 8-bit value to the internal
bus. The registers B, C, D, E, H, and L are connected to the internal bus through a multiplexer/demul-
tiplexer. The register select unit sends the appropriate code to the demultiplexer so that D register
receives the contents of the internal bus from the demultiplexer.
The register select unit in the 8085 selects one of the register pairs (BC, DE, HL, SP, PC, or WZ) for
sending it to the address latch unit. For example, let us say the contents of PC is C200H. If the regis¬
ter selection unit selects PC, it sends C200H from PC to the internal address latch. The latch holds on
to this value, and sends it out on the address pins after buffering. The MS byte of address, C2H, is sent
out on A15_g, and the LS byte of address, 00H, is sent out on the pins AD7_0.
A little later, the PC value may be incremented, but still the latch will continue to have the value
C200H. This value of C200H is still sent out on the address pins of 8085 even if the PC content is
already incremented.
1 3.1 .9 INCREMENTER/DECREMENTER
It is actually a function performed by the ALU. But in Fig. 13.1, it is shown as a separate unit just for
convenience.
The incrementer is used to increment the PC value, after the 8085 has fetched a byte of the instruc¬
tion. This way, the PC will be pointing to the next instruction by the time the current instruction is
fully fetched. It is also used for incrementing the SP value after a byte is popped out from the stack
top. It is also used for incrementing an 8-bit or a 16-bit register.
The decrementer is used for decrementing the SP value before a byte is pushed above the stack top.
It is also used for decrementing an 8-bit or a 16-bit register.
Accumulator will have to receive data from internal bus in instructions like ‘MOV A, C\ It has to send
out data to internal bus in instructions like ‘MOV D, A’. As such, the Accumulator communicates with
the internal bus in a bi-directional way.
Flags register will have to receive data from internal bus in instructions like POP PSW. It has to
send out data to internal bus in instructions like PUSH PSW. As such, the Flags register communicates
with the internal bus in a bi-directional way.
140
In instructions like ‘MVI M, 25H’, Temp register will have to first of all temporarily receive data
value of 25H from internal bus. Later this data in Temp register has to be sent out to memory via the
internal bus. As such, the Temp register communicates with the internal bus in a bi-directional way.
IR register will always have to receive the opcode from memory via the internal bus for any
instruction. It is never required to send out opcode to the internal bus. As such, the IR register only
o receives opcode via the internal bus.
Register B will have to receive data from internal bus via demultiplexer in instructions like ‘MOV
o
A, B’. It has to send out data to internal bus via multiplexer in instructions like ‘MOV B, A’. As such,
sa register B communicates with the internal bus in a bi-directional way. Similarly, registers C, D, E, H,
s and L also communicate with the internal bus in a bi-directional way.
■S In instructions like ‘LDA 2345H’, W and Z registers will have to first of all temporarily receive
w address value of 2345H from the internal bus. Later they have to send out this address in W and Z reg¬
oo
s isters to memory via the internal bus. As such, W and Z registers communicate with the internal bus
in a bi-directional way.
Jj
H
Program and data are stored in memory, which is external to the microprocessor. To execute an
instruction of the program, the following steps are to be performed by the 8085.
1. Fetch the opcode from memory;
2. Decode the opcode to identify the instruction;
3. Fetch the remaining bytes, if instruction length is 2 or 3 bytes;
4. Execute the instruction.
These steps put together constitute the instruction cycle. These steps are described in detail with
the help of Fig. 13.1 taking several instructions as examples. These instructions are assumed to be in
memory, at memory locations as shown in the following.
Example 1
Execution of ‘MOV A, B’ instruction. Let us say that PC has C000H as its contents. Then it means
that 8085 desires to fetch and execute the instruction starting at memory location C000H. Let us say, at
location C000H, we have 78H, which is the opcode for the instruction ‘MOV A, B\ This instruction
141
needs only four clock cycles for the opcode fetch and execution. The action performed by the 8085 in
these four clock cycles is explained in the following.
The content of PC is sent to address latch inside the 8085. It latches on to the value C000H. This
value of C000H is sent out on the address pins of 8085 after buffering. The MS byte of address, COH,
is sent out on Al5.8, and the LS byte of address, 00H, is sent out on the pins AD7_0. This is done dur¬
ing the beginning of Tl. >
«5
In addition to address information, status signals as shown here are activated by the control unit
during Tl. hi
ALE = 1, indicating address present on AD7_0 53
IO/M* = 0, indicating that the address is meant for memory
SI = 1, SO = 1 indicating that it is opcode fetch machine cycle
Address information is sent out on AD7_0 only during Tl. In T2, the 8085 stops sending out the
address and tristates AD7_0. It expects that memory chip should now drive the AD7.0 pins with the
opcode.
Logic 1 value is sent out on ALE pin during Tl. During this clock period, LS byte of address is
sent out on AD7_0. The address latch does latching of this LS byte of address, when ALE is made 0
before the end of Tl. Only during the beginning of T2, RD* will be made 0. This enables the mem¬
ory to output the opcode after the access time of the memory chip.
At the end of T2, the 8085 will sense Ready input. If it is 0, the 8085 understands that memory is
not yet ready to supply the opcode. In such a case, the next clock cycle is not termed as T3. It will be
termed as Twait. At the end of Twmt Ready input will be sensed once again. If it is still logic 0, it enters
another Twmt state once again. The 8085 goes to T3 state only when Ready signal is at logic 1 when
sensed by the 8085 at the end of T2 or Twait.
It is the responsibility of the system designer to introduce appropriate number of wait states if the
memory or peripherals are slow. However, present-day memory chips and peripherals are so fast that
wait states will not be needed for 8085 working at 3 MHz. In such a case, the ready pin can be per¬
manently tied to logic 1. In the ALS kit, ready pin is tied to logic 1. Memory access time requirement
calculations are shown later in the chapter.
Thus at the beginning of T3 the opcode will be received by the 8085 in the IR register on AD7.0
pins. During the middle of T3, RD* is deactivated by the 8085. This results in floating of AD7_0 lines.
Also, Ready input is deactivated.
Thus in three clock cycles (if there are no wait states), the opcode is received in IR register. The
decoding is done in T4 state. The control unit understands that the opcode corresponds to the 1-byte
instruction whose mnemonic is ‘MOV A, B\ Then it generates control signals such that B register
contents are moved to A the register, via the multiplexer and the internal bus. This decoding and exe¬
cution is completed in T4.
In fact, after activating RD* during the beginning of T2, the 8085 increments the PC value using
the incrementer logic. Thus PC value would have become C001H by the time the opcode is received
in IR. That is why it is said that PC points to the instruction to be executed next.
Fig. 13.4 depicts opcode fetch, decode, and execution of MOV A, B instruction in a simple way.
These four clock cycles in Fig. 13.4 constitute the OF machine cycle. But in these four clock cycles
opcode fetch, decode, and also execution were completed. Flowever, in 2- and 3-byte instructions, and
also in some 1-byte instructions like ‘MOV B, M’, only OF and decode operations will be completed
8 in these four clock cycles. Thus OF machine cycle consists of opcode fetch and decode operation, and
a> in some cases execution also. For some instructions like DCX B, the OF machine cycle uses six states.
o Flenceforth, we just indicate for a machine cycle, like OF, the number of T states needed when there
2
a are no wait states. The wait states, if needed, depends on the speed of the memory and peripheral chips
2 used in the system. Waveforms for OF machine cycle, which uses fourT states are shown in Figs. 13.5
.2
s and 13.6.
in
soo Example 2
Execution of ‘MOV C, M’ instruction. Let us say, at location C001FI, we have 4EFI, which is the
Jj
H opcode for the instruction MOV C, M. This instruction needs seven clock cycles for the OF, decode,
and execution. The first four clock cycles constitute the OF machine cycle, and the next three clock
cycles will be memory read (MR) machine cycle. The action performed by the 8085 in these seven
clock cycles is the topic of following explanation.
In the first three clock cycles 4EFI will be received in IR register from memory location COO1 FI.
The decoding is done in T4 state. The control unit understands that the opcode corresponds to the
1-byte instruction whose mnemonic is MOV C, M. These fourT states constitute the OF machine cycle,
described earlier. In the meanwhile, PC contents would have been incremented to C002FI. But C002FI
will not be sent out from PC at this point, as the instruction MOV C, M is not yet executed.
decode + execution.
Opcode fetch _ in some cases
Clock
Indicates change
out
A 15-8 From PCH
Tristate
Out In
AD7.o From PCL ToIR
ALE
IO/M*, SI, SO
IO/M*=0, S1= 1 . S0=l
RD*
Fig. 13.5
Ready Waveforms for OF machine
cycle (without wait state)
143
decode + execution,
Opcode fetch with one in some case?
wait state
-Tl -T2 TWait -T3 -14-
Clock
Out >
«5
A 15-8 PCH
Out In
hi
AD7_0
PCL
Tristate
IR
53
ALE
IO/M*, SI, SO
IO/M*=0, Sl = l,S0fl
RD*
Fig. 13.6
Waveforms for OF
machine cycle (with
Ready one wait state)
The control unit understands that for instruction execution, information should be received in C
register from memory location whose address is present in HL pair. This is performed in a MR
machine cycle using three clock cycles, as described in the following.
Let us say HL pair has the value D300H and the content of memory location D300H is 45H. Then
45H should be received in the C register. The control unit directs the register select unit to select HL
pair. Then HL contents are latched by the internal address latch, and then buffered and sent out on
A15_8 and AD7_0 pins. This is done during T1 of MR machine cycle.
In addition to address information, status signals as shown in the following are activated by the
control unit during Tl.
ALE = 1, indicating address present on AD7_0;
IO/M* = 0, indicating that the address is meant for memory;
SI = 1, SO = 0 indicating that it is MR machine cycle.
Address information is sent out on AD7_0 only during Tl. In T2, the 8085 stops sending out the
address and tristates AD7_0. It expects that memory chip should now drive the AD7_0 pins with the data.
Logic 1 value is sent out on ALE pin during Tl. During this clock period, LS byte of address is
sent out on AD7_0. The address latch does latching of this LS byte of address, when ALE is made 0
before the end of Tl. Only during the beginning of T2, RD* will be made 0. This enables the mem¬
ory to output the data after the access time of the memory chip.
At the end of T2, the 8085 will sense Ready input. If it is 0, the 8085 understands that memory is not
yet ready to supply the data. In such a case, the next clock cycle will be rwait. During the end of rwait,
Ready input will be sensed once again. If it is still logic 0, once again it enters another 7ÿ state. The 8085
goes to T3 state only when ready signal is at logic 1 when sensed by the 8085 at the end of T2 or rwait.
Thus at the beginning of T3 the data will be received by the 8085 in the C register, on AD7_0 pins.
During the middle of T3, RD* is deactivated by the 8085. This results in floating of AD7_0 lines. Thus
in three clock cycles (if there are no wait states), the data is received in C register via the internal bus
and the demultiplexer.
144
In this case, after activating RD* during the beginning of T2, the 8085 does not increment the PC
value. Thus PC value remains as C002H when the data is received in C register. If PC were to be incre¬
mented, the 8085 had to wrongly skip the instruction at memory location C002H! It can be verified
that PC value will have to be incremented in a MR machine cycle only when PC value is sent out as
address.
Fig. 13.7 depicts OF, decode, and execution of ‘MOV C, M’ instruction in a simple way. Thus ‘MOV
8 C, M’ instruction needs two machine cyles. The first machine cycle, denoted as Ml, is of type OF with
a>
o four T states, and the second machine cycle, denoted as M2, is of type MR with three T states.
2
a
2
.2 C001 FI 4EH ToIR 3+1 clocks OF (MOV C, M)
s
U) C002H
00
s D300FI 45H ToC 3 clocks MR Fig. 13.7
Jj
H Illustrating MOV C, M
Total of 7 clock cycles with 2 machine cycles instruction execution
The last three clock cycles in ‘MOV C, M’ instruction is an example for MR machine cycle. Wave¬
forms for MR machine cycle is shown in Fig. 13.8.
T1 -T2 -T3
Clock
Out
A 15-8
I Notel:
Out Tristate In
1
AD7_O
I Notel: Note 2:
ALE
RD*
/
Ready
Fig. 13.8
Notel: Addresses sent out from PC, BC, DE, HL, SP or WZ Waveforms for MR machine
Note2: Data received in any 8-bit register except IR cycle (without wait state)
The address sent out from a register pair in a MR machine cycle depends on the MR machine cycle
within an instruction as shown in the following table.
145
Instruction Operation Address reg.
LDA 1234H Loading W with 12H (or Z with 34H) PC
POP B Popping information from stack top SP
MOV C, M Loading C from memory pointed by HL HL
LDAXB Loading A from memory pointed by BC BC
LDAXD Loading A from memory pointed by DE DE >
LDA 1234H Loading A from memory location 1234H WZ «5
3;
The data received in a register during a MR machine cycle depends on the MR machine cycle within ghijr
®
an instruction as shown in the following. Here, ‘r’ stands for any of the registers A, B, C, D, E, H, or L. 3
Instruction Operation Data reg.
MOV r, M Loading r from memory pointed by HL r
POP PSW Loading flags from stack top Flags
MVI M, 25H Loading Temp with 25H from memory Temp
Example 3
Execution of ‘MVI M, 25H’ instruction. Let us say, at location C002H, we have 36H, which is the
opcode for the instruction ‘MVI M, d8’. This instruction needs ten clock cycles for the OF, decode,
and execution. The first four clock cycles constitute the OF machine cycle, the next three clock cycles
will be MR machine cycle, and the last three clock cycles will be MW machine cycle. The action per¬
formed by the 8085 in these ten clock cycles is the topic of following explanation.
In the first three clock cycles 36H will be received in IR register from memory location C002H.
The decoding is done in T4 state. The control unit understands that the opcode corresponds to the
2-byte instruction whose mnemonic is MVI M. These four T states constitute the OF machine cycle,
described earlier. In the meanwhile, PC contents would have been incremented to C003H.
As the control unit understands that MVI M instruction is 2-bytes long, it decides to fetch from the
next memory location the second byte of the instruction. The 8085 performs a MR machine cycle, by
sending out the PC value of C003H, and receiving 25H in the Temp register. This MR operation takes
three clocks as described earlier. In the meanwhile, PC contents would have been incremented to
C004H.
Only now the control unit has understood that the instruction to be executed is ‘MVI M, 25H’. The
control unit understands that for instruction execution, 25H in Temp register should be moved to
memory location whose address is present in HL pair. This is performed in a MW machine cycle using
three clock cycles, whose description follows.
Let us say HL pair has the value D300H. Then contents of D300H should change to 25H. The con¬
trol unit directs the register select unit to select HL pair. Then HL contents are latched by the internal
address latch, and then buffered and sent out on A15.8 and AD7_0 pins. This is done during T1 of MW
machine cycle.
In addition to address information, status signals as shown in the following are activated by the
control unit during Tl.
The last three clock cycles in ‘MVI M, 25H’ instruction is an example for MW machine cycle.
Waveforms for MW machine cycle are shown in Fig. 13.10.
The address sent out from a register pair in a MW machine cycle depends on the MW machine
cycle under consideration as shown in the following.
Clock
ALE
WR*
Ready
Fig. 13.10
Notel: Addresses sent out from BC, DE, HL, SP or WZ (but never from PC) Waveforms for MW machine
Note2: Data sent out from any 8-bit register except IR, W and Z cycle (without wait state)
The data sent out from a register during a MW machine cycle depends on the MW machine cycle
within an instruction as shown below. Here, ‘r’ stands for any of the registers A, B, C, D, E, H, or L.
r Saving register r in memory pointed by HL in ‘MOV M, r’
Flags Pushing information above stack top in ‘PUSH PSW’
Temp Saving of 25H in memory pointed by HL in ‘MVI M, 25H’
The address sent out from a register pair in a MW machine cycle depends on the MW machine
cycle within an instruction as shown in the following.
The data sent out from a register during a MW machine cycle depends on the MW machine cycle
within an instruction as shown in the following. Here, ‘r’ stands for any of the registers A, B, C, D, E,
H, or L.
Example 5
Execution of PUSH PSW instruction. Let us say, at location C007H, we have F5H, which is the
opcode for the instruction PUSH PSW. This instruction needs 12 clock cycles for the OF, decode, and
execution. It uses an OF machine cycle of six clocks, followed by two MW machine cycles, each of
three clocks. The action performed by the 8085 in these 12 clock cycles is explained in the following.
In the first three clock cycles, F5H will be received in the IR register from memory location
C007H. The decoding is done in T4 state. The control unit understands that the opcode corresponds
to the single-byte instruction whose mnemonic is PUSH PSW. In the meanwhile, PC contents would
have been incremented to C008H. In the next two clock cycles of this OF machine cycle, SP value
will be decremented by 1. These six T states constitute the OF machine cycle in this case.
By the end of the T4 state in the OF machine cycle, 8085 understands that for instruction execu¬
tion, contents of the Accumulator and the Flags register should be stored above the stack top. Suppose
149
SP content is C708H, Accumulator content is 30H, and Flags value is 40FI, then C707FI contents should
change to 30FI, and C706FI contents should change to 40FI.
To push accumulator above the stack top, first of all SP contents have to be decremented from
C708FI to C707FI using the decrementer. This is performed in T5 and T6 states of OF machine cycle.
Only then MW machine cycle begins by sending out the address C707FI from SP, and the data 30FI
from the Accumulator. As it is a MW machine cycle, PC value remains unaltered at C008FI. Flowever, >
«5
in the meanwhile the SP value would be decremented from C707FI to C706FI. This MW machine
cycle uses three clock cycles.
Then the next MW machine cycle begins by sending out the address C706FI from SP, and the data
hi
40FI from Flags register. As it is a MW machine cycle, PC value remains unaltered at C008FI. Even 53
SP value remains unaltered at C706FI in this case. This MW machine cycle uses three clock cycles.
Fig. 13.12 depicts OF, decode, and execution of PUSFI PSW instruction in a simple way. In this
figure, the original contents of C706FI and C707FI are shown as 52FI and 42FI, respectively.
1. When the instruction execution requires storing information above the stack top. Instructions
in this category are PUSH rp, CALL al6, RSTn, and conditional call instructions.
2. When the instruction is INX rp or DCX rp.
3. When the instruction is PCHL or SPHL.
4. When it is a conditional return instruction.
Example 6
Execution of OUT 25H instruction. Let us say, at location C008H, we have D3H, which is the opcode
for the instruction ‘OUT a8\ This instruction needs ten clock cycles for the OF, decode, and execution.
It uses an OF machine cycle of four clocks, followed by a MR and an IOW machine cycle, each of three
clocks. The action performed by the 8085 in these ten clock cycles is explained in the following.
In the first three clock cycles D3H will be received in IR register from memory location C008H.
The decoding is done in T4 state. The control unit understands that the opcode corresponds to the
2-byte instruction whose mnemonic is OUT. These four T states constitute the OF machine cycle. In
the meanwhile, PC contents would have been incremented to C009H.
As the control unit understands that the OUT instruction is 2-bytes long, it decides to fetch the
second byte of the instruction from the next memory location. The 8085 performs a MR machine
cycle, by sending out the PC value of C009H, and by receiving 25H in the W register and also the Z
register. The point to be noted here is that both W and Z registers are loaded with the same value, 25H.
This MR operation takes three clocks. In the meanwhile, PC contents would have been incremented
to C00AH.
150
Only now the control unit has understood that the instruction to be executed is OUT 25H. The
control unit understands that for instruction execution, contents of the Accumulator should be stored
in an output port whose address is 25H. This is performed in an I/O write (IOW) machine cycle using
three clock cycles.
If the Accumulator has the value D5H, then contents of the output port 25H will change to D5H.
Just as with a MW machine cycle, PC value remains unaltered at COOAH in this IOW machine cycle.
8 Fig. 13.13 depicts OF, decode, and execution of OUT 25FI instruction in a simple way. In this figure,
a>
o the original content of output port 25FI is shown as 52FI.
sa
s
.2
S C008H D3H To IR 3+1 clocks OF (OUT a8)
U) C009H 25H To W and Z 3 clocks MR
00
s COOAH
Jj
H
O/P port 25H |52Ht)5H |- From Acc 3 clocks IOW Fig. 13.13
Illustrating ‘OUT 25H’
Total of 10 clock cycles with 3 machine cycles instruction execution
-
<• T1- -T2- -T3-
Clock
Out
Al5-8 from W
Out Out
AD7_O from Z from A
ALE
WR*
Fig. 13.14
Ready
Waveforms for IOW machine
cycle (without wait state)
Note that in an IOW machine cycle, W and Z will have identical 8-bit port address. There is a definite
advantage because of address duplication on A15.8 and AD7_0 when using 8755 (2K X 8 EPROM and
two 8-bit ports) and 8155 (combination of 256 X 8 RAM, 3 I/O ports, and 14-bit timer). A 8085-based
151
microcomputer can be formed using only these two chips in addition to 8085. However, these chips are
not in favour these days, except when minimum chip microcomputer configuration is desired.
The 16-bit value in register pair WZ will be sent out as the address in the 10W machine cycle. Also,
note that in an IOW machine cycle, only the Accumulator contents will be sent out as the data to the
addressed output port. In 8085, we come across IOW machine cycle only during the execution of OUT
a8 instruction. >
«5
Example 7
Execution of IN 35H instruction. Let us say, at location COOAH, we have DBH, which is the opcode hi
for the instruction ‘IN a8’. This instruction needs ten clock cycles for the OF, decode, and execution. It
uses an OF machine cycle of four clocks, followed by a MR and an IOR machine cycle, each of three
53
clocks. The action performed by the 8085 in these ten clock cycles is explained in the following.
In the first three clock cycles, DBH will be received in IR register from memory location COOAH.
The decoding is done in the T4 state. The control unit understands that the opcode corresponds to the
2-byte instruction whose mnemonic is IN. These four T states constitute the OF machine cycle. In the
meanwhile PC contents would have been incremented to COOBH.
As the control unit understands that IN instruction is 2-bytes long, it decides to fetch the second
byte of the instruction from the next memory location. The 8085 performs a MR machine cycle by
sending out the PC value of COOBH, and by receiving 35H in the W register and also the Z register.
The point to be noted here is that both W and Z registers are loaded with the same value 35H. The
advantage of address duplication in WZ pair in IN a8 instruction is same as that in a OUT a8 instruc¬
tion. This MR operation takes three clocks. In the meanwhile, PC contents would have been incre¬
mented to C00CH.
Only now the control unit has understood that the instruction to be executed is IN 35H. The
control unit understands that for instruction execution, the Accumulator should be loaded with contents
of the input port whose address is 35H.
This is performed in a I/O read (IOR) machine cycle using three clock cycles. If input port 35H has
the value D5H, then contents of the Accumulator will change to D5H. Just as with a IOW machine
cycle, PC value remains unaltered at C00CH in this IOR machine cycle.
Fig. 13.15 depicts OF, decode, and execution of IN 25H instruction in a simple way. In this figure,
the original content of the Accumulator is shown as 52H.
Acer I JZ¥TD5H U-From i/p port 35H 3 clocks IOR Fig. 13.15
Illustrating ‘IN 35H’
Total of 10 clock cycles with 3 machine cycles instruction execution
Clock
RD*
So far we have come across OF, MR, MW, IOR, and IOW machine cycles. The other possible machine
cycles in 8085 are BI (bus idle) and INA (interrupt acknowledge) machine cycles. They will be
discussed in the chapter on 8085 interrupts. Now the differences between some of the machine cycles
are presented in the following tables.
■ No. of T states 4 3
■ Status of SI and SO SI = 1, SO =1 SI = 1, SO = 0
■ Data received in IR only any byte register except IR*
■ Address sent out from PC PC, BC, DE, HL, SP, or WZ
■ PC incremented by 1 by 1 only if address sent out by PC
* If it is MR in an IN or OUT instruction, the information is received in both W and Z registers.
At the end of the T2 state in a machine cycle, 8085 senses the Ready input pin. If it is logic 0, 8085
enters 7ÿ state, else it enters the T3 state. The Ready input is permanently tied to logic 1, if the
memory chips and the I/O ports in the system are having compatible speed with 8085. Otherwise,
appropriate number of wait states has to be generated by external circuitry. In fact, in the ALS kit the
Ready pin is tied to logic 1. But then, how to conclude whether the memory chips are having compat¬
ible speeds or not? That is the topic of this section.
As an example, let us check up if the 27128A-20 16K X 8 EPROM chip can be used without any
wait states with a 8085 operating at 320 nS clock period. For interfacing a memory chip, the most
important timing parameters to be considered are fAcc, fCE, and fOE. The memory read timing for a typ¬
ical EPROM chip is shown in Fig. 13.17.
For 27128A-20 chip, the characteristics are as follows.
8 CE*
a>
o
sa <CE
s
.2 OE*
S
in
00
s« Tristate
*0E
Data output
H Fig. 13.17
Memory read timing
Data
available Data available by this for 27128A-20
by this time time in the worst care 16KX8 EPROM
tAcc = 200 nS means that, it takes a maximum of 200 nS to output the data after stable address is
received by the 27128, under the following assumptions.
1. The memory chip would have been selected atleast 200 nS (7CE) before the data is output by it.
2. The output enable line of the memory chip would have been activated atleast 75 nS (/0E)
before the data is output by it.
/CE = 200 nS means that, it takes a maximum of 200 nS to output the data after the 27128 has been
selected, under the following assumptions.
1. The memory chip would have received stable address atleast 200 nS (/Acc) before the data is
output by it.
2. The output enable line of the memory chip would have been activated atleast 75 nS (/0E)
before the data is output by it.
Generally address and chip select signals are received by a memory chip at about the same time.
Of course, there will be some time difference depending on the propagation delays of the chips
between the microprocessor and the memory chip.
tQE = 75 nS means that, it takes a maximum of 75 nS to output the data after the 27128 output has
been enabled, under the following assumptions.
1. The memory chip would have received stable address atleast 200 nS (rAcc) before the data is
output by it.
2. The memory chip would have been selected atleast 200 nS (ÿE) before the data is output by it.
Generally, it is the read signal that enables the output buffers of a memory chip to output the data.
This signal is activated a little while after the address and chip select signals are received by the chip.
The 27128 chip needs a maximum of 75 nS to output the data after the output of the chip is enabled.
155
The bus timing characteristics of MR machine cycle is shown in Fig. 13.18. The important parame¬
ters to be considered are tMA tLDR, and is the time interval between valid address on A15.0 and
valid data on AD7_0. tLDR is the time interval between trailing edge of ALE and valid data on AD7_0
during a read operation. ;RD is the time interval between leading edge of RD* and valid data on AD7_0.
>
«5
A 15-8
I (AD
hi
as
AD7_O
I
ALE
*AL ADR
tcc
RD*
TC'I
*AC fRD Fig. 13.18
\ Time at which 8085 reads data Bus timing character¬
T \Data available by this istics of MR machine
Data available at
time in the worst case cycle
this time
The maximum values of these parameters in nanoseconds, as provided by Intel manuals, for
8085AH and 8085AH-2 are as shown in the following. In this table, T stands for cycle time and
N stands for number of wait states.
8085AH 8085AH-2
If we assume that there are no wait states, and T value as 320 nS (3.125 MHz) for 8085AH, and
200 nS (5 MHz) for 8085AH-2, we get maximum values of these parameters as:
8085AH 8085AH-2
The other important parameters to be considered are tAL, tAC, and tLC. tAL is the time interval
between valid address on A15.0 and trailing edge of ALE. tAC is the time interval between valid address
on AD15_0 and leading edge (1 to 0 transition) of RD* control signal during a read operation. tLC is the
time interval between trailing edge (1 to 0 transition) of ALE and leading edge of RD* control during
a read operation.
The minimum values of these parameters in nanoseconds, as provided by Intel manuals, for
8085AH and 8085AH-2 are indicated as follows.
156
8085AH 8085AH-2
8M If we assume T value as 320 nS (3.125 MHz) for 8085AH, and 200 nS (5 MHz) for 8085AH-2, we
get minimum values of these parameters as:
o
S
a 8085AH 8085AH-2
S
.2 IAL 115 nS 50 nS
s ‘AC 270 nS 115 nS
in
oo Ac 130 nS 60 nS
S
* The other uselul parameter to be considered is tcc. It is the width of the RD* control signal in the
H logic 0 state during a read operation. The minimum value of this parameter in nanoseconds, as pro¬
vided by Intel manuals, for 8085AH and 8085AH-2 are as shown.
8085AH 8085AH-2
If we assume that there are no wait states, and T value as 320 nS (3.125 MHz) for 8085AH, and
200 nS (5 MHz) for 8085AH-2, we get minimum value of this parameter as:
808SAH 8085AH-2
Ac 400 nS 230 nS
The 8085 reads the data on the AD7.0 lines when RD* makes 0 to 1 transition. Thus maximum
value of tAD should be ideally tAC + tcc. So for 8085AH working at 320 nS, maximum value of tAD
should be 270 nS + 400 nS = 670 nS. But practically, the data should be present on AD7_0 some time
before the 0 to 1 transition of RD* signal. Thus Intel manuals specify a maximum value of 575 nS for
7AD when 8085AH is working at 320 nS.
Similarly, maximum value of 7LDR should be ideally tLC + Ac- So for 8085AH working at 320 nS,
maximum value of 7LDR should be 130nS + 400 nS = 530 nS. But practically, the data should be
present on AD7_0 some time before the 0 to 1 transition of RD* signal. Thus Intel manuals specify a
maximum value of 460 nS for 7LDR when 8085AH is working at 320 nS.
Similarly, maximum value of 7RD should be ideally tcc. So for 8085AH working at 320 nS, fRD
should be 400 nS. But practically, Intel manuals specify a maximum value of 300 nS for 7RD when
8085AH is working at 320 nS.
Fig. 13.19 shows the actual interfacing details of 27128A-2 chip in the ALS kit. Using this
figure and figures 13.17 and 13.18, we can assess the compatibility of this chip with 8085AH work¬
ing with 320-nS clock period.
We start the calculations assuming that stable address and IO/M* signals are sent out by the
8085AH at time 0 nS. Then, ALE goes to 0 state at 115 nS (rAL), and RD* is activated at 270 nS (rAC).
Address A13_8 is received by the 27128 from 8085AH via 74LS244 octal line driver, which has a prop¬
agation delay of 12 nS. Address A7_0 is received by the 27128 from 8085AH via 74LS373 octal latch,
157
Octal bus
transceivers >
3
74LS245 Z
8 ns 8. g
oo Z
o c
00
01 ®
8085AH 365 ns
3.125 MHz (242 ns)
320 ns clock
0 ns
\7 Latch 357 ns
74LS373 (234 ns)
8085AH-2 AD7.0 30 ns
5 MHz D7-0
200 ns clock Jl
ALE
A7-0 +200 ns
115 ns
(50 ns) =230 ns
30 ns
16KX8
A15-8 EPROM
27128A-20
0 ns A13-8
12 ns a a
o rn
C« Cfl
- a?
m in §
Octal ¥V T
line driver
CS* OE*
74LS244
34 ns 282 ns
12 ns
(127 ns)
0
A15 h 07*
II
06*
a14 12 ns h
3 to 8
05*
Octal
0 ns line driver decoder
74LS244 IO/M* 74LS138 03*
12ns 22 ns
IO/M* 02*
Ej*
RD* Oi*
282 ns 0 E2* O0*
RD* 1 E3
(127 ns)
270 ns
(115 ns)
8
M The 74138 receives A15_14 from 8085 via 74LS244 (octal line driver, with 12-nS delay). It simultane¬
0 ously receives IO/M* signal from 8085 via 74LS244 (octal line driver, with delay of 12 nS). Then CS*
S
a signal is received by the 27128 from 74LS138 (3 to 8 decoder, with delay of 22 nS). Thus, CS* sig¬
s
.2
nal is received by 27128 at the end of 34 nS. Thus, the data can only come out on D7.0 pins of 27128
S by 34 nS + tCE = 34 nS + 200 nS = 234 nS.
in
00
s 1 3.4.3 EARLIEST DATA OUTPUT TIME CONSIDERING 1Qe
jj
H
The 8085AH activates RD* signal at 270 nS. This signal goes to OE* pin of 27128 via 74LS241 (octal
line driver, with 12nS delay). Thus, OE* signal is received by 27128 at the end of 282 nS. Thus, the
data can only come out on D7.0 pins of 27128 by 282 nS + tOE = 282 nS + 75nS = 357nS.
From the discussion in the previous three paragraphs, it should be clear that the earliest data out¬
put time will be 357 nS, considering all the three parameters tACC, tCE, and tOE. In fact, tAcc can be as
large as 200 nS + (357 nS - 230 nS) = 327 nS, without affecting the time at which data comes out of
memory chip. Similarly tCE can be as large as 200nS + (357nS - 234nS) = 323 nS, without affect¬
ing the time at which data comes out of the memory chip.
The 8085AH receives this data from 27128 via 74LS245 (octal bus transceiver, with delay of 8 nS).
Thus, valid data is received by the 8085AH at 357 nS + 8 nS = 365 nS.
Now let us perform memory compatibility check with respect to the parameters ?AD, fLDR, and fRD.
Compatibility with respect to tAc,: tAD is the time interval between valid address on A15_0 and valid
data on AD7_0. For 8085AH working with a T state of 320 nS, it is a maximum of 575 nS. But in this
case, the valid data is available in 365 nS itself. Thus the memory speed is compatible, with actually
an excess time margin of 575 nS - 365 nS = 210nS.
Compatibility with respect to tÿ: ?LDR is the time interval between trailing edge of ALE and valid
data on AD7_0 during a read operation. For 8085AH working with a T state of 320 nS, it is a maximum
—
of 460 nS. The trailing edge of ALE occurs at 115 nS. Thus tLDR in this case is only 365 nS 115 nS
= 250 nS. Thus, the memory speed is compatible, with actually an excess time margin of 460 nS —
250 nS = 210 nS.
Compatibility with respect to tÿ: rRD is the time interval between leading edge of RD* and valid
data on AD7_0. For 8085AH working with a T state of 320 nS, it is a maximum of 300 nS. The 8085AH
activates RD* signal at 270 nS and the valid data is available by 365 nS itself. Thus tRD in this case
—
is only 365 nS 270 nS = 95 nS. Thus, the memory speed is compatible with actually an excess time
margin of 300nS - 95 nS = 205 nS.
159
Thus, 27128-20 EPROM chip is speed compatible with 8085AH. In fact, as per the discussion in
the previous three paragraphs, there is an excess time margin of atleast 205 nS. Thus, the 8085AH can
be interfaced with memory chips, which have the worst case specifications as shown in the following.
The 8085AH-2 works with 200nS clock period. We start the calculations assuming that valid address,
and IO/M* signals are sent out by the 8085AH-2 at time 0 nS. Then, ALE goes to 0 state at 50 nS
(TAL), and RD* is activated at 115 nS (tAC).
Earliest data output time considering tAcc: Address A13.18 is received by the 27128 from 8085 via
74LS244 (octal line driver, with delay of 12 nS). Address A7_0 is received by the 27128 from 8085 via
74LS373 (octal latch, with delay of 30 nS). Thus, address A13_0 is received by 27128 at the end of 30 nS.
Thus, the data can only come out on D7_0 pins of 27128 by 30 nS + tAcc = 30 nS + 200 nS = 230 nS.
Earliest data output time considering tCE: The 74138 receives A15_14 from 8085 via 74LS244 (octal
line driver, with 12 nS delay). It simultaneously receives IO/M* signal from 8085 via 74LS241 (octal
line driver, with delay of 12 nS). Then CS* signal is received by the 27128 from 74LS138 (3 to 8
decoder, with delay of 22 nS). Thus, CS* signal is received by 27128 at the end of 34 nS. Thus, the
data can only come out on D7_0 pins of 27128 by 30 nS + fCE = 34 nS + 200 nS = 234 nS.
Earliest data output time considering toE The 8085AH-2 activates RD* signal at 115nS. This sig¬
■
nal goes to OE* pin of 27128 via 74LS241 (octal line driver, with 12nS delay). Thus, OE* signal is
received by 27128 at the end of 127 nS. Thus, the data can only come out on D7_0 pins of 27128 by
127 nS + TOE = 127 nS + 75nS = 202nS.
From the discussion in the previous three paragraphs, it should be clear that the earliest data
output time will be 234 nS, considering all the three parameters tACC, tCE, and rOE. In fact, tAcc can be
as large as 200 nS + (234 nS - 230 nS) = 204 nS, without affecting the time at which data comes out
of memory chip. Similarly TOE can be as large as 75 nS + (234 nS - 202 nS) = 107 nS, without affect¬
ing the time at which data comes out of memory chip.
The 8085AH-2 receives this data from 27128 via 74LS245 (octal bus transceiver with delay of
8 nS). Thus, valid data is received by the 8085AH-2 at 234 nS + 8nS = 242 nS.
Now let us perform memory compatibility check with respect to the parameters TAD, rLDR, and rRD.
Compatibility with respect to tAD: For 8085AH-2 working with a T state of 200 nS, it is a maximum
of 350 nS. But in this case, the valid data is available in 242 nS itself. Thus the memory speed is com¬
patible with actually an excess time margin of 350 nS - 242 nS = 108 nS.
160
Compatibility with respect to tLDR: For 8085AFI-2 working with a T state of 200 nS, it is a maximum
of 270 nS. The trailing edge of ALE occurs at 50 nS. Thus tLDR in this case is only 242 nS — 50 nS =
192 nS. Thus, the memory speed is compatible, with actually an excess time margin of 270 nS —
192 nS = 78 nS.
8M Compatibility with respect to tRD: For 8085AFI-2 working with a T state of 200 nS, it is a maximum
of 1 50 nS. The 8085AFI-2 activates RD* signal at 11 5 nS and the valid data is available by 242 nS
o itself. Thus, tRI) in this case is only 242 nS — 115 nS = 127 nS. Thus, the memory speed is compati¬
S
a ble, with actually an excess time margin of 150 nS — 127 nS = 23 nS.
s
.2 Thus, 27128-20 EPROM chip is speed compatible with 8085AFI-2 also. In fact, as per the discus¬
S sion in the previous three paragraphs, there is an excess time margin of atleast 23 nS. Thus, the
in
00 8085AFI-2 can be interfaced with memory chips, which have the worst case specifications indicated
s as follows:
Jj
H
'ACC = 204 nS + 23 nS = 227 nS
tCE = 200 nS + 23 nS = 223 nS
tOE = 107 nS + 23 nS = 130 nS
In this case, 204 nS is the largest tAcc possible without affecting the time (234 nS) at which the data
comes out of memory chip. Corresponding values for tCE and tOE are 200 nS and 107 nS respectively.
23 ns in the margin still available when 8085AFI-2 is interfaced with memory chips.
The present day memory and peripheral chips are fast enough for a 8085 working at 3 MHz. So wait
states are generally not needed at all. If 8085AH-2 is used, which can work at 5 MHz, there may be a
need to insert one wait state, between T2 and T3. The circuit shown in Fig. 13.20 easily achieves this.
The circuit uses two D-type positive edge-triggered flip flops, with active low Reset inputs. At the
beginning of Tl, ALE goes high and causes Q1 to go high. As Q1 and D2 are connected, D2 remains
high throughout Tl . The positive edge of T2 clock causes Q2* to become 0. This is connected to ready
Resetl* Reset2*
1 D1 Q1 D2 Q2
Edge Edge
triggered triggered
D flip-flop D flip-flop
ALE Clkl Ql* CLk out Clk2 Q2*
of 8085 To Ready
input of
8085
ALE
Q1 = D2
1. What is the difference between the programmer’s view of 8085 and architecture of 8085?
2. With a neat diagram explain the architecture of 8085 needed for instruction execution.
3. Explain the role of IR, Temp, W and Z registers in the architecture of 8085.
4. Indicate with reason, whether the following registers are connected to the internal bus in a
unidirectional or bi-directional way.
a. Accumulator
b. Temp
c. IR
d. Flags
e. W
f. D
5. Mention the steps involved in an instruction cycle and explain the steps with an example.
6. Explain the terms clock cycle (T state), machine cycle, and instruction cycle.
7. List the machine cycles, the registers involved, and the total number of clock cycles needed
in the fetching and execution of the following instructions (check your answer with the
information provided in the appendix).
a. MVIB, 25H; b. STAX D; c. LXI B, 1234H; d. XRAB;
e. JNC 1234H; f. CALL 1234H; g. DAD D; h. POP B;
i. RST 5; j. PCHL; k. XTHL; 1. SPHL;
m. XCHG; n. RNC; o. SHLD 1234H; p. OUT 25H.
162
8. Give two examples for each of the following.
a. Instructions that need only OF with 4 T states;
b. Instructions that need only OF with 6 T states;
c. Instructions that need OF and MR;
d. Instructions that need OF, MR, and MR;
8 e. Instructions that need OF, MR, and MW.
a>
o 9. Give atleast one example for each of the following.
sa
s
.2
a. Instructions that need OF, MR, MR, MW, and MW;
b. Instructions that need OF, MR, and IOR;
S
in c. Instructions that need OF, BI, and BI;
00
s d. Instructions that need OF, MW, and MW.
Jj 10. Compare the following machine cycles.
H
a. OF with MR;
b. MR with IOR;
c. MW with 10W.
11. Explain the terms tAcc, tCE, and tOE for a memory chip.
12. Explain the following terms with reference to a MR cycle.
a. tAD;
b- h.DR;
c- ?RD;
d- ?AL;
e. tAC.
f- *LC-
13. Suppose a memory chip has the following specifications.
fAcc = 350 nS
tQE = 360 nS
tog = 180 nS
Can this chip be interfaced with (assume interface circuit of Fig. 13.19):
a. 8085AH with a T state of 350 nS?
b. 8085AH-2 with a T state of 220 nS?
14. Calculate the number of wait states needed if 8085AH-2 working with a T state of 200 nS is
required to be interfaced with a memory chip having the following specifications. Assume
interface circuit of Fig. 13.19.
Gcc = 550 nS
= 500 nS
= 220 nS
15. Explain with a neat diagram a circuit to insert one wait state between T2 and T3 states in a
machine cycle.
□ Assembly
Language
Programs
Chapter Heads
14 Simple Assembly Language
Programs
1 5 Use of PC in Writing and Executing
8085 Programs
16 Additional Assembly Language
Programs
1 7 More Complex Assembly Language
Programs
I
INTRODUCTION
In this part, we will mainly discuss the programs for a number
of interesting problems. This part comprises of Chapters 14 to
17. In Chapter 14, we deal with simple 8085 assembly language
programs. In Chapter 1 5, we deal with the use of PC in writing
and executing 8085 programs. In Chapter 16, we discuss addi¬
tional assembly language programs, while Chapter 17 discusses
comparatively more complex assembly language programs.
14 Simple Assembly
m \
P: Language
Programs
■ Exchange 1 0 bytes
• Entry of data and code
• Executing the program and checking result
■ Add two multi-byte numbers
■ Add two multi-byte BCD numbers
■ Block movement without overlap
■ Block movement with overlap
•Approach methodology
■ Add N numbers, of size 8 bits
•Monitor routines
■ Check the fourth bit of a byte
■ Subtract two multi-byte numbers
■ Multiply two numbers of size 8 bits
•Trace of the program
■ Divide a 1 6-bit number by an 8-bit number
■ Questions
This chapter deals with various assembly language programs, such as programs to exchange
10 bytes, to add/subtract two multi-byte numbers, to add two multi-byte BCD numbers, to multiply two
8-bit numbers, and to divide a 16-bit number by an 8-bit number. These assembly language programs
will be manually translated using the opcode information provided in the appendix.
Write an 8085 assembly language program to exchange 10 bytes of data stored from location X with
10 bytes of data stored from location Y.
Flowchart for the program
Flowchart for solving the problem is shown in Fig. 14.1.
166
0
w
w
0)
o A
0 E LDAXD
DE CIOOH = X 1 1H -- ->
o.
S 22H CD
0 33H © STAX D
VV°
w
oo
o
©
00
0)
B A
£ OVB, M MOV A, B
H HL C200H = Y 01H >
02H © ©
03H
Start
Point DE to block X
Point HL to block Y
Load C with 10 (0AH)
-
LDAXD
MOV B, M
MOV M, A
MOV A, B
STAX D
INXD
INXH
DCRC
N (C)=0?
HLT
ORG C000H
LXI D, X / Load DE with C100H (address X)
LXI H, Y / Load HL with C200H (address Y)
MVI C, OAH/ Load C with OAH (C used as down counter)
/ The instructions from here to JNZ LOOP perform exchange
/ of bytes at memory locations pointed by DE and HL
LOOP: LDAX D / Load A from memory pointed by DE
MOV B, M / Load B from memory pointed by HL
MOV M, A / Store A in memory pointed by HL
MOV A, B
STAX D //Store B in memory pointed by DE
/In the convention followed in this book, a single '/' after an instruction
/provides comments for the instruction. If there are N number of '/' after an
/instruction, it provides comments for N instructions preceding the N number
/of semicolons.
INX D
INX H //Increment address pointers DE and HL
DCR C / Decrement C
JNZ LOOP / If not zero jump to LOOP
HLT / Stop when all the bytes are exchanged
The data and code to be entered on the kit for the previous program is shown in the following.
Entry of data: In the previous program, X is symbolic memory location C100H, and data is stored
from C100H as follows.
168
Address Data
C100 11H
C101 22H
C102 33H
C103 44H
0 C104 55H
M
M C105 66H
o C106 77H
U
o C107 88H
a C108 99H
2
.2 C109 AAH
S
in
00
o Similarly, Y is symbolic memory location C200H, and data is stored from C200H as follows.
00
x
Address Data
C200 01H
C201 02H
C202 03H
C203 04H
C204 05 H
C205 06H
C206 07H
C207 08H
C208 09H
C209 0AH
Entry of code: In the previous program, code is stored from C000H as follows with addresses and
code in hexadecimal.
MOV M, A C00A 77
MOV A, B C00B 78
STAX D C00C 12
INX D C00D 13
INX H C00E 23
DCR C C00F 0D
JNZ LOOP C010 C2 08 CO
HLT CO 13 76
It can be noticed from this that LOOP is a symbolic memory location, which is actually memory
location with address C008H. Thus, JNZ LOOP is manually translated as C2 08 CO, where C2 is code
for JNZ and 08 CO is C008H stored in memory in byte reversal form.
169
1 4.1 .2 EXECUTING THE PROGRAM AND CHECKING RESULT
When the program is run by typing on the kit ‘Go C 0 0 0 Exec’, we see the display ‘E’ in the address
field of the display. At this point the 8085 has halted processing because of the HLT instruction exe¬
cution. To check the results after program execution, we have to first of all reset the 8085 by typing
‘Reset’ key. Then the monitor program gains control over the kit, and we can see the contents of Cl 00 2
3
to Cl 09, and C200 to C209. 1
Alternatively, we can end the program with ‘RST 1’ (code = CF) instruction instead of FILT u *
instruction, when we are using ALS kit. Execution of RST 1 instruction on ALS kit results in the mon¬ 5 2
*
itor program gaining control over the kit. So we are now in a position to see the results in memory. ISSL I
fl U-
Thus we can avoid the step of resetting the 8085. The reader must note that with other kits he may
■o <
have to terminate the program with say, RST 5, to transfer control to the monitor program. Refer to 3
10
kit manual to obtain correct information. 2
3</>
Write a 8085 assembly language program to add two multi-byte numbers. The numbers are stored
from locations X and Y in byte reversal form. The size in bytes of the multi-byte numbers is given in
the location, SIZE. The result is to be stored in location Z, in byte reversal form, using 1 byte more
than the size of multi-byte numbers.
Flowchart for the program
Flowchart for solving the problem is shown in Fig. 14.2.
Program to add two multi-byte numbers
;FILE NAME C:\ALS\ADLRGBYT.ASM
8085 ALP TO ADD 2 MULTI BYTE NUMBERS.
THE NUMBERS ARE STORED FROM LOCATIONS X AND Y.
AT X AND Y, THE LS BYTE OF MULTI BYTE NUMBER IS PRESENT.
THE SIZE IN BYTES OF THE MULTI BYTE NUMBERS IS GIVEN IN LOCATION SIZE
THE RESULT IS STORED FROM LOCATION Z, USING SIZE+1 BYTES.
ORG C050H
SIZE DB 03H
ORG C100H
X: DB 12H, 34H, 56H
ORG C200H
Y: DB 56H, 78H, ACH
ORG C000H
Z: EQU C300H; Z is equal to C300H
C050H=Size 03
56 34 12H 4- Number at X
0 C100H=X LSB
w 12H + AC 78 56H 4- Number at Y
w BC
0) 34H
o MSB 01 02 AC 68H 4- Result at Z
0 56H
a LDAX B A
2
0
LSB ADC M
C200H=Y 56H
w
00
O HL 78H + ©
00 ACH MSB
0)
£
H
STAX D
XX 68H LSB
DE
C300H=Z
ACH
©
X 02H
X 01H MSB
Start
Point BC to X
Point HL to Y
Point DE to E
Clear Cy
■»
LDAX B
ADC M
STAX D
INXB
INX II
INXD
Deer. Size
N
(Size)=0?
I Store 00H as MS
Store 01 as MS Y byte of result
byte of result
N Y
Cy=0? HLT
HLT
Notice that INX B, INX H, and INX D instructions do not affect any flags. Also note that DCR A
instruction affects all flags except Cy. Thus, when we execute JNC SKIP later on in the program, the
jump takes place based only on the value of the Cy flag after execution of the ADC M instruction. In
fact, in view of such requirements only, the designers of 8085 have implemented INX instructions
such that flags are not affected, and DCR/INR instructions such that Cy flag is not affected.
In this program, we faced the problem of shortage of registers, although seven registers are
provided in 8085. So we had to make use of memory location SIZE. In Motorola 6800, with only two
registers inside, we do not face any problem! This is because of the powerful indexed addressing mode
provided in 6800. See chapter on 6800 microprocessor for details.
Write an 8085 assembly language program to add two multi-byte BCD numbers. The numbers are
stored in locations X and Y in byte reversal form. The size in bytes of the multi-byte numbers is given
in the location, SIZE. The result is to be stored in location Z, in byte reversal form, using 1 byte more
than the size of multi-byte numbers.
Flowchart for the program
Flowchart for solving the problem is shown in Fig. 14.3.
172
C050H = Size 03
DE XX 12 ®
'X 55
X 01
Start
Point BC to X
Point HL to Y
Point DE to Z
Clear Cy
LDAX B
ADC M
DAA
STAX D
INXB
INXH
INXD
Deer. Size
N
(Size)=0?
Y
N
Cy=0?
Y
Store 00 as Store 01 as
MSB of result MSB of result
HLT HLT
ORG C200H
3
ft
Y: DB 56H, 78H, 98H
ORG C000H
Z: EQU C300H
STC
CMC ;;Clear Carry flag
;The instructions from here to JNZ LOOP perform decimal addition with carry
;of a byte pointed by BC with a byte pointed by HL. The result is stored in
/memory pointed by DE. BC, HL, and DE pointers are incremented, and contents
;of location SIZE is decremented.
LOOP: LDAX B /Load A from memory pointed by BC
ADC M /Add with Cy memory contents pointed by HL
DAA /Perform decimal adjustment
STAX D /Store result in memory pointed by DE
INX B
INX H
INX D ///Increment the pointers BC, HL, and DE by 1
LDA SIZE
DCR A
STA SIZE ///Decrement contents of location SIZE
JNZ LOOP /If result of decrement is nonzero jump to LOOP
/When we are out of this loop, contents of A will be 00H
JNC SKIP
INR A
SKIP: STAX D ///Store 00 or 01 in memory pointed by DE based on Cy
HLT /Alternatively terminate with RST 1
174
■ 1 4.4 BLOCK MOVEMENT WITHOUT OVERLAP
Write an 8085 assembly language program to perform block movement. The blocks are assumed to
be non-overlapping. The block starting at location X is to be moved to the block starting at Y. The
o
M block size is provided in the location, SIZE.
M
U
o Flowchart for the program
g Flowchart for solving the problem is shown in Fig. 14.4.
S
in
00
s
0
C050H=Size 04 C
STAX D
C100H=Y XX 11
XX 22 ©
33
:x 44
A
MOV A. M
C200H=X 11
22 ©
33
44
Start
Point HL to X
Point DE to Y
Load C with size
MOV A, M
STAX D
1NXH
INXD
DCRC
N
(c)=0?
Y Fig. 14.4
Performing block movement
HLT
without overlap
175
Program to perform block movement without overlap
/FILE NAME C:\ALS\BLKMOV.ASM
9 9"
SIZE: DB 04H ’
0
*
IS
ORG C200H 0>
X: DB 11H, 22H, 33H, 44H 3«
ORG C000H
Y: EQU C100H
LDA SIZE
MOV C, A /Load C with contents of location SIZE
LXI H, X /Load HL with C200H (address of X)
LXI D, Y /Load DE with C100H (address of Y)
/The instructions from here to JNZ LOOP perform movement of the byte pointed
/by HL to memory pointed by DE. HL and DE pointers are incremented. Counter C
/is decremented.
LOOP: MOV A, M
STAX D / /Move the byte pointed by HL to memory pointed by DE
INX H
INX D
DCR C ///Increment the pointers HL and DE. Decrement C.
JNZ LOOP /Jump to LOOP if result after decrement is nonzero
Write an 8085 assembly language program to perform block movement. The block starting at location
X is to be moved to the block starting at Y. The block size is provided in the location, SIZE. The pro¬
gram should work irrespective of overlap of blocks or not.
In this program we have used C register as a down counter to store source block on the stack. We have
used B register as a down counter to pop out the source block on the stack to the destination block.
The program works even if there is overlap of blocks.
176
We load B and C registers with the size of the block. Then load HL and DE with the starting address
of source block and destination block, respectively.
Then the byte pointed by HL is moved to A. It is then pushed above the stack top. Even flags reg¬
ister is pushed on the stack top because of the execution of PUSH PSW. But we do not care about it.
Incrementing the pointers HL and DE, and decrementing the counter C is done. If C contents are not
8 yet zero, we repeat the operations indicated in this paragraph.
a> Thus when we come out of the loop, the source block is stored on the stack. Of course, the stack
o size would have become twice the block size, because of pushing flags register also every time. The
s
a DE register will now be pointing to the next location after the last location in the destination block.
E Now decrement pointer DE. Pop the stack top to A and flags. Store A value in memory pointed by
.2
S DE. Just ignore the flags value popped out. Now decrement the counter B. If B contents are not yet
in
00 zero, we repeat the operations indicated in this paragraph.
s« Halt when we come out of this loop, as the block movement is over.
H
Flowchart for the program
Flowchart and the method for solving the problem is shown in Fig. 14.5.
Program to perform block movement even with overlap
/FILE NAME C:\ALS\BLKMOV2.ASM
ORG C050H
SIZE: DB 04H
ORG C100H
X: DB 11H, 22H, 33H, 44H
ORG C102H
Y: DB 33H, 44H, 55H, 66H
ORG C000H
LDA SIZE
MOV B, A
MOV C, A ///Load B and C registers with block size
LXI H, X /Load HL with C100H (address X)
LXI D, Y /Load DE with C102H (address Y)
/The instructions from here to JNZ LOOP1 perform pushing the byte pointed by
/HL to the stack, and then incrementing the pointers HL and DE, and
/decrementing the counter C.
©MOV A, M A
C100H=X 11
HL s'* 22 y block X </>
C102H=Y 3 11 3
DEÿ"* 4] 22 ■o
15 33 block Y r
&)
«
6 44
@ STAX D 3
(Q
>
(A
c <A
jU (0
Start
■»
MOV A, M
PUSH PSW
INX H
1NX D
OCR C
N
(C)=0?
*Y
DCXD
POP PSW
STAX D
DCRB
N
(B)=0?
Fig. 14.5 Performing block
Y movement even with overlap
N/
HLT
178
DCR C /Decrement C
JNZ LOO PI /Jump to L00P1 if C is not decremented to 00H
/When are out of this loop, DE will be pointing to the next location
/after end of destination block
0 /The instructions from here to JNZ L00P2 perform decrementing the pointer DE
M
M /and then popping a byte from the stack and storing in memory pointed by DE.
0
u
o /Finally decrements the counter B.
a
2 L00P2: DCX D /Decrement the pointer DE
.2 POP PSW
S
w STAX D ;;Pop the byte from stack top to memory pointed by DE
oo DCR B ;Decrement B
o
00
0) JNZ LOOP2 ;Jump to L00P2 if B is not decremented to 00H
£
H HLT
Write an 8085 assembly language program to perform addition of N 1-byte numbers. N value is pro¬
vided in location X. From location X+ 1, the N numbers are stored. Store the result in location Y and
Y+ 1. Also display the result in the address field.
ORG C100H
X: DB 03H, ECH, DDH, 0EH
ORG C000H
Y: EQU C200H
CURAD: EQU FFF7H /Applicable only if ALS kit is being used
UPDAD: EQU 06BCH /Applicable only if ALS kit is being used
I I o1
C100H=X 03 ■» C EC C9
X+l ECH + DD + 0E
HL
DDH C9 D7 </)
3
OEH ■o
r 9
■
3 >
C200H=Y x: D7 %&)
s
9
Y+l X 01
(c
9
3
2
TJ <
0
a
i
0)
3V)
Start
Load B with 00
Load C with one less
than the no. of bytes
Point HL to X+l
MOV A, M
INXH
ADD M
N
Cy =0?
INRB
DCRC
N
(C)=0?
Y
Move result in
B and A to HL
Display result
in address field
Fig. 14.6
Addition of N 1-byte numbers
HLT
180
;The instructions from here to JNZ AGAIN performs the following. It adds the
/next byte to A. B will be incremented by 1 if there is Carry. Decrements the
/counter C, and if nonzero repeats the operations.
AGAIN: INX H
ADD M //Add to A the next byte
8M JNC NOINRB
0 INR B Z/B is incremented by 1 if there is Cy
s
a
E NO INRB: DCR C /Decrement C
.2 JNZ AGAIN /If not zero jump to AGAIN
S
in /When we come out of the loop, B will have MS byte of sum and A will have LS
00
s« /byte of sum
H MOV H, B
MOV L, A //Load HL with the sum in B and A registers
SHLD Y /Store the sum in word location Y
SHLD CURAD
CALL UPDAD //Display the result in address field
HLT /Make sure to terminate with HLT instead of RST 1
In the previous program, suppose we terminate the program with RST 1 instead of HLT. Then the
program displays the result in the address field, and the next moment returns control to monitor pro¬
gram, which displays the sign on message. Thus, the user fails to see the result in the address field. If
the program is terminated with HLT, the processor halts after the program displays the result in the
address field. Thus, the result is displayed till the processor is reset.
Any 8085 microprocessor kit generally provides a number of utility routines, which can be called by
the user. These routines form part of the monitor program in the EPROM/ROM of the kit. These
routines simplify program development. As a general rule, the user should save all the registers of
interest before calling a monitor routine. The registers should be restored with original values after
returning from the monitor routine.
UPDAD routine: In this program, we have used the monitor routine UPDAD, which is used to update
the address field on the kit. The routine displays the contents of the word at symbolic location
CURAD.
On the ALS kit UPDAD routine is at address 06BCH, and CURAD is word location FFF7H.
Thus, if we have 78H in location FFF7H and 56H at location FFF8H, execution of ‘CALL
UPDAD’ instruction results in display of ‘5678’ in the address field. CALL UPDAD is coded as
CD BC 06.
In addition, if B register content is 00H, there will not be any dot at the end of the address field. If
B contents are 01H, a dot will be displayed at the end of the address field.
The contents of the registers A, B, C, D, E, H, L, and flags are all altered on executing CALL
UPDAD.
The other common routines like UPDDT, RDKBD are described later.
181
1 4.7 CHECK THE FOURTH BIT OF A BYTE
Write an 8085 assembly language program to check whether the fourth bit of a byte at location X
is a 0 or a 1. If 0, store 00 at location Y. Else, store FF at Y. Display the number and the result in the
address field. if.
3
Flowchart for the program TL
r »
Flowchart for solving the problem is shown in Fig. 14.7. 3 >
%| W
I3
fll
9 5T
Start TJ <
0
a
i
0)
Get number in 3
A and H
«
ANI 10H
MVI L, 00
N (A)=00?
.
MVI L, FF
Y
Display result in
address field
Fig. 14.7
To check the fourth bit of a
HLT
given byte
ORG C100H
X: DB 35H
ORG C000H
CURAD: EQU FFF7H
UP DAD: EQU 06BCH
LDA X
MOV H , A //Load A and H from contents of location X.
MVI L, 00H ; Load L with 00.
ANI 10H /Reset all bits to 0, except 4th bit.
JZ OUTZERO ; If result is 00H, jump to OUTZERO.
0
w
w
0)
o
0
a
1 4.8 SUBTRACT TWO MULTI-BYTE NUMBERS
S
o
w Write an 8085 assembly language program to perform subtraction of two multi-byte numbers. The
oo number of bytes in these multi-byte numbers is stored in the location, SIZE. The numbers are stored
o
00
0) in locations starting from X and Y. The number starting at X is subtracted from the number starting at
£
H Y. Result is stored in locations starting from Z.
Flowchart for the program
Flowchart for solving the problem is shown in Fig. 14.8.
Program to subtract two multi-byte numbers
;FILE NAME C:\ALS\SBLRGBIN.ASM
;8085 ALP TO SUBTRACT TWO MULTI BYTE BINARY NUMBERS. THE NUMBER OF BYTES IN
;THESE MULTI BYTE NUMBERS IS STORED AT LOCATION SIZE. THE NUMBERS ARE STORED
;IN LOCATIONS STARTING FROM X AND Y. THE NUMBER STARTING AT X IS SUBTRACTED
;FROM THE NUMBER STARTING AT Y. RESULT IS STORED IN LOCATIONS STARTING FROM
;z.
;IF THE NUMBER STARTING AT LOCATION X IS THE LARGER ONE, THE RESULT WILL BE
;IN 2' S COMPLEMENT FORM.
ORG C000H
STC
CMC ;;Clear Carry flag
C100H=Size
C200H=X </)
HL s'* 3
© ■o
© SBBM r «
C300H=Y
LDAX D
■»
3
IQ “
c w
>
W
DEÿ A a
(c
<p
3
2
TJ <
C400H=Z
_® STAX B
a
i
0
BC/ÿ 0)
3V)
Start
Point HL to X
Point DE to Y
Point BC to Z
Clear Cy
..
LDAX D
SBBM
STAX B
INX H
INX D
INX B
Deer. Size
N
(Size)=0?
Y Fig. 14.8
Subtraction of two multi-byte
HLT
numbers
HLT
184
■ 1 4.9 MULTIPLY TWO NUMBERS OF SIZE 8 BITS
Write an 8085 assembly language program to multiply two 8-bit numbers stored at locations X and Y.
Store the 16-bit result in locations Z and Z + 1. Also display the result in the address field.
o
M
M Flowchart for the program
U
Flowchart for solving the problem is shown in Fig. 14.9.
O
a
2
.2
S
in
00 C100H=X 05H DE
a
o
C200H=Y 04H A
C300H=Z 14H
Z+l 00
Start
Move second
number to A
I
LXI H, 0000
Y
(A)=0?
rx
DAD D
DCR A
N
(A)=0?
/8085 ALP TO MULTIPLY 2 ONE BYTE BINARY NUMBERS STORED AT LOCATIONS X AND
;Y. DISPLAY THE 16 BIT RESULT IN THE ADDRESS FIELD.
if.
;IT USES REPEATED ADDITION, AND AS SUCH IS NOT AN EFFICIENT METHOD. 3
■E.
ORG C100H r9
3 >
X: DB 05H
2|
Y:
ORG C200H
DB 04H
I3
Qi (P
9 9"
’
0
*
ORG C000H IS
u
Z: EQU C300H 3«
CURAD: EQU FFF7H
UPDAD: EQU 06BCH
LXI H, X
MOV E,M
MVI D,00H ///Load DE with the byte at location X
LXI H,Y
MOV A,M //Load A with the number at location Y
EXIT: SHLD Z
SHLD CURAD //HL contents stored in word locations Z and CURAD
CALL UPDAD /Display result in the address field
HLT /Make sure to terminate with HLT instead of RST 1
In a program, knowing the actual values in affected registers and/or memory locations at the end of
the execution of every instruction for a given set of sample data is called ‘tracing the program’. Many
times, it is easier to understand the working of the program by the trace for the program than by its
flowchart or algorithm.
What follows is the trace for multiplication of two 8-bit numbers. The numbers are taken to be 05 H
and 04H.
Suppose several instructions are executed in a loop, then we indicate the contents of the affected
register or memory location for each pass through the loop on a single line separated by ‘|’ character.
186
For example, in the portion of the trace shown below we go through the loop four times. The
values of HL and A at the end of each pass are shown separated by ‘|\ In the third line of the trace
portion, J and NJ stand for ‘jump’ and ‘no jump’, respectively.
ORG C100H
X: DB 05H
ORG C200H
Y: DB 04H
ORG C000H
Z: EQU C300H
CURAD: EQU FFF7H
UPDAD: EQU 06BCH
LXI H, X ; HL = C100H
MOV E, M ; E = 05H
MVI D, 00H ; DE = 0005H
LXI H, Y ; HL = C200H
MOV A, M ; A = 04H
LXI H, 000 OH; HL = 0000H
CPI 00H ; 04H vs 00H
JZ EXIT ; NJ
Write an 8085 assembly language program to divide a 16-bit number by an 8-bit number. The 16-bit
number is at locations X and X + 1. The 8-bit number is at location Y. Display quotient in the address
field and remainder in the data field. if.
3
Flowchart for the program r
■E.
9
Flowchart for solving the problem is shown in Fig. 14.10. “ *>
is
I 3
Numerator —>» 0025H = 37 JQ Tl<
C100H=X 25H
00H
A
B
DENOMINATOR —
QUOTIENT — » =
OAH = IOJQ
3 0003H
a
i
0
0)
3«
Remainder —> = 7 07H
C200H=Y OAH c |o |o 1 0 1 3 1 10 1 71
Address Data
field field
Start
Load B. A with
16-bit numerator.
Load C with denominator
INRB
LXI H, 0000
-
SUB C
N
Cy = 0?
Y
•-
DCRB
INX H
N
(B) = 0?
ADD C
I
Display A (remainder)
in data field
Display HL (quotient)
in address field
Fig. 14.10
HLT
Division of a 16-bit number
by an 8-bit number
188
Program to divide a 16-bit number by an 8-bit number
;FILE NAME C:\ALS\DIV1.ASM
;8085 ALP TO DIVIDE A 16 BIT BINARY NUMBER STORED AT X AND X+l BY AN 8
;BIT BINARY NUMBER STORED AT Y. DISPLAY QUOTIENT IN THE ADDRESS FIELD
0 ;AND THE REMAINDER IN THE DATA FIELD.
w
w
0)
o ;USES INEFFICIENT REPEATED SUBTRACT ALGORITHM
0
a ORG C100H
s
o X: DW 0025H
w ORG C200H
oo
o Y: DB OAH
00
0)
£ ORG C000H
H
UPDAD EQU 06BCH
UPDDT EQU 06D3H
CURAD EQU FFF7H
CURDT EQU FFF9H
LXI H, X
MOV A, M ;;Load A with LS byte of the numerator.
INX H
MOV B, M
INR B ;;;Load B with one more than MS byte of numerator.
LXI H,Y
MOV C, M ;;Load C with the denominator.
LXI H,0;Initialize HL with 0. HL will finally have the quotient.
AGAIN:
SUB C /Subtract C contents from LS byte of numerator.
JNC INC_QUO ;If Cy = 0, jump to INC_QUO.
DCR B ;If Cy = 1, decrement MS byte of numerator.
JZ EXIT ;If zero, jump to EXIT. At this point, if we add
;C contents to A, we get correct remainder in A.
INC_QUO:
INX H
JMP AGAIN ;;Increment quotient and jump to AGAIN.
EXIT:
ADD C ;Now, A contains the remainder.
STA CURDT /Store remainder in CURDT.
SHLD CURAD
CALL UPDAD //Display quotient in address field.
CALL UPDDT /Display remainder in data field.
HLT
189
UPDDT routine: In this program, we have used the monitor routine UPDDT, which is used to update
the data field on the kit. The routine displays the contents of the byte at symbolic location CURDT.
On the ALS kit UPDDT routine is at address 06D3H, and CURDT is byte location FFF9H. Thus,
if we have 78H in location FFF97H, execution of ‘CALL UPDDT’ instruction results in display of
‘78’ in the data field. CALL UPDDT is coded as CD D3 06.
</>
In addition, if B register content is 00H, there will not be any dot at the end of the data field. If B
3
content is 01H, a dot will be displayed at the end of the data field. 1
The contents of the registers A, B, C, D, E, H, L, and flags are all altered on executing CALL ST ®
UPDDT. |g
The other common routine, RDKBD, is described in a later chapter.
“• 5E
r
S
3u>
1. Modify the program to exchange 10 bytes such that it exchanges 4 bytes, and provide trace
for the program.
2. Write an 8085 assembly language program to find the largest and smallest numbers in a set
of N byte-sized numbers. Display them in the address field.
3. Provide trace for the previous program when N = 4.
4. Write an 8085 assembly language program to find the number of Is and 0s in a given byte.
Display the number of Is and 0s in the address field.
5. Write an 8085 assembly language program to check if a given set of N bytes are unsorted,
sorted in ascending order, or sorted in descending order. Store in location RES the value 00,
01, or 02 accordingly.
6. Write an 8085 assembly language program to find if a triangle can be formed given the
length of three sides. If triangle can be formed, store 01 in location RES, else store 00.
7. Write an 8085 assembly language program to find the length of a rectangle, given the
breadth and the perimeter. Strore the result in location LEN.
1 Mi,
15
■
Use of PC in Writing
IM f :
■
and Executing 8085
Programs
In Chap. 5, we have discussed the use of the kit in the keyboard mode. In this case, the user had to
translate the assembly language program into machine code, and enter the program in hexadecimal
using the keyboard. This translation is quite monotonous, especially when the program is quite
long.
If we have a computer with an ASCII keyboard, the program can be entered into the computer mem¬
ory straightaway as an assembly language program. Then, the assembler program in the computer can
translate this assembly language program to machine code, relieving the user from this monotonous task.
The machine code can finally be downloaded from the computer to the kit using RS-232C serial
link. The user can then execute the downloaded program on the kit.
Also, the user can upload a machine language program from the memory of the kit to the hard disk
or the floppy disk on the computer. The hard disk or floppy disk is non-volatile, which means it does
191
not lose the information even after power is switched off and then turned on. Thus the user programs
can be stored as disk files on the computer, for downloading later to the kit for execution.
However, the cost goes up in serial mode, as a computer is a must in addition to the kit. Also, there
must be an assembler software on the computer. The circuitry and the monitor software on the kit also
is increased. But, with the reduction in cost of hardware and software, and the increase in cost of tech¬
nical manpower, software development tools become necessary. Assembler is one such major software Sr0
development tool. Use of assembler is explained in this chapter. 0
m
§3
5. 5
■ 15.1 STEPS NEEDED TO RUN AN ASSEMBLY LANGUAGE PROGRAM
»
-
The following steps are needed for running an assembly language program. 3 “ÿ
IS
3
■ Entering the program on a PC using a text editor. The file so created is given a suitable file name 3
with .ASM extension.
■ The file with .ASM extension is assembled to machine code using a 8085 cross-assembler. This
generates the file with .OBJ extension.
■ The file with .OBJ extension is linked to generate Intel Hex file using a linker. This generates the
file with .HEX extension.
■ The file with .HEX extension is downloaded to the kit using RS-232C serial interface and driver
software.
■ The program can now be executed on the kit using the keyboard mode of operation. Alternatively,
serial monitor commands can be used to run the program. In such a case, the commands are issued
using the PC, and the results are displayed on the CRT terminal of the PC. This method is more
user-friendly, and the development of programs will be faster. Figure 15.1 illustrates the steps
needed for running an assembly language program.
I
Generate .OBJ file using
cross-assembler
I
Generate .HEX file using
linker
I
Download .HEX file to
the kit using PCLINK
software
ORG Directive: This directive provides the origin or starting address for the assembly. It can also be Srn
written as ORIGIN. If this directive is not used, the starting address defaults to 0000H. For example, 0
ORG C 100H directive informs the assembler to assemble the code or data that follows the ORG direc- S?
tive from memory location C100H. 8O
£5
DB Directive: DB stands for define byte. It can also be written as .DB or .BYTE or BYTE. This direc¬ IS
tive reserves 1 byte of memory and initializes it with the value following the DB directive. If no value issI
is indicated after the DB directive, the value defaults to 00H. If more than one value is indicated with u
comma as the separator, they are stored in consecutive byte locations. 3 “ÿ
IS
Example 1 3
3
ORG C150H
P: DB 12H
This tells the assembler that P is a symbolic memory location where a byte value of 12H is stored.
Because of the ORG C 1 50H directive, symbolic address P is treated as memory address C 1 50H. In
the previous example, 12H could have been written in any of the following equivalent ways:
DW Directive: DW stands for define word. It can also be written as .DW or .WORD or WORD. This
directive reserves one word of memory (2-byte locations) and initializes it with the value following
the DW directive. If no value is indicated after the DW directive, the value defaults to 0000H. If more
than one value is indicated with comma as the separator, they are stored in consecutive word locations.
194
Example 1
ORG C150H
P: DW 1234H
This tells the assembler that P is a symbolic memory location where a word value of 1234H is stored.
Because of the ORG C150H directive, symbolic address P is treated as memory address C150H. In
8 location P the value 34H is stored, and in location P +1 the value 12H is stored. This is the byte rever-
a>
o sal method of word information storage. In the earlier example, 1234H could have been written using
Q. decimal, octal, or binary notation also.
s
,S» Example 2
s
U) ORG C400H
00
s« Q: DW
This tells the assembler that Q is a symbolic memory location where a word value of 0000H (default
H
value) is stored. Because of the ORG C400H directive, symbolic address Q is treated as memory
address C400H.
Example 3
ORG C500H
R: DW 04H, 23
Because of the ORG C500H directive, symbolic address R is treated as memory address C500H.
Value 0004H is stored in byte reversal form in word location C500H, and 0017H (23 decimal) is
stored in byte reversal form in word location C502H. Memory location containing 17H (23 decimal)
is referred to as symbolic location R+2.
EQU Directive: This directive equates the label to the left of EQU directive with the value to the right
of the EQU directive. The value on the right of the EQU directive could be a number or an arithmetic
expression or another symbol.
Normally, there will be a number to the right of the EQU directive. The EQU directive can also be
written as EQUAL. The EQU directive does not reserve any memory locations, and it does not initial¬
ize the contents of any memory location.
Example
CURAD : EQU FFF7H
This indicates that wherever we come across CURAD, it is to be treated as FFF7H. For example, if
we come across the instruction ‘SHLD CURAD’, it is to be treated as ‘SHLD FFF7H’.
END Directive: This directive defines the physical end of a program. There can be a symbolic address
immediately following the END directive. It is optional. If the address is specified, it indicates the
program starting address. Without this address, the program execution will be from the first executable
instruction in the program.
In the earlier program we have used ‘END BEGIN’. It could have been just END directive with¬
out the address following. In fact, the program is correctly translated even without the END directive.
As such, END is not compulsory in a program. The assembly process is terminated when end of the
file is encountered.
What is the need for END directive when HLT instruction informs the processor to halt? The
answer is: HLT is an executable instruction of 8085. It is translated by the assembler. Generally
HLT is the last instruction in many simple programs. But, in more lengthy programs, HLT instruction
195
can be seen at many places in the program. In such a case, the translation operation by the assembler
should not stop at the first occurrence of the HLT instruction! The END directive appears only at the
end of the program, and clearly indicates to the assembler that the translation is to be stopped.
Symbolic Addresses: In the earlier program we came across the instruction ‘LXI H, Y\ In this
instruction, Y is a symbolic memory location that stands for memory location C200H. Thus ‘LXI H, Sr0
Y’ is same as ‘LXI H, C200H’. This is because, using the ORG directive as shown below, we have 0
indicated that Y stands for memory location C200H. m
§3
ORG C200H 5. 5
Y: DB 05H
»
-
I*
Similarly X stands for memory location C100H, and Z stands for memory location C300H.
Labels: In the earlier program we see the instruction ‘AGAIN: DAD D’. This instruction is at mem¬ is
ory location C012H. In this instruction, AGAIN is a label. It stands for memory location C012H, z
n
i
where the instruction is stored. There must be a colon between a label and an instruction.
A little later in the program we come across the instruction ‘JNZ AGAIN’, which now stands for
‘JNZ C012H’. With this feature, the programmer simply provides a symbolic name or label to a branch
destination, and uses that symbolic name in the branch instruction as the branch destination. Similarly
in the instruction ‘EXIT: SHLD Z’, EXIT is a label, which stands for memory location C017H.
Comments: Comments are liberally used to make the program understandable. What is present in a
line after a semicolon is treated as a comment, and is not translated. A comment can be just after an
instruction or an assembler directive, and is separated by a semicolon. If we want the entire line to be
a comment, the line should start with a semicolon.
The assembly language program is entered using any editor, preferably a screen editor. While enter¬
ing the program, the exact column at which label, instruction, and comment start is unimportant. But
the instruction should start after column 1. However, the program is easy to read if the various fields
are properly aligned. Any editor generally provides a help feature, using which the user can become
familiar with the editor commands, and use them to enter the program.
The file so created is given a suitable file name with .ASM extension. For example, the earlier
program may be entered as a file using, say, Norton editor with MULT.ASM as the file name. This
.ASM file is called the source file.
The MULT.ASM created using an editor is just a text file. It cannot be directly executed. We have to
first assemble, and then link it. The assembly step involves translating the assembly language program
into machine code. This step generates an .OBJ file.
In the examples given in this text, we have used ‘2500 A.D. 8085 cross-assembler— version 4.01’.
196
1 5.3.1 TRANSLATION IN PROMPT MODE
Type ‘X8085<cr>’ to perform translation in prompt mode. Here, ‘<cr>’ stands for ‘carriage return’,
which is done by typing the key on which ‘Enter’, is marked. The assembler prompts at every step in
this mode. The advantage then is that there is no need to memorize the required steps.
s
M
After typing ‘X8085<cr>’ the assembler will prompt with:
0
S Listing destination? (N, T, P, D, E, L, <cr> = N>)
a
2 where listing stands for listing the source code, object code, and details of errors encountered, if any.
S The listing could be on terminal, or printed using printer, or stored as a file with .LST extension on
in
00 disk. Further the listing, if desired, could be confined to listing only erroneous source code lines.
s In the equation shown, the abbreviations stand for:
Jj
H
N— None (no listing will be made),
T— Terminal (listing will be on terminal),
P— Printer (listing will be printed using printer),
D— Disk (listing stored as a .LST disk fde),
E— Error only will be listed,
L— List On/Ofif (refer to manual for details).
If we are not interested in listing, just type ‘<cr>’. However, it is good to respond with typing ‘T’.
Then, the listing will be on terminal. The listing will pause at the source code where an error occurs.
The user can note the error, and then type ‘<cr>’ to proceed further and see the next error. If we
respond with typing ‘E’, the assembler will prompt the user as follows:
Input filename :
The user should respond with the source file name. In this example, the user should respond with
‘MULT.ASM’. Even ‘MULT’ will suffice. Then the assembler prompts the user for output file name
as follows:
Output filename :
If the user responds with ‘<cr>’, the output file name in this example would be ‘MULT.OBJ’. If
the user responds with ‘DIV’, the output file name would be ‘DIV.OBJ’. Generally, the user responds
to the output file name prompt with ‘<cr>’.
Suppose we choose to generate MULT.LST file on disk for this example. Then using a text editor
we can see the contents of MULT.LST and identify the errors. After this, we make the necessary
corrections in MULT.ASM file using the editor.
Finally, when the assembly process is successful without any error, the result will be placed in
MULT.OBJ file.
197
1 5.3.2 TRANSLATION IN COMMAND MODE
The translation in command mode takes less number of steps. But the steps have to be remembered.
We generally use this mode when we are very familiar with the necessary steps.
Type ‘X8085 MULT.ASM <cr>’ to perform translation in command mode. Even ‘MULT’ will suf¬
fice in place of ‘MULT.ASM’. The source code in MULT.ASM is translated to the object code. The Sr0
result will be placed in MULT.OBJ file. 0
To help in debugging the source code during assembly, we commonly use the following options m
depending on the user taste and convenience. It is assumed that the source file name is ‘MULT.ASM’. § 3
5. 5
Option
X8085 MULT-T
Function
Displays listing on terminal. It pauses
»
-
I*
X8085 MULT-P
X8085 MULT-D
when error is encountered
Continues when <cr> is pressed
Prints listing using printer
Generates .LST file on disk
n
is
Z
X8085 MULT-ET Displays only error listing on terminal i
X8085 MULT-EP Prints only error listing using printer
X8085 MULT-ED Generates .LST file containing errors only
Suppose we choose to generate MULT.LST file on disk for this example. Then using a text editor
we can see the contents of MULT. LST and identify the errors. After this, using the editor we make the
necessary corrections in MULT.ASM file.
Finally, when the assembly process is successful without any error, the result will be placed in
MULT.OBJ file. For more details about the assembler, refer to the assembler manual.
The next step is to link the object module MULT.OBJ, which contains the machine code. We use the
‘2500 A.D. linker’. The linker enables the user to write assembly language programs consisting of
several object modules. The linker will resolve external references and performs address relocation.
This linker is capable of generating all of the most common file formats. Thus, it eliminates the need
for an additional format conversion. The linker may be invoked in prompt mode, command line mode,
or data file mode.
To run the linker in this mode type ‘LINK85<cr>’. The linker will respond with a prompt requesting the
user for file name. In this example, respond with ‘MULT.OBJ<cr>’. Even ‘MULT<cr>’ will suffice.
Then the linker will prompt for the offset address. This offset value input by the user is added to
the value of any ORG statements in the file. Generally, the user responds with <cr>, so that there is
no offset value.
The linker then prompts for an output file name. Generally the user responds with <cr>. This causes
the linker to generate an output file with the same name as the input file, but with a three-character
extension that is determined by the output file type.
198
After the output file name is entered (or after responding with <cr>), the linker will prompt for
library file names. We respond with <cr> in simple programs where we are not using library file names.
Then the linker will prompt for any linker options. For this we respond with <cr>, which results in
generating .FIEX file. In our example, the file created by the linker will be MULT.FIEX, which will be
in Intel FIEX format. For more details about the linker refer to the ‘X8085 cross-assembler’ manual.
s
M
0
2 15.4.2 COMMAND MODE
a
2
.2 To run the linker in this mode type ‘LINK85 -C MULT.OBJ<cr>’. Just ‘MULT’ is enough in place of
S
in ‘MULT.OBJ’ in the above command. The -C option indicates that we are running the linker in com¬
00
s mand mode. This command generates MULT.FIEX file, which will be in Intel Hex format. For more
details about the possible options, refer to ‘X8085 cross-assembler’ manual.
Jj
H
01 C100 00 04 3A
01 C200 00 05 38
02 C300 00 00 00 3B
10 C000 00 21 00 Cl 5E 16 00 21 00 C2 7E 21 00 00 FE 00 CA 90
10 C010 00 17 CO 19 3D C2 12 CO 22 F7 FF 22 00 C3 CD BC 06 D3
01 C020 00 76 A9
00 0000 01 FF
The file consists of several lines called records. A record starts with the character The next two
characters indicate the record length field in hexadecimal. If this value is 00, as it is in the last record,
it indicates EOF (end-of-file) record. This will be the last line of the file. The next four characters indi¬
cate the load address field in hexadecimal. The next two characters indicate the record-type field. It
will be 00 for a data record, and 01 for end of the file record. Even program code is stored as data
record. Thus, only the last record will have 01 in this field. After the record-type field, except the last
two characters, we have the data bytes. The last two characters form the check sum. It is generated as
the 2’s complement of modulo 8-bit addition of length field, load address field, record-type field, and
data bytes.
For example, in the fourth line of MULT.HEX we have
: 10 C000 00 21 00 Cl 5E 16 00 21 00 C2 7E 21 00 00 FE 00 CA 90
It is interpreted as follows. 10 indicates that there are 10H = 16 data bytes in the record. C000 indi¬
cates that they are stored in locations starting from C000H. The 00 indicates that the record-type is
Data record. The 16 data bytes in hexadecimal are 21,00, Cl, ... , and CA. 90 is the check sum and is
obtained as follows.
10 + CO + 00 + 00 + 21 + 00 + Cl +"'+ CA = 70 with carry of 5. Ignoring the carry we get
the result of modulo 8-bit addition. Thus the result of modulo 8-bit addition is 70H = 0111 0000B. Its
2’s complement is 1001 0000B = 90H, which is the check sum.
199
■ 1 5.5 DOWNLOADING THE MACHINE CODE TO THE KIT
Now we have to download the MULT.HEX file, which is in Intel Hex format to the 8085 kit. A driver
program that is used for communication between a PC and the ALS-SDA-85 kit using RS-232C inter¬
face does this. In the discussion that follows, we use PCLINK as the driver software for communica¬ Srn
tion. The kit is assumed to be ALS-SDA-85M. PCLINK driver program can be used with any 8085 0
kit that has RS-232C interface. x Z
5. °
First of all connect the kit to COM2 serial port of the PC using RS-232C cable. This is because «
generally in present-day computers, COM1 port is used for mouse, and is not free as such. However, 5'
if COM1 port is free, it can be used to connect to the kit. In the following discussion, it is assumed
that COM2 port is used for connecting the kit.
»
§:
-
Invoke the driver program by typing ‘PCLINK<cr>’. It is a menu driven program. The PCLINK
u 2}
program first displays a welcome message, and then displays the main menu as shown in the g a.
is
following.
z
i
MAIN MENU
1 Terminal mode
2 Disk catalog
3 File download
4 File upload
5 Configuration
6 Exit program
Enter your choice [1-6]?
First of all, select configuration choice by typing 5. Then the configuration menu appears on the
screen. It indicates the current status, and allows you to change the current status by displaying the
menu as follows.
CONFIGURATION MENU
1 Baud rate
2 Word length
3 Stop bits
4 Parity
5 Serial port
6 Exit Configuration
Enter your choice [1-6]?
Select serial port choice by typing 5. Then the display indicates the current status, and allows you
to select COM1 or COM2 by displaying the menu as follows.
SERIAL PORT
1 COM1
2 COM2
Enter your choice [1-2]?
Select COM2 by typing 2. Then the display indicates the current status. The current status of
COM2 will be generally as follows.
200
Baud rate : 9600 Word length : 8 bits Parity : none
Stop bits : 1 Port : COM2
The menu allows you to change the current status by displaying the menu as follows.
To change, select from the following:
s 1 Baud rate
a>
o 2 Word length
sa 3 Stop bits
2 4 Parity
.2
s 5 Serial port
in
oo 6 Exit Configuration
s
Jj Enter your choice [1-6]?
H
The current status with baud rate of 9,600, word length of 8 bits, no parity bit, and l-stop bit is
quite satisfactory. So we select Exit configuration choice by typing 6. Then once again the Main menu
as shown in the following is displayed.
MAIN MENU
1 Terminal mode
2 Disk catalog
3 File download
4 File upload
5 Configuration
6 Exit program
When COM2 is programmed for 9,600 baud, it is necessary that the kit is also programmed for 9,600
baud. Loading location FFA6H on the kit with 0AH and location FFA7H with 00H does this. As per
the kit manual, this sets the baud rate to 9,600. Then the ‘Reset’ button on the kit is pressed. Finally,
the keys ‘E’ and ‘0’ on the kit are pressed, which transfers the control to the PC. The keyboard on the
kit will now be disabled (except the ‘Reset’ and ‘Vect Intr’ key). The ‘>’ prompt will now appear on
the PC screen. Now we are ready to use the ‘ALS-SDA-85 serial monitor version 2.00’ commands.
For setting up 8085 kits from other manufacturers, you have to refer to relevant kit manuals.
To know the various commands available in the ALS serial monitor, type ‘H<cr>’ when the ‘>’
prompt appears on the screen. Remember to type ‘H’ and not ‘h’. It accepts commands typed in cap¬
itals only. Now the following menu will be displayed on the PC screen.
201
** ALS-SDA-85 Monitor Ver 2.00 **
* Summary of Monitor Commands * Memory Commands * Utility Commands
Now that the program has been downloaded to the kit from the PC, we can run the program in the
keyboard mode, which is quite familiar by now. Just press the ‘Reset’ key on the kit. The ‘-Sda 85’
prompt appears on the seven-segment display of the kit. Now we can run the program by typing ‘Go’
key followed by the starting address of the program. The result is displayed in the address or data field
of the display, if we have used UPDAD or UPDDT utilities in the program. Otherwise, we can use
‘subst mem’ key followed by memory address to check the results stored in memory locations.
Now that the program has been downloaded to the kit from the PC, we can run the program on the kit
by issuing commands to the kit in serial mode, using the PC as a terminal. The result is also transmitted
from the kit to the PC for display on the terminal. In this mode, the six-digit display on the kit will be
202
blank. The program can be run in a single burst without any pause after every instruction execution.
Or, we can step through the program one instruction at a time for debugging purposes.
This mode is useful when we find that our program has not yielded desired results after executing in
a single burst. In other words, it is used for debugging the program.
At the ‘>’ prompt, type ‘S’. Notice the absence of <cr> after S. The system prompts as follows:
Starting address: xxxx - yy/
where xxxx is a memory address and yy is the content of that memory location. It allows the user to
respond with the desired starting address for single-stepping. If our program is to be executed from
location xxxx, we just respond with <sb>. Here <sb> stands for pressing the SPACE BAR key, which
is the widest on the keyboard. If our program is from location C000H, we respond with ‘C000<sb>’.
Then the system displays ‘C000-21/’, where 21 is the content of memory location C000H. Now we
respond with <sb>. Then the instruction at C000H is executed, and it displays the address of the next
instruction along with the contents of that location. Only when we respond with <sb> the instruction
is executed, and the address of the next instruction and the contents of that location are displayed. If
we desire to stop the single-stepping, and check the contents of various registers and memory loca¬
tions, we respond with <cr> instead of <sb>. Then the ‘>’ prompt reappears.
203
Examine registers command: To check register values, type ‘X’ without <cr>. The system prompts
as follows:
Register :
The user is required to respond with the desired register name. If you want to see contents of the
C register, type C without <cr>. Immediately, it responds with Sr0
C=xx- m
0
where xx is the content of the C register, and it allows the user to enter a new value in place of xx. For § 3
example, if we want C register to have 36H, we respond with 36 and <cr> or <sb>. If the user does 5. 5
not want to change the contents of the register, he has to respond with <cr> or <sb>. If it is <cr>, the
X command will be terminated, and the “>’ prompt reappears. If it is <sb>, the system displays » -
I*
content of D register automatically, and allows the user to optionally enter a new value into D the reg¬
ister. Using <sb> repeatedly, we can see the contents, and if desired modify the contents, of all regis¬
ters. If we start with Register A, the sequence of registers will be as follows: n
IS
z
A, B, C, D, E, F, I, H, L, SPH, SPL, PCH, and PCL i
where F is the Flags register, SPFI and SPL are the MS and LS bytes of SP, PCFI and PCL are the MS
and LS bytes of PC. The 8 bits of register I provide the interrupt mask status as shown in the following.
Display memory command: To display memory contents, type ‘D’ without <cr>. The system
prompts as follows:
Starting address :
The user is required to respond with the desired starting address for memory display. The user is
required to provide the memory address in hexadecimal without the H suffix and <cr> (e.g.
‘C200<cr>’).
Then the system prompts the user for ending address. When the user provides the ending address,
the contents of the desired memory locations are displayed on the terminal, and the ‘>’ prompt reap¬
pears. Using this command we can only see the contents of a number of memory locations, but not
alter them. To modify the contents of memory locations we have to use the ‘modify memory
command’.
Continuing with single step after checking registers/memory: Let us say, we are satisfied with the
register contents and memory contents after single-stepping through the program by a few instruc¬
tions. Then we want to continue with the remaining program in single-step mode.
204
At the “>’ prompt, type ‘S’ without <cr>. The system prompts as follows:
Starting address: xxxx - yy/
where xxxx is a memory address and yy is the content of that memory location. Here xxxx is the cor¬
rect instruction address where the single-stepping has to continue. So keep responding with <sb> as
many times as desired to single step through the instructions. To terminate single-stepping respond
8w with <cr>, and the “>’ prompt reappears. Once again, the register contents and memory contents can
0 be checked, and the single-stepping may be continued till the end of the program.
2
!
s
in
oo
§
0
H
1 . List the steps needed for executing an 8085 assembly language program.
2. What are assembler directives? Explain with examples the following assembler directives.
a. ORG
b. DB
c. DW
d. EQU
e. END
In Chap. 14, a number of simple assembly language programs were discussed. Those programs were
manually translated by the user, were loaded using the keyboard, and finally executed using the
commands issued by the keyboard of the kit. Further, in the previous chapter, the use of assembler,
and that of PC in executing a program in serial mode were discussed.
From now on, the user is encouraged to use the PC to enter his program, do the translation, download
the program to the kit, and run the program using commands issued by the PC in serial mode. In this
chapter we deal with the simple 8085 assembly language programs mentioned previously in the list,
along with their flowcharts.
Write an 8085 assembly language program to search for a given byte in an array of bytes using linear
search algorithm. Location X contains the size of the array and location X+ 1 contains the element to
be searched. The elements of the array are stored from location Y onwards. The program should dis¬
play in the address field, the search element and the position where it was found. If the search element
is not found, the position should be indicated as 00.
Flowchart for solving the problem is shown in Fig. 16.1.
c
C100H=X 04H *1 I
X+l 33H *1 I
A
>
a
C200H=Y 55H a
HL 44H r ©
» 3
22H £c SL
66H >
$
Q)
Start
71<
(fi
0)
3(/>
Load C with no. of
elements in array
Load A with search
element
INRB
CMPM
Y
No. found?
N
INXH
DCRC
N
(C) = 0?
Y
Reload B with 00
MOV H, A
MOV L, B
Display HL value
in address field
Fig. 16.1
Flowchart for
HLT
linear search
208
ORG C200H
Y: DB 55H,44H,22H,66H
ORG C000H
CURAD: EQU FFF7H
UPDAD: EQU 06BCH
0
M LXI H,X
M
O MOV C,M ;;Load C with the number of elements in the array.
u
o
a INX H
o
MOV A,M ;;Load A with the element to be searched.
S MVI B,00H;Initialise B with 0.
in ;It finally indicates the position where the element was found.
00
o
00
LXI H,Y /Point HL to the beginning of the array.
x
REP: INR B /Increment B. B indicates element number being checked.
CMP M /Compare A and memory pointed by HL.
JZ EXIT /If they are same, jump to EXIT.
INX H
DCR C //Else, increment HL and decrement C.
JNZ REP/ If C value is nonzero, jump to REP.
/If we come out of this loop because C is 00,
/it means search is unsuccesful. Hence make B as 00.
MVI B,00H /Load B with 00H.
/At this point B will have the position where the element
/was found. If element was not found, B will be 00H.
EXIT: MOV H,A /Load H with the search element.
MOV L,B /Load L with the position the element was found.
SHLD CURAD
CALL UPDAD //Display the result in the address field.
HLT /Make sure to end with HLT for keyboard mode.
/For serial mode RST 1 is preferred.
Write an 8085 assembly language program to find the smallest of N 1-byte numbers. The N value is
provided at location X, and the numbers are present from location X + 1. Display the smallest number
in the data field, and its location in the address field.
Flowchart for solving the problem is shown in Fig. 16.2.
C100H=X 04H c
23H
45H
12H
36H >
a
a
r ©
» 3
Start
£c SL
Q) >
$
Load C with
number of elements
71<
(fi
Decrement C 0)
3
</>
N
(A) smaller?
y
<r MOV A, M
SHLD CURAD
DCRC
N
(C)=0?
Display result:
STA CURDT
CALL UP DDT
CALLUPDAD
Fig. 16.2
HLT Flowchart to find
the smallest number
Write an 8085 assembly language program to find the HCF of two 8-bit numbers. The numbers are
stored at locations X and Y. Display the numbers in the address field, and their HCF in the data field.
Flowchart for solving the problem is shown in Fig. 16.3.
ORG C100H
X: DB 0FH
ORG C200H
Y: DB 19H
ORG C000H
CURDT: EQU FFF9H
UPDDT: EQU 06D3H
211
C100H=X OFH H
>
a
a
r ©
Start § 3
c >
Move no. at X to H I 38
0)
Move no. at Y to
L and A.
7
0 E
(fi <
SHLD CURAD 0)
<- 3</>
CMP H
Y
(A) =(H)?
N
Display result:
STA CURDT
N
CALL UPDDT (A) >(H)
CALL UPDAD
Y (A) and (H)
J
HLT
Fig. 16.3
(A) -(A)-(H)
Flowchart to
find the HCF
0
M
M
O
0
2
a ■ 1 6.4 CHECK FOR '2 OUT OF 5' CODE
o
S
in Write an 8085 assembly language program to check if the byte at location X belongs to ‘2 out of 5’
oo
o code or not. It belongs to ‘2 out of 5’ code, if the MS 3 bits are 000, and there are two Is in the LS 5
00
bits. Display in the address field, the number and FF in the case of valid code, or the number and 00
x
in the case of invalid code.
Flowchart for solving the problem is shown in Fig. 16.4.
/8085 ALP TO CHECK IF THE BYTE AT X BELONGS TO '2 OUT OF 5' CODE OR NOT.
/ ( IT IS VALID CODE IF THE MS 3 BITS ARE 000, AND THERE ARE TWO 1' S IN
/THE LS 5 BITS )
ORG C100H
X: DB 14H
ORG C000H
CURAD: EQU FFF7H
UPDAD: EQU 06BCH
LDA X
MOV H,A //Load A and H with the number to be tested.
MVI L, 00H /Load L with 00. It will be changed later to FF
/if the number belongs to '2 out of 5' code.
ANI 11100000B / Reset to 0 the LS 5 bits of A,
/with MS 3 bits unchanged.
JNZ ZERO /If MS 3 bits are not 000 jump to ZERO to indicate
/that the number does not belong to '2 out of 5' code
MOV A, H /Load A with the number to be tested, present in H.
MVI C, 05H /Load C with 5, the number of bits to test.
MVI D, 00H /Initialise D with 00. It will finally have
/the number of 1' s in the LS 5 bits.
LOOP: RRC /Rotate right Accumulator A.
JNC NOINCD /If there is no Carry, jump to NOINCD,
213
>
a
Start a
r ©
§ 3
Load H and A with C w
the number
Load L with 00H
{s
70 3E
Reset to 0, LS 5 bits
(fi <
0)
of Acer. 3(/>
N
(A) =00?
-
Y
Display result:
SHLD CURAD Load A with the no.
CALL UPDAD Load C with 05
Load D with 00
HLT
RRC
Y
Cy =0?
INRD
■»
DCRC
(C) =0? N
N
f (D) =2?
Y
Fig. 16.4
MVIL, FF Flowchart to check
for two out of five code
Write an 8085 assembly language program to convert an ASCII hex character to the equivalent binary.
ASCII hex number is at location X. Display the ASCII hex number and its binary equivalent in the
address field.
Flowchart for solving the problem is shown in Fig. 16.5.
/ASCII code for character '0' is 30H, for '1' it is 31H, etc
/and for '9' it is 39H. The ASCII code for 'A' is 41H, for 'B'
/it is 42H, etc and for character 'F' it is 46H.
/For example, 42H 37H = 0BH, where 42H is ASCII code for 'B' .
ORG C100H
X: DB 43H
ORG C000H
CURAD: EQU FFF7H
UPDAD: EQU 06BCH
215
>
a
a
r» 5o
Start £C s.
£>
{S
• 9
Load A and H with 7 3
ASCII value 0 Z
(c <
0)
SUI 30H
3</>
Y
(A) <0AH
N
SUI 07
MOV L, A
Display result:
SHLD CURAD
CALL UPDAD
Fig. 16.5
HLT Flowchart to convert
ASCII hex to binary
LDA X
MOV H, A ;;Load A and H with contents of Location X.
NUMB: MOV L, A
SHLD CURAD
CALL UPDAD/ // Display ASCII code and its hexadecimal
/equivalent in the address field.
HLT
216
■ 1 6.6 CONVERT BINARY TO ASCII
Write an 8085 assembly language program to convert a two-digit hexadecimal number to two equiv¬
alent ASCII codes. The two-digit hexadecimal number is at location X. Display the hexadecimal
oM number in the data field and its equivalent ASCII code (2 bytes) in the address field.
M
O Flowchart for solving the problem is shown in Fig. 16.6.
0
o
a
o
C100H=X 2BH
2 Result display:
w 131214121 EH
oo
o Address field Data field
00
0)
£
H
Hex-Asc Start
I
Display result:
SHLD CURAD
CALLUPDAD
I
HLT
ORG C100H
r» 5o
X: DB 2BH £C s.w
ORG C000H {s
• 9
CURDT: EQU FFF9H 7 3
0 Z
UPDDT: EQU 06D3H (c <
CURAD: EQU FFF7H 0)
Write an 8085 assembly language program to convert a two-digit BCD number to binary. The two-
digit BCD number is at location X. Display the BCD number and its binary equivalent in the address
o
M
field.
M
U Generally, there is more than one way of solving a given problem. To illustrate this idea, two meth¬
O ods of converting a BCD number to binary are described in this section. A flowchart for the first
a method of solving the problem is shown in Fig. 16.7a.
«
S
in
00
a
o C100H=X 95 A, as well as H
Result display:
M5I5IFI
Start
Y
(A) = 00?
N
MVIA, 00
Increment
A in decimal
L in binary
(A) = (H)?>2L
Y
Display result:
SHLD CURAD
CALL UPDAD Fig. 16.7a
I Flowchart to convert a two-digit
BCD to binary — Method 1
HLT
219
16.7.1 PROGRAM TO CONVERT A TWO-DIGIT BCD TO BINARY— METHOD 1
C100H=X 95 A, as well as H
Result display:
0
(A
W
1 9|5|5 1 Fl
0)
o
0
a
o
Start
.2
2
w
oo Move the BCD number
O
00 to A and H
0)
£
H
Make MS digit of
A as 0
MOV B, A
Move to C the MS
digit of BCD no.
MVI A, 00H
-
ADI 0AH
DCRC
N
(C) = 0?
ADD B
MOV L, A
Display result:
SHLD CURAD
CALL UPDAD Fig. 16.7b
Flowchart to convert a two-digit
BCD to Binary — Method 2
HLT
Write an 8085 assembly language program to convert an 8-bit binary number to equivalent BCD
number. The binary number is at location X. Display the binary number in the data field, and its equiv¬
alent BCD number in the address field.
222
There are various methods of solving this problem. Three of them are described in this text. A flow¬
chart for the first method of solving the problem is shown in Fig. 16.8a.
o
M C100H=X 1DH
M
O
U Result display:
o
a 101012191 mDl
o
S
in
CO Start
o
CO
X Load C with the
8-bit no.
Store it in CURDT
MVI H, 00
MVI A, 00
Increment A in decimal
Y
(Cy) = 0?
N
INRH
Reset carry
-
DCRC
N
(C) = 0?
Display result:
MOV L, A
SHLD CURAD
CALLUPDAD
CALLUPDDT
Fig. 16.8a
Flowchart to convert an 8-bit
HLT
binary to BCD — Method 1
223
16.8.1 PROGRAM TO CONVERT AN 8-BIT BINARY TO BCD— METHOD 1
MOV L, A
SHLD CURAD
CALL UPDAD/ // Display equivalent BCD in address field.
CALL UPDDT /Display Hexadecimal number in data field.
HLT
224
Flowchart for the second method of solving the problem is shown in Fig. 16.8b.
Result display:
0
i oi o 12 1 9i ms
w
w
0)
o
0
o. Start
o
o Load A and B with the
8-bit no.
w Store in CURDT
oo
o I
00
0) Convert LS digit
£ of A to BCD and
H
save in C
I
Save the MS digit of
no. in D
1
MVI H, 00
MVI A, 00
Add 16 decimal to A
N
Cy = 1?
1NRH
Clear carry
-
DCRD
(D) = 00?ÿ>ÿ
Y
ADD C
DAA
MOV L, A
(cy)=l? N
Y
INRH
*
Display result:
SHLD CURAD
CALLUPDAD
Fig. 16.8b
CALL UPDDT
Flowchart to convert
an 8-bit binary to
HLT
BCD — Method 2
225
1 6.8.2 PROGRAM TO CONVERT AN 8-BIT BINARY TO BCD — METHOD 2
C100H=X FFH A
Result display:
1 012 1 5 1 51
[FlFl
>
a
Q.
Start r ©
§ 3
C w
Load A with the no.
Store in CURDT {s
• 0
70 3E
MVI H, 00 (fi <
MVI L, 00 0)
3
</>
Y
(A) < 64H
N
Y
(A) < OAH
INRH
SUI 64H
N
MOV B, A I
INRL
SUI OAH
MOV A, L
I
Rotate left A
4 times
ADD B
MOV L, A
Display result:
SHLD CURAD
CALLUPDAD
CALLUPDDT
I
HLT
LDA X
STA CURDT ;;Load A from location X. Store in CURDT.
MVI H, 00H /Initialise H with 00. H will finally have the
/number of 100' s in the BCD result.
MVI L, 00H /Initialise L with 00. L will finally have the
/number of 10' s in the BCD result.
228
;The next 5 instructions store in H the number of 100' s.
REPl: CPI 64H /Compare A with 64H i.e. 100 decimal.
JC REP2 ;If < 100, jump to REP2 to find number of 10' s.
INR H ;If >= 100, increment H.
SUI 64H /Decrement A by 100 decimal.
0
w JMP REPl /Jump to REPl.
w
0)
o /When we are out of this loop, H will have number of 100' s
o
a /and A contents would have become less than 100.
o
.2 /The next 5 instructions store in L the number of 10' s.
2
w
oo
REP2: CPI 0AH /Compare A with 0AH i.e. 10 decimal.
o JC EXIT /If <10, jump to EXIT.
00
0)
£ INR L /If >= 10, increment L.
H SUI 0AH /Decrement A by 10 decimal.
JMP REP2 / Jump to REP2.
/When we are out of this loop, L will have number of 10' s
/and A contents would have become less than 10.
EXIT: MOV B, A /Save in B the units value of BCD result.
MOV A, L /Load A with number of 10' s in BCD result.
RLC
RLC
RLC
RLC ////Move this information to MS digit of A,
/with LS digit of A as 0.
ADD B /This results in A having number of 10' s in MS
/digit, and units value of result in LS digit.
MOV L, A /Save A value in L.
/Thus the total BCD result is now in HL.
SHLD CURAD
CALL UPDAD/ / Display BCD equivalent in address field.
CALL UPDDT /Display Hexadecimal value in data field.
HLT
Write an 8085 assembly language program to check if the 8-bit number at location X is a palindrome
or not. Display the number and the result in the address field. If it is a palindrome the result should be
FF, else 00.
229
Flowchart for the method of solving the problem is shown in Fig. 16.9.
Cy
Start
•s
|
7 3 *
0 Z
Load A and H with Ifi <
the no. 0)
3«
MVI C, 08
I
Rotate left H register,
moving bit at MS
position to carry
I
DCRC
(C) = 0? N
Y
(H) = (D)?
MVI L, FF N
MVI L, 00
Display result:
SHLD CURAD
CALLUPDAD
I Fig. 16.9
HLT Flowchart to check
for palindrome
230
1 6.9.1 PROGRAM TO CHECK FOR PALINDROME
DCR C /Decrement C.
JNZ AGAIN /If not zero, jump to AGAIN.
/By the time we come out of the loop, D register will have
/the reverse of the byte in location X. And because of 8
/rotations, H register will have the original number.
MOV A, H
CMP D //Compare the number and its reverse.
JZ PALIN /If they are same, jump to PALIN.
MVI L, 00H /Load L with 00, if the byte is not a palindrome.
JMP EXIT /Jump to EXIT to display result.
PALIN: MVI L,FFH /Load L with FF, if the byte is not a palindrome.
EXIT: SHLD CURAD
CALL UPDAD/ / Display result in the address field.
HLT
Write an 8085 assembly language program to compute the LCM of two 8-bit numbers stored in loca¬
tions X and Y. Display the result in the address field.
231
Flowchart for the method of solving the problem is shown in Fig. 16.10.
C 100H=X 19H c
£§ 2.
3
C w
Start
{s
• 9
Load C from locn. X 7 3
0 E
Load B with 00. (fi <
So BC contains the s
3
number at X (/>
LXI H, 0000H
-
DAD B
SHLD CURAD
<ÿ
DADD
Y
(Cy) = 0?
LHLD CURAD N
(HL) = 0? N
Y
Display result:
CALLUPDAD
Fig. 16.10
Flowchart to compute
HLT
the LCM of two bytes
LDA Y
CMA
MOV E, A ///Load E with 1' s complement of the number at Y.
MVI D, FFH /Now DE contains 1' s complement of number at Y.
INX D /DE contains 2' s complement of the number at Y.
ORG C100H
X: DB 04H,33H,22H,44H,11H
ORG C000H
LXI H, X
MOV C, M //Load C from memory location X.
DCR C /Decrement C. C now has the number
/of passes still to be performed.
OUTLOOP:
MVI E, 01H /Load E with 1. E contains one more than the
/number of exchanges made in a pass so far.
MOV B, C /Load B from C contents. B now has the number
/of comparisons still to be performed in a pass.
INX H /Point HL to the first element of the array.
INLOOP:
MOV A, M /Load A from memory pointed by HL.
INX H /Point HL to next element of array.
CMP M /Compare A (previous element) and
/next element of the array.
2 Point HL to X Start
w Load C with one less than the
oo no. of elements. C has no. of
o
00 passes to be performed
0) Load C with no. of elements-1
£
H 4*
MVI E, 01
Compare consecutive elements
MOV B, C
of unsorted array. Exchange
INXH
them if they are not in order.
4- Now the last element of
unsorted array is in proper
MOV A, M position
INXH
CMPM
DCRC
N
(Cy) = 0?
N
(C) = 0?
Y
Exchange contents of Y
memory pointed by HL
with its preceding Stop
element. Increment E
4
DCRB
N
(B) = 0?
Point HL to X DCRE
DCRC
Y
(E) = 0
N
(C) = 0
N
YL
HLT
Write an 8085 assembly language program to sort N 1-byte binary numbers in ascending order, using
selection sort. N is stored at location X, and the numbers from location X+l.
Flowchart for the method of solving the problem is shown in Fig. 16.12.
ORG C000H
X: EQU C100H
/ In this program, we have not indicated data values. The user is
/required to provide them, using the kit in keyboard mode, or
/using the PC in serial mode.
2 i
w MOV B, C
00
o INX II
00
0)
Method MOV A, M
£ MOV E, L
H
Start -
INX H
C is loaded with no. of CMPM
elements-1
Y
(Cy) = 0?
Find the largest no. in the
unsorted array N
MOV A, M
MOV E, L
Exchange it with last element
of unsorted array. ■>
Now the last element of unsorted
DCRB
array is in proper position
N
DCRC (B) = 0?
Y
N Exchange the largest
(C) = 0?
no. with the last
Y element of the array.
Point HL to X. DCR C
Stop
N
(C) = 0
HLT
OUTLOOP:
MOV B, C /Load B from C. B indicates the number of a
/comparisons still to be made in a pass. S:
INX H /Point HL to the first element of the array. r 3S.
o
MOV A, M /Load A with the first element of the array. By the £"S * ]>
/end of the pass, A will have the largest element.
MOV E, L /Load E from L. E indicates the position in • 3*s
|
/the array of the element in A. 7Z
0
Ifi <
INLOOP:INX H /Point HL to next element of array. a
CMP M /Compare A (previous element) and next element.
3«
JNC SKIP /If (A >= next element) jump to SKIP.
MOV A, M
MOV E, L //If (A < next element), load A with next element,
/and E with position of next element.
Write an 8085 assembly language program to perform as a decimal up counter (from 00 to 99). The
count should be incremented every half second, and is to be displayed in the data field.
Flowchart for the method of solving the problem is shown in Fig. 16.13.
238
Delay Start
<r
0
(A
DCXB Display the value
w
0) in A in data field
o
0
a N
0 (BC) = 0? Delay Generate delay
o subroutine for 0.5 second
Y
w
oo
o RET Increment A in Fig. 16.13
00
0) decimal Flowchart to simulate
£
H decimal up counter
ORG C000H
CURDT: EQU FFF9H
UPDDT: EQU 06D3H
MVI A, 00H /Initialise A with 00.
REP: STA CURDT /Store A value in CURDT.
CALL UPDDT /Display in the data field contents of CURDT.
CALL DELAY /Generate a delay of 0.5 seconds
LDA CURDT /Reload A from location CURDT.
ADI 01H
DAA //Increment A in decimal notation.
JMP REP / Jump to REP.
Subroutine to generate a delay of 0.5 second. Its
working is explained at the end of this program.
DELAY: LXI B, FFFFH /Load BC with FFFFH.
AGAIN: DCX B /Decrement BC. It does not affect flags.
MOV A, B
ORA C /So, perform OR of B and C registers.
JNZ AGAIN /If not zero, jump to AGAIN.
RET /Return, after generating delay of 0.5 second.
Note: Replace ‘JMP REP’ with the following three instructions, if it is required to stop after counting
upto 99.
CPI 00H
JNZ REP
HLT
239
1 6.1 3.2 GENERATION OF TIME DELAY
There are times when we are required to generate time delays. In this example, we want the count
value to change once every half second. Later in the chapter, we simulate a real time clock, where the
time display should change once every second. Such time delays can be generated by a microproces¬
sor. The principle behind time delay generation by the microprocessor is as follows. Execution of a
every instruction needs some clock cycles. Thus, to generate the required time delay, a microproces- sj
sor will have to execute the required number of instructions. This is the principle of time delay u §
generation by the microprocessor. -
NOP instructions for time delay: The immediate reaction will be to use NOP instructions to gener¬
?3
&
s!
ate the required time delay. Each NOP instruction uses four clocks for fetching, decoding, and execut-
ing. In the discussion that follows, it is assumed that 8085 is working with a crystal frequency of jj
J
6 MHz, and as such with an internal frequency of 3 MHz. Thus each clock period is one-third of a 3
microsecond. In such a case, a NOP instruction needs only 1.33 |xs for execution. Even if we use 64K
NOP instructions, which is the maximum possible in memory, the time delay would only be
64KX 1.33 |xs = about 86 jxs. Also, the program size has become too much, for too little work done.
Thus, we use a series of NOP instructions only when our interest is to generate small time delays of
a few microseconds.
Use of an 8-bit register as counter in a loop: A much better way to generate a larger time delay
with a smaller program size, is to execute a series of instructions repetitively in a loop. An example is
shown below.
MVI C, FFH ;Uses 7 T states.
REP: DCR C ;Uses 4 T states.
JNZ REP ;Uses 10 T states in the case of jump
;and 7 T states in the case of no jump.
RET ;Uses 10 T states.
In this example, C is loaded with maximum possible value of FFH. Then, C is decremented. If the
result is non-zero, the decrement operation is repeated. The program is quite short, and generates a
delay of 1.191 |xs, as explained below.
By the time we come out of the loop, the number of T states used up will be
Use of a register pair as counter in a loop: Instead of using an 8-bit register, we can use a regis¬
ter pair as counter in a loop, to generate much larger time delays using smaller program size. This is
8 the method used in the above program to generate a half-second delay. The calculation for the delay
a> is shown in the following.
o
s
a
DELAY:LXI B,FFFFH /Uses 10 T states
s AGAIN:DCX B /Uses 6 T states
.2 MOV A, B /Uses 4 T states
S ORA C /Uses 4 T states
in
00 JNZ AGAIN /Uses 10 T states for jump case
s /and 7 T states for no jump case
Jj RET /Uses 10 T states
H
Number of T states, N = 10 + (6 + 4 + 4 + 10) * FFFFH -3 + 10
= 17 + 24 * FFFFH = 1572857
Thus, time delay = 1572857 * 1/3 p.s = 0.52428 s.
This delay is generally taken as about a half-second delay. The user is left with the exercise to com¬
pute the count value to be loaded in the register pair for exact 0.5-s delay.
However, if we use the microprocessor to generate large time delays, the microprocessor will not
be available for any other operation for the entire delay time. So it is advisable to restrict the time
delay generation by the microprocessor to only a few micro- or milliseconds in practical applications.
For large time delays, it is better to use dedicated timer chips like Intel 8253, which is discussed in a
later chapter.
Write an 8085 assembly language program to perform as a decimal down counter (from 99 to 00). The
count should be decremented every half second, and the count is to be displayed in the data field.
Flowchart for the method of solving the problem is shown in Fig. 16.14.
Delay Start
►
Display A value a
DCXB S:
in data field
r o
£"S *
3
N
(BC) = 0? Delay Generate delay ]>
Y
subroutine of 0.5 s
•s
|
7 *3
RET 0 Z
Decrement A in Fig. 16.14 <a <
decimal Flowchart to simulate U
3«
decimal down counter
Write an 8085 assembly language program to alternately display 00 and FF in the data field, with a
delay of N seconds, where N is less than or equal to 2.
242
Flowchart for the method of solving the problem is shown in Fig. 16.15.
Start
k-
MVI A, 00
0
w
w DISP
0)
o Display A contents
0 DISP
o. in data field for subroutine
0 STA CURDT 1 second
.2 CALL UPDDT
2
w MVI A, FF
00
O Generate delay DELAY
00
0)
of 1 second subroutine
£ Display A contents
H DISP
in data field for subroutine
RET 1 second
DELAY
MVI D, 02
LXI B, FFFF
I
DCXB
(BC) = 0?>ÿ-
DCRD
N
(D) = 0?
RET
Write an 8085 assembly language program to simulate a real time clock, which displays hours and
minutes in the address field, and seconds in the data field, using 24-hour format.
Flowchart for the method of solving the problem is shown in Fig. 16.16.
ORG C000H
CURDT: EQU FFF9H
UPDDT: EQU 06D3H
CURAD: EQU FFF7H
UPDAD: EQU 06BCH
BEGIN: LXI H,0000H /Initialise HL with 0000. H and L indicate
244
Start
Result display:
k-
LXI H, 0000
Hours Minutes Seconds
■>
0 H L A
w reg. reg. reg. SHLD CURAD
0)
o MVI A, 00H
0
a
o
o Display
HL in address field
w A in data field
oo
o DELAY
00
0)
£ DELAY Generate delay of
H MVI C, 02 subroutine 1 second
DCXD
N
(A) = 60?
N
(DE) = 0? Y
Y Increment L in
decimal
DCRC
N N
(C) = 0? 4 (L) = 60?
Y Y
RET Increment H in
decimal
N
(H) = 24?
• 3*s
|
/which is present in L.
LHLD CURAD /Reload HL from CURAD. 7Z
0
MOV A, L Ifi <
u
ADI 01H 3«
DAA
MOV L, A ////Increment L in decimal notation.
CPI 60H
JNZ HR_MIN //If L value is not equal to 60, jump to HR_MIN.
/A will be made 00, after jumping to HR_MIN.
CPI 24H
JNZ HR_MIN //If H value is not equal to 24, jump to HR_MIN.
JMP BEGIN /If H value is 24, jump to BEGIN.
Note : To test the program fully with a 1-s delay, we need 24 hours! To solve this problem, we can test
the working of the seconds display, with seconds display getting updated every second. Then we
change the delay such that the minutes field gets updated every second. Thus, the minutes display can
be tested in just 60 more seconds. Then we change the delay such that the hours field gets updated
every second. Thus, the hours display can be tested in just 24 more seconds.
246
To change the display in the minutes field approximately once every second, load DE pair with
0400H in this subroutine. To change the display in the hours field approximately once every second,
load DE pair with 001OH in this subroutine.
o
«
E
a
E
.2
2 1. Write an 8085 assembly language program to find the largest and smallest numbers in an
in
array of unsigned numbers. Display them in the address field.
2. Write an 8085 assembly language program to convert a two-digit octal number to BCD.
j; Display the result in the address field.
3. Write an 8085 assembly language program to convert a two-digit BCD number to octal.
Display the BCD number in the data field, and the octal number in the address field.
4. Write an 8085 assembly language program to work as up counter to count in octal from 00
to 77 octal, the display in the data field changing once every second.
5. Write an 8085 assembly language program to generate a delay of:
a. Exactly 0.5 second;
b. Exactly 1.0 second;
assuming that the internal frequency of operation is 3 MHz.
6. Write an 8085 assembly language program to convert ASCII hex value to binary, using look
up table approach. Display ASCII value and equivalent binary in the address field.
7. Write an 8085 assembly language program to convert a two-digit hex value to two equiva¬
lent ASCII codes, using look up table approach. Display the hex value in data field and
equivalent ASCII value in the address field.
8. Write an 8085 assembly language program to find the LCM of two given 8-bit numbers,
using a method other than that described in this chapter. Display the result in the address
field.
9. Write an 8085 assembly language program to check if a given byte is a palindrome or not,
using a method other than that described in this text. Display the result in the address field.
10. Write an 8085 assembly language program to simulate a real-time clock, which displays
hours, minutes, and seconds in 12-hour format. It should display a dot at the end of the data
field if the time is afternoon.
*: .
17
More Complex
MM;r- \
Assembly Language
Programs
■ Subtract multi-byte BCD numbers
•Program for subtraction of multi-byte BCD numbers
■ Convert 1 6-bit binary to BCD
•Program to convert a 1 6-bit binary number to BCD
■ Do an operation on two numbers based on the value of X
•Program to do an operation on two numbers based on the contents of X
■ Do an operation on two BCD numbers based on the value of X
•Program to do an operation on two BCD numbers based on the contents of X
■ Bubble sort in ascending/descending order as per choice
•Program to perform bubble sort in ascending/descending order
•Alternative program to perform bubble sort based on choice
■ Selection sort in ascending/descending order as per choice
•Program to perform selection sort in ascending/descending order
■ Add contents of N word locations
•Program to add the contents of N word locations
■ Multiply two 8-bit numbers (shift and add method)
•Program to multiply two 8-bit numbers (shift and add method)
■ Multiply two 2-digit BCD numbers
•Program to multiply two 2-digit BCD numbers
■ Multiply two 1 6-bit binary numbers
•Program to multiply two 7 6-bit binary numbers
■ Questions
248
So far simple assembly language programs have been discussed. In this chapter, comparatively more
complex programs and their flowcharts are dealt with. These include programs to subtract multi-byte
BCD numbers, to convert 16-bit binary to BCD, bubble sort and selection sort in ascending/descending
order, to multiply two 8-bit numbers, to multiply 2-digit BCD numbers, etc.
o
o ■ 1 7.1 SUBTRACT MULTI-BYTE BCD NUMBERS
u
a
o
S Write an 8085 assembly language program to subtract two multi-byte BCD numbers. The numbers are
in stored from locations X and Y in byte reversal form. The size (in bytes) of the multi-byte numbers is
00
o provided at location SIZE. The number starting at Y is subtracted from that starting at X. The result
00
0 is stored in locations starting from X.
Flowchart for solving the problem is shown in Fig. 17.1.
ORG C100H
SIZE: DB 03H
ORG C200H
X: DB 56H, 78H, 94H
ORG C300H
Y: DB 67H, 23H, 48H
ORG C000H
LXI H, SIZE
MOV C, M / (C) = 3. C register is used as loop counter.
LXI D, X / (DE) = C200. DE points to Minuend.
LXI H, Y / (HL) = C300. HL points to subtrahend.
/The execution of the following 4 instructions load A with 10' s
/complement of the LS byte of subtrahend the first time through the
/loop. In other passes through the loop, it loads A with 9' s
/complement of the higher bytes of subtrahend plus carry generated by
/previous addition.
STC Z (Cy) = 1
AGAIN: MVI A, 99H / (A) = 99 | 99 I 99
ACI 00H / (A) = 9A | 99 | 9A
SUB M / (A) = 33 | 76 I 52
249
C100H=Size 03 c
94 78 56 94 78 56
Same as
48 23 67 » + 51 76 33
the addition
46 54 89 46 54 89
—
DE » C200H=X 54
7/
89
54
0
»4 46 r«
§ §
<s 3
5 o
— % x
HL »C300H=Y 67
23
I>
0 g
48 (fi 2
s3 i
U <
Start
Set carry
4-
DAA
Store result in location
pointed by DE
INXD
INX II
DCRC
(C)=0 N
HLT
Write an 8085 assembly language program to convert a 16-bit binary number to BCD. Display the
binary number in the address field for a second, and then display the BCD value using all the six dig¬
its of display.
Flowchart for solving the problem is shown in Fig. 17.2.
Result display:
C100H=X FFH
X+l
IFIFIFIFI m Initial
FFH
zl
I if
u
Generate delay of 1 sec
using monitor routine
(HL)=0?
Display result:
Display DE in address field
Display B in data field
Fig. 17.2
I Flowchart for converting a
HLT 16-bit binary number to BCD
CALL DELAY
CALL DELAY ;;/Generate a delay of 1 second.
LHLD X ;Load HL with the binary number.
LXI D, 0000H /Initialise DE with 0000.
MVI A, 00H /Initialise A with 00.
AGAIN: ADI 01H
DAA //Increment A in decimal.
MOV B, A /Save A value in B register.
JNC SKIP /If A value is <= 99, jump to SKIP.
MOV A, E
ADI 01H
252
DAA
MOV E, A A = 00 and Cy = 1, increment E in decimal.
JNC SKIP ;If E value is <= 99, jump to SKIP.
MOV A, D
ADI 01H
ow DAA
w MOV D, A ;;;;If E = 00 and Cy = 1, increment D in decimal.
0)
o
0 SKIP DCX H
o.
o MOV A, H
a2 ORA L
MOV A, B
w
oo AGAIN
JNZ ii ii r Decrement HL. If nonzero jump to AGAIN
o
00 ;after loading A from saved value in B.
0)
H ;When we come out of the loop, DE and A will have the BCD
;value, with A having the LS byte.
XCHG
SHLD CURAD ;;Store DE contents in word location CURAD.
STA CURDT
CALL UPDDT ;;Display A contents in data field.
CALL UPDAD ;Display DE contents in address field.
HLT
Write an 8085 assembly language program to perform an operation on two binary numbers at X+l
and X+2, based on the contents of X. The operation to be performed is addition, subtraction, or mul¬
tiplication if the contents of X are 00, 01, or 02, respectively.
Flowchart for solving the problem is shown in Fig. 17.3.
x 00 X 01 X 02
97H 97H 05
88H 88H 04
0
3
mnm m IQIOIOIFI m i o io 1 1 141 m r o§
S 3
Addition Subtraction Multiplication
e 3L
W 0
(fl
Start 0 *X
ADD 7 S’
2
<a8z
Load A with 3 i
contents of X
Get operand 1 in A if
w
Y N
(A)=0? Add operand 2 to A
Perform Y N
ADD (X+l) + (X+2) (A)=l? Store result in L.
Store in H 00 or 01
Perform N depending on carry
(A)=2?
SUB (X+l) -(X+2)
Y Return to
I
DISP
Monitor
Perform
MULT (X+l) * (X+2)
MULT
Display result
DISP
in address field
Load DE with operand 1
I
HLT
Load C with operand 2
SUB
LXI H, 0000H
Get operand 1 in A
DADD
Subtract operand 2 from A DCRC
DISP
Store result in L. N
SHLD CURAD Store in H 00 or FF (C)=0?
CALLUPDAD depending on carry
Y
0
CPI 00H
w JZ ADD ;;If A value is 00, perform Addition.
w
0)
o CPI 01H
0
o. JZ SUB ;;If A value is 01, perform subtraction.
o
o CPI 02H
JZ MULT ;;If A value is 02, perform multiplication.
w
oo
o RST 1 ;If none of the above, return to monitor.
00
0) PROGRAM SEGMENT TO PERFORM ADDITION
£
H
ORG C020H
ADD: INX H
MOV A, M ;;Get the first operand in A.
INX H
ADD M ;;Add the second operand to A.
MOV L, A ;Store result in L.
JNC SKIP
MVI H, 01H;If Cy = 1, load H with 01.
JMP DISP
SKIP: MVI H, 00H ;If Cy = 0, load H with 00.
JMP DISP ;Jump to DISP, to display the result.
; PROGRAM SEGMENT TO PERFORM SUBTRACTION
ORG C040H
SUB: INX H
MOV A, M ;;Load A with first operand.
INX H
SUB M ;/Subtract second operand from A.
MOV L, A /Load L with the result.
JNC SKIPl
MVI H, FFH ;Load H with FF, if Cy = 1.
JMP DISP
SKIPl: MVI H, 00H /Load H with 00, if Cy = 0.
JMP DISP /Jump to DISP, to display the result.
Write an 8085 assembly language program to perform an operation on two BCD numbers at X + 1 and
X+2, based on the contents of X. The operation to be performed is addition, subtraction, or multipli¬
cation if the contents of X are 00, 01, or 02, respectively.
Flowchart for solving the problem is shown in Fig. 17.4.
ADD
X 00 X 01 X 02
97 97 05 Get operand 1 in A
88 88 04
0
I
w Add operand 2 to A.
w
0) Decimal adjust A
o
□
0
a Gumm o I ol 0|9 | 01 0| 21 0 I
0
Addition Subtraction Multiplication Store result in L.
Store in H 00 or 0 1
2 depending on carry
w Start
00
o
00 DISP
0)
£ Load A with
H
contents of X
MULT
Y N
(A) = 0?
Load B with operand 1
Perform BCD Load C with operand 2
ADD addition of (A)=l? N Load H with 00H
(X+l) and (X+2) Load A with 00H
N
Perform BCD (A) = 2?
SUB Y
subtraction of (C)=0?
(X+l) -(X+2) Y Return to
Monitor N
I Perform BCD
MULT multiplication of
(X+l) and (X+2) Add B to A.
Decimal adjust A
Display result
DISP SUB
in address field Carry = l?ÿ>2i
Y
Get operand 1 in B
HLT
Increment H by 1
I in decimal
Store 2’s complement
f
of operand 2 in A
Decrement C in decimal
ADD B
DAA N
DISP (C)=0?
Store result in L. rv
SHLD CURAD Store in H FF or 00
CALL UPDAD
MOV L, A
depending on carry
DISP
HLT DISP
Fig. 17.4 Flowchart to do an operation on two BCD numbers based on the contents of X
257
JZ MULT //If A value is 02H, jump to MULT
RST 1
PROGRAM SEGMENT TO PERFORM BCD ADDITION
ORG C020H
ADD: INX H
MOV A, M ;;Load A with contents of location X+l 0
ft
INX H r«
ADD M ;;Add contents of location X + 2 S 3i
e
DAA /Decimal adjust W 3L
9
MOV L, A /Load L with result in A ft .*
>
75
/The next 5 instructions load H with 01 or 00 depending on whether 0 ft£
aA '1
/Cy = 1 or 0, and jumps to DISP to display result in the address field. si
JNC SKIP
if
u
MVI H, 01H
JMP DISP
SKIP: MVI H,00H
JMP DISP
; PROGRAM SEGMENT TO PERFORM BCD SUBTRACTION
/The method followed here is as follows. Suppose we want to
/perform 85 38. We do it as 85 + 62 = 47 with Cy = 1. In this case,
62 is the 10' s complement of 38. Note that when Cy = 1, result /is
positive.
/As another example, suppose we want to perform 38 85. We
/should get the correct answer as -47. We do it as 38 + 15 = 53 with
/Cy = 0. In this case, 15 is the 10' s complement of 85. Note that
/when Cy = 0, result is negative, and the result will be in 10' s
/complement notation. Here, 53 is 10' s complement of 47.
ORG C040H
SUB: INX H
MOV B, M //Load B with contents of location X+l
/The next 4 instructions result in A having 10' s complement
/of the number at location X+2
MVI A, 99H /Load A with 99
INX H
SUB M /Perform (99 contents of X + 2) to get 9' s complement
ADI 01H /Add 01 to get 10' s complement
ADD B /Equivalent to performing (X+l) (X + 2)
DAA /Perform decimal adjustment
MOV L, A /Load L with result in A
Write an 8085 assembly language program to perform sorting in ascending or descending order, based
on the contents of location CHOICE, using bubble sort technique. If the contents of CHOICE = 00,
sorting should be in ascending order, else in descending order. o
Flowchart for solving the problem is shown in Fig. 17.5a. 3
r«
S i3
C E.
W ft
1 7.5.1 PROGRAM TO PERFORM BUBBLE SORT IN ASCENDING/DESCENDING ORDER *
5«
;FILE NAME C:\ALS\BUBSRT2.ASM ≤8
3|
;8085 ALP TO SORT N ONE BYTE BINARY NUMBERS IN ASCENDING / DESCENDING ORDER 3
a
ft <
;USING BUBBLE SORT. N IS AT LOCATION X, AND THE NUMBERS FROM LOCATION X+l.
;IF CONTENTS OF LOCATION CHOICE = 00, IT IS SORTED IN ASCENDING ORDER,
;ELSE IN DESCENDING ORDER.
ORG C100H
X: DB 04H, 33H, 22H, 44H, 11H
ORG C200H
CHOICE:DB 00H
ORG C000H
;The next 8 instructions store in location MODIFY the opcode
;for JNC if conents of CHOICE = 00. If contents of CHOICE is <> 00,
;it stores in MODIFY the opcode for JC.
LDA CHOICE ;Load A from location CHOICE.
CPI 00
JZ ASCEND ;;If CHOICE = 00, jump to ASCEND.
MVI A, D2H ;Load A with D2, the opcode for JNC.
STA MODIFY ;If CHOICE <> 00, store code for JNC in MODIFY.
JMP BEGIN ;Jump to BEGIN, to sort in descending order.
ASCEND:MVI A, DAH ;Load A with DA, the opcode for JC.
STA MODIFY ;As CHOICE = 00, store code for JC in MODIFY,
;and begin sorting in ascending order.
BEGIN: LXI H, X
MOV C, M ;;Load C from location X.
DCR C ;Decrement C. C indicates the number of
;passes still to be made.
OUTLOOP:
MVI E, 01H ;Load E with 1. Contents of E indicates one more
;than the number of exchanges made in a pass.
MOV B, C ;Load B from C. B indicates the number of
/comparisons still to be made in a pass.
INX H /Point HL to the first element of the array.
260
Start
Y
(choice)=0?
0 N
w
w Store opcode for JC at Store opcode for JNC at
0)
0
0
location MODIFY location MODIFY
a I
o
Point HL to X
Load C with one less than the no.
s of elements. C has no. of passes to
in
00 be performed
o
00 u
x MVI E, 01
MOV B, C
INXH
*
MOV A, M
INX H
CMP M
Y Numbers in'
proper order?
N
Exchange contents of memory
pointed by HL with its
preceding element. Increment E
I
DCRB
N
(B) = 0?
Y
DCRE
Y
(E)=0?
C100H = X 04 N
Point HL to X
11
DCRC
22
33
N
44 (C)=0?
C200H=Choice 00 Y k-
HLT
MODIFY:NOP
DW SKIP ;;If they are in proper order, jump to SKIP. 0
3
;The following 6 instructions perform exchange of the bytes r«
/which were not in order, and increments E register. 5 3i
EXCH: MOV D, M
C E.
W ft
MOV M, A *
DCX H 5«
MOV M, D ≤38
INX H |3
a <
INR E ft
In the above program, it is to be noted that the program itself is modified based on the contents of
location CHOICE. However, if we do not want the modification of the program, an alternative pro¬
gram is shown, with flowchart as in Fig. 17.5b.
/8085 ALP TO SORT N ONE BYTE BINARY NUMBERS IN ASCENDING / DESCENDING ORDER
/USING BUBBLE SORT. N IS AT LOCATION X, AND THE NUMBERS FROM LOCATION X+l.
ORG C100H
X: DB 04H, 33H, 22H, 44H, 11H
ORG C200H
CHOICE:DB 00H
ORG C000H
LXI H, X
MOV C, M //Load C from location X.
DCR C /Decrement C. C indicates the number of
/passes still to be made.
262
Start
Point HL to X
Load C with one less than the no.
of elements. C has no. of passes to
0
be performed
0) ->
0
0
MVI E, 01
a
o MOV B, C
INXH
s -
in
00 MOV A, M
O
00 INXH
tl CMPM
X
tChoice) = (V?>ÿ
Y
Y Y
Carry= 1? Carry =0?
N N
JI
Exchange conlents of memory
pointed by TIL with its
preceding element. Increment E
I
DCR B
(B)=0?
Y
DCR E
Y
(E) = 0?
N
C100H=X 04
Point HL to X
DCR C
22
33
44 N
(C)=0?
C200H=Choice 00 Y k-
HLT
Fig. 17.5b Flowchart showing an alternative method to perform bubble sort based on CHOICE
263
OUTLOOP:
MVI E, 01H /Load E with 1. Contents of E indicates one more
/than the number of exchanges made in a pass.
MOV B, C /Load B from C. B indicates the number of
/comparisons still to be made in a pass.
INX H /Point HL to the first element of the array. 3
o
INLOOP: it
MOV A, M /Load A from memory pointed by HL.
if°
M
INX H /Point HL to the next element of the array.
CMP M /Compare A (previous element) and next element.
Write an 8085 assembly language program to perform sorting in ascending or descending order based
on the contents of location CHOICE, using selection sort technique. If the contents of CHOICE = 00,
sorting should be in ascending order, else in descending order.
Flowchart for solving the problem is shown in Fig. 17.6.
264
Start
0
Y
w .(Choice) =0?
w
0)
o N
0
o. Store opcode for JNC at Store opcode for JC at
o location JC-JNC location JC-JNC
s i -
in
CO Point HL to X. Load C with one
o less than the no. of elements. C has
CO
no. of passes to be performed
X
MOV B, C
INXH
MOV A, M
MOV E, L
«ÿ
INXH
CMPM
Y Numbers in
proper order?
N
MOV A, M
MOV E, L
DCRB
N
(B)=0?
N
(C)=0?
Y
C200H=CHOICE
HLT
Fig. 17.6 Flowchart to perform selection sort in ascending/descending order based on CHOICE
265
1 7.6.1 PROGRAM TO PERFORM SELECTION SORT IN ASCENDING/DESCENDING ORDER
;FILE NAME C:\ALS\SELS0RT3.ASM
;8085 ALP TO SORT N ONE BYTE BINARY NUMBERS IN ASCENDING / DESCENDING ORDER
;USING SELECTION SORT. N IS AT LOCATION X, AND THE NUMBERS FROM
;LOCATION X+l. 0
ft
;IF CONTENTS OF LOCATION CHOICE = 00, IT IS SORTED IN ASCENDING ORDER, r«
;ELSE IN DESCENDING ORDER. § §3
e
ORG C000H w E.
a
X: EQU C100H *
CHOICE:EQU C200H
3 S
(c5
LDA CHOICE
CPI 00H
JZ ASC ;;;If CHOICE = 0, jump to ASC.
ii
u
MVI A, DAH ;If CHOICE <> 0, load A with code for JC.
STA JC JNC ;Store code for JC in location JC JNC.
JMP SORT ;Begin sorting in descending order.
SORT: LXI H, X
MOV C, M //Load C from location X.
DCR C /Decrement C. C contains number of passes still to perform.
OUTLOOP:
MOV B, C /Load B from C. B contains number of
/comparisons still to perform in a pass.
INX H
MOV A, M //Load A with the first element of array. Finally A will have
/largest (smallest) number for ascending (descending) order.
MOV E, L /Load E with the position of the element.
INLOOP:INX H
CMP M //Compare A contents with the next element.
PROCEED:
MOV A, M /Load A with larger (smaller) element for
/ascending (descending) order.
MOV E, L /E will have the position of larger (smaller) element.
LXI H, 0000H
SHLD Y+2 //Initialize word location Y+2 with 0000H.
LXI B, X
LDAX B
STA TEMP ///Save N value in location TEMP.
REP: INX B
LDAX B
MOV E, A ///E loaded with LS byte of a 16 bit number.
INX B
267
Start
INXB
LDAXB
MOV D, A
DADD
Y
Carry=0?
N
Increment contents of
word location Y+2
Decrement contents
of location Temp
N
(Temp)=0?
1234H
C100H=X 03 +5678H Y
34 +9ABCH Store HL value in
10368H
12 word location Y
Result display:
78
56 io i o o 1 11
| m Display contents of
BC 1 sec delay word location Y+2 for
9A 1 second in address field
|0|3|6|8| m
Display contents of
C200H=Y 68 word location Y in
03 address field
Y+2 01
00 HLT
Write an 8085 assembly language program to perform multiplication of two 8-bit numbers. The
numbers are at locations X and Y. Display the 1 6-bit result in the address field.
Flowchart for solving the problem is shown in Fig. 17.8.
1 7.8.1 PROGRAM TO MULTIPLY TWO 8-BIT NUMBERS (SHIFT AND ADD METHOD)
□
E
C100H=X OAH
A
C200H=Y 05 □
0
ft
r «§
Result display: S3
(fl
1 0 1 013 1 2 | m e E.
w
« 9X
ft
Start 73 5>
(c
5S
Load E with contents of X.
Load D with 00H
ii
M
Y
Carry = 0?
N
Increment HL by
contents of DE
I
Double DE contents
Decrement C
N
(C)=0?
Y
SHLD CURAD
CALL UPDAD Fig. 17.8
Flowchart to multiply
two 8-bit numbers
HLT
(shift and add method)
LXI H, X
MOV E, M
MVI D, 00H ;Load DE with the number at location X.
LDA Y ;Load A with the number at location Y.
MVI C, 08 ;Load C with 8. It indicates the number of
;shift and add to be still performed.
LXI H, 0 /Initialize HL. HL will finally have the product.
270
;The next 8 instructions perform the following. It checks a
;bit of A, starting with the LS bit. If the bit is 0, nothing is
/added to HL value. If the bit is 1, DE contents is added to HL. Then
/immaterial of the value of the bit, DE is doubled. Counter C is
/decremented. If not zero, the loop is repeated.
Write an 8085 assembly language program to perform multiplication of two 2-digit BCD numbers.
The numbers are at locations X and X + 1. Display the four-digit BCD result in the address field.
Flowchart for solving the problem is shown in Fig. 17.9.
C100H=X 04 B
05 C
3
o
it
Result display: JT O
|0 1 0 1 2 1 0 |m =%u TLJ><
£
#
w .
Start
3|
si
Load B with operand 1
Load C with operand 2
if
w
MVI E, 00H
MVI H, 00H
MVI A, 00H
Y
Operand 2=0?
4TN
ADD B to A
Decimal adjust A
Carry=l?Nÿ
Y
Increment H by 1
in decimal
I
Increment E by I
in decimal
N
(E) = operand 2?
MOV L, A
I
SHLD CURAD
CALL UPDAD
I
HLT
Write an 8085 assembly language program to perform multiplication of two 16-bit binary numbers.
The numbers are at locations X, X+l and Y, Y+l. Store the 32-bit result in four locations starting
from location Z.
Flowchart for solving the problem is shown in Fig. 17.10.
BC
C200H=Y 0005H ■»
0
(t
C300H=Z 0014
Result ro
3I
Z+2 0000 §
(a
e E.
«(P 5
>
0 •
Start 7
(cS5
Load DE with contents
of word location X
ii
(A
LXI H, 0000H
Store 0000 in word
location Z+2
+
DADD
Y
Carry=0?
N
Increment contents
of word location Z+2
■>
'V
Decrement BC
N
(BC)=0000?
Y
Store HL value in
word location Z
I Fig. 17.10
Flowchart to multiply two 16-bit
HLT
binary numbers
LHLD Y
PUSH H
POP B ;;;Load BC with contents of word location Y.
LXI H, 0 ;Load HL with 0000H.
SHLD Z+2 ;Store the value 0000H in word location Z+2.
274
REP: DAD D ;Add the first number to HL.
JNC NOINCR ;If Cy = 0, jump to NOINCR.
PUSH H
LHLD Z+2
INX H
0 SHLD Z+2
POP H ;;;;If Cy = 1, increment word location Z+2 contents.
E
a
NOINCR:DCX B ;Decrement BC contents.
MOV A, B
E ORA C
.2
2 JNZ REP ;;;If non zero, jump to REP.
■n
;When we come out of this loop, word location Z+2 will have
;MS word of result, and HL will have LS word of result.
j;
SHLD Z ;Store LS word of result in word location Z.
HLT
1. Write an 8085 assembly language program to exchange a block of words (16 bits) of data
starting at locations X and Y. Size in words of the block is provided in location N.
2. Write an 8085 assembly language program to search for a given 8-bit data in an array of
sorted bytes, using Binary search algorithm. The number of elements is provided in location
X, the element to be searched is provided in location X+l, and the array of sorted bytes starts
at location X+2. If search is successful, store FF in location Y and store the 16-bit address
of the position the element was found in word location Y+l. If search is unsuccessful, store
00 in location Y and store 0000H in word location Y+l.
3. Write an 8085 assembly language program to compute the HCF of two 16-bit numbers
stored in word locations X and X+2. Store the result in word location X+4.
4. Write an 8085 assembly language program to compute the LCM of two 16-bit numbers
stored in word locations X and X+2. Store the result in word locations X+4 and X+6.
5. Write an 8085 assembly language program to compute the HCF of two 4-digit BCD num¬
bers stored in word locations X and X+2. Store the result in word location X+4
6. Write an 8085 assembly language program to compute the LCM ot two 4-digit BCD num¬
bers stored in word locations X and X+2. Store the result in word locations X+4 and X+6.
7. Write an 8085 assembly language program to convert a 32 bit binary number stored in loca¬
tions X to X+3 to equivalent BCD number. Store the result in locations X+4 to X+8.
8. Write an 8085 assembly language program to convert a 8-digit BCD number stored in loca¬
tions X to X+3 to equivalent binary number. Store the result in locations X+4 to X+7.
9. Write an 8085 assembly language program to sort a given set of 16-bit numbers in ascend¬
ing order using Bubble sort algorithm. The number of elements to be sorted is provided in
location X. The elements are in word locations starting from X+l.
10. Write an 8085 assembly language program to sort a given set of 16-bit numbers in descend¬
ing order using Selection sort algorithm. The number of elements to be sorted is provided in
location X. The elements are in word locations starting from X+l.
ED Programmable
and Non¬
programmable
I/O Ports
Chapter Heads
1 8 Interrupts in 8085
19 8212 Non-Programmable 8-Bit
I/O Port
20 8255 Programmable Peripheral
Interface Chip
21 Programs Using Interface Modules
INTRODUCTION
In this part, we will deal in detail with 8255 PPI, which is a very
popular chip that is used for interfacing I/O devices. Theoreti¬
cally, any I/O device could be interfaced with the microprocessor
using the 8255. This part comprises of Chaps. 18 to 21. Chapters
18 and 19 deal with the interrupts in the 8085 microprocessor
and the non-programmable chip, Intel 8212, respectively.
Chapters 20 and 21, which form the latter portion of this part
deal with the programmable I/O port, 8255 and programs using
interface modules.
18
Interrupts in
MM 8085
8
a>
o ■ 18.1 DATA TRANSFER SCHEMES
S
a
s When the 8085 is executing a program, it can get interrupted half way through the program by an I/O
S
in
device. An I/O device interrupts the working of the processor, because it may want to urgently com¬
a municate with the processor. It may want to send some information to the processor, or receive some
s information from the processor.
Jj A microprocessor does not directly communicate with an I/O device. It communicates with an I/O
H
device via an I/O port. Data transfer can be in parallel or serial form. Parallel data transfer is possible
using programmed I/O or Direct Memory Access (DMA). Serial data transfer and DMA data transfer
are explained in later chapters. There are three different ways a microprocessor can communicate with
an I/O port for parallel data transfer with programmed I/O. They are:
Parallel Serial
This is the simplest of the data transfer schemes. This method is useful when we have accurate know¬
ledge of the I/O device timing characteristics. When we know that the device is ready for data trans¬
fer, we execute IN or OUT instruction, depending on the required direction of data transfer. This is the
case when the I/O port is connected in the system as an I/O-mapped I/O port. If the port is connected
as a memory-mapped I/O port, ‘MOV M, A’, ‘MOV A, M’, or any other memory reference instruc¬
tion is used depending on the direction of data transfer.
As an example, let us say we have a hypothetical multiplier chip. Registers R0 and R1 are used to
load the two 8-bit numbers to be multiplied. The 16-bit product will be available in R2H and R2L
when the multiplication is over. Address pins A1 and A0 select a register as shown in the following.
279
A1 AO Selected register
0 0 RO
0 1 R1
1 0 R2L (LS byte of product)
1 1 R2H (MS byte of product)
Let us say the chip is connected as I/O-mapped I/O, as shown in Fig. 18.1. As per the chip select 2
=
circuit, the address for RO, Rl, R2L, and R2H is 40H, 41H, 42H, and 43H, respectively. Let us say 5
■o
the data sheet for the multiplier specifies a maximum of 50 pts for completing the multiplication. M
5
o A7 Multiplier chip
U1
1 A6
0 As 8 bits
0 A4 OR CS* 40H | | RO
0 A3
0 A2 Ai 41H | [Rl
1 IO/M*
Ao 42H | |R2L
43H | | R2H
Fig. 18.1
D7-0
Simple I/O data transfer
With this set up, the following program segment performs multiplication of 05H and 08H, and the
result will be stored in the BC register pair.
MVI A, OSH
OUT 4 OH ;Load RO with OSH
MVI A, 08H
OUT 4 1H ;Load Rl with 08H
CALL DELAY /Generate a delay of 50 micro seconds
;The subroutine is not shown for simplicity
IN 42H
MOV C, A /Store LS byte of product in C register
IN 43H
MOV B, A /Store MS byte of product in B register
In the previous example, even if the multiplication is over in 20 |xs, we are required to wait for
50 p,s, as per the data sheet. The advantage of simple I/O is its simplicity, but its disadvantage is that
it is not very efficient. We come across a number of examples later in the text for this type of data
transfer. The flowchart for simple I/O is illustrated in Fig. 18.2.
This is more complex than simple data transfer. This method is used when we do not have accurate
knowledge of the I/O device timing characteristics. The processor should get status information about
the readiness of the I/O device for data transfer. Generally, the processor will be in a loop checking
for the readiness of the device. The moment the device is ready, it comes out of the loop, and executes
IN or OUT instruction depending on the requirement. In this case, the processor has simply wasted its
time in a loop till the device got ready.
280
Start
Stop
Fig. 18.2
Flowchart for simple I/O
As an example, let us once again consider the hypothetical multiplier chip. Let the chip be pro¬
vided with command and status registers also. Let us say when the MS bit of the command register is
set to 1, it is a command to start the multiplication of the two numbers in RO and Rl. Let us say the
LS bit of the status register is set to 1 when the product is available in R2H and R2L.
Address pins A2, Al, and AO select a register as shown in the following.
A2 Al AO Selected register
0 0 0 RO
0 0 1 Rl
0 1 0 R2L (LS byte of product)
0 1 1 R2H (MS byte of product)
1 0 0 Command register
1 0 1 Status register
Let us say the chip is connected as I/O-mapped I/O, as shown in Fig. 18.3. As per the chip select
circuit, the address for RO, Rl, R2L, R2H, command register and status register is 40H, 41H, 42H,
43H, 44H, and 45H, respectively.
In this case, the microprocessor sends the two bytes to be multiplied to the registers RO and Rl.
Then a command is issued to start the multiplication process. This is done by setting the MS bit of the
command register. Then the status register is continuously monitored for the completion of the multi¬
plication operation. This is done by checking the LS bit of status register. When the LS bit of status
register becomes 1, the microprocessor reads the result.
With this set up, the following program segment performs multiplication of 05H and 08H, and the
result will be stored in the BC register pair.
281
Multiplier chip
44H Start
40H [ ]R0
Command
port
41H [ ]R1
A2 A2 3
o A7 !T
A, Ai 42H [ R2L
1 A6 — t>o- Ao Ao Status
0 A, port
43H [ ] R2H ■o
CS* 45H
Over 5T
0 A4 -> OR s'
0 A3 - Fig. 18.3 09
O
1 IO/M* Status check data 09
D7-O transfer
VI
MVI A, 05H
OUT 40H /Load R0 with 05H
MVI A, 08H
OUT 41H /Load R1 with 08H
MVI A, 10000000B
OUT 44 H / Issue Start multiplication command
WAIT: IN 45H
RRC
JNC WAIT /Be in wait loop till LS bit of Status
/register becomes 1
IN 42H
MOV C, A /Store LS byte of product in C register
IN 43H
MOV B, A /Store MS byte of product in B register
In status check I/O, as soon as the device is ready, the data transfer is performed. Status check I/O
is more efficient than simple I/O, but is more complex. This method of data transfer is also known as
handshake data transfer.
We come across a number of examples later in the text for this type of data transfer. The flowchart
for status check I/O is illustrated in Fig. 18.4.
Start
N
Device ready?
This method is used when we do not have accurate knowledge of the I/O device timing characteris¬
tics, except that it takes quite a long time for the device to get ready. If we resort to status check data
transfer, the processor will have to waste a long time in the loop for the device to get ready. To avoid
8 this problem, interrupt-driven data transfer can be used. In this case, the processor will go ahead with
a> its required work, and whenever the device gets ready for data transfer, the corresponding I/O port
o
sa will send a interrupt request signal to the processor. The interrupt request may arrive even half way
through an instruction execution. Then the processor will complete the instruction. After this, the
s
.2 processor will perform data transfer with the I/O device using IN or OUT instruction. Then it resumes
S the execution of the interrupted program. Flowchart for interrupt-driven data transfer is provided in
in
00 Fig. 18.5a.
s
Jj
H
program
—N
Any \
interrupt pin
active?
[Y
Execute interrupt service
subroutine to service Fig. 18.5a
interrupting device
Flowchart for interrupt-
driven data transfer
This method is obviously the most complex of the three types of data transfer. But the advantage
now is that the processor is not wasting its time in a loop checking for the readiness of the I/O device.
For example, if the 8085 wants to read from a keyboard, one way is to continuously scan
the keyboard looking for a pressed key. This is the status check method. It may so happen that
once in a second or so, it finds that a key is pressed. The 8085 is in a loop for this amount of
time just waiting for a key depression. In this time, the 8085 could have executed about
500,000 instructions, assuming an average execution time of 2 jxs per instruction! In interrupt-
driven data transfer, the 8085 would execute about 500,000 useful instructions, by which time the
user presses a key on the keyboard. This causes an interrupt signal to be sent to the 8085 by the
I/O port. Then the 8085 reads from the port attached to the keyboard, after which it goes ahead
with the program.
In the remaining portion of this chapter, we mainly discuss the interrupt pins of 8085, interrupt-
related instructions, and some programs that use interrupts. These programs clearly describe the inter¬
rupt-driven data transfer.
283
■ 1 8.2 GENERAL DISCUSSION ABOUT 8085 INTERRUPTS
Interrupt pins of 8085 are used by I/O devices to initiate transfer of data to or from 8085, without wast¬
ing much of the CPU time. As seen previously, this is very useful, when the timing characteristics of
the I/O device is not well known, and it takes a long time for the device to get ready for data transfer.
There are five interrupt pins on 8085, as shown in Fig. 18.5b. They are input pins of 8085. They are 2
=
TRAP, RST7.5, RST6.5, RST5.5, and INTR.
2
■o
M
5
8085 Cl
Trap
RST7.5
> Vectored interrupts
->l
RST6.5
RST5.5 J
-»l Fig. 18.5b
Intr Non-vectored interrupt
Interrupt pins of 8085
Note that RST7, RST6, and RST5 are instructions of 8085, whereas RST7.5, RST6.5, and RST5.5
are interrupt pins of 8085. An interrupt pin can be activated by an I/O port even half way through an
instruction, without having any relation to a clock signal. Thus the interrupts are asynchronous in
nature. The 8085 checks all these interrupt pins at about the end of every instruction. Specifically, they
are checked in the penultimate clock cycle of the last machine cycle of an instruction. However, the
interrupt pins are not checked at the end of an instruction, if the instruction belongs to the branch
group, JMP, CALL, RET, etc. In such cases, the interrupt requests are sensed after a lapse of about
15 clock cycles. At this point, if several interrupts are active simultaneously, the 8085 services them
as per the priority shown in the table that follows.
When an I/O port activates one of these pins, the 8085 gets interrupted if each of the following con¬
ditions are satisfied.
■ The intermpt system is enabled by setting the interrupt enable (IE) flip-flop of 8085. This condition
is not applicable to TRAR The method of setting the interrupt enable flag is discussed later.
■ The interrupt pin has not been masked. This condition is not applicable to TRAP and INTR. The
method of masking or unmasking an interrupt pin is discussed later.
■ Higher priority interrupts are not active at the same time.
284
The following is the broad action taken by the 8085 when it gets interrupted. Finer details are given
later in the chapter.
Interrupt service
.ÿsubroutine.,
I
Disable further interrupts.
This is automatically done
in 8085
Fig. 18.6
Return
Typical layout of an ISS
285
■ 1 8.3 El AND Dl INSTRUCTIONS
In the previous section, a broad outline was provided regarding the action to be taken by the 8085
when an I/O port activated an interrupt pin. The finer details are discussed in the rest of the chapter.
The 8085 can be thought of as having five internal interrupt signals that correspond to the five
external interrupt pins. Only when the internal interrupt signal is activated, the 8085 gets interrupted, 2
=
provided higher priority internal interrupt signals are not active at the same time. The 8085 checks all
2
■o
M
these internal interrupt signals in the penultimate clock cycle of the last machine cycle of an instruction.
There is a flip-flop in 8085 called IE flip-flop. Here IE stands for Interrupt Enable. Whenever this 5
flip-flop is reset to the 0 state, 8085 interrupt system is disabled. That is, even if an external interrupt
01
pin is activated, the corresponding internal interrupt signal is not activated. This can be seen from
Fig. 18.7, which provides the internal architecture of 8085 interrupt structure. From this figure it can
also be noticed that this flip-flop state has nothing to do with the internal interrupt signal correspon¬
ding to TRAP. TRAP is a non-maskable interrupt. It means whenever the external TRAP pin is
activated, the corresponding internal interrupt signal is always activated. Further, TRAP being the
highest priority interrupt, it always interrupts the 8085.
The IE flip-flop is reset to the 0 state by the following three conditions as shown in Fig. 18.7.
1. Execution of DI instruction;
2. Recognition of an interrupt request;
3. Resetting of 8085.
The DI instruction stands for ‘disable interrupts’. It is an 1-byte instruction. When this instruction
is executed, the IE flip-flop is reset. This disables the 8085 interrupt system except for the TRAP pin.
When the 8085 recognizes an interrupt, it branches to an ISS. By recognition we mean that, an inter¬
nal interrupt signal is activated, and higher priority internal interrupt signals are not active. In the ISS,
the 8085 would not like to be interrupted again. That is the reason, as to why the 8085 will place itself
in DI state, when it recognizes an interrupt. However, TRAP can still interrupt an ISS. Thus, when the
control is transferred to an ISS, interrupt system is disabled automatically. Accordingly, there is no need
for the programmer to write a DI instruction at the beginning of an ISS. Whether the programmer writes
a DI instruction at the beginning of an ISS or not, interrupt system remains disabled, except for TRAP.
Intel 8085 has a RESETIN* pin. This is an active low input pin. The 8085 is reset by placing a logic
0 on this pin for atleast 0.5 jxs, after power is supplied to Vcc pin of 8085. However, in practice the
RESET IN* is placed in logic 0 state for atleast a few milliseconds. A typical reset circuit, used in
ALS 8085 kit, is shown in Fig. 18.8.
The moment power supply is switched on, Vcc pin gets +5 V power. However, the RESET IN*
pin remains in the logic 0 state for a time dependent on the RC time constant. In the circuit in
Fig. 18.8, the RC time constant is about 50jxs. Thus, the RESET IN* pin will be in logic 0 for a
time longer than the minimum required 0.5 p,s.
When the system is already powered and running, sometimes we need to reset the 8085. There is
no need to switch off, and then switch on, to reset the 8085. There is a manual reset switch as shown
in Fig. 18.8. When we momentarily press and release it, the RESET IN* pin goes to logic 0, and then
286
8085
—> D Q Trap ■>
0 Clk ack
M Trap Trap internal
M
u Trap Flip Flop
0
Clr
a
o
S Reset in*
U) RST7.5 internal
CB Trap ack
O
CB
0 1_ RST7.5 ■>
D Q
-» ack
Clk RST7.5 M7.5
RST7.5 ->
Flip-flop
Clr SIM
MSE-
SIM
R7.5
RST6.5
r RST7.5 ack RST6.5 internal
RST6.5 ■>
- ack
M6.5 RST5.5
4
internal RST5.5 ■>
RST5.5
— —
) » ack
—> M5.5 =f>
Intr
Inta*
-7
El
Set Q Intr internal
IE
flip-flop
Reset
7
D1
4,
SIM, El, and DI signals activated by the control unit when the 8085 executes SIM, El, and DI instructions, respectively.
MSE, M7.5, M6.5, M5.5 are bit 4, bit 3, bit 2, and bit 1 of the accumulator, respectively. R7.5 is bit 4 of the accumulator.
8085
Vcc
ResetKey 51K£2 Vcc
n IOUFJ
Reset in* I
Reset in*
Fig. 18.8 2
=
2
■o
Reset circuit for
Reset out M
Time 8085
5
Cl
comes back to logic 1 state after a time dependent on the RC constant. In this case, if the RESETIN*
pin is kept at logic 0 for atleast three clock cycles (1 |xs) the 8085 will be reset.
Some of the important actions performed by the 8085 when it is reset are as follows.
If the RESETIN* signal is at logic 0, the 8085 sends out a logic 1 on the RESETOUT pin at the
beginning of the next clock cycle. This is an active high signal. RESET OUT goes to the logic 0 state
only in the next clock cycle after RESET IN* goes to logic 1 state. This RESET OUT signal is used
to reset other chips in the microcomputer system, such as 8255 PPI, 8251 USART, etc. The timing
relationship between RESET IN* and RESET OUT is shown in Fig. 18.9.
Clk
Reset in*
Fig. 18.9
Timing relationship
between RESETJN*
Reset out and RESET_OUT
Earlier, we have discussed the method of resetting to 0, the IE flip-flop. Now let us discuss the
method of setting to 1 the IE flip-flop. It is achieved by the execution of the El instruction. The El
instruction stands for ‘enable interrupts’. It is an 1-byte instruction. When this instruction is executed,
the IE flip-flop is set to 1. This enables the 8085 interrupt system. But the interrupts will be enabled
only after the next instruction after El is executed. The reason for this is as follows.
It was stated earlier that when the 8085 branches to an ISS, the interrupt system is disabled auto¬
matically. Thus, before we come out of the ISS, it is necessary to again enable the interrupt system
using El instruction. Otherwise, when the control is back with the interrupted program, it cannot be
interrupted again, except by TRAR But immediately after El, the interrupt system should not get
288
enabled. If it gets enabled, there is the danger of branching to another ISS before the RET instruction
in the current ISS is executed. To overcome this problem, the interrupt system gets enabled only after
one instruction is executed subsequent to the execution of the El instruction. Thus, if we end an
ISS with El followed by RET, the interrupt system is enabled only when the control is back with the
interrupted program.
8
a>
o
2
a ■ 1 8.4 INTR AND INTA* PINS
s
.2
S
in INTR is an active high input pin. This is having the lowest priority. It can interrupt the 8085 only if
oo
s the 8085 is not required to service any other interrupt at the same time. It is a level-sensitive input.
The line should remain high till the 8085 checks the internal interrupt signal corresponding to INTR
Jj
H at about the end of an instruction. The INTR internal interrupt signal is activated only when INTR pin
is in logic 1 state and IE flip-flop is in logic 1 state, as can be seen from Fig. 18.7. The 8085 gets inter¬
rupted because of INTR pin, only if the following conditions are met.
If these conditions are not met, even though INTR pin is activated, the 8085 does not get inter¬
rupted. In such a case, the INTR line should remain high till the above conditions are met in order to
interrupt the 8085.
We always start with a reset of the 8085, because of the switching on of the microcomputer sys¬
tem. Thus, all interrupts except TRAP are disabled to start with. So, if we want INTR pin activation
to interrupt the 8085, it is necessary that we have El instruction in our program. The interrupt system
will then be enabled after executing the next instruction after El. It is important to note that without
El instruction in the main program, the main program will never be interrupted by any of the 8085
interrupts, except TRAP.
INTR is a non-vectored interrupt in 8085. By this we mean that the 8085 does not know by itself
the starting address of the ISS. It has to be provided to the 8085 by an external I/O port, or by an inter¬
rupt controller like Intel 8259. The action taken by the 8085 when INTR pin is activated is detailed in
the following.
It is assumed that interrupt system is enabled using El instruction, and higher priority internal inter¬
rupt signals are not active.
1. In the penultimate clock cycle of the last machine cycle of every instruction, the 8085 senses
all the internal interrupt signals.
2. If INTR internal signal is at logic 1, the 8085 enters an interrupt acknowledge (INA) machine
cycle.
3. The interrupt from the I/O port is acknowledged by the 8085 by activating INTA* pin in the
T2 state of the INA machine cycle. INTA* is an active low pin. In response to INTA*, the inter¬
rupting port should send code for CALL instruction to IR register of 8085 on AD7_0 pins. Intel
289
T1 T2 T3 T4 T5 T6
Clock
Decode Decrement SP by 1
A15-8
Out
From PC, but 2
=
ignored by memory 2
■o
M
Out In 5
AD7_O From PC
(ignored)
ToIR
01
4
Code for CALL or RSTw
ALE
Inta*
Ready
8259 interrupt controller is capable of sending such a CALL instruction to 8085. Intel 8259 is
discussed in a later chapter. Note that the CALL instruction does not come from memory in
this case. So this Ml machine cycle is not an OF machine cycle from memory. Also, it is not
an IOR machine cycle, because INTA* is activated, and not RD*. In this INA machine cycle,
IO/M*, SI, and SO will all be in 1 state. This INAmachine cycle is shown in Fig. 18.10. INTA*
signal becomes a 1 after the 8085 receives the code for CALL during this INA machine cycle.
This INA machine cycle that fetches the code for CALL and decodes it, uses six T states.
4. In M2 machine cycle, which is also an INA machine cycle, LS byte of ISS address is trans¬
ferred to the Z register of 8085 by the 8259. This will be in response to activation of INTA*
during T2 of this INA machine cycle. This machine cycle uses only three T states, as the 8085
does not have to decode this information.
5. In M3 machine cycle, which is also an INA machine cycle, MS byte of ISS address is trans¬
ferred to the W register of 8085 by the 8259. This will be in response to activation of INTA*
during T2 of this INA machine cycle. This machine cycle also uses only three T states, as the
8085 does not have to decode this information. Now the 8085 has received the complete ISS
290
address. After this, save on the stack top the return address available in PC, before branching
to the ISS.
6. In M4, which is a MW machine cycle, MS byte of the PC is pushed onto the stack. This takes
three T states.
7. In M5, which is also a MW machine cycle, LS byte of PC is pushed onto the stack. At this point,
the address specified in the CALL instruction, which is present in the WZ register pair, is moved
8 to the PC. All this takes three T states. This results in the starting of the ISS execution.
a>
o
sa
S It can be noticed from these points that, after completion of the instruction during which the 8085
.2 got interrupted, it takes 6 + 3 + 3 + 3 + 3= 18 T states before the 8085 transfers control to the ISS.
S Incidentally, digressing a bit, it may be appropriate to describe the fetching and execution of a
in
00 3-byte CALL instruction stored in memory. This also needs 18 T states, but the machine cycles
s involved are: six T-state OF, followed by two MR machine cycles each of three T states, and two MW
Jj machine cycles each of three T states.
H
In response to activation of INTA*, instead of CALL instruction, an RST instruction can be transferred
to the IR register of 8085 by an I/O port. The RST instruction is only an 1-byte instruction, and so
there will be saving of time in receiving the instruction. The action taken by 8085 during this alterna¬
tive will be as follows.
It is assumed that the interrupt system is enabled using El instruction, and higher priority internal
interrupt signals are not active.
1. In the penultimate clock cycle of the last machine cycle of every instruction, the 8085 senses
all the internal interrupt signals.
2. If INTR internal signal is at logic 1, the 8085 enters an INA machine cycle.
3. The interrupt from the I/O port is acknowledged by the 8085 by activating INTA* pin in the T2
state of INA machine cycle. In response to INTA*, an I/O port should send code for an RSTrc
(n = 0 to 7) instruction to IR register of 8085 on AD7_0 pins. An I/O port chip like Intel 8255 is
capable of sending such an RSTrc instruction to 8085. Intel 8255 is discussed in a later chapter.
Note that the RSTrc instruction does not come from the memory in this case. So this Ml machine
cycle is not an OF machine cycle from memory. Also, it is not an IOR machine cycle, because
INTA* is activated, and not RD*. In this INA machine cycle, IO/M*, SI, and SO will all be in
1 state. This INA machine cycle has already been shown in Fig. 18.10. INTA* signal becomes
a 1 after the 8085 receives the code for RST/J during this INA machine cycle. This INA machine
cycle that fetches the code for RST/J and decodes it, uses six T states. Now the 8085 knows that
it has to transfer control to the ISS at location n * 8. For example, if the instruction received was
RST6, the control should be transferred to the ISS at 6 * 8 = 0030H. This address will be inter¬
nally stored in the WZ register pair by the control unit of 8085. Now it is required to first of all
save on the stack top the return address available in the PC, before branching to the ISS.
4. In M2, which is a MW machine cycle, MS byte of PC is pushed onto the stack. This takes
three T states.
5. In M3, which is a MW machine cycle, LS byte of PC is pushed onto the stack. At this point,
the ISS address, which is present in the WZ register pair, is moved to the PC. This takes three
T states. This results in the starting of the ISS execution.
291
It can be noticed from these points that, after completion of the instruction during which the 8085
got interrupted, it takes 6 + 3 + 3= 12 T states before the 8085 transfers control to the ISS.
Generally at location 6 * 8 we will not have the ISS. For example, in the ALS kit there is JMP
FFA8H instruction in the 3 bytes starting at 6 * 8 = 0030H. FFA8H is a system RAM location in ALS
kit. The system RAM area is used by the kit for storing system data. As such, the ISS cannot start at
location FFA8H also. In this system RAM area, 3 bytes starting from FFA8H are reserved for the user
to store a jump instruction, say C900H. In such a case, the ISS for INTR when RST6 is received in 2
=
response to INTA*, starts at location C900H. This is illustrated in Fig. 18.11. 2
■o
M
5
6 * 8 = 0030H JMP
A8 01
FF
©
C900H
ISS for INTR
El
RET
©
FFA8H JMP
00
C9 Fig. 18.11
Jumping to ISS when RST6
is received by 8085
Incidentally, digressing a bit, it may be appropriate to describe the fetching and execution of the
single-byte RSTrc instruction stored in the memory. This also needs 12 T states, but the machine cycles
involved are: six T-state OF, followed by two MW machine cycles each of three T states.
Both RST5.5 and RST6.5 pins are level-sensitive inputs. RST6.5 has a higher priority than RST5.5.
RST5.5 in turn has higher priority than INTR. The function of RST5.5 and RST6.5 are very similar.
These pins should remain high till the 8085 checks all the internal interrupt signals at about the end
of an instruction. As can be easily seen from Fig. 18.7, the RST5.5 and RST6.5 internal interrupt sig¬
nals are activated only when
The 8085 gets interrupted because of RST5.5 or RST6.5 pin, only if the following conditions are met.
292
1. RST5.5 or RST6.5 internal interrupt signal is active;
2. Higher priority internal interrupt signals are not active.
If these conditions are not met, even though RST5.5 or RST6.5 pins are activated, the 8085 does
not get interrupted. In such a case, these pins should remain high till these conditions are met in order
to interrupt the 8085.
8 As we always start with a reset of the 8085, because of the switching on of the microcomputer sys¬
a>
o tem, all interrupts except TRAP are disabled to start with. So, if we want these pins to interrupt the
sa 8085, it is necessary that we have El instruction in our program. The interrupt system will then be
s
.2 enabled after executing the next instruction after El.
S RST5.5 and RST6.5 are vectored interrupts in 8085. By this we mean that the 8085 knows by itself
in
00 the starting address of the ISS. It is 5.5 * 8 = 002CH in the case of RST5.5, and 6.5 * 8 = 0034H in
s the case of RST6.5. The action taken by 8085 when RST5.5 or RST6.5 is activated, is discussed a
Jj little later.
H Generally at locations 5.5 * 8 and 6.5 * 8 we will not have the ISS. For example, in the ALS kit
there is JMP 05C3H instruction in the 3 bytes starting at 5.5 * 8 = 0024H. 05C3H is an EPROM loca¬
tion in ALS kit. The EPROM contains the monitor program. At 05C3H in the EPROM there is ISS for
RST5.5. In the ALS kit, whenever the user presses a key on the keyboard (except RESET key and
VECT INTR key), the RST5.5 pin of 8085 is activated. Thus, in ALS kit the RST5.5 interrupt pin is
used by the keyboard to interrupt the 8085. Similarly, at location 6.5 * 8 we will not have the ISS. For
example, in the ALS kit there is JMP FFABH instruction in the 3 bytes starting at 6.5 * 8 = 0034H.
FFABH is a system RAM location in ALS kit. The system RAM area is used by the kit for storing
system data. As such, the ISS cannot start at location FFABH also. In this system RAM area, 3 bytes
starting from FFABH are reserved for the user to store a jump instruction, say CC00H. In such a case,
the ISS for RST6.5 starts at location CC00H.
RST7.5 pin is an edge-sensitive input. This is used by peripherals that send a pulse, rather than a sus¬
tained high level, for interrupting the processor. Internal to 8085 there is a flip-flop connected to
RST7.5 interrupt pin. This flip-flop is set to 1, when a positive-going edge occurs on RST7.5 input.
This can be visualized from Fig. 18.7. The waveform for RST7.5 pin and Q output of RST7.5 flip-
flop is shown in Fig. 18.12.
RST7.5 internal interrupt signal has a higher priority than the internal interrupt signals of RST6.5,
RST5.5, and INTR. As can be seen from Fig. 18.7, the RST7.5 internal interrupt signal is activated
only when
The 8085 gets interrupted because of RST7.5 pin, only if the following conditions are met.
RST7.5
2
=
2
■o
Q output of
M
RST7.5 flip-flop
5
Reset to 0 because of any of the following.
ci
1. Reset in * activated
2. SIM instruction with R7.5 bit= 1
3. Acknowledgement of RST7.5 interrupt
If these conditions are not met, even though a rising edge occurs on RST7.5 pin, the 8085 does not
get interrupted. As we always start with a reset of the 8085, because of the switching on of the micro¬
computer system, all interrupts except TRAP are disabled to start with. So, if we want the RST7.5 pin
to interrupt the 8085, it is necessary that we have El instruction in our program. The interrupt system
will then be enabled after executing the next instruction after El.
RST7.5 is a vectored interrupt in 8085. By this we mean that the 8085 knows by itself the starting
address of the ISS. It is 7.5 * 8 = 003CH. The action taken by 8085 when RST7.5 is activated is dis¬
cussed a little later.
Generally at location 7.5 * 8 we will not have the ISS. For example, in the ALS kit there is JMP
FFB1H instruction in the 3 bytes starting at 7.5 * 8 = 003CH. FFB1H is a system RAM location in
ALS kit. As such, the ISS cannot start at location FFB1H also. In this system RAM area, 3 bytes start¬
ing from FFB1H are reserved for the user to store a jump instruction, say CD00H. In such a case, the
ISS for RST7.5 starts at location CD00H.
There is no pin of 8085 that can be used to reset the RST7.5 flip-flop. So, RST7.5 flip-flop has to
be reset by software. The SIM instruction, to be discussed a little later, can be used to reset the RST7.5
flip-flop. Also, the moment when the 8085 recognizes RST7.5 interrupt request and branches to ISS,
the RST7.5 flip-flop is automatically reset to 0.
Trap is a non-maskable interrupt. This implies that whenever this pin is activated, the 8085 will always
get interrupted, even if the 8085 is in DI state. Trap input is both edge- and level-sensitive. Thus the
Trap line must make a transition from 0 to 1, and must remain in 1 state till the end of the execution
of an instruction in order to interrupt the 8085. Figure 18.13 shows the conditions for which Trap input
can cause interrupt of 8085. In this figure t0 indicates the time at which an instruction execution starts
and t\ indicates the time at which it ends.
294
Case 1 Trap /
8
M Case 2 Trap
✓
0
S
a
s
.2 /
S
in
00
s« Case 3 Trap
Fig. 18.13
H Condition for trap to
k h ■time interrupt the 8085
In Fig. 18.13, only in case 1, the Trap input interrupts the 8085 at time tx. This is because both the
edge and level conditions are satisfied. In case 2, the level condition is not satisfied at time tx. In case 3,
the edge condition is not satisfied at time tx. However in case 3, 8085 is interrupted because of interrupt
at t0 itself.
At this point it is appropriate to note that an interrupt input is deactivated by an I/O port only after
the port is serviced by the 8085. The port needs to perform data transfer with 8085 in the ISS in order
that the port is satisfied. This generally happens at about the middle of an ISS. This is in view of the
instructions to save the registers in the beginning of the ISS, and instructions to restore registers at the
end of the ISS. Thus, an interrupt pin is deactivated by an I/O port only in the middle of the ISS.
Now, it should be clear as to why the Trap input should be both edge- and level-sensitive. If it were
to be just edge-sensitive, it would have been susceptible to noise. If it were to be only level-sensitive
the problem would be as follows. The Trap pin would still be in the logic 1 state till at about the mid¬
dle of the ISS, as explained previously. So surely at the end of the first instruction in the ISS, the Trap
pin will still be in logic 1 state causing the 8085 to again get interrupted because of the Trap. To solve
this problem, Trap is designed to be both edge- and level-sensitive.
Internal to 8085 there is a flip-flop connected to the Trap interrupt pin. As can be seen from
Fig. 18.7, the Trap internal interrupt signal is activated whenever a 0 to 1 transition occurs on Trap
pin, and the Trap pin remains in the 1 state till the end of an instruction. Trap internal interrupt signal
has a higher priority than all the other internal interrupt signals of 8085. So, whenever the Trap inter¬
nal interrupt signal is activated, the 8085 gets interrupted even if 8085 is in DI state.
The moment when the 8085 recognizes Trap interrupt request and branches to ISS, the Trap flip-
flop is automatically reset to 0. It is also reset by activation of reset input of 8085, as can be seen from
Fig. 18.7. Thus, even if the external Trap signal remains at logic 1, it cannot further interrupt the 8085
in the ISS for Trap. To again interrupt the 8085, the external Trap signal should be brought to logic 0,
then it must go to logic 1, and remain at logic 1 till the end of the instruction to interrupt the 8085.
Trap is a vectored-interrupt in 8085. The 8085 knows by itself the starting address of the ISS as
4.5 * 8 = 0024H. Thus, Trap pin could have been named equivalently as RST4.5 pin. However,
Intel prefers to refer to it as Trap. This is probably because, in computer terminology, Trap is a non¬
maskable interrupt, whereas an interrupt is a maskable one. The action taken by 8085 when Trap is
activated is discussed a little later.
Generally at location 4.5 * 8 we will not have the ISS. For example, in the ALS kit there is JMP
0182H instruction in the 3 bytes starting at 4.5 * 8 = 0024H.
295
In the ALS kit, memory location 0182H is in the EPROM. The EPROM contains the monitor
program. At 0182H in the EPROM there is ISS for trap. In the ALS kit, the trap interrupt is used
for single-stepping through the program, for debugging purposes.
Similarly, the condition for the other vector interrupts to cause an interrupt to the 8085 has already
been discussed. However, the action taken by the 8085 when it gets interrupted because of any vector
interrupt, is almost the same. Hence, only the action taken when 8085 is interrupted because of
RST6.5 interrupt pin is explained as follows.
The 8085 recognizes the RST6.5 interrupt at about the end of the execution of an instruction. At
the end of this instruction, the 8085 enters a (BI) machine cycle. In this BI machine cycle, we can
imagine that the IR register is loaded with the code for ‘RST6.5’ instruction. However, there is no such
instruction in reality. As such, we can as well imagine that the IR register is loaded with code for
CALL instruction, and the WZ register pair is loaded with 6.5 * 8 = 0034H. This is because, RST6.5
is equivalent to CALL 0034H. This happens the moment when the 8085 recognizes RST6.5 interrupt.
The 8085 spends three clock cycles for decoding it. Decoding is purely an internal operation for 8085.
During this time, there is no activity on address, data, or control bus. Hence the name ‘bus idle’ for
this machine cycle is justified. This BI machine cycle, which is the first machine cycle Ml, needs only
three clock cycles (compared with the six clock cycles needed for the first INA machine cycle when
INTR causes interrupt to 8085). This is because, the opcode does not have to come from memory or
I/O ports. There cannot be any wait states in the BI machine cycle. This is because neither memory,
nor I/O port is communicating with 8085 in the BI machine cycle.
The 8085 then stores the contents of the PC, which is the return address, on the stack. M2, which
is a MW machine cycle, is used for storing PCH (high byte of PC) on the stack top. It takes three clock
cycles.
Similarly, M3, which is also a MW machine cycle, is used for storing PCL (low byte of PC) on the
stack top. Then the PC is loaded with 0034H from the WZ register pair. This takes a total of another
three clock cycles.
Generally, at location 0034H we will not have the ISS. For example, in the ALS kit there is JMP
FFABH instruction in the 3 bytes starting at 0034H. FFABH is a system RAM location in ALS kit.
The system RAM area is used by the kit for storing system data. As such, the ISS cannot start at loca¬
tion FFABH also. In this system RAM area, 3 bytes starting from FFABH are reserved for the user to
store a jump instruction, say CC00H. In such a case, the ISS for RST6.5 starts at location CC00H.
Thus, when the 8085 gets interrupted because of a vector interrupt, there will be a BI machine cycle
followed by two MW machine cycles. The only other occasion when the 8085 enters a BI machine
cycle is during the execution of ‘DAD rp’ instruction.
296
■ 1 8.8 EXECUTION OF 'DAD rp' INSTRUCTION
Let us digress a bit at this point to discuss about the execution of the ‘DAD rp’ instruction. As an
example, let us study the execution of the DAD B instruction. If content of BC is 5678H, and content
8 of HL is 12A7H, execution of DAD B should result in HL having 691FH, as shown in the following.
a> (BC) = 56 78H
o
sa (HL) = 12A7H
s
.2
S (HL) = 69 1FH
in
00 In the first machine cycle Ml, the opcode for DAD B instruction is fetched from the memory into
s the IR register of 8085. This is then decoded by the 8085 to interpret it as the opcode for DAD B
Jj instruction. This OF machine cycle takes a total of 3 + 1 =4 clock cycles. Now it is required to add
H
the contents of HL and BC register pairs. Thus the data needed for this instruction execution is avail¬
able within the 8085. It is similar to ADD B instruction, where the data needed for instruction execu¬
tion is available within 8085. Generally in such cases the execution portion takes only a fraction of a
clock cycle. But in the case of DAD B, as the required operation is addition of 16-bit numbers, it needs
much larger time. This is because, in 8085 we have only an 8-bit ALU, with accumulator and temp
registers providing the two inputs to the ALU.
In the second machine cycle M2, the following actions take place.
This machine cycle uses up three clock cycles. It is a BI machine cycle because:
In the third machine cycle M3, the following actions take place.
This machine cycle uses up three clock cycles. This is also a BI machine cycle because:
Thus, the DAD B instruction needs a total of ten clock cycles. It consists of OF machine cycle (four
clock cycles), followed by two BI machine cycles (each of three clock cycles).
297
■ 1 8.9 SIM AND RIM INSTRUCTIONS
It was noted earlier that RST7.5, RST6.5, and RST5.5 interrupt pins can be masked or unmasked. But
then what is the need for masking?
=
5
2
■o
1 8.9.1 NEED FOR MASKING
M
5
Let us say, the 8085 has been interrupted because of RST6.5 pin and has branched to the ISS for
RST6.5. Then, even without DI instruction at the beginning of this ISS, all the interrupts except TRAP
01
are disabled. So, even if RST7.5 pin is activated in the middle of the execution of RST6.5 ISS, the
8085 will not get interrupted because of RST7.5. Actually RST7.5 is a higher priority interrupt, but
now it cannot interrupt the lower priority ISS of RST6.5! We can hope to solve this problem by speci¬
fically having El instruction at the beginning of the ISS for RST6.5. Now RST7.5 can definitely inter¬
rupt the RST6.5 ISS, but the problem is that even RST5.5 can interrupt the RST6.5 ISS!
This problem can be solved using the concept of masking of interrupts. It provides the ability to
selectively disable the interrupts. An interrupt pin that is masked cannot interrupt, even if the interrupt
pin is activated and interrupts are in general enabled using El instruction. Similarly, an interrupt pin
that is unmasked can interrupt, when the interrupt pin is activated and interrupts are in general enabled
using El instruction.
Thus, all that is needed to prevent RST5.5 from interrupting RST6.5 ISS, while allowing RST7.5
to interrupt the RST6.5 ISS is:
SIM stands for ‘set interrupt mask’. It is a single-byte instruction. Each of the instructions of 8085 that
we have come across so far have a single purpose. But the SIM instruction is a multi-purpose instruc¬
tion. It is used for the following purposes.
When SIM instruction is executed, how does the 8085 know which interrupt is to be masked or
unmasked? The contents of the accumulator decides the action to be taken. Thus, it is essential to load
the accumulator with the desired value before SIM instruction is executed. The meaning of the vari¬
ous bits of the accumulator when SIM is executed is as shown in Fig. 18.14. Note that except bit 5,
298
7 6 5 4 3 2 1 0 Bit number
It may be noted that only RST7.5, RST6.5, and RST5.5 can be masked or unmasked using SIM
instruction. TRAP and INTR cannot be masked or unmasked using SIM. TRAP is not allowed to be
masked because it is the highest priority interrupt. INTR does not need the facility of masking because
it is the lowest priority interrupt. After reset of 8085 RST7.5, RST6.5, and RST5.5 interrupts will be
in masked condition.
Reset R5T7.5 flip-flop: Bit 4 (R7.5) of accumulator is used for resetting to 0 RST7.5 flip-flop output,
when SIM instruction is executed. If R7.5 = 0, SIM instruction is not being used for resetting of
RST7.5 flip-flop. Thus, if R7.5 = 0, there is no change in the RST7.5 flip-flop output. If R7.5 = 1,
the RST7.5 flip-flop gets cleared. This can be visualized from Fig. 18.15, which is actually a part of
Fig. 18.17.
RST7.5
flip-flop
Clear
SIM
Reset in* R7.5
RST7.5 ack Fig. 18.15
Clearing of RST7.5 flip-flop
299
In Fig. 18.15, SIM signal is activated when SIM instruction is executed. R7.5 signal is activated
when bit 4 (R7.5) of accumulator = 1. So the RST7.5 flip-flop receives logic 1 to its clear input and
thus gets cleared when bit 4 of accumulator = 1 and SIM instruction is executed. Alternatively,
RST7.5 flip-flop gets cleared when RST7.5 interrupt is recognized by the 8085 or whenever the 8085
is reset.
Serial output of data and SOD pin: Intel 8085 is an 8-bit processor. Thus, it normally sends or 2
=
receives 8 bits of data at a time. This is known as 8-bit parallel data transfer. In some situations it may 2
■o
not be practical to perform parallel data transfer. Suppose data is to be transmitted over telephone M
lines, then the 8-bit parallel data will have to be sent out in serial form, one bit after another. Also, we 5
are required to perform serial data transfer when the processor needs to communicate with a serial
device, like mouse. Serial communication is much cheaper, because the data transfer takes place on a 01
single line, instead of the eight lines needed in parallel data transfer. However, the speed of commu¬
nication in serial format is much slower compared with parallel communication. Very few micro¬
processors provide for on-chip serial communication facility, 8085 being one of them.
Intel 8085 provides SOD (serial output of data) pin and SIM instruction to facilitate serial output
of data. From where inside the 8085 does a single bit of information come out on the SOD pin? The
MS bit of the accumulator comes out on the SOD pin, when the user commands the 8085 to send out
the data. The user commands the 8085 to send out the MS bit of accumulator by making bit 6 of accu¬
mulator as 1, and executing the SIM instruction. This can be visualized from Fig. 18.16.
8085
SIM | Switch 2 SOD pin 8085
Switch 1
SOD bit
or SOD pin
ISODISOEI SOE bit SIM Fig. 18.16
Accumulator
Serial output
of data
In Fig. 18.16, switch 1 gets closed when bit 6 of accumulator is at logic 1. Switch 2 gets closed
when SIM instruction is executed. So MS bit of the accumulator comes out on SOD pin of 8085 only
when bit 6 of accumulator is at logic 1, and SIM instruction is executed. In view of this, the meanings
of bits 6 and 7 of the accumulator will be as follows, when we execute SIM instruction.
Bit 6: This is the serial output enable (SOE) bit. If this bit = 0, SIM instruction is not being
used for serial output of data. In such a case, the MS bit of accumulator is not having
any useful information. If SOE bit = 1, the SIM instruction is used for serial output of
data. Then the MS bit provides the data to be sent out on the SOD pin of 8085.
Bit 7: This is serial output data (SOD) bit. This bit is meaningful only if SOE bit = 1. If SOE = 1
and SIM instruction is executed, then the SOD bit comes out on the SOD pin of 8085.
Suppose we have some 8-bit data in the B register, and we want to send it out serially on the SOD
pin, starting from the LS bit. Then we have to execute a program that does the following.
1. Bring to MS bit of the accumulator a bit from the B register, starting from the LS bit;
2. Set to 1, bit 6 of the accumulator;
3. Execute the SIM instruction;
4. Repeat these steps eight times in a loop.
300
1 8.9.3 RIM INSTRUCTION
RIM stands for ‘read interrupt mask’. It is a single-byte instruction. Like the SIM instruction, the RIM
instruction is also a multi-purpose instruction. It is used for the following purposes.
7 6 5 4 3 2 1 0 Bit number
Mask status of interrupts: The LS 3 bits of the accumulator are used to provide mask status of inter¬
rupts. Note that they are not used for masking or unmasking. Masking or unmasking has to be done
using the SIM instruction.
Bit 0: This is mask RST5.5 (M5.5) bit. If this bit = 1, it means that RST5.5 interrupt has been
masked. If M5.5 = 0, RST5.5 interrupt is unmasked.
Bit 1: This is mask RST6.5 (M6.5) bit. If this bit = 1, it means that RST6.5 interrupt has
been masked. If M6.5 = 0, RST6.5 interrupt is unmasked.
Bit 2: This is mask RST7.5 (M7.5) bit. If this bit = 1, it means that RST7.5 interrupt has been
masked. If M7.5 = 0, RST7.5 interrupt is unmasked.
Interrupt enable status: Bit 3 of the accumulator provides the status of IE flip-flop after the RIM
instruction is executed. If IE = 1, it means that the interrupt system is enabled. This will be the situ¬
ation if El instruction is executed sometime prior to the RIM instruction. If IE = 0, it means that the
interrupt system is disabled. This will be the situation if some time prior to execution of the RIM
instruction, one of the following things have occurred.
Bit 4: This is interrupt pending RST5.5 (IP5.5) bit. If this bit = 1, it means that RST5.5 inter¬
rupt is pending, waiting to be serviced. This will be the situation when RST5.5 interrupt
pin is activated, but RST5.5 is masked, or the interrupt system is disabled. If IP5.5 = 0,
the RST5.5 interrupt is not pending. 2
=
Bit 5: This is interrupt pending RST6.5 (IP6.5) bit. If this bit = 1, it means that RST6.5 2
■o
interrupt is pending, waiting to be serviced. If IP6.5 = 0, the RST6.5 interrupt is not M
pending. 5
Bit 6: This is interrupt pending RST7.5 (IP7.5) bit. If this bit = 1, it means that RST7.5
interrupt is pending, waiting to be serviced. If IP7.5 = 0, the RST7.5 interrupt is not 01
pending.
Serial input of data and SID pin: We have already seen the utility of serial communication. Intel
8085 provides SID (serial input of data) pin and RIM instruction to facilitate serial input of data. The
logic state on the SID pin of 8085 enters the MS bit of the accumulator when RIM instruction is
executed. This can be visualized from Fig. 18.18.
8085
SID pin
T .SID pin
or RIM
H Accumulator SID
Accumulator
In Fig. 18.18, the switch gets closed when the RIM instruction is executed. So MS bit of the accu¬
mulator receives the data present on the SID pin of 8085 when the RIM instruction is executed. In
view of this, the meaning of bit 7 of the accumulator will be as follows, after we execute the RIM
instruction.
Bit 7: This is SID bit. After the RIM instruction is executed, the data on the SID pin of 8085
gets loaded into this bit position.
Suppose that a serial device is sending some 8-bit data in a serial fashion, starting with the LS bit
first. Let us say, we want to receive this information on the SID pin serially and store it in the B reg¬
ister. Then we have to execute a program that does the following.
s
8 «18.10 HLT INSTRUCTION
S
jj
H HLT is the mnemonic for ‘Halt the microprocessor’ instruction. It is a 1-byte instruction. When this
instruction is executed, the 8085 halts further processing and enters Halt state. This is indicated by SI
and SO output signals becoming 0 0. The 8085 comes out of the Halt state when a valid interrupt
occurs. In such a case, it executes the corresponding interrupt service subroutine and then continues
with the instruction after the HLT instruction. However, in most programs the HLT instruction is used
for terminating the program. Also activation of reset in * causes 8085 to come out of Halt state.
To acquaint the reader with the usage of interrupts, a few programs that use interrupts are presented
in the following.
Write an 8085 assembly language program to simulate the throw of a die using an interrupt.
In this program we will have a counter, which counts from 1 to 6, and then repeats the count
sequence endlessly in an infinite loop. The throw of the die is simulated by pressing ‘Vect Intr’ key
on the keyboard. On the ALS kit when ‘Vect Intr’ key is pressed, RST7.5 pin is activated. It interrupts
the counter, with the counter having a random value in the range 1 to 6. The 8085 branches to the
RST7.5 ISS. In this ISS, the current value of the counter is displayed in the data field, and the control
returns to the main program to continue with the counter operation. Thus, every time the ‘Vect Intr’
key is pressed a random value between 1 to 6 is displayed in the data field, thus simulating the throw
of a die.
;FILE NAME DIE.ASM
/Main program to reset RST7.5 flipflop, unmask RST7.5, enable interrupts,
/and count from 1 to 6 endlessly in an infinite loop
ORG C000H
CURDT: EQU FFF9H
UPDDT: EQU 06D3H
DELAY: EQU 04BEH
303
MVI A, 00011011B
SIM /Reset RST7.5 flipflop, Unmask RST7.5
El /Enable interrupt system
Write an 8085 assembly language program to simulate a stopwatch to display minutes and seconds in
the address field. There should be a provision to stop the stopwatch, with the display continuing to
show the time just before the stop command.
In this program we will have a counter, which generates a delay of 1 s. After the delay generation,
the stopwatch display is incremented by 1 s. This is repeated in a loop. To stop the stopwatch, the user
is required to press the ‘Vect Intr’ key on the ALS kit. On the ALS kit when ‘Vect Intr’ key is pressed,
RST7.5 pin is activated. It interrupts the stopwatch program. The 8085 branches to the RST7.5 ISS.
In this ISS, we just have a HLT instruction. Thus, the program comes to a halt, with the display
continuing to display the time just before the ‘Vect Intr’ key is pressed.
w
00
LHLD CURAD
o MOV A, L
00
0) ADI 01H
£
H DAA / / / Increment L value in decimal
CPI 60H
JZ INC MIN /If L 60, jump to INC_MIN
MOV L, A
JMP REPEAT
INC MIN:
MVI L, 00H
MOV A, H
ADI 01H
DAA ////Make L = 0, and increment H in decimal
CPI 60H
JZ RESET /If H = 60, jump to RESET
MOV H, A
JMP REPEAT
INLOOP: DCX D
MOV A, D
ORA E
JNZ INLOOP
DCR B
JNZ OUTLOOP
RET
Write an 8085 assembly language program to find the square of a single digit (0 to 9) using a look-up
table. Display the number and its square in the address field.
The method involves storing a look-up table of squares in RAM, say at location X. Thus, store the
following values 00, 01, 04, 09, 16, 25, 36, 49, 64, and 81 in locations starting from X. Then the square a
of a number is picked up from the look-up table, and displayed in the address field. The program
assumes that X address has LS 2 digits as 00H, say C100H. ■o
It waits for the user to press a key in the range 0 to 9. If the user presses any other key, it will remain 5f
a
in the loop. On the ALS kit, whenever the user presses a key other than ‘RESET’ and ‘Vect Intr’,
RST5.5 interrupt pin is activated.
g
§
VI
X: DB 00H, 01H, 04H, 09H, 16H, 25H, 36H, 49H, 64H, 81H
ORG C000H
AGAIN:
DI
RIM
ANI 00010000B
JZ AGAIN /Stay in this loop till a key is pressed
El
NOP /RST5.5 interrupts the 8085 now. Only after NOP is
/executed, interrupt system is enabled.
LDA IBUFF
CPI 0AH
JNC AGAIN //If code is >= 0AH, jump to AGAIN.
SHLD CURAD
CALL UPDAD /Display the number and its square.
8 JMP AGAIN /Jump to read the next value from the keyboard.
a>
o
sa
2 1 8.1 1 .4 PROGRAM FOR DECIMAL DOWN COUNTER
s
in Write an 8085 assembly language program to implement a decimal down counter (from 99 to 00). The
oo
s count should be decremented every time a key (other than RESET and VECT INTR key) is pressed.
The count is to be displayed in the data field. The counter operation should repeat endlessly in a cyclic
Jj
H manner.
The program works as follows.
ORG C000H
MVI A, 00001110B
SIM /Unmask RST5.5
El /Enable Interrupt system
Write an 8085 assembly language program to implement a decimal down counter (from 99 to 00).
The count should be decremented every time VECT INTR key is pressed. The count is to be displayed
in the data field. The counter operation should repeat endlessly in a cyclic manner.
3
;FILE NAME C:\ALS\DNCNTR1.ASM 0
ORG C000H c
■o
W
CURDT: EQU FFF9H
UPDDT: EQU 06D3H
09
DELAY: EQU 04BEH O
09
Ul
MVI A, 99H ;Initialize A with 99.
STA CURDT ;Store A value in CURDT.
CALL UPDDT ;Display contents of CURDT in data field.
MVI A, 00011011B
SIM ;Unmask RST7.5 and reset RST7.5 flip flop
El /Enable Interrupt system
;The next 4 instructions forms an infinite loop during
/which VECT INTR key is pressed, causing activation of RST7.5
AGAIN: NOP
NOP
NOP
JMP AGAIN
/RST7.5 ISS to decrement count value by 1 in decimal
/and display it in data field. Then reset RST7.5 flip flop
/and enable interrupt system after a delay of 0.5 seconds.
Write an 8085 assembly language program to input two 2-digit hexadecimal numbers from the key¬
board, add them and output the result in the address field.
/FILE NAME C:\ALS\AD2INPTS.ASM
8M ORG C000H
0 CURAD: EQU FFF7H
S
a
UPDAD: EQU 06BCH
s
.2
CLEAR:
GTHEX:
EQU
EQU
044AH
052FH
S
U) MVI A, OEH
00
s« SIM
El ;; /Unmask RST5.5 and enable interrupts
H
MVI B, 01
CALL GTHEX //Input a 2 digit number and display in data field
MOV A, E
STA C100H //Store the 2-digit hex number in C100H
MVI B, 01
CALL GTHEX //Input a 2 digit number and display in data field
LDA C100H
ADD E
STA CURAD ///Add the two numbers and store in CURAD
JNC SKIP
MVI A, 01
STA CURAD+ 1 ///If Carry =1, store 1 in CURAD+1
SKIP: CALL CLEAR /Blank the entire display
CALL UPDAD /Display sum in address field.
HLT
In the above program GTHEX and CLEAR monitor routines were used for the first time. The
working of these routines are explained below.
GTHEX monitor routine: It stands for GeT HEXadecimal values from keyboard. This routine is
used to get 2 hex digits or 4 hex digits from keyboard. Before calling GTHEX, keyboard interrupt
(RST5.5) should be enabled by executing the following instructions.
MVI A, OEH
SIM
El
If contents of B register is 00H, 4 hex digits are received from the keyboard, and they will be dis¬
played in the address field. They are also stored in DE register pair. If more than 4 digits are entered from
the keyboard, only the last 4 digits are accepted. For example, if 345678 is entered, it is treated as 5678. If
less than 4 digits are entered, like 345 it is treated as 0345. The user entry is terminated with any of the
following valid terminators and the keycode of the terminator will be returned in A register.
EXEC
NEXT
PREV
309
If contents of B register is 01H, 2 hex digits are received from the keyboard, and they will be dis¬
played in the data field. They are also stored in E register. If more than 2 digits are entered from the
keyboard, only the last 2 digits are accepted. For example, if 5678 is entered it is treated as 78. If less
than 2 digits are entered, like 5 it is treated as 05. The user entry is terminated with any of the valid
terminators as stated above.
On the ALS kit, the GTHEX routine starts at 052FH.
2
=
CLEAR monitor routine: This routine blanks out the display in both address and data fields. If con¬ 2
■o
tents of B register were 01, a dot will be displayed in the address field. If the contents of register B M
were 00, even the dot will be blanked out. 5
All the general purpose registers and Flags register are affected by this routine. On the ALS kit, the §
CLEAR routine starts at 044AH. 01
Write an 8085 assembly language program to add 4 hex digits of a 16-bit number input from the key¬
board and display the result in the data field.
/FILE NAME C:\ALS\AD4INPT.ASM
ORG C000H
CURDT: EQU FFF9H
UPDDT: EQU 06D3H
GTHEX: EQU 052FH
HXDSP: EQU 05A1H
OBUFF: EQU FFFAH
MVI A, 0EH
SIM
El ;;/Unmask RST5.5 and enable interrupts
MVI B, 00
CALL GTHEX //Input a 4 digit number and display in address field
CALL HXDSP /Store the 4 hex digits in 4 locations starting from OBUFF
LXI H, OBUUF
MOV A, M
MVI C, 03
AGAIN: INX H
ADD M
DCR C
JNZ AGAIN /When we are out of this loop, A will have the sum.
STA CURDT
CALL UPDDT /Display sum in DATA field.
HLT
In the above program HXDSP monitor routine was used for the first time. The working of this
routine is as explained overleaf.
310
HXDSP monitor routine: This routine converts the value in DE register pair to 4 unpacked hex
bytes and stores them in 4 byte locations starting from OBUFF (output buffer) onwards. The LS
unpacked hex byte is stored at OBUFF. On ALS kit, OBUFF is location FFFAH, and HXDSP routine
STARTS AT 05A1H. Thus, if (DE) = 3456, we will have
FFFAHpa
0
FFFBH 05
1> FFFCH 04
E FFFDH 03
a
E
.2
2
in
j;
1. Describe the various methods of parallel data transfer. Bring out their merits and demerits.
2. With a neat diagram describe the architecture of the 8085 interrupt system.
3. Explain El and DI instructions.
4. Under what circumstances would the 8085 be in the disable interrupt state?
5. Describe the means that are used in a kit to reset the microprocessor.
6. What happens when the 8085 is reset?
7. Describe the action taken by 8085 when INTR is activated.
8. Describe the action taken by 8085 when a vectored interrupt pin is activated.
9. Explain SIM and RIM instructions.
10. Explain the execution of the DAD B instruction.
11 .Explain how serial communication can be achieved in a 8085 system.
12. Write a 8085 program, which displays FF in the data field if RST5.5 interrupt is pending,
else it should display 00.
13. Write an 8085 assembly language program to implement a decimal down counter (from
99 to 00). The count should be decremented every time VECT INTR key is pressed.
The count is to be displayed in the data field. The counter operation should stop after count¬
down to 00.
14. Write an 8085 assembly language program to implement a decimal down counter (from 99
to 00). The count should be decremented every time a key (other than RESET and VECT
INTR key) is pressed. It is assumed that RDKBD monitor routine is not available to the user.
The count is to be displayed in the data field. The counter operation should stop after count¬
down to 00.
19
m \
P:
■
8212 Non-Programmable
8-Bit I/O Port
■ Working of 821 2
•Pin diagram of 8212
• Intel 8212 in mode 0
• Intel 8212 in mode 1
■ Applications of 821 2
•Applications of 8212 in mode 0
•Applications of 8212 in mode 1
■ Questions
I/O ports are of two types. Programmable I/O ports and non-programmable I/O ports. Programmable
I/O ports are more popular because their function can be changed by software. There is no need to
change the wiring or the hardware to change the function of the I/O port. A very popular programma¬
ble I/O port chip is the Intel 8255. It will be discussed in detail in the next chapter.
On the other hand, non-programmable I/O ports require change of wiring or hardware to change its
function. An example is Intel 8212, which is the topic of this chapter. As will be seen later, connec¬
tion has to be changed if the 8212 has to work as an input port instead of an output port. Such non¬
programmable I/O ports are simple in design.
The 8212 is available as a dual in-line package chip with 24 pins. Its functional pin diagram is
illustrated in Fig. 19.1. The actual pin diagram of 8212 is provided in Fig. 19.2.
312
Vcc INT*
Gnd
DI7-0 8212 => D«7-O
DS1* 24 pin DIP
CLR*
8M DS2 Fig. 19.1
STB
0 MD Functional pin diagram
sa of 8212
s
.2
s
U) nsi*-> l 24
00
s« MD-» 2
DIO -s 3
23 -> INT*
22 «- DI7
H DOO «- 4 21 -> D07
Dll -* 5 20 t- DI6
DO 1 <- 6 8212 19 -> D06
DI2 —) 7 24pin DIP ig «-DI5
It works on a dc supply of +5 V. The 8-bit latch in 8212 receives information present on DI7.0 (data
inputs). The condition for latching the information present on DI7.0 depends on the logic state of the
MD (mode) pin. The information present in the latch comes out on DO7.0 (data output) pins. The con¬
dition to be satisfied for the latched information to come out depends on the MD pin.
There is an edge-triggered D-type flip-flop in the 8212. It is called the service request flip-flop. It
is mainly responsible for generation of an interrupt request on INT*. INT* is an active low output pin,
which is useful in interrupt-driven data transfer. The internal architecture responsible for activation of
INT* signal is shown in Fig. 19.3.
8212
‘0
— D
Edge-
triggered
service
Q SRQ INT*
Set
DS1 CS
DS2
Fig. 19.3
CLR* I>> Circuit for activation of
INT* in 8212
313
It can be seen from Fig. 19.3 that INT* is activated in the following two cases.
■ When DS1 * = 0 and DS2 = 1 so that the internal CS (chip select) signal is activated.
■ When STB makes a high to low transition so that the internal SRQ (Q output of service request
flip-flop) signal becomes 0.
a
INT* is deactivated when CS signal = 0 and SRQ = 1. SRQ becomes 1 whenever CLR* = 0 or
to
CS = 1.
Details of connection inside the 8212 for a latch flip-flop are shown in Fig. 19.4. There are eight
such latches in 8212. They are all level-triggered D-type flip-flops.
_
eoZ
n|
-a
!J
?z
8212 S 3
3
B
Din D Q DOn
EN
CS Level-
MD triggered
Clk flip flop
STB CS MD
Clear
CLR*
The following conclusions can be drawn from Fig. 19.4. If MD = 1, CS acts as the clock for the
latches. If MD = 0, STB acts as the clock for the latches. As long as the clock is in a high state, the
Q output of the latch follows the corresponding DI input. When the clock makes high to low transi¬
tion, the data gets latched. The data at the Q output comes out on the corresponding DO pin when the
internal EN (enable) signal is activated. The EN signal is activated whenever CS = 1 or MD = 1.
Brief summary of the pins of 8212 is provided in the following.
Jj ■DS1*
H STB /
■DS2
0
MD ■INT* Fig. 19.5
Working of 8212 in
Data bus mode 0
This mode is generally used when we want the 8212 to function as an input port. An input device
may be connected to DI7.0, and the microprocessor can receive the information on DO7.0. In this mode
STB acts as the clock for the latches. The 8-bit latch in 8212 follows the information present on DI7_0
as long as STB = 1. When STB makes a high to low transition, the 8212 latches the information on
DI7_0. High to low transition of STB activates INT* so that interrupt-driven data transfer may be per¬
formed if desired. The 8-bit latched information comes out on DO7_0 only when the output buffers are
enabled. The output buffers are enabled when DS1* = 0 and DS2 = 1.
This mode can also be used when we want the 8212 to function as an output port. In such a case,
STB is used by the microprocessor to load the latches with the data to be output. Output device
receives the data by activating DS1* and DS2. However, mode 1 operation is more commonly used
for output port function.
8212
DO7_0 Ouput
8085 A
DI7-0 / device
DS1*
DS2
I Fig. 19.6
MD INT* Working of 8212 in
Data bus mode 1
315
This mode is generally used when we want the 8212 to function as an output port. In this mode,
microprocessor may drive DI7_0, and an output device may receive the information on DO7.0. CS pro¬
vides the clock to the latches in this mode. The 8212 latches the information on DI7_0 when CS makes
high to low transition. The activation of CS signal activates INT* so that interrupt-driven data trans¬
fer may be performed if desired. The output buffers are always enabled in this mode. So the 8-bit
latched information straightaway comes out on DO7.0 without any condition. a
■ 1 9.2 APPLICATIONS OF 82 1 2
_
s?
s?
to
-a
!J
?z
Intel 8212 can be used in a variety of applications, limited only by the ingenuity of the user. A few 1 |
applications are indicated below for both mode 0 and mode 1 operations.
■ Gated buffer;
■ Bi-directional bus driver;
■ Interrupting input port;
■ RSTn interrupt instruction port;
■ As supplier of eight RST instructions.
Intel 8212 as gated buffer: Whenever 8212 works in mode 0, it converts a weak logic signal to a
strong logic signal. The 8212 outputs in mode 0 are capable of sinking 15 mA in 0 state, and provid¬
ing a minimum high output voltage of 3.65 V in 1 state. Intel 8212 is driven properly even if the input
signal is capable of driving 0.25 mA only. Thus, the 8212 when working in mode 0 acts as a buffer.
Intel 8212 as a gated buffer is indicated in Fig. 19.7.
8212
Weak signal DI7_0
DO7_0
0.25 mA A Strong signal
(3.65 V in 1 state
0 and 15mA sinking
MD current in 0 state)
DS1*
I Fig. 19.7
STB DS2
8212 as a gated buffer
We connect STB to logic 1 so that the latch inside 8212 follows the data on DI7_0. It will come out
as a strong signal on DO7_0 only when DS1* = 0 and DS2 = 1, justifying the name for the
application.
Bi-directional bus driver: Sometimes it is necessary to have bi-directional buffering. Such is the case
with the data lines of a microprocessor. This can be achieved using two 8212s, as shown in Fig. 19.8.
316
Data
8212
Weak logic signal Strong logic signal
\
DI7-Q DO7.0
/
Strong
logic
1 DS2 MD 0
signal
8M
u
sa DS1* STB i
2
.2
s Direction
U)
00 control 8212
s 0 Left to DS2 MD 0
Jj right
H
Both the 8212s are operated in mode 0. It may be noticed that they are connected as gated buffers.
When direction control = 0, DS1* becomes 0 for upper 8212, and so the weak logic signal on the left
gets transmitted to the right as a strong logic signal.
When direction control = 1, the output buffers of upper 8212 get tristated, but the output buffers
of lower 8212 get enabled as DS2 becomes 1. So the weak logic signal from the right gets transmit¬
ted to the left as a strong logic signal.
Interrupting input port and RSTn interrupt instruction port: Figure 19.9 illustrates these two appli¬
cations of 8212. The first 8212 is working as an interrupting input port. It performs data transfer with
the microprocessor, after interrupting it. The second 8212 supplies RSTn code to 8085 in response to
INTA* from 8085, when the first 8212 interrupts the 8085 on INTR pin.
When input device has some data on DI7_0, which is to be sent to the accumulator of 8085, it sends
a positive pulse on STB of first 8212. When STB makes a high to low transition, first 8212 latches the
information on DI7_0. Also the high to low transition of STB causes INT* to become 0. This interrupts
8085 on INTR pin after inversion.
In response to INTR, 8085 sends logic 0 on INTA*. The second 8212 receives it on DS1*. For the
second 8212, DS2 is tied to logic 1. On DI7_0 of this 8212, RST/i code is wired up. As STB of this
8212 is tied to logic 1, RSTn code is available in the latch of this 8212. Thus when INTA* becomes
0, it activates DS1*, and so the RSTn code comes out of the second 8212 and enters the IR register of
8085.
Now 8085 branches to n * 8. At this location, there is a jump instruction to jump to say, location
2500H. From location 2500H onwards the ISS for INTR is stored. During this ISS execution, IN 50H
instruction will be executed. This causes DS1* to become 0 and DS2 to become 1 for the first 8212
because of the chip select circuit shown in Fig. 19.9. Then the latched information in the first 8212
will enter the accumulator of 8085. Once the device selection is deactivated, CS signal makes a high
to low transition, which causes deactivation of INT* output of first 8212. This can be explained from
Fig. 19.10, which provides timing diagram for INT* output.
317
Data bus
/ \
First 8212 Second 8212 8085
STB DO7.0 1 ■» STB DO7-0 1 RST» 1 IR
Input Data \
device
DI7-0 00
MD 0 MD DS1* •e INTA* ro
0
10
Chip DS1* | Data |Acc « Z
-_
DI7-0
select
circuit DS2 INT*
RSTw
DS2 1
°§ TJ
7 INTR
O 0
to
{x> 0 aS
3- 3
Chip select circuit
3
&>
2
fp
0 A7 -
A(, £0
0 A5 *
A4 {>>
0 A3 * To DS1* of first 8212
0 A2
0 A, ■*
0 AQ %
IO/M*
■» To DS2 of first 8212
0 RD*
STB
SRQ
INT*
CS
In this case any of the three devices can interrupt the 8085 on INTR pin. So the 8085 does not know
the origin of the interrupt request. Thus, in the ISS for INTR, which is now called ILS (interrupt level
subroutine), using software the 8085 has to identify the device that needs service. It is essentially
checking the interrupt status of each of the devices, till the device that needs service is identified. This
process is called ‘polling’. Once the device that needs service is identified, branch should take place
to the appropriate ISS. However, polling is slow and thus the interrupt response becomes slow.
This problem can be solved to a limited extent in 8085. Upto eight devices are allowed to interrupt
on INTR pin and an 8212 supplies automatically a unique RSTrc instruction based on the interrupting
device. The interrupting device is identified by hardware in this case resulting in fast interrupt
response. This mechanism is illustrated in Fig. 19.12.
Let us say we have eight devices, device 0-7, which would like to perform interrupt-driven data
transfer with 8085. Let us say device 0 has the highest priority and device 7 the lowest priority. These
devices send their interrupt requests to the clock inputs of eight D-type flip-flops. As can be seen from
the figure, whenever a device generates an interrupt request, the Q output of the corresponding D flip-
flop is reset to 0. Initially, all these flip-flop outputs are set to 1 state using the ResetOut signal of
8085. These Q outputs are connected to the eight active low inputs of a 74148 priority encoder. In this
priority encoder X7 is the highest priority input and X0 is the lowest priority. As such, Q0 output is
connected to X7 input. If several inputs are active simultaneously for the priority encoder, the three
active low outputs on A2_o will depend on the highest priority input that is active. Thus, if all the inputs
are active, the output on A2_o will be 000 indicating that X7 is the highest priority input that is active.
The relevant truth table for 74148 is provided in Fig. 19.13.
The Q outputs of the D flip-flops are connected to a NAND gate. The output of the NAND gate is
connected to INTR pin of 8085. So whenever an interrupt request is generated by an I/O device, the
319
Data bus
8085
1 STB DO7-0 «
RSTn IR
1
0
CLR*
MD
DS2
8212 DS1*
lÿF
INTA*
Reset out
J~L
■{X>
_
s?
s?
N
-a
PI7 PI6 PI5 PI4 PI3 PI2 DII Dio Mono Intr !J
?z
TT
l l l l l
shot a. 3
3
B
0 NAND
A
1
ET X7 Qo DQ 0
74148 x6
A2 Clk
INTO
A1 |1 • Set* (Highest
priority)
AQ
GS Xo /ANDN
h i: Io CS1*
Oo*
Oi*
CS2*
02* 0
I CS3 74138 Q7 °7
3 to 8
decoder Clk
INT7
(Lowest
07* Set* priority)
AND
n
Fig. 19.12 8212 as supplier of eight RSTn instructions
corresponding Q output is reset to 0 causing the 8085 to be interrupted on INTR pin. The 8085 com¬
pletes the instruction that is being currently executed, and then generates INTA*.
The INTA* output of 8085 is connected to DS1* input of an 8212. For this 8212, MD pin is con¬
nected to logic 0. STB and CLR* are connected to logic 1. DS2 is connected to GS output of 74148
after inversion. Also the data inputs DI7, DI6, DI2, DI,, and DI0 are tied to logic 1. DI5, DI4, and DI3
are connected to the A2, A,, and A0 outputs of the priority encoder. The input on DI7_0 corresponds to
RSTn code, as explained in successive paragraphs. So when INTA* is activated, the 8212 sends out
on D07_q, the RSTn code present on DI7_0. Intel 8085 receives the RSTn code present on DO7_0 into
320
Vcc
Gnd 74LS148
8 to 3 line
X7-0 —
(active low)
priority
encoder
A2-0 (active low)
s EI*
16 pinDIP — > EO (enable output)
the IR register. Now 8085 branches to n* 8. At this location there is a jump instruction to jump to say,
location 3500H. From location 3500H onwards the ISS for INTR when device ‘n’ needs service is
stored.
It may be noted that the code for an RST instruction is 1 lnnnl 11, where nnn can have values from
000 (for RST0) to 111 (for RST7). Thus, the data inputs of the 8212 have RSTrc code depending on
priority encoder output. If device 0 interrupt request occurs, the 8212 will have at its data inputs the
code for RST 0. If device 4 interrupt request is currently the highest priority interrupt among the inter¬
rupt requests that are active, 8212 will receive RST4 code at its data inputs. If only device 7 interrupt
request is currently active, 8212 will receive RST7 code at its data inputs.
After device n interrupts the 8085 and the 8085 branches to the appropriate ISS, the corresponding
Q output of the D flip-flop should be set to 1, so that INTR becomes deactivated. This is achieved
using the 74138 (3 to 8 decoder) chip. It has three active high inputs I2, Ii, and I0. These are driven by
A2, Ah and A0 outputs of priority encoder. It has eight active low outputs O0_7. If A2, A,, A0 = 000,
then O0 of 74138 will become logic 0, which sets Q0 to 1. Similarly, if A2, Ab A0 = 111, then 07 of
74138 will become logic 0, which sets Q7 to 1. The monoshot in the figure ensures that Q output
of D flip-flop is set to 1 only after 8085 has received the RSTn instruction. Connection of GS output
of 74148 to CS2* input of 74138 ensures that 74138 is selected only when valid A2, Ab A0 values are
output by 74148.
321
1 9.2.2 APPLICATIONS OF 821 2 IN MODE 1
Intel 8212 as low-order address latch: In 8085, low-order address lines and data lines are multi-
plexed and are available as AD7_0. Many times it is convenient to have address and data separated on -a
_ to
“2 §o
different lines. For example, if we want to connect a chip like 2716 (2KX8 EPROM) in a 8085-based !Jz
system, the 2716 needs 11-bit address on its address pins, and it has separate 8 pins for data. If we ? g g
connect the AD7_0 pins of 8085 to the LS 8 address pins of 2716, the address will be available for only 3
B
one clock cycle in a machine cycle. Thus, demultiplexing of address and data by latching the
low-order address becomes necessary. This can be achieved by using the 8212 as a latch as shown in
Fig. 19.14.
Address bus
In this case, 8212 is used in mode 1. So DO7_0 lines are always enabled. DS1* is tied to 0. DS2 is
connected to the ALE output of 8085. When address is present on AD7_0, ALE signal is pulsed by 8085
during the first clock cycle T1 of any machine cycle (except BI machine cycle). This causes activa¬
tion of DS2, and the address information present on DI7_0 comes out on DO7_0. After this first clock
cycle in the machine cycle ALE goes to 0. This results in latching of address information in 8212.
DO7_0 is always enabled in mode 1, and so the latched address information is continuously available
on D07_O for the entire machine cycle. This is connected to the address bus.
Only during the second clock cycle T2 of a machine cycle, RD* or WR* signal is activated by
8085. By that time the information on AD7_0 will be data, and hence they are connected to the data bus
of the microcomputer.
Intel 8212 as an interrupting output port: When an output device desires to perform interrupt-
driven data transfer with 8085, it sends a positive going pulse on STB pin of 8212, as shown in
Fig. 19.15.
322
Data bus
/f \
A7-0
High to low transition on STB results in activation of INT*. This results in 8085 getting interrupted
on RST5.5 line as per the figure. Program control is then transferred to location 5.5 * 8. In this
location we have a jump instruction to jump to say 2600H. So from 2600H the ISS for RST5.5 starts.
During this ISS, OUT 35H instruction will be executed, which momentarily makes DS1* =0 and
DS2 = 1 for the 8212-causing internal CS signal to be pulsed. Then the accumulator contents enter
the 8212 and is output to the output device. Also the INT* is deactivated. A little later, execution of
the ISS is completed and control returns to the main program.
P: Peripheral Interface
Chip
■ Description of 8255 PPI
• Interface with microprocessor
• Interface with I/O devices
■ Operational modes of 8255
■ Control port of 8255
• Mode definition control word
• Port C bit set/reset control word
■ Mode 1 — strobed I/O
Interrupt-driven and status check data transfers
•
• Interrupt-driven input operation
• Interrupt-driven output operation
• Status check input operation
• Status check output operation
■ Mode 2 — bi-directional I/O
Interrupt-driven bi-directional operation
•
• Status check bi-directional operation
■ Questions
In the previous chapter non-programmable I/O port chip 8212 was discussed. However, programmable
peripheral chips are more popular because their setting can be changed by the program, without
recourse to change in wiring or hardware. A very popular programmable I/O port chip is the Intel 8255.
This will be discussed in detail in this chapter.
Intel 8255 is a programmable peripheral interface (PPI) chip. It means that it is a programmable chip
used for interfacing or connecting peripheral devices. Peripheral device is another name for I/O
324
device. Also, it is well known that I/O ports are used for connecting I/O devices. Thus, in very simple
words, 8255 is a programmable I/O port chip.
The 8255 is available as a 40-pin chip in a dual in line package. Figure 20.1 illustrates the functional
pin diagram of 8255. The actual pin diagram of 8255 is provided in Fig. 20.2. Figure 20.3 provides a
simplified architecture of 8255. It works on a power supply of +5 V dc. It has two programmable I/O
s ports each of 8 bits in size, and two programmable I/O ports each of 4 bits in size. They are called Port
A, Port B, Port C upper, and Port C lower, respectively. These port pins have the ability to source 1 mA
a>
o of current at 1.5 V, when programmed to work as output pins. This provides the capability of directly
sa driving Darlington transistors for applications, such as printers and high-voltage displays.
2
.2
s
in
00
r vcc Port A \ FA7-0
Gnd
S / \
* D7-0' PortC
H
Interface RD* upper PC7_4 Interface
Control
with WR* port PortC
V with
micro PC3-0 I/O devices
processor CS* lower
Reset
Fig. 20.1
At Port B PB7-0 Functional pin diagram
A0 of 8255
PA3ÿ l —
40 5 > PA4
PA7 ) — 2 39 5-4 PA5
PAI<-S 3 38
PAQ-5-) 4 37 5->PA7
RD* 5 36 WR*
CS* — 1 6 35 Reset
Gnd 7 34 C4 D0
A1 8 33 5— > D|
A0-) 9 —
32 5 > D2
PC7-5-> 10 31 6-4 D3
PC6ÿ 1 1 —
30 5 D4
29 5-4 D5
PC5<-» 12
PC4 <-4 13 28 D6
PC0<-> 14 27 D7
PC, -5-4 15 26
PC2<-» 16 25 6-4 PB?
PC, -5-4 17 24 5-4 PB(>
PB0ÿ 18 23 5-4 PB5
PB, 44 19 22 6-4 PB4
Fig. 20.2
PB2-5-4 20 21 H- PB3
Pin diagram of 8255
Port C upper and Port C lower are addressed as if they constitute a single 8-bit port. This can be
verified from Table 20.1, which indicates port selection. Thus Port C can be thought of as being
divided into two portions of 4 bits each. They can be independently programmed as input or output
lines. So, for example, Port C lower can be programmed as input and Port C upper can be programmed
as output. However, in most applications Port C is connected to an 8-bit input device or output device.
In such cases both portions of Port C are programmed for the same function.
325
VCC(+5V)
ID7-0
Gnd ID7-0 PA7-0
Data Port A
/ bus
buffer a
D7-0 PB7-0
01
Control port PortB /
/
1D7-0
I
MS bit
V
I3 \ PortC /
PCM
\
?!
1c
■[
PortC
Mode LS / upper \ / 5T 2
bit 7 bits - 1D7-4
SH—
definition T3
set/reset
control PortC
control
I 7
ID3-0
lower PC/I I PC3-ox
\ T>
%
CS*
*Port A IE flip-flop for output
**Port A IE flip-flop for input
i.
RD* Read/write ***Port B IE flip-flop.
WR* control
At logic
A0
Reset-
Also any line of Port C, which is programmed as output can be set to logic 1, or reset to logic 0
using the single bit set/reset feature of Port C also. This feature reduces software requirement in
control-based applications. This facility is provided only for Port C. This feature is also used for
enabling/disabling interrupts from 8255 ports, as will be discussed later.
The functionality of these three ports is decided by the contents of the control port. The control port
can only be written by the microprocessor. Intel 8085 cannot read it. Thus, there are three ports which
can be used for I/O operations, and a control port to control the function of these ports. A port inside
the 8255 is selected for communication by the 8085 by the address-input pins A! and A0, as shown in
Table 20.1. The direction of data transfer is dictated by the RD* and WR* input signals. Of course,
the 8255 chip should be first of all selected by activation of CS* signal before a port inside 8255 can
be selected. For example, the control port is written with the contents sent out by 8085 on D7_0 pins
of 8255 when CS* = 0, WR* = 0, A! = 1, and A0 = 1. Thus, A! and A0 together with RD*, WR*,
and CS* decide the manner in which 8085 communicates with 8255.
326
8255
0 A15
0 A[4
1 Al3 -|>>
0 Al2 OR cs*
0 An Ag Ai
eM 0 A'O
0 1 IO/M A8 A0 Fig. 20.4
2a. 8255 connected as
2 The address pins in the above circuit could be A7_o instead of Ai5_8 l/O-mapped I/O
.2
S
in
The 8255 can be connected in a microcomputer system as either memory-mapped I/O or I/O-
mapped I/O. Suppose we want 8255 connected as I/O-mapped I/O with addresses of Port A, Port B,
« Port C, and control port as 20H, 21H, 22H, and 23H, respectively. Then one of the possible chip select
H circuits is shown in Fig. 20.4. In this figure A7_0 could have been used instead of A15.8.
Similarly, suppose we want 8255 connected as memory-mapped I/O with addresses of Port A,
Port B, Port C, and control port as FFFCH, FFFDH, FFFEH, and FFFFH respectively. Then one of
the possible chip select circuits is shown in Fig. 20.5. In this figure A7.0 is the LS byte of address
generated using 8212 or 74LS373 as an address latch.
1 Ais -
1 A14 -
1 A13 -
1 A, 2 - 8255
1 An -
1 Aio -
1 A9 -
1 Ag - CS*
1 A7 -
Ai
1 A6 -
1 As - Ao
1 A4 -
1 A3 - Fig. 20.5
1 A2 - 8255 connected as
0 IO/M* H> memory-mapped I/O
The pins of 8255 that are used for interfacing with a microprocessor are described in the following:
CS*: It is an active low input pin for 8255. If this pin is at logic 0, the 8255 chip is selected
for communication with the microprocessor. If the chip is not selected the data lines
D7_0 of 8255 will be in tristate.
These pins are the data pins, which are used by 8255 for communication with the
microprocessor. They are connected to the data bus of the microcomputer system.
RD*: It is an active low input pin for 8255. It is connected to RD* output of 8085. The 8085
activates the RD* input of 8255 when it wants to read the data present in a port of 8255.
WR*: It is an active low input pin for 8255. It is connected to WR* output of 8085. The 8085
activates the WR* input of 8255 when it wants to write data to a port of 8255.
AI, A0: These are address-input pins. They select one of the ports inside 8255 for communi¬
cation with the microprocessor, as indicated in Table 20. 1.
327
Reset: It is an active high input pin. It is connected to ResetOut output of 8085. It is used to
reset the 8255. After a reset of 8255, all the three ports of 8255 work as input ports in
mode 0, which is the simplest mode of operation. Operational modes of Ports are
described later.
a
20.1 .2 INTERFACE WITH I/O DEVICES in
The pins of 8255 that are used for interfacing with I/O devices are described in the following. ?!a
i
5T S
PA7.0:
PB7.0:
These eight pins are used by the 8255 for communicating with an I/O device. These
pins are output pins if Port A is programmed as an output port. They are input pins if
Port A is programmed for input operation.
These eight pins are used by the 8255 for communicating with an I/O device. These
il
pins are output pins if Port B is programmed as an output port. They are input pins if
%
Port B is programmed for input operation. 3
PC7.4: These four pins are used by the 8255 for communicating with an I/O device. These
pins are output pins if Port C upper is programmed as an output port. They are input
pins if Port C upper is programmed for input operation.
PC3.0: These four pins are used by the 8255 for communicating with an I/O device. These
pins are output pins if Port C lower is programmed as an output port. They are input
pins if Port C lower is programmed for input operation.
However, Port C pins can have other functions assigned to them in some cases as will be described
next.
Intel 8255 supports three modes of operation. They are mode 0, mode 1, and mode 2.
Mode 0 is called simple I/O or basic I/O, as it is the simplest mode of operation. All of the I/O ports
in 8255 are capable of being programmed to work in mode 0. This mode is used with I/O devices whose
timing characteristics are clearly known. For example, if we have an input device that wants to send to
the microprocessor a byte every 0.5 s, we can execute an IN instruction every 0.5 s to receive the
information. In this mode the port inputs are not latched. Thus, the input device must continue with the
data on port pins till the port data is read by the microprocessor. So it is useful for reading switch set¬
tings, but not useful for reading from a keyboard. Similarly, if we have an output device that wants to
receive from the microprocessor a byte every 0.25 s, we can execute an OUT instruction every 0.25 s to
send the information. In this mode the port outputs are latched. Thus, the microprocessor is not required
to continuously send the data to the port till the output device receives the port data. It is useful, as an
example, for sending data to LED display that updates the display based on the latched output.
Mode 1 is called strobed I/O or handshake I/O. This mode is useful when, for example, an input
device supplies data to the microprocessor at irregular intervals. In such a case the input device must
somehow tell the input port that new data has entered the port. Then the port must inform the proces¬
sor to read the data. Finally once the processor has read the data, the port must inform the input device
that the processor has already read the data. These informations are provided by signals called ‘hand¬
shake signals’. It is something like the handshake between two strangers, to come to know about each
328
other before they enter into a dialogue. A port programmed to function in mode 1 uses three hand¬
shake signals. Port C provides these handshake signals. Only Port A and Port B can work in mode 1.
Port A uses three lines of Port C for handshaking purposes. Port B uses another three lines of Port C
for handshaking. Remaining two lines of Port C can be used for simple I/O in mode 0. If only Port A
or Port B is working in mode 1, then five lines of Port C are free for use in mode 0.
s In mode 0 or mode 1, a port is required to work as an input port or as an output port. It depends
on whether an input device or an output device is connected to the port. In contrast with this, mode
a>
o 2 is called bi-directional handshake I/O. It is useful when the microprocessor sometimes desires to
sa receive information, and at some other times desires to send information to the I/O device con¬
2 nected to 8255. An example is communication with a floppy disk controller card. As mode 2 is bi¬
.2
s directional handshake I/O, it needs more handshake lines than the unidirectional mode 1 strobed
in
oo I/O. Thus, mode 2 operation makes use of five lines of Port C for handshaking purposes. Only Port
s A can work in mode 2. The remaining three lines of Port C are used for handshaking if Port B is in
Jj mode 1. However, if Port B is functioning in mode 0, these three lines of Port C are used for
H simple I/O.
In view of this it can be concluded that:
When the 8255 is reset Port A, Port B, and Port C are initialized to work as input ports in mode 0.
This is done to prevent destruction of circuitry connected to a port to which an input device is con¬
nected. If ports were initialized as output ports after reset or power-on, a port might try to output to
an input device, which might destroy the port, and/or the input device. However, when ports are ini¬
tialized as input ports, even if output devices are connected to the ports there cannot be any damage
to the ports or the output devices.
The contents of the control port decide the 8255 configuration, that is, the way in which the 8255
is programmed to work.
There are two types of command words or control words in 8255. They are:
Both these are written to the control port only. From the point of view of the microprocessor there
is a single 8-bit control port, which is selected when CS* = 0, WR* = 0, A! = 1 and A0 = 1. But
internally there are two control ports, one for mode definition control and another for Port C bit
set/reset control. The contents of the control port get latched in mode definition control port if the MS
bit of control port = 1. If the MS bit of control port = 0, the contents of the control port gets latched
in Port C bit set/reset control port. This can be seen from Fig. 20.3.
329
20.3.1 MODE DEFINITION CONTROL WORD
7 6 5 4 3 2 1 0 Bit number
j»
1 MA2 MAI PAI PCul MB PBI PC1I in
After a while if we want to send out logic 0 on PC0 without affecting other lines of Port C, we have
to execute the following instructions.
IN 22H
ANI 11111110B
OUT 22H
Intel 8255 provides an alternative way of sending out logic 1 or 0 on any pin of Port C that is
configured as an output pin. It is done using the single bit set/reset feature of Port C. This feature
reduces software requirement in control-based applications. This facility is provided only for Port C.
Figure 20.7 explains the Port C bit set/reset control word.
7 6 5 4 3 2 1 0 Bit number
Bit 7 must be 0 to indicate that the control port contains Port C bit set/reset control word. Bits 3,2,
and 1 select a bit of Port C that is to be set or reset. Bit 0 decides whether the selected bit of Port C is
to be set or reset. Bits 6, 5, and 4 are not used in this control word. They are generally loaded with 000.
Thus, the meaning for the various bits of control port when it contains Port C bit set/reset control
word is as follows.
Thus, to reset PC0 using the Port C bit set/reset feature we have to execute only the following two 8|
instructions.
MVI A, 0 000 000 0B
!?!
OUT 23H 0
%T
Similarly, the following two instructions set PC2.
3
MVI A, 0 000 010 IB
OUT 23H
The main use of Port C bit set/reset control word is for enabling or disabling Port A and Port B
interrupts when they work in strobed mode operation. This will be discussed later.
Mode 1 is called strobed I/O or handshake I/O. This mode is useful when an input device supplies data
to the microprocessor at irregular intervals or an output device desires data from the microprocessor
at irregular intervals. A port programmed to function in mode 1 uses three handshake signals. Port C
provides these handshake signals. Only Port A and Port B can work in mode 1.
PC2, PCi, and PC0 pins provide the handshake signals for Port B when it is configured as hand¬
shake input port or output port. Similarly, PC5, PC4, and PC3 pins provide the handshake signals for
Port A when it is configured as handshake input port. However, PC7, PC6, and PC3 pins provide the
handshake signals for Port A when it is configured as handshake output port. Note that PC3 pin will
be a handshake line for Port A in input and also output operations. If both Port A and Port B work in
mode 1, the remaining two pins of Port C can be used for simple I/O in mode 0. If only Port A or Port
B is working in mode 1, then five pins of Port C are free for use in mode 0.
Example 1: Configure Port A as strobed input port, Port B as strobed output port, and PC7, PC6
as output lines.
The required mode definition control word is shown in Fig. 20.8.
7 6 5 4 3 2 1 0 Bit number
1 0 1 1 0 1 0 X
S Example 2: Configure Port A as strobed output port, Port B as strobed input port, and PC5, PC4
in
00
as input lines.
s The required mode definition control word is shown in Fig. 20.9.
Jj
H
7 6 5 4 3 2 1 0 Bit number
1 0 1 0 1 1 1 X
Note that bit 3 must be an 1 to indicate that PC5 and PC4 are input lines. In this case it does not
mean that the entire Port C upper is input, as PC7 and PC6 are used for handshaking purposes.
The instructions to achieve this requirement assuming the chip select circuit of Fig. 20.4 are as
follows.
MVI A, AFH; Treating X as 1
OUT 23H
Among the three pins provided by Port C to a port for the purpose of handshake data transfer, one of
them called INT is used for interrupting the microprocessor. Actually Port C provides INTA as inter¬
rupt from Port A and INTB as interrupt from Port B. These interrupt request outputs of 8255 can be
inhibited by resetting to 0 the associated El flip-flop or enabled by setting to 1 the associated El flip-
flop. This is accomplished by Port C bit set/reset control function. This function allows the user to
allow or disallow a specific peripheral to interrupt the 8085, without altering the general interrupt
structure.
For example, let us say INTB is connected to interrupt the 8085 on TRAP pin. Then whenever the
TRAP pin is activated, the 8085 is always interrupted. But at some point in the program we may not
want the 8085 to be interrupted from Port B. At that point even if DI instruction is executed, it cannot
disable the TRAP interrupt. Other interrupts of 8085 also get disabled. But our interest may be to dis¬
able only TRAP. To solve this problem, facility is provided in 8255 to disable a Port interrupt, for
example, from Port B. Then the INTB line is never activated, and so the TRAP pin is never activated.
As another example, let us say INTA is connected to interrupt the 8085 on INTR pin. Then when¬
ever the INTR pin is activated, the 8085 is interrupted. But at some point in the program we may not
want the 8085 to be interrupted from Port A. At that point if the DI instruction is executed, it will
disable all the interrupts except TRAP. But our interest may be to disable only INTR. To solve this
333
problem, facility is provided in 8255 to disable a Port interrupt, for example, from Port A. Then the
INTA line is never activated, and so the INTR pin is never activated.
If Port interrupt is enabled, then the data transfer can be performed in the interrupt-driven mode. If
Port interrupt is disabled, we have to resort to status check data transfer.
When Port B is programmed to work in mode 1 (input or output), if PC2 bit is set to 1, Port B El
flip-flop will be set to 1 thus enabling Port B interrupt. This can be seen from Fig. 20.3. It is to be a
noted that when Port B works in mode 1, PC2 pin will receive an input handshake signal. The design in
is such that the logic value on this pin does not enter PC2 bit. So the PC2 bit can be set or reset using jT 5
Port C bit set/reset control without regard to the logic value on PC2 pin. * is
“
If Port B is working in mode 1 input and PC2 is set to 1, INTB will be activated when Port B is 5T s
filled with new data by the peripheral. If PC2 bit is reset to 0, even if new data is supplied by the
peripheral, INTB will not be activated.
If Port B is working in mode 1 output and PC2 bit is set to 1, INTB will be activated when the
peripheral empties data from Port B. If PC2 bit is reset to 0, then even if the peripheral empties data
ti
0
Let us say we have an input device that supplies data at irregular intervals. Then we connect it to say
Port B of 8255, which is configured to work in mode 1. Figure 20.10 depicts the details. Figure 20.11
provides the timing diagram for this data transfer.
It makes use of three handshake signals supplied by Port C. STB* is an input pin to 8255, and IBF
and INT are output pins of 8255 as described in the following.
STB*: It is an active low STRobe input pulse to the 8255. The peripheral sends a low-going
pulse with a minimum width of 500 ns on this input of 8255 whenever it has data to
send to the port. When STB makes logic 0 to logic 1 transition the peripheral data on
the port pins are latched by the port buffer. The data on the port pins should be held
for atleast 180 ns after this transition. For Port A, the signal is called STBA*, and for
Port B it is called STBb*. This handshake pin is the same as PC4 for Port A and PC2
for Port B.
IBF: It is an active high output pin of 8255. It indicates input buffer full status to the periph¬
eral and the microprocessor. The buffer is called an input buffer, as it is a strobed input
operation. IBF goes to logic 1 within 300 ns after STB* goes to logic 0. IBF goes to
logic 0 within 300 ns after the port data is read by the microprocessor. For Port A, the
signal is called IBFA, and for Port B it is called IBFb. This handshake pin is the same
as PC5 for Port A and PC! for Port B.
INT: It is an active high output pin of 8255. This signal is used to interrupt the microprocessor
if interrupt-driven data transfer is desired. If status check data transfer is contemplated,
this pin is left open without connecting to an interrupt pin of the microprocessor. INT goes
334
Data bus
8255
AD7.0 D7-0
Ag ■» AI Port B
0 Data
w Data Acc Input
w Data
0) As 3- AQ \ device
o PB7-0
0
L-
2300H
IN 2III
>ISSfor RST6.5
El
RET
STB* 500 ns
min
<ÿ ■»
•<-300 ns — > 300 ns max
max
IBF
<ÿ
300 ns 400 ns
max max
INT <ÿ ■»
180 ns
mins
Data from
peripheral
RD*
Let us say we have an output device that needs data at irregular intervals. Then we connect it to say
Port B of 8255, which is configured to work in mode 1. Figure 20.12 depicts the details. Figure 20.13
provides the timing diagram for this data transfer.
It makes use of three handshake signals supplied by Port C. ACK* is an input pin to 8255, and
OBF* and INT are output pins of 8255 in the description that follows.
336
Data bus
8255
AD7_0 D7-0
Port B PB7_o
0 A9 Ai
w Data Output
0)
| Data | A As -> AQ
device
o
0
OBFH ■»
a Chip select cs*
0 ->
circuit as AC KB (r
.2 in Fig. 21.4
2
w RST5.5 4 INTB
00
o 8085
00
0)
£
H
2400H
OUT 21FI
>ISS for RST5.5
El
RET
ACK* 300 ns
min
4-
350 ns 4
max 850 Ins
INT ma;x
Data from
port
WR*
INT: It is an active high output pin of 8255. This signal is used to interrupt the microprocessor %T
if interrupt-driven data transfer is desired. If status check data transfer is contemplated, 3
this pin is left open without connecting to an interrupt pin of the microprocessor. INT goes
to logic 1 within 350 ns after ACK* goes to logic 1. This is so, provided that the OBF*
signal is inactive and port interrupt is enabled by setting to 1 the El flip-flop associated
with the port. After a reset of 8255, Ports A and B interrupts are disabled. They are also
disabled following the execution of mode definition control word.
INT goes to logic 0 within 850 ns after the WR* input signal goes to logic 0 for the pur¬
pose of writing data to the port by the microprocessor. This is true if the Port interrupt was
enabled. If Port interrupt is disabled, INT output will always be in logic 0.
For Port A, the interrupt signal is called INTA, and for Port B it is called INTB. This
handshake pin is same as PC3 for Port A and PC0 for Port B.
From this it should be clear that PC2, PCb and PC0 are the handshake signals for Port B when it is
configured as handshake output port. Note that the same pins were the handshake signals in input
mode also. Similarly, PC7, PC6, and PC3 are the handshake signals for Port A when it is configured as
handshake output port. Note that the handshake signals for Port A differ in input and output modes
except for PC3, which is INTA in both input and output modes.
For the interrupt-driven output operation, the port interrupt should be enabled. If we want Port B
in mode 1 output (without bothering about other ports), we have to use mode definition control as
shown in the following. The chip select circuit for 8255 is assumed to be as in Fig. 20.4.
MVI A, 1XXXX10XB; X' s are don' t care bits
OUT 23H
Then to enable Port B interrupt, PC2 bit is required to be set to 1 using Port C bit set/reset control
as follows.
MVI A, 0 XXX 010 IB; X' s are don' t care bits
OUT 23H
The interrupt-driven handshake data transfer takes place as follows. Whenever the peripheral wants
to receive data from the port, it senses the OBF* output of 8255. If OBF* = 0, indicating that the
output buffer is full, it sends a low-going pulse on ACK*. By the time ACK* makes a 0 to 1 transi¬
tion, the data in the port would have been received by the output device. By this time OBF* output is
338
deactivated indicating that the output buffer has become empty. Also INT output becomes logic 1.
In Fig. 20.12, the 8085 is interrupted on RST5.5 pin because of INT output. It is essentially indicating
to the 8085 that it has to send data to the port of 8255.
Intel 8085 finishes execution of the current instruction. Then it stores the contents of the PC on the
stack top, and branches to memory location 5.5 X 8 = 44 = 002CH. This of course is true if inter¬
rupts are enabled, RST5.5 is unmasked, and finally RST6.5, RST7.5, and TRAP, which are the higher
8 priority interrupts, are not active. At location 002CH there will be a jump instruction to say 2400H,
a>
o where the actual ISS starts. In the ISS the 8085 executes OUT 21H, assuming Port B address to be
sa 21H. This results in activation of CS* and WR* inputs of 8255. Also A, becomes 0 and A0 becomes
s
.2 1 thus selecting Port B. Thus the Port B is written by the 8085 in the ISS. Since the interrupting port
S is serviced, INT output of 8255 is deactivated. As the output buffer has been filled by the 8085, the
in
00 OBF* output is activated. This completes the handshake data transfer by the output device in the ISS.
s
Jj
20.4.4 STATUS CHECK INPUT OPERATION
For this type of data transfer, the Port interrupt should be disabled. If we want Port B in mode 1 input
(without bothering about other ports), we have to use mode definition control as shown in the follow¬
ing. The chip select circuit for 8255 is assumed to be as in Fig. 20.4.
MVI A, 1XXXX11XB; X' s are don' t care bits
OUT 23H
Then to disable Port B interrupt, PC2 bit is required to be reset to 0 using Port C bit set/reset con¬
trol as follows.
MVI A, 0 XXX 010 0B; X' s are don' t care bits
OUT 23H
In fact it does not matter even if these two instructions are not executed. This is because after exe¬
cution of mode definition control the Port interrupts are automatically disabled.
In this type of data transfer the 8085 is not going to be interrupted by the 8255. So the 8085 has to
perform a status check of 8255 to decide whether data transfer has to be performed or not. Port C pro¬
vides status information about Ports A and B when they are in handshake mode. The details are shown
in Fig. 20.14.
7 6 5 4 3 2 1 0 Bit number
Bits PC2, PCi, and PC0 provide status information about Port B when Port B works in mode 1. If
Port B is working in mode 0, these bits of Port C are available for use in simple I/O. Note that bit 2
indicates El status for Port B, and not STB* or ACK* status of Port B. Among these three status bits,
the most useful one is bit 1, which provides IBFB/OBF*B information. If Port B is working in mode 1
input and if this bit = 1, it means that the Port B input buffer is full, and so 8085 is required to read
data from Port B. Similarly, if Port B is working in mode 1 output and if this bit = 1, it means that
339
the Port B output buffer is empty, and so 8085 is required to send data to Port B. Thus, whenever bit 1
of Port C is 1, the 8085 is required to perform data transfer with Port B.
Bits PC5, PC4, and PC3 provide status information about Port A when Port A works in mode 1 input.
Then bits PC7 and PC6 are available for use in simple I/O. Note that bit 4 of Port C indicates El sta¬
tus for Port A in input mode, and not STB* status of Port A. Among these three status bits, the most
useful one is bit 5, which provides IBFA information. If Port A is working in mode 1 input and if this j»
bit = 1, it means that the Port A input buffer is full, and so 8085 is required to read data from Port A. in
Bits PC7, PC6, and PC3 provide status information about Port A when Port A works in mode 1 out- jT 5
put. Then bits PC5 and PC4 are available for use in simple I/O. Note that bit 6 indicates El status for ? «
Port A in output mode, and not ACK* status of Port A. Among these three status bits, the most useful o §j
one is bit 7, which provides OBF*A information. If Port A is working in mode 1 output and if this bit * 3
= 1, it means that the Port A output buffer is empty, and so 8085 is required to send data to Port A. 3! E
If Port A is working in mode 0, all the five MS bits of Port C are available for use in simple I/O.
The status check handshake data transfer in input mode takes place as follows. Whenever the 0
peripheral has data to be sent to Port B, it senses the IBFB output of 8255. If IBFB = 0, indicating that %
T
the Port B input buffer is empty, it sends a low-going pulse on STB*b. When STB*B makes a 0 to 1 3
transition, the data on Port B pins are latched by the input buffer. By this time IBFB output is activated
indicating that the input buffer has become full.
However, INTB output remains at logic 0 because Port B interrupt is disabled. Thus in this case the
8085 has to read the IBFB status of 8255 to decide whether it is required to perform data transfer with
Port B or not. If the chip select circuit for 8255 is assumed to be as in Fig. 20.4, the following pro¬
gram segment is executed to perform status check data transfer with Port B in mode 1 input.
WAIT: IN 22H; Read Port C to check if IBFB has become 1.
ANI 00000010B; Reset all bits of Accumulator except bit 1
JZ WAIT; If IBFB = 0 then wait till it becomes 1
IN 21H; Now read from Port B
For this type of data transfer, the Port interrupt should be disabled. If we want Port A in mode 1
output (without bothering about other ports), we have to use mode definition control as shown in the
following. The chip select circuit for 8255 is assumed to be as in Fig. 20.4.
MVI A, 1010XXXXB; X' s are don' t care bits
OUT 23H
Then to disable Port A interrupt, PC6 bit is required to be reset to 0 using Port C bit set/reset
control as follows.
MVI A, 0 XXX 110 0B; X' s are don' t care bits
OUT 23H
In fact it does not matter even if these two instructions are not executed. This is because after exe¬
cution of mode definition control the Port interrupts are automatically disabled.
The status check handshake data transfer in output mode takes place as follows. Whenever the
peripheral desires to receive data from Port A, it senses the OBF*A output of 8255. If OBF*A = 0, indi¬
cating that the output buffer is full, it sends a low-going pulse on ACK*a. By the time ACK*A makes
a 0 to 1 transition, the data in the port would have been received by the output device. By this time
OBF*A output is deactivated indicating that the output buffer has become empty.
340
However, INTA output remains at logic 0 because Port A interrupt is disabled. Thus in this case the
8085 has to read the OBF*A status of 8255 to decide whether it is required to perform data transfer
with Port A or not. If the chip select circuit for 8255 is assumed to be as in Fig. 20.5, the following
program segment is executed to perform status check data transfer with Port A in mode 1 output. The
data to be sent to Port A is assumed to be at location DATA.
8M WAIT: IN 22H; Read Port C to check if OBF* A has become 1
ANI 10000000B ; Reset all bits of Accumulator except bit 7
0
S JZ WAIT; If OBF*A = 0 then wait till it becomes 1
a LDA DATA
s
.2 OUT 21H; Now send new data to Port A
S
in
00
s
1H m 20.5 MODE 2 -BI-DIRECTIONAL I/O
In mode 0 or mode 1, a port is required to work as an input port or as an output port. It depends on
whether an input device or an output device is connected to the port. In contrast with this, mode 2 is
called bi-directional handshake I/O. It is useful when the microprocessor sometimes desires to receive
information, and at some other times desires to send information to the I/O device connected to 8255.
An example is communication with a floppy disk controller card. As mode 2 is bi-directional hand¬
shake I/O, it needs more handshake lines than the uni-directional mode 1 strobed I/O. Thus, mode 2
operation makes use of five lines of Port C for handshaking purposes. These five lines of Port C are
PC7 to PC3. For the purpose of input operation, PC5, PC4, and PC3 pins provide the handshake signals
for Port A when it is configured as mode 2. For the purpose of output operation, PC7, PC6, and PC3
pins provide the handshake signals for Port A when it is configured as mode 2. Note that PC3 pin will
be a handshake line for Port A in mode 2 for input as well as output operations. Only Port A can work
in mode 2. The remaining three lines of Port C are used for handshaking if Port B is in mode 1. How¬
ever, if Port B is functioning in mode 0, these three lines of Port C are used for simple I/O.
Example 1: Configure Port A as bi-directional I/O port, Port B as basic input port, and PC2_o as
output lines.
The required mode definition control word is shown in Fig. 20.15.
7 6 5 4 3 2 1 0 Bit number
I I X X X 0 1 0
Bit 0 must be a 0 to indicate that PC2_o are output lines. In this case it does not mean that the entire
Port C lower is output, as PC3 is used for handshaking purposes.
It is to be noted that the bit 3 is a don’t care. This is because all the four lines of Port C upper are used
for handshaking purposes some of which will be input pins and some others will be outputs. The 8255
will automatically configure these handshake pins, and so it is unimportant whether bit 3 is a 0 or 1.
341
Bit 4 is a don’t care because Port A is required to work in bi-directional mode. The instructions to
achieve this requirement assuming the chip select circuit of Fig. 20.4 are as follows.
MVI A, C2H; Treating X as 0
OUT 23H
Example 2: Configure Port A as bi-directional I/O port, and Port B as strobed input port. a
The required mode definition control word is shown in Fig. 20.16. in
7 6 5 4 3 2 1 0 Bit number
Hs
5T
is
Fig. 20.16
1 1 X X X
Let us say we have an I/O device, which supplies/receives data at irregular intervals. Then we connect
it to Port A of 8255 that is configured to work in mode 2.
If we want Port A in mode 2 (without bothering about other ports), we have to use mode definition
control as follows. The chip select circuit for 8255 is assumed to be as in Fig. 20.4.
MVI A, 11XXXXXXB; X' s are don' t care bits
OUT 23H
Then to enable Port A interrupt for input operation, PC4 bit is required to be set to 1 using Port C
bit set/reset control as follows.
MVI A, 0 XXX 100 IB; X' s are don' t care bits
OUT 23H
The interrupt-driven handshake data transfer from Port A to 8085 takes place as was explained
earlier for mode 1 interrupt-driven input operation.
Similarly, to enable Port A interrupt for output operation, PC6 bit is required to be set to 1 using
Port C bit set/reset control as follows.
MVI A, 0 XXX 110 IB; X' s are don' t care bits
OUT 23H
The interrupt-driven handshake data transfer from 8085 to Port A takes place as was explained
earlier for mode 1 interrupt-driven output operation.
342
20.5.2 STATUS CHECK BI-DIRECTIONAL OPERATION
First of all to configure Port A in mode 2 (without bothering about other ports), we have to use mode
definition control as shown below. The chip select circuit for 8255 is assumed to be as in Fig. 20.4.
MVI A, 11XXXXXXB ; X' s are don' t care bits
0
OUT 23H
HI
E For this type of data transfer, Port A interrupt should be disabled. To disable Port A interrupt for
a input operation, PC4 bit is required to be reset to 0 using Port C bit set/reset control as follows.
E
.2
2 MVI A, 0 XXX 100 OB; X' s are don' t care bits
in OUT 23H
Similarly, to disable Port A interrupt for output operation, PC6 bit is required to be reset to 0 using
Port C bit set/reset control as follows.
MVI A, 0 XXX 110 0B; X' s are don' t care bits
OUT 23H
In this type of data transfer, the 8085 is not going to be interrupted by the 8255. So the 8085 has
to perform a status check of 8255 to decide whether data transfer has to be performed or not.
Bits PC5, PC4, and PC3 provide status information about Port A in mode 2 input operation. Among
these three status bits, the most useful one is bit 5, which provides IBFA information. If this bit = 1,
it means that the Port A input buffer is full, and so 8085 is required to read data from Port A.
Bits PC7, PC6, and PC3 provide status information about Port A in mode 2 output operation. Among
these three status bits, the most useful one is bit 7, which provides OBF*A information. If this bit = 1,
it means that the Port A output buffer is empty, and so 8085 is required to send data to Port A.
The status check handshake data transfer in mode 2 input is performed as was explained earlier
for mode 1 handshake input operation. Similarly, the status check handshake data transfer in mode 2
output is performed as was explained earlier for mode 1 handshake output operation.
IM f
v
:
■
using Interface
Modules
■ Description
of logic controller interface
• Evaluation of boolean expression
• Evaluation of boolean expression (alternative)
• Decimal counter using logic controller
• Simulation of 4-bit ALU
• Simulation of 8 to 1 multiplexer
■ Successive approximation ADC interface
■ Dual slope ADC interface
■ Digital to analogue converter interface
•Generation of rectangular wave using DAC interface
•Generation of triangular wave using DAC interface
■ Stepper motor interface
Rotation of stepper motor in forward and reverse directions
•
■ Questions
In the previous chapter 8255 PPI chip was discussed at length. In this chapter connection of some stan¬
dard interface modules to ALS microprocessor kit using 8255 will be described. A number of interest¬
ing programs using these interface modules are dealt with.
A logic controller is used in industry to control a process by software. It typically accepts multiple
inputs and performs a specified sequence of arithmetic and logical operations. The multiple outputs
are used to maintain the process within the desired limits. It also provides visual display of the state
of the process at any instant of time.
In this section ALS-NIFC-05 model logic controller is described. It connects to ALS-SDA-85M kit
using a 26-core flat cable. The connector Cl on the logic controller is connected to I/O connector P3
on the ALS kit. The 26-core flat cable connects to the connector Cl on the logic controller interface in
345
only the correct orientation. We say, connector C 1 on the logic controller interface is polarized. Thus,
we are prevented from making a wrong connection. However, care should be taken while connecting
the other end of the flat cable to connector P3 (or P4) on the ALS kit. This is because P3 (or P4)
connectors on the ALS kit are not polarized. Power supply of + 5 V and Gnd is also connected to the
logic controller. The circuit description of the logic controller is shown in Fig. 21.1a. Fig. 21.1b
provides the physical layout of the interface to help the user in making the desired connections.
The logic controller interface provides 12 buffered output lines and 12 buffered input lines to the <§
user. The 12 output lines get connected to Ports A and C (lower) of 8255 on the ALS kit. The status = §3
it 1»
3. c
jiPkit Logic controller interface 8
Q
l
3
Buffer Invertor 330n
Xt On (off)o+5V PB7
PB7 1(0) 0(1) input
; Port B -f -o £.
1(0) It
u
PB0 PC4
input
PC7 -o
PC4
8255
Port C
<]--[>o 1 — Kl -o +5 V
PC3
-£>0-AAAA- fc o+5V
PC0 t output
PA7
; Port A 1(0) 1 (0) 0(1) On (off) PA0
\ PA0
-+
1 > -
T -[>0- >AAAA- Xl- ■0+5 V output
1 (0)
Values within parantheses correspond to each other. Similarly those outside parentheses correspond to each other. For example if PB7 input is al,
the buffer output will also be al , the invertor output will be 0, and the LED will be On.
PC0PCi PC7
• • O O O O O O O O*"'
L.E.D.
Power Connector
PB0 PB, PB7 PA0 PA, PA7
OOOOOOOO OOOOOOOO
Cl
Rest of logic controller
circuitry
26-pin
P3 P2 PI FRC conn :ctor
Write an 8085 assembly language program to evaluate two 4-variable Boolean expressions, using
logic controller interface.
Let us say we want to evaluate the following Boolean expressions.
X = PQRS + PQRS + PS and Y = PQRS + PRS
First of all, truth table for the Boolean expressions is written down as shown in the following table.
P Q_ R S X Y
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 1 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 0 1 0 0
0 1 1 0 1 0
0 1 1 1 0 0
1 0 0 0 0 I
1 0 0 I 0 I
1 0 I 0 I 0
1 0 1 1 0 0
1 1 0 0 0 0
1 1 0 1 1 1
1 1 1 0 0 0
1 1 1 1 0 0
PQRS inputs are connected to PB3, PB2, PB1, and PBO of 8255 respectively. X and Y outputs are
connected to PA1 and PAO, respectively. In the program that follows, the user is required to keep
changing the PQRS inputs manually. The program will be in a loop outputting the X and Y values (dis¬
played using LEDs) corresponding to the PQRS inputs.
347
The truth table is stored in memory as a look up table, say from location C100H. The following
values (corresponding to X and Y in the LS bit positions) are stored in consecutive locations starting
from C100H.
02, 00, 02, 00, 02, 00, 02, 00, 01, 01, 02, 00, 00, 03, 00, 00
■o
Program
/FILE NAME BOOLEAN.ASM
i
ii«
0
ORG C100H
TABLE DB 02H, 00H, 02H, 00H, 02H, 00H, 02H, 00H I?
O 2.
0 3
DB 01H, 01H, 02H, 00H, 00H, 03H, 00H, 00H
s*
ORG C000H a
PA EQU D8H 5
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
MVI A, 10001010B
OUT CTRL /Configure 8255 ports
LOOP: IN PB
ANI 0FH /Now A will contain PQRS input value
LXI H, TABLE
ADD L
MOV L, A /Point HL to proper row in the truth table
MOV A, M
OUT PA /Output XY from truth table to display
JMP LOOP
Write an 8085 assembly language program to evaluate two 4-variable Boolean expressions, using
logic controller interface. The program should test the output by automatically changing the inputs
from 0000, 0001, ... to 1111 whenever a key (other than Reset and Vect Intr) is pressed.
PQRS inputs are connected to PB3, PB2, PB1, and PB0 of 8255, respectively. X and Y outputs are
connected to PA1 and PA0, respectively. Program Port C lower to be the output and connect PC3-0 to
the input lines PB3-0. Keep incrementing the value in Port C lower by 1 whenever a key is pressed.
This results in Port B inputs getting incremented by 1, for every key depression.
Store a look up table in memory at location TABLE, as in the previous example. In the program
that follows, whenever the user presses a key on the keyboard, the PQRS inputs change to the next
higher value. The program will be in a loop outputting the X and Y values (displayed using LEDs)
corresponding to PQRS inputs.
Program
/FILE NAME BOOLEAN2.ASM
ORG C100H
TABLE DB 02H, 00H, 02H, 00H, 02H, 00H, 02H, 00H
DB 01H, 01H, 02H, 00H, 00H, 03H, 00H, 00H
348
ORG C000H
RDKBD EQU 0634H
PA EQU D8H
PB EQU D9H
PC EQU DAH
s
M
CTRL EQU DBH
0 MVI A, 10001010B
S
a
OUT CTRL /Configure 8255 ports
2 MVI A, 00H
s REPEAT: PUSH PSW
in OUT PC
00
s IN PB
Jj ANI OFH /Now A will contain PQRS input value
H
LXI H, TABLE
ADD L
MOV L, A /Point HL to proper row in truth table
MOV A, M
OUT PA /Output XY from truth table to display
MVI A, 00001110B
SIM /Unmask RST 5.5 (Keyboard interrupt)
CALL RDKBD /Read a value from keyboard and discard it
POP PSW
INR A /Increment PQRS value
JMP REPEAT
Write an 8085 assembly language program to implement a decimal counter using logic controller
interface. The starting count should be input through the interface and the count should be displayed
on the interface.
Program
The program that follows will be in an infinite loop till the user inputs a valid two-digit BCD value to
Port B. Then this initial count value is displayed by sending it to Port A. After every 0.5 s delay the
count value is incremented by 1 in decimal and sent to Port A for display. Once the count value rolls
over to 00 from 99, the operation repeats endlessly.
/FILE NAME CTR_IFC.ASM
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
DELAY EQU 04BEH
349
MVI A, 10001010B
OUT CTRL /Configure 8255 ports
/The next 9 instructions ensure that control is transferred to next
/portion of program only after Port B receives a valid 2-digit BCD
input.
■o
AGAIN: IN PB
ANI OFH i
CPI OAH ii«
0
JNC AGAIN
IN PB
I?
O 2.
0 3
ANI FOH s*
a
CPI AOH
JNC AGAIN 5
IN PB
REPEAT: OUT PA /Display count value
PUSH PSW
LXI D, FFFFH
CALL DELAY /Generate delay of 0.5 second
POP PSW
ADI 01H
DAA /Increment A value in decimal
JNZ REPEAT
JMP AGAIN
Write an 8085 assembly language program to simulate a 4-bit ALU using logic controller interface.
The ALU should perform addition, subtraction, AND operation, or OR operation on 4-bit inputs,
based on the desired operation.
The pins of the 4-bit ALU to be simulated is assumed to be as shown in Fig. 21.2.
For this ALU simulation, the inputs and outputs are provided by 8255 ports as indicated in the
following.
si so z
0 0 X+Y
0 1 X-Y
1 0 XANDY
1 1 X OR Y
350
4-bit ALU
\
x3-o Cy
Z3-0 /
0
w
0
Y3-0
o SI SO
0
a
0
o
8085 kit for 4-bit ALU simulation
w
00
o 8255
00
0)
£ PB7_4
H X3-0
Port B 4-bit ALU
Y3-0 simulator
PB3-0 program
Port A
PA4 >Cy
PA3-0
/
>Z3-0
Port C
✓K
PC7 PC6
SI so
Fig. 21.2 Pin details of 4-bit ALU
Program
; FILE NAME ALU IFC.ASM
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
MV I A, 10001010B
OUT CTRL /Configure 8255 ports
ANI 0FH
MOV C, A ;C will now have Y input
MOV A, B
ANI F0H
351
RRC
RRC
RRC
RRC
MOV B, A ;B will now have X input
IN PC /Read SI and SO values from Port C ■o
ANI 11000000B i
RLC
RLC ;LS 2 bits of A will now have SI and SO
ll«
0
O 2.
CPI 00H 0 3
JZ ADD ;If SI = 0 and SO = 0 do Add operation s*
a
CPI 01H
JZ SUB ;If SI = 0 and SO = 1 do Subtract operation 5
CPI 02H
JZ AND ;If SI = 1 and SO = 0 do AND operation
OR: MOV A, B
ORA C
JMP DISP
ADD: MOV A, B
ADD C
JMP DISP
SUB: MOV A, B
SUB C
JMP DISP
AND: MOV A, B
ANA C
DISP: OUT PA
JMP LOOP
2 1 . 1 .5 SIMULATION OF 8 TO 1 MULTIPLEXER
Write an 8085 assembly language program to simulate an 8 to 1 multiplexer, using logic controller
interface.
The pins of the 8 to 1 multiplexer to be simulated are assumed to be as shown in Fig. 21.3.
For this multiplexer simulation, 8255 ports as indicated in the following provide the inputs and
outputs.
First of all, the values 01H, 02H, 04H, 08H, 10H, 20H, 40H, 80H are stored as a look up table start¬
ing at location TABLE.
352
8 to 1 MUX
h
h
h
Out ■»
0
tfi
w h
0)
o I1
0
o. T0 CS S2 SI SO
o
o
w
oo
o 8085 kit for 8 to 1 MUX simulation
00
0)
£
H Port B Port A
PB7_O
I7-0
PA0 ■> Out
8 to 1 MUX
Port C simulator
[ ] program
/ \
PC7 PC6,5,4
CS S2,SI,SO
Program
;FILE NAME MUX8T01.ASM
ORG C100H
TABLE DB 01H, 02H, 04H, 08H, 10H, 20H, 40H, 80H
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
MVI A, 10001010B
OUT CTRL /Configure 8255 ports
LOOP: IN PC
RLC
JNC LOOP /Wait in the loop till chip is enabled
RLC
353
RLC
RLC
ANI 07H ;Now LS 3 bits of A will have select inputs
LXI H, TABLE
ADD L
■o
MOV L, A /Point HL to proper row of look up table
IN PB
i
ANA M /AND Port B with a value from look up table ii«
0
JZ SKIP
MVI A, 01H
If
0 2.
0 3
SKIP: OUT PA /Send out the value of selected input s*
JMP LOOP a
5
■ 21.2 SUCCESSIVE APPROXIMATION ADC INTERFACE
Model ALS-NIFC-07 successive approximation ADC is described here. It also has a programmable
timer interface. It connects to ALS-SDA-85M kit using a 26-core flat cable. The connector Cl on the
interface is connected to the I/O connector P3 on the ALS kit, using the flat cable. Power supply of
+ 12V, -12 V, +5 V, and Gnd are also connected to the interface. The circuit description of the
successive approximation ADC interface is shown in Fig. 21.4a. Figure 21.4b provides the physical
layout details that are necessary to make the desired connections to the interface.
ADC 0809 is an 8-bit ADC. The interface uses a crystal oscillator, which provides 768 kHz as the clock
input to the ADC 0809. The conversion takes 64 clocks (about 100 |xs). ADC 0809 has eight analog inputs
(I7_0). One of them is selected by S2, SI, SO inputs, which are driven by PB2, PB1, and PB0 of 8255.
The address on S2, SI, SO is latched by ADC 0809 when ALE input becomes 1. If a ‘0’ is sent out
on PB5 of 8255, ALE input of ADC 0809 becomes a 1.
SOC (start of conversion) should become a 1, for the conversion to start. This is achieved by
sending a 0 on PB6. Then the EOC (end of conversion) output of the ADC becomes a 1. When the
conversion is over, EOC output is made 0 by the ADC. So, after the SOC signal is provided, we have
to wait for atleast 100 |is, for the conversion to be over.
Then the digital output D7 0, corresponding to the selected analog input, comes out on the ADC output
pins if the OE (output enable) input of the ADC is at logic 1. The OE input of ADC becomes logic 1, if
logic 0 is sent out on PB7. Then 8085 reads this digital value from Port A and displays it in the data field
of the 8085 kit. When using this interface, it is required to configure the 8255 ports on the kit as follows.
PA as input PB as output PC as input
PC in not used by the successive approximation ADC interface. Port addresses for 8255 connected
to connector P3 are as follows.
Port A: D8H Port B: D9H Port C: DAH Control: DBH
It is possible to connect the second 8255 on the ALS kit to the ADC interface. For this purpose, I/O
connector P4 on the ALS kit is connected to the interface, using the 26-core flat cable. Port addresses
for the second 8255 on the ALS kit are:
Port A: F0H Port B: F1H Port C: F2H Control: F3H
Problem: Write an 8085 assembly language program to interface a successive approximation ADC.
Display the digital equivalent of the analog value in the data field.
354
ADC 0809
□i
ALS-SDA-85 kit +5 V
Ref +
PC Ref
0
M
PB7 -tx> OE
M
u PB6 -Oo- soc
0 PBs -t>o- ALE
a.
o
8255 : PB 8 analog
PB2 - S2 inputs
PB, !7-0
s PBQ - SI
U) SO
00
o
00
0
PA
0, \
PA7-0
D7-0
EOC
768 KHz
Clk
■+ 8 Vcc Gnd
I
Crystal +5 V
oscillator
HnH6.14 MHz
KEY
IN0
INI 4
To CRO Gnd
-> 8253 Timer 2
Output to CRO
IN 7
C| 26 pin C2 26 pin
To Gnd To -12 V
To +5 V
To +12 V
C| is connected to connector P3 (or P4) on ALS kit, for ADC interface purpose.
C2 is connected to connector P2 on ALS kit, for timer interface.
Fig. 21 ,4b Physical layout of the successive approximation ADC
355
Program: In this program, the user presses a key on the microprocessor kit in the range 0-7 to select
an analog input.
;FILE NAME ADC1 IFC.ASM
ORG C000H
■o
PA EQU D8H 0
(fi
PB EQU D9H
PC EQU DAH I3
Q </)
MVI A, 99H
OUT CTRL /Configure 8255 ports
MVI A, OEH
SIM /Unmask RST 5.5 (keyboard interrupt)
LOOP: El
CALL RDKBD
CPI 08H
JNC LOOP /Select analog input (0 to 7) for conversion
MVI A, 11100000B
MVI A, 01100000B
OUT PB /Activate OE. Deactivate SOC and ALE
IN PA /Read converted digital value from Port A
STA CURDT
CALL UPDDT /Display in data field
JMP LOOP
356
■ 21.3 DUAL SLOPE ADC INTERFACE
Dual slope technique provides high degree of noise immunity in the conversion process. However, the
conversion process is slow compared with the successive approximation ADC. Model ALS-NIFC-10
provides the user with the following.
s
M
o 1. DAC for ADC interface;
S 2. Temperature sensor interface;
a
2 3. Dual slope ADC interface.
.2
s
in In the discussion that follows, only dual slope ADC interface is explained. It connects to ALS-
a
s SDA-85M kit using a 26-core flat cable. The connector Cl on the interface is connected to I/O con¬
nector P3 on the ALS kit, using the flat cable. Power supply of + 12 V, — 12 V, + 5 V, and Gnd are also
Jj
H connected to the interface. When using this interface, it is required to configure the 8255 ports on the
kit as follows.
SWl
SW2
lgF
Tin
6S+5V)
Fixed
time To XI +
■Comparator
SW3 (Zero crossing
detector)
Tref Integrator
(-8V)
■o
i
ci
ll
26 pin
O 2.
(9 3
3“*
1KQ Pot a
5
PR5
82 KQ IFF
Fin
(—+5 V) SW2
from 1K£2
pot PR5 XI
Drive circuit
PB0
Fref
(-8V) SW3
Drive circuit
PB2
PB3
l
Fig. 21.6 Circuit details of dual slope ADC
The analogue input voltage Vin (positive voltage < = 5 V) is then integrated for a fixed time T0.
This is done by sending out an 1 on PBO, which results in closing SW2. This provides analogue input
to the integrating capacitor. Now the output of the integrator reaches a negative value depending on
the value of the analogue input. In the program that follows, T0 is chosen as 4.4 |xs.
A fixed reference voltage VTef (negative voltage of 8 V) is then provided as input to the integrator.
This is done by sending out an 1 on PB2, which results in closing SW3. This provides -8 V input to
358
Voltage Integrate for a Integrate Freftill
OV fixed period T0 integrator output becomes OV
— T0 = 4.4 ms Tx -K Time
1 ms
reset
integrator
s
a>
o
s
a Fig. 21 .7 Waveform for the integrator
2
.2
s the integrating capacitor. Now the output of the integrator starts moving towards 0 V from the earlier
in
00
s„ negative voltage. Let us say it takes Tx time to reach 0 V. The waveform for the integrator output is
provided in Fig. 21.7.
H Then Vm is given by the formula
_ ref X
To
After providing Kref as input to the integrator, FIL pair is used as an up counter in a loop till the inte¬
grator output performs zero crossing. The zero crossing is detected by the comparator. When the inte¬
grator output just goes positive, the PC3 input becomes 0. At this point of time, the value in FIL pair
is proportional to Tx. To be exact, Tx = contents of FIL X 11 jxs. This is because, a pass through the
loop uses 33 clocks, each of 1/3 jxs.
Thus,
_ VKf X Tx _ 8 V X (11 (AS X HL contents)
Vin To 4.4 jxs
= 20 mv X contents of HL
The contents of HL are displayed in the address field by the program. If Vm = 5 V = 5000 mV, HL
pair will have 250 = 00FAH, and this value will be displayed. By slightly increasing T0 we can get
00FF display for 5 V input.
Problem: Write an 8085 assembly language program to display in the address field the digital equiv¬
alent of the analog input, using dual slope ADC interface.
Program: The program displays the digital equivalent of the analog input in the address field. Then it
waits for the user to press any key (other than Reset and Vect Intr). After a key is pressed the conversion
operation starts once again and converts the latest analogue input to digital. The analogue input can be
changed from 0 to 5 V using a screwdriver on the 1 Kfl potentiometer PR5 on the interface card.
; FILE NAME ADC2_IFC . ASM
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
LXI D, 0226H 5
CALL DELAY /Generate delay of TO = 4.4 millisecond
MVI A, 04H
OUT PB /Switch on SW3. So Vref is i/p to integrator
LXI H, FFFFH
AGAIN: INX H
IN PC
ANI 08H
JNZ AGAIN /Be in loop till PC3 becomes 0. Loop needs 33 clocks.
Model ALS-NIFC-06 dual DAC interface is described here. It also has a cassette interface, and an
opto I/O interface. It connects to ALS-SDA-85M kit using a 26-core flat cable. The connector Cl on
the interface is connected to I/O connector P3 on the ALS kit, using the flat cable. Power supply of
+ 12 V, -12 V, +5 V, and Gnd are also connected to the interface.
The circuit description of the DAC portion of the interface is shown in Fig. 21.8a. The phys¬
ical layout of the interface is provided in Fig. 21.8b. The DAC portion of the interface consists
of two DACs, as it uses two DAC chips (DAC 0800). Our programs make use of only one of
these.
DAC 0800 is an 8-bit DAC. The 8-bit digital input is fed to DI7.0 inputs of the DAC. In the
interface under consideration, Port A of 8255 on the kit provides the digital input to the DAC. Port B
of 8255 on the kit provides digital input to the second DAC on the interface board.
The DAC generates analog current (not voltage) output on 7out pin. It is a sinking current, as shown
in Fig. 21.8. Maximum current is output for the maximum digital input value of FFH. The maximum
360
current value will be close to /ref, which in this case is 2 mA. (To be exact, the maximum /out will be
/ref * 255/256.)
DAC 0800 provides complementing current outputs /out and /out*, such that always /out + /out* =
/ref. This current output is converted to voltage output, using the opamp circuit shown in Fig. 21.8a.
If J1 is shorted to J2, we get unipolar Kout as shown in the table that follows.
oM
M
O
o pP Kit DAC portion of interface
a
o DAC 0800 10.2 V from precision
5.1 K source
s
U)
Kef+ -AAA-Iref=2mA
00 DIy-0 vKf- -AAA
O PA 5. IK
00
0 vw—
/out
\Rf DAClo/p
* Fout
/out v+
8255 R=Rf=2.7K
R|J.11 J2
PB
\ Second DAC interface J3 J4
DAC2 o/p
DI7-O 1
Aiut + 4ef 2 mA
Gnd
12V
+5 V +12 v
iT Physical layout
PI 26-pin
P3 P2 1 2 3 4 FRC
connector
ToCRO ToCRO
Unipolar Case
Digital Input Current Vou, = Iou,XRF
00H 0 mA 0V
80H 1 mA 2.7 V
FFH 2 mA 5.4 V
If J1 is not shorted to 32, we get bipolar Kout, as shown in the table that follows.
-<? &iZ
Bipolar Case 3. c
Digital input lout hut* v+ = -lout* XR Kut - C+ + loutXRF
8
0
l
3
00H 0 mA 2 mA -5.4 V -5.4 V
80H 1 mA 1 mA -2.7 V 0V 0
FFH 2 mA 0 mA 0V +5.4 V £.
9
U
In this interface, the other DAC 0800 receives its digital input from Port B of 8255 on the ALS kit.
Port C of 8255 connects to cassette and opto I/O interfaces. As such, when using this interface with
8085 kit, we have to configure the 8255 ports as follows.
Port A as output Port B as output
Port C Upper as input Port C Lower as output
Port addresses for 8255 connected to connector P3 are as follows.
Port A: D8H Port B: D9H Port C: DAH Control: DBH
It is possible to connect the second 8255 on the ALS kit to the ADC interface. For this purpose, I/O
connector P4 on the ALS kit is connected to the interface, using the 26-core flat cable. Port addresses
for the second 8255 on the ALS kit are:
Port A: FOH Port B: F1H Port C: F2H Control: F3H
In the programs using this interface, we have shorted J1 and J2, and so we get unipolar output.
Write an 8085 assembly language program to generate rectangular waveform of a given duty cycle
using DAC interface. Display the waveform on a cathode ray oscilloscope.
Program: To get unipolar output, J1 is shorted to J2 on the interface. To display the waveform on a
CRO connect pin 1 of connector PI to CRO signal pin, and pin 2 of connector PI to CRO ground pin.
; FILE NAME DACRECT .ASM
ORG C100H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
362
MVI A, 88H
OUT CTRL /Configure 8255 ports
LOOP: LHLD Y
XCHG
LHLD X /Now DE contains 00C0H and HL contains OOFFH
0 MVI A, 00H
M
M OUT PA /Send 00H to DAC through Port A
o
u
o CALL DELAY /Generate delay proportional to HL contents
a
o XCHG /Now HL contains 00C0H
MVI A, FFH
S OUT PA /Send FFH to DAC through Port A
in
00 CALL DELAY /Generate delay proportional to HL contents
o
00
JMP LOOP
X
/Subroutine to generate a delay proportional to contents of HL
DELAY: DCX H
MOV A, H
ORA L
JNZ DELAY
RET
Write an 8085 assembly language program to generate triangular waveform using DAC interface.
Display the waveform on a cathode ray oscilloscope.
Program: To get unipolar output, J1 is shorted to J2 on the interface. To display the waveform on a
CRO, connect pin 1 of connector P 1 to CRO signal pin, and pin 2 of connector P 1 to CRO ground pin.
/FILE NAME DACTRIAN.ASM
ORG C100H
X DW OOFFH /Rise / Fall time is proportional to this value
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
MVI A, 88H
OUT CTRL /Configure 8255 ports
/The next 7 instructions generate rising portion of triangular
/waveform. This is done by sending to DAC through Port A values from
/00H to FFH, in steps of 01. The increment is done after a small time
/ delay.
CALL DELAY
s*
a
POP PSW
DCR A 5
CPI FFH
JNZ DESCEND
JMP LOOP
/Subroutine to generate delay proportional to contents of word location X
DELAY: LHLD X
AGAIN: DCX H
MOV A, H
ORA L
JNZ AGAIN
RET
ALS-NIFC-01 is a stepper motor interface. It is connected to ALS kit using 26-core flat cable. It is
used for interfacing two stepper motors. In our experiment, we use only one stepper motor. The motor
has a step size of 1.8°.The stepper motor works on a power supply of + 12V. Power supply of +5V
(white wire), Gnd (black), and + 12 V (red) is provided to the interface. Note that - 12 V supply is not
used by the interface. Make sure that the + 12 V supply has adequate current rating to drive the step¬
per motor. This is ensured by using the power supply provided with the step motor interface.
The stepper motor is connected to the interface using five-way powermate connector. The step motor is
a two-phase, six-wire motor. The six wires are for the D, B, C, A inputs and VM connection (two wires).
The five-way powermate connector is used for connection purpose. Ensure that red wire is connected to A1
on the interface. PC3_0 is used to provide DBCA inputs to one stepper motor, and PC7 4 provides DBCA
inputs for the other motor, as shown in Fig. 21.9a. Thus, while using the stepper motor interface, the 8255
port C should be configured as an output port. The physical layout of the interface is provided in Fig. 21.9b.
It is possible to have four-step sequence with ‘one-phase ON’ scheme, as shown in the following.
In this case the step size will be 1.8°.
D B C A
1 0 0 0 8
0 1 0 0 4
0 0 1 0 2
0 0 0 1 1
364
Stepper motor 1 Stepper motor 2
VM VM
0
M PC, -M Driver Lÿ- PC5->| Driver [-C
M
U
o PC0->| Driver |— — Driver l —
a
5 Fig. 21.9a DBCA inputs of a stepper motor
i
U)
00
s
0
2 Wires Stepper motor
Gnd VM D B C A
+12 V
8085 kit
+5 V
J
<
P3 Al
C
26-pin
FRC Stepper motor
connector interface
circuitry
A2
No VM D B C A
connection v Y
To second stepper motor, if needed
The 4 step sequence that we send to the stepper motor interface is 88H, 44H, 22H and 11H instead of
08H, 04H, 02H, 01H so that the step motor can be connected to any one of the two connectors
provided on the interface board.
If the sequence is reversed, the rotation is also reversed.
Write an 8085 assembly language program to rotate a stepper motor by N steps in the forward direc¬
tion, then backward by the same number of steps and repeat it forever using the stepper motor con¬
troller interface.
365
Program: The program shown here makes the step motor rotate 100 steps of 1.8 degrees each, result¬
ing in half revolution, then, it rotates half revolution in the opposite direction. This sequence is
repeated forever. To stop the operation we have to reset the microprocessor kit.
;FILE NAME STEPMOTR.ASM
ORG C100H ■o
0
N DB 100 ;100 STEPS OF 1.8° = 0.5 REVOLUTION (fi
PA
ORG C000H
EQU D8H
I3
Q
3, C
</)
0)
PB EQU D9H o HI.
a> s
PC EQU DAH (fi
1 . With a neat diagram explain the functioning of logic controller interface. Write programs
o using the logic controller interface to implement the following.
« a. Decimal down counter to start from a preset value.
E
a b. Multiplication of 4-bit numbers and display 8-bit result.
E c. Division of 8-bit number by 4-bit number and display 4-bit quotient and 4-bit remainder.
.2 If quotient is larger than 4 bits, then overflow indication is also to be provided.
2
in
2. With a neat diagram explain the working of successive approximation ADC interface.
3. With a neat diagram explain the working of dual slope ADC interface.
j; 4. With a neat diagram explain the working of DAC interface. Write a program using DAC
interface to generate the waveform shown below.
+5V--
Voltage
+2.5 V
0
1 2 3 4' 5 6 7 8 9 10 11 12 Time
ms ms ms ms ins ms ms mi ms ms ms ms
-2.5 V-
—5 —
Figure of waveform
5. With a neat diagram explain the working of stepper motor interface. Write a program using
stepper motor interface to rotate the stepper motor by one full revolution, then rotate by one
full revolution in the opposite direction, and finally stop.
E Support
Chips
Chapter Heads
22 Interfacing of I/O Devices
23 Intel 8259A— Programmable
Interrupt Controller
24 Intel 8257— Programmable DMA
Controller
25 Intel 8253— Programmable
Interval Timer
26 Intel 8251 A— Universal Synchronous
Asynchronous Receiver
Transmitter (USART)
27 Zilog Z-80 Microprocessor
28 Motorola M6800 Microprocessor
29 8051 Microcontroller
30 Advanced Topics In 8051
INTRODUCTION
This part comprises of Chaps. 22 to 30. In the previous part of
the book, 8255 PPI, which is a very popular chip that is used for
interfacing I/O devices, was described in detail. Theoretically
any I/O device could be interfaced with the microprocessor
using the 8255. However, interfacing functionally complex I/O
devices, such as keyboard and seven-segment display by using
8255 requires a lot of programming effort by the user. In such
cases, it is better to use support chips that are meant specially for
interfacing specific I/O devices. In this part of the book a few
such popular support chips (as listed below), which are used in
an 8085-based system, are described in detail.
sE
Io «22.1 INTERFACING 7-SEGMENT DISPLAY
s
w
§
CO A very commonly used output device, especially in 8-bit microprocessor kits, is the 7-segment LED
o
display. In a 7-segment LED display, there are actually eight segments, including the as shown in
H
Fig. 22.1 in the shape of character 8 with a decimal point after it. The segments are denoted as ‘a, b,
c, d, e, f, g, and dp’ where dp stands for the decimal point. Each of these segments is actually an
LED or a series of LEDs. The internal circuitry of a 7-segment display is as shown in Fig. 22.2.
f b
9
e c Fig. 22.1
Segments of a 7-segment
d
•dp display
4-
b
s4
S
d
s Common
4- Anode
c
s 4-
f
s
Fig. 22.2
g Internal circuitry of a 7-segment
dp 4 common anode display
The 7-segment LED comes in two different types: the common anode type and the common cath¬
ode type. In our discussion, common anode-type 7-segment LEDs are used. In common anode
7-segment LED, the anodes of all the eight LEDs are connected together and brought out on an exter¬
nal pin as shown in Fig. 22.2. This pin is connected to a +5-V dc supply. The cathode ends of the eight
segments are brought out on the pins of the display.
To make an LED segment glow, it is necessary to pass current through the LED segment. Typically
about 10-mA current is needed to make the segment glow sufficiently bright. This is achieved by
typically using 330-0 resistor in series with an LED segment and connecting the free end of the
371
resistor to logic 0 state. The resistor used in this circuit is commonly called a ‘current limiting resis¬
tor’ for obvious reasons. The LED segment is now forward biased. There will be about 1.5-V drop
across the forward-biased LED segment, and 3.3-V drop across the 330-fl resistor when 10-mA cur¬
rent is passing. This works out to almost 5 V, which is the potential difference between the anode and
the free end of the resistor. If more/less brightness is desired the current passing through should be
increased/decreased, by decreasing/increasing the series resistance. The condition for glowing of an =
5*»
<P
LED segment is shown in Fig. 22.3.
On
*5
‘0’ =0V •
-AV-M- <— o +5V 1-2.
0
33011 jo w
mA
If current does not pass through an LED segment, it does not glow. This is achieved by typically
using 330-fi resistor in series with an LED segment and connecting the free end of the resistor to logic
1 state. The LED segment is not forward biased now. There will be 0-V drop across the LED segment,
and 0-V drop across the 330-fl resistor as there is no current passing. This works out to 0 V, which is
the potential difference between the anode and the free end of the resistor. The condition for not glow¬
ing of an LED segment is shown in Fig. 22.4.
Soff
33012
w ° +5V
0mA
If it is desired to display the character ‘3’ on the display, then the segments a, b, c, d, and g must
be made to glow. The other segments (e, f, and dp) of the 7-segment LED must not glow. Thus the
required logic state on the various inputs (free end of the resistors) of the 7-segment LED are as
follows.
a b c d e f _g_ dp
= ODH
0 0 0 0 1 1 0 1
Thus if ODH = 00001101 is input to the 7-segment display, the character ‘3’ would be displayed. In
this example, ODH is called the 7-segment code for character ‘3’.
A microprocessor does not directly communicate with an output device. It communicates via an
output port. The simplest output port is basically a latch. Suppose it is required to display the charac¬
ter ‘3’ on a 7-segment display. For this, if there is no latch, the microprocessor must continuously send
ODH for the desired duration of display. It could be several seconds (several million microseconds)
easily in a typical application. Thus the processor is not available for any other function for this long
duration of several million microseconds. If there is a latch between the processor and the 7-segment
display, the processor will send the data to the latch in a very short time (about a microsecond). Then
the latch output is used to drive the 7-segment display. The processor is used very efficiently this way.
The use of 74373 latch for interfacing a 7-segment display is shown in Fig. 22.5.
372
74373 latch
from 0
A]5 Accumulator 0 -b-
of 8085 0
0 d
e '-+5 V
1
o
s2 A8 — [X>4 3 l dp
/ÿ
IO/M
2
Latch [oE (Output enable)
.2
I WR enable Q
Note: To keep the figure simple, only two of the eight segment connections are shown. The other six segment connections are similar.
s
10 Fig. 22.5 Use of 74373 latch for interfacing a 7-segment display
§
co
o
H
In Fig. 22.5, the 74373 latch is used as an I/O mapped I/O port with the port address as FEH. This
could be easily verified from the chip select circuit used in the figure. The following instructions are
to be executed to display character ‘3’ on the 7-segment display.
MVI A, ODH
OUT FEH
When OUT FEH instruction is executed by the 8085, FEH = 11111110 is sent out on both AD7.0 and
A15_s during T1 of IOW machine cycle. Then gate-1 (NAND) output becomes 0. During Tl, IO/M*
also becomes 1, while during T2 of this machine cycle, WR* becomes 0. Hence gate-2 (NAND) out¬
put also becomes 0. This results in gate-3 (NOR) output becoming 1. This enables the latch. During
T2 of this IOW machine cycle, accumulator content is sent out by the 8085 and will be present on the
eight latch inputs. Thus the 74373 latches the accumulator content when OUT FEH is executed. After
this, the data need not be present on the latch inputs. It is already stored in the latch. The output of
74373 is permanently enabled by connecting its output control pin to logic 0, as can be seen from the
figure. So the latched data comes out on the output pins of 74373 and displays the character ‘3’ on the
7-segment display.
More commonly, instead of using a simple latch as an output port, a port of 8255 PPI chip is used.
This would result in the reduction of a number of chips, as the 8255 has three 8-bit I/O ports. Also,
programmable devices are more convenient to use than non-programmable devices like 74373. The
simple circuit to interface a 7-segment display as described has two main drawbacks which are indi¬
cated in the following.
In many equipments, there might be a need for several digits of display. For example, a frequency
counter may need eight-digit display and eight output ports. If 74373 is used as the output port, we
need eight of them. This would be quite expensive. Even if it decided to use 8255 ports, we need three
chips of 8255, as each 8255 has only three ports which is quite expensive.
If character ‘8’ is to be displayed in a digit position, all the seven segments are required to
glow. As already seen, each segment when glowing draws a current of 10 mA from the power
supply. Hence, if character ‘8’ is to be displayed, the drain on the power supply would be
7X10 mA = 70 mA. If a situation arises where all the eight digits of the display are required to
display the value ‘88888888’, then the drain on the power supply is 8 X 70 mA = 560 mA. It is
quite a large power drain indeed, especially if the equipment needs to be operated on batteries.
Some equipments may have to be operated on batteries in a battlefield, where 230-V ac supply is
obviously not available.
373
74138
8255 PPI 7
6
5 1
PB
=
5*»
<P
0
LED-0 LED-7
PA
1-2.
0
(/>
The use of ‘dynamic display’ technique, as described in the following, could solve the problem of
excessive power supply drain. It is also called ‘multiplexed display’ technique and the scheme is
indicated in Fig. 22.6.
Let us say it is required to display ‘34567890’ on the eight digits of the 7-segment display. First of
all, the 7-segment code for character ‘3’ is sent to all the eight LEDs for about a microsecond using
Port A of 8255. For the 3 to 8 decoder the input is provided as 000 using the LS three pins of Port B
of 8255. As a result the LED-0 alone gets the power supply of +5 V. Thus the character ‘3’ is dis¬
played only on LED-0 for about a microsecond and the other LEDs do not display anything.
Next, the 7-segment code for character ‘4’ is sent to all the eight LEDs for about a microsecond.
For the 3 to 8 decoder the input is provided as 001. As a result only LED-1 gets the power
supply of +5 V. Thus the character ‘4’ is displayed only on LED-1 for about a microsecond and the
other LEDs do not display anything.
The operation is continued in a similar way for all the eight LEDs. This takes a total time of 8 p,s.
This constitutes one cycle of the display, in which all the required characters are displayed, but not
simultaneously and continuously. If the cycles are performed one after another endlessly, the cycles
are repeated (1000 p.s/8 p,s) = 125 times per second. This rate is higher than the persistence of vision
that is about 16 times per second. As such, all the eight LEDs appear to be glowing simultaneously
and continuously. The same principle is used even in the television display and cinema projections.
Using this technique, even if the display is ‘88888888’ the current drain from the power supply is only
70 mA, as only one digit is glowing at any instant. Thus the power supply drain problem is solved.
However, it poses other new problems as described below.
The LED that is displaying a character should be switched off after a microsecond, and the next
LED should be switched on. Once the cycle of displaying all the characters is achieved, the cycles
should be repeated endlessly at a rate greater than 16 times per second. This requirement keeps the
microprocessor busy most of the time for display of information. Then the processor is not much free
to do processing of data, which is the most important job of the processor.
To overcome these drawbacks, a very specialized chip is used for interfacing 7-segment display
units in many microprocessor-based equipments. It is the Intel 8279 display controller IC. This single
chip could be used to interface up to sixteen 7-segment display units. It uses the technique of multi¬
plexed display, but without burdening the microprocessor in this task. The details of the
working of Intel 8279 are provided a little later. As would be seen later, the 8279 is also used for
interfacing a matrix keyboard. As such, the 8279 chip is called ‘keyboard and display controller’.
374
■ 22.2 DISPLAY INTERFACE USING SERIAL TRANSFER
In this section, interfacing four numbers of 7-segment LEDs using serial data transfer is explained.
The display portion of the interface has four 7-segment LEDs as can be seen from the physical layout
o of the interface provided in Fig. 22.7. This figure is of help to the user in making the required connec¬
sE tions. The interface connects to the ALS-8085 kit using a 26-core flat cable. The connector Cl on the
interface is connected to I/O connector P3 on the ALS kit. A power supply of + 5 V and Gnd is also
I
.2
connected to the interface.
S
w +5V Gnd
§
CO
0
H
Power connector
4-digit display
Cl
7 8 9 +
4 5 6
26-pin
FRCC 1 2 3 FI F2 20-key keyboard
CE 0 C %
The interface uses four 8-bit serial shift registers (type no. 74164). The outputs of these shift reg¬
isters are connected to the segments of the LEDs as shown in Fig. 22.8 that provides circuit details of
the display interface. PC7, PC6, PC5, and PC4 are used to provide clock pulses to the four shift regis¬
ters. The bit value on PB0 enters segment ‘a’ of the MS digit, when the clock makes a 0 to 1 transi¬
tion. When another clock transition occurs, the data at segment ‘a’ is shifted to segment ‘b’ and the
new bit value at PB0 enters segment ‘a’. Proceeding this way, after eight clock transitions, a charac¬
ter is formed in the MS digit position. After eight more clock transitions, the character at MS digit
position is shifted right to the next digit position. Thus, after 8 X 4 = 32 clock pulses, four digits are
displayed on the interface.
When using this interface it is necessary to configure the 8255 ports as follows.
Port B as output;
Port C upper portion as output;
Configuration of other ports is unimportant.
8255 PPI
: : I
PB
PBp abcde f g dp a dp a dp a dp
=
PC7
330
a
s :rial i/p
5* »
ft
PC
Q0Q1Q2 Q7 Qo Q7 Qo Q7 Qo Q7 *5
PC4 74164 74164 74164 74164 i-o a
</>
J" elk elk elk JT elk
It is possible to connect the second 8255 on the ALS kit to this interface. For this purpose, the I/O con¬
nector P4 on the ALS kit is connected to this interface using the 26-core flat cable. Port addresses for
the second 8255 on the ALS kit are:
Write an 8085 assembly language program to implement a moving display of a given string of digits
on the display interface. The display content should move by a one-digit position to the right every
second.
The program which follows displays the characters ‘0’ to ‘9’ in a moving display fashion. The char¬
acters to be displayed on the interface in consecutive seconds are indicated as follows.
!
.2
‘0’ = 03H
‘5’ = 49H
‘1’ = 9FH
‘6’ = 41H
‘2’ = 25H
‘7’ = 1FH
‘3’ = ODH
‘8’ = 01 H
‘4’ = 99H
‘9’ = 09H
S
in The 7-segment codes for any character can easily be derived. For example, the derivation of
§ 7-segment code for ‘3’ is reproduced as follows from earlier discussion.
co
«
a b c d e f g dp
H
0 0 0 0 1 1 0 1 = ODH
After storing the above mentioned ten 7-segment codes, the 7-segment code for blank (FFH) is stored
in the next four memory locations. Thus, the 7-segment codes for the 14 characters to be displayed on
the interface in moving display fashion are stored in consecutive memory locations.
Program
ORG C100H
TABLE DB 03H, 9FH, 25H, ODH, 99H, 49H, 41H
DB 1FH, 01H, 09H, FFH, FFH, FFH, FFH
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
DELAY EQU 04BEH
MVI A, 10000000B
OUT CTRL /Configure PB and PC upper as output ports
AGAIN: LXI H, TABLE ;HL points to TABLE
MVI B, OEH ;(B) = 14, the no. of characters in the sequence
NXTCHAR: MVI E, 08H ;(E) = 08, the no. of segments per character
MOV A,M ;7-segment code for the character to display
/moved to A
NXTSEG: OUT PB /Send segment value on PBO to serial input of
/shift register
MOV D,A /Save A value in D
MVI A, 00H
OUT PC /Send 0000 on PC7-4
MVI A, FOH
OUT PC /Send 1111 on PC7-4. Generate 0 to 1 transition
/ for clock
DCR E / Decrement segment counter
JZ OVERCHK /If the character display over, check if it was
/last character
377
MOV A, D /Reload A from D
RRC /Load LS bit of A with new segment value
JMP NXTSEG /Go to display next segment of character
OVERCHK: CALL DLY1SEC /Generate delay of 1 sec after a character is
Z formed
DCR B /Decrement character counter =
5*»
ft
JZ AGAIN /Repeat the sequence if all characters already
/ displayed
A very commonly used input device is the keyboard. Let us say there are only eight keys on the sim¬
ple keyboard. Whenever the user presses a key on this keyboard, the microprocessor should immedi¬
ately identify the key pressed. The action to be performed by the processor depends on the key that is
pressed. For example, on a calculator keyboard, addition operation is to be performed when ‘+ ’ key
is pressed.
A microprocessor does not directly communicate with an input device. It does it via an input port.
The simplest input port is basically a set of tristate gates. A commonly used tri-state buffer is the
74244 chip. It has totally eight buffers arranged as two groups of four buffers with non-inverted tri¬
state outputs as shown in Fig. 22.9.
The 4-bit input comes out on the corresponding outputs of the 74244 only when the enable pin for
the 4-bit group is at logic 0. It is like having a short circuit between the input and the output when the
enable pin is at logic 0. If the enable pin is at logic 1, the input does not come out on the correspon¬
ding output and is blocked from reaching the output. It is like having an open circuit between the input
and the output when the enable pin is at logic 1. In such a case, the outputs are said to be in high
impedance state, also called as tristate. The two active low enable pins are connected together when
the chip is to be used as an octal tristate buffer.
In Fig. 22.9, the 74244-tristate buffer is used as an I/O-mapped I/O port with the port address as 77H.
This could be easily verified from the chip select circuit used in the figure. When IN 77H instruction
is executed by the 8085, 77H = 01110111 is sent out on both AD7.0 and A 15_8 during T1 oflORmachine
378
0 A15-0O» 74244 IC
e
1 A 14 Enable 2
i
i
o An .No* i
v
l
1 From
1 As 3 ' i/p ■< > to 8085
0
device
1 IO/M
s2 0 RD
2
Fig. 22.9
Io Octal tri-state
2 buffer chip— 74244
.2 Enable 1
s
10
§ cycle. Then gate-1 (NAND) output becomes 0. During Tl, IO/M* also becomes 1. During T2 of this
co
« machine cycle, RD* becomes 0. Hence gate-2 (NAND) output also becomes 0 which results in gate-3
H (OR) output becoming 0. This enables the tristate buffer. During T2 of this IOR machine cycle, the data
presented by the input device is received in the accumulator of 8085 via the data bus. Thus the
74244-tristate buffer content is received in the accumulator of 8085 when IN 77H is executed.
In a microcomputer system there could be a number of input devices. The processor would like to
select one of them and read the data present in the input device. All the input devices send their data
to the data bus using tristate gates as shown in Fig. 22.10.
00H 7 FP
Device- 1 PI
7
Suppose the input devices directly send their data to the data bus without the intervening tristate
buffers. Also suppose that device-1 and device-2 send the data 00H and FFH respectively. Then the
data bus is attempted to be loaded with 00H and FFH simultaneously. This will damage the micro¬
computer system. As per the scheme shown in Fig. 22.10, the data bus is loaded with 00H if the
processor selects PI and FFH if it selects P2.
More commonly, instead of using a simple tristate buffer as an input port, a port of 8255 PPI chip
is used. This would result in a reduction of the number of chips, as the 8255 has three 8-bit I/O ports.
Also, programmable devices are more convenient to use than non-programmable devices like 74244.
A circuit that uses 74244 to interface a simple keyboard with eight keys is shown in Fig. 22.11.
In Fig. 22.11, one end of all the keys is connected to logic 1. The other end of the key that is con¬
nected to the 74244 input is also connected to ground through a resistor. This resistor is normally
called a pull-down resistor. A pull-down resistor helps in bringing the logic level at a key that is not
pressed to logic 0. Pull-down resistor value used is typically 1 Kfi. If only key 3 is pressed, then 13
input of 74244 will be logic 1. In this case the pull-down resistor will have no effect in the circuit oper¬
ation. All other inputs of 74244 will be logic 0. The pull-down resistors help in bringing these inputs
of 74244 to logic 0. Thus, when only key 3 is pressed, the inputs to the 74244 would be 00001000 =
08H. This value gets loaded into the accumulator when IN 77H instruction is executed.
379
74244
I? JL7
Note:
0 -L6
For simplicity, pull down
0 _L_5 resistors are shown for
To Accumulator 0 _L 4 I0, 1, and I7 inputs only.
in 8085 0 Flowever, they are present
O7-0 l I3 _L2
+5 V
for the remaining inputs =
5*»
0 ft
also.
0 _L I
0 ii _L 0 Fig. 22.11
Io Interfacing a simple
Chip Select Circuit
ETIIEÿT
1K£2
keyboard using i-o a
</>
74244-tristate buffer
A15.8 RD, IO/M
The circuit to interface a simple keyboard as described earlier has several drawbacks. They are
indicated as follows.
■ Assume that when IN77H instruction is executed, the accumulator receives the value
00000000 = 00H. It means that none of the eight keys are pressed at that moment. But the user
expects the processor to detect and identify the key as soon as it is pressed. For this reason, the
processor should keep checking in a loop till a key is pressed, as indicated below.
AGAIN: IN 77H
CPI 00H
JZ AGAIN
■ The processor exits the loop only when a key is pressed. Let us say the user presses a key on the
keyboard once every second on an average, which means that the processor spends 1 s in the
loop. In 1 s, the processor could have executed several hundred thousand useful instructions.
Instead of that, it is simply wasting its time in the loop. This is a very inefficient way to utilize
the capabilities of the processor. To solve this problem, a mechanism should be provided that
interrupts the processor whenever a key is pressed. Only then the processor should read the input
port. This would avoid the waste of time by the processor in the loop waiting for a key to be
pressed.
■ The keys used in the keyboard are spring loaded mechanical keys. So when a key is
pressed, it performs a number of ‘makes’ and a number of ‘breaks’, before it settles down to
make the contact. Similarly, when the finger is removed from a pressed key, it performs a
number of ‘breaks’ and a number of ‘makes’, before it settles down to break the contact. This
problem is called ‘contact bouncing’. The design of the keyboard is such that the bouncing dies
down in about 20 |xs. Thus, when a key is found to be pressed, provision has to be made to
check it again after a debouncing time of about 20 |xs, to make sure that the key is really
pressed.
■ If the number of keys is more, say 64, then eight numbers of 74244 chips are needed, which
increases the cost. Even if it is decided to use 8255 ports, three 8255 chips are needed, as each
8255 has only three ports.
Using a matrix keyboard solves the last problem. Even if there are 64 keys, just one 8-bit output
port and one 8-bit input port are enough for a matrix keyboard. The interfacing of matrix keyboard is
described next.
380
■ 22.4 INTERFACING A MATRIX KEYBOARD
A matrix keyboard will have keys arranged in the form of a matrix of several rows and columns.
Figure 22.12 indicates the interfacing of a matrix keyboard having four rows and four columns. At the
o intersection of every row and column a key is connected. Thus there are a total of 4 X 4 = 16 keys in
sE the matrix. The column lines are connected to Gnd through pull-down resistors.
!
.2
S
in Row 3
§ From juup
00 j/r _ */ÿ
■e
H
Row 0 3/»- 2/,
col 2
J/.
col 2 col I col 0
PortC input port
of 8255
Fig. 22.12
to p,p <f: Interfacing a matrix keyboard
Even if the matrix size were to be 8 X 8, for a total of 64 keys, just one output port and one input
port would suffice. In the case of a simple keyboard discussed earlier, eight input ports were needed for
this size of the keyboard. The identification of the key pressed on the keyboard takes place as follows.
For example, let us say the user presses key 9 that is at the intersection of row 2 and column 1. The
microprocessor performs a scan of the matrix keyboard, a row at a time. It starts this operation with
row 0. Using the output port the processor sends logic 1 on row 0. On the other rows, logic 0 is sent
out. This is done very easily using the instructions
MVI A, 0000000 IB
OUT PC
Then the processor reads the input port. Presently, logic 1 is sent out only on row 0. But no key is
pressed on this row. As such, the input port receives the value 0000 from the keyboard because of the
pull-down resistors. The reader might think about the key 9 that is pressed on row 2. But logic 0 is
sent out presently on row 2. Hence only logic 0 is received on column 1 via the pressed key. The
processor compares the value that is read from the input port with 0000. If both are same, it means
that no key is pressed on row 0.
The processor then scans row 1. For this purpose, using the output port the processor sends logic
1 on row 1, while logic 0 is sent out on the other rows. The actual programming details are omitted at
this stage for simplicity. Then the processor reads the input port. Presently, logic 1 is sent out only on
row 1. But no key is pressed on this row. As such, the input port receives the value 0000 from the
keyboard because of the pull-down resistors. The reader might think about the key 9 that is pressed
on row 2. But logic 0 is sent out presently on this row. Hence only logic 0 is received on column 1 via
the pressed key. The processor compares the value that is read from the input port with 0000. If both
are the same, it means that no key is pressed on row 1 also.
The processor scans row 2 next. For this purpose, the processor sends logic 1 on this row, while
logic 0 is sent out on the other rows. Then the processor reads the input port. Now because of the logic
1 present on row 2, and the pressing of key 9, the input port receives 0010, which means logic 1 on
381
column 1 and logic 0 on the other columns. The processor compares the value that is read from the
input port with 0000. As they are not same, it means that a key is pressed. The pressed key is present
on row 2, as this row was presently scanned. The pressed key is present on column 1, as the input port
received logic 1 from column 1. Thus it means that the key pressed was at the intersection of row 2
and column 1.
This row and column information about the key pressed must be converted by the software to the =
5*»
ft
value 9 so that the processor identifies that key 9 was pressed. In this example, multiplying the row
number by 4 (as there are four keys in every row) and adding the column number as shown in the
following achieves the objective.
*5
4 X row no. + col. no. !ÿ
o a
4X2 + 1 =9 </>
The advantage of matrix keyboard is that only one input port and one output port are enough even
when there are eight rows and eight columns, for a total of 64 keys in the matrix.
The disadvantages of the matrix keyboard are as follows.
■ Suppose after scanning all the rows it was found that no key was pressed by the user. But the user
expects the processor to detect and identify the key as soon as it is pressed. For this reason, the
processor should keep scanning the entire keyboard in an infinite loop till a key is pressed. This
is obviously a very inefficient way to utilize the capabilities of the processor. To solve this prob¬
lem, a mechanism should be provided that interrupts the processor whenever a key is pressed.
Only then the processor should perform the scanning. This would avoid the waste of time by the
processor in the scanning process.
■ The contact bounce problem still persists in the matrix keyboard. Thus, when a key is found to be
pressed, a provision has to be made to check it again after a debouncing time of about 20 |xs, to
make sure that the key is really pressed.
■ The processor has to execute a lengthy program to scan the matrix keyboard. This is because the
processor has to execute a number of instructions to scan the keyboard a row at a time. When a
key is found pressed, it has to confirm the pressing of the key after debouncing. Finally it has to
convert the row number and column number to proper key value.
To overcome these drawbacks, a very specialized chip is used for interfacing matrix keyboards in
many microprocessor-based equipments. It is the Intel 8279 keyboard and display controller IC. This
single chip can be used to interface an 8 X 8 matrix keyboard. This chip automatically scans a row at
a time. If a key is found pressed, it will wait for the de-bounce time, and then again scans the key¬
board. Only after debouncing the keyboard, it interrupts the processor if it finds a key pressed. In the
interrupt service routine the processor reads from this chip to find out about the key pressed. In this
way, the processor is completely freed from the problem of scanning the keyboard, wait for de-bounce
time, and the like. The details of the working of Intel 8279 are provided a little later.
The purpose of this section is to acquaint the reader with the programming complexity involved in
interfacing a matrix keyboard using conventional I/O ports of 8255 PPI. This would help the reader
appreciate the benefits derived from using 8279 chip as described later.
382
For this purpose, readily available keyboard and display interface (Model ALS-NIFC-09) from M/s
Advanced Electronics Systems is made use of. This interface has two parts: keyboard interface and
display interface. In this section only the keyboard interface portion is explained. The display inter¬
face has already been explained in an earlier section.
The keyboard interface contains 20 keys. They are 0, 1 9, \\ ‘+ \ ‘-H\ ‘X’, ‘%’, C, CE,
o FI, and F2. The physical layout in Fig. 22.7 gives the impression that it is a matrix keyboard with four
rows and five columns. The circuit details of the keyboard interface portion are provided in Fig. 22.13.
sE From this figure it should be clear that the keys are arranged as a matrix of three rows and eight
columns. As such, 24 keys could have actually been there on the keyboard. Flowever, as the keyboard
!
.2 is intended for a simple calculator, the 20 keys provided on the keyboard is adequate.
S
w HP kit Keyboard interface
§
CO 8255
0
H
PA7
PA
Port A as input;
Port C lower portion as output;
Configuration of other ports is unimportant.
Write an 8085 assembly language program that uses 3x8 matrix keyboard interface, and display in
the data field of the kit the scan code of the key that is pressed on the interface.
383
The scan codes assigned for the various keys on the keyboard interface are as follows.
7 07
8 08 -o°~
<P
9 09 v>
0A
OB
+ oc
OD
X OE
% OF
C 10
CE 11
FI 12
F2 13
In the 3 X 8 matrix keyboard, the scan code of the key pressed is obtained by multiplying the row
number by 8 (as there are eight keys in every row) and adding the column number.
Program
ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH
UPDDT EQU 06D3H
CURDT EQU FFF9H
MVI A, 10010000B
OUT CTRL ;Configure PA as input and PC lower as output port
AGAIN: CALL SCANKBD ;Get the scan code of key pressed in A register
MVI B, 00H
STA CURDT
CALL UPDDT ;Display scan code in Data field with no dot
JMP AGAIN ;Remain in infinite loop
SCANKBD: MVI C, 03H ;C indicates the no. of rows still to scan
MVI D, 00H ;D indicates the row number being scanned
MVI A, 8 OH
NXTROW: RLC ;Now, A has required pattern to scan desired row
MOV B, A ;Save A value in B
OUT PC ;Send logic 1 on the row to be scanned
384
IN PA /Read the columns of the keyboard
CPI 00H /Compare with 00H
JNZ KEY_ID /If a key is pressed on the row scanned go to KEY_ID
MOV A, B /Restore A value
INR D /D points to next row
0 DCR C Z
JNZ NXTROW ;Go to NXTROW if scan of all rows not complete
sE JMP SCANKBD /Be in an infinite loop if no key is pressed
KEY_ID: MVI C, 08H ;C used as down counter. No. of columns is 8
!
.2
MVI E, 00H /E indicates the column being checked
S REPEAT: RRC /Logic state of column E brought to Carry
in
JC SKIP /Go to SKIP if logic state of column E is 1
§
co INR E /E points to next column
0
H DCR C/
JNZ REPEAT /Go to REPEAT if testing of all columns not over
Intel 8279 is a specialized I/O chip that has two major functions. It takes care of scanning a matrix
keyboard, as well as refreshing a multiplexed display so that the processor is relieved of these
mundane tasks. A very brief description of the working of 8279 is indicated in the following two para¬
graphs and details are provided later.
The 8279 scans a matrix keyboard continuously. During the scan if a key on the keyboard is found
pressed, it performs the scan again after about 10 |is, allowing for the key bounce to die down. If the
key is still found pressed, it is interpreted as a valid key depression. Then the scan code of the key
pressed is stored in the FIFO RAM. The scan code indicates the location of the key in terms of row
position and column position, as well as the status of shift and Ctrl keys. FIFO RAM is inside the 8279
having a size of eight words, each of 8 bits width. Then IRQ, the interrupt request output, is activated
by the 8279. The IRQ output is connected to an interrupt request pin of the microprocessor. The
processor reads the FIFO RAM on a first-in first-out basis to receive from the 8279 the scan code of
the key pressed. As the scanning, debouncing, and storing of scan code are all automatically
performed by the 8279, the processor is relieved of these mundane tasks.
The 8279 is also used for refreshing multiplexed display consisting of as many as sixteen
7-segment LEDs. The 7-segment codes for the characters to be displayed are stored in the display
RAM. Display RAM is inside the 8279 having a size of 16 words, each of 8-bits width. There are 16
locations in the display RAM corresponding to the sixteen 7-segment LEDs. The 8279 automatically
sends out the 7-segment code from a display RAM location and displays a character on the
corresponding LED. After a short time of about half a millisecond, the LED that is presently display¬
ing a character is switched off. The 7-segment code from the next display RAM location is then sent
385
out and a character is displayed on the corresponding LED. The operation is continued until all the 16
LEDs are lighted up one at a time. This cycle takes only about 10 p,s. The cycles are then repeated
endlessly so that about 100 cycles are performed in a second. Because of the principle of ‘persistence
of vision’, all the 16 LEDs seem to display the characters simultaneously and continuously. As all the
aforementioned tasks are performed automatically by the 8279, the processor is relieved of these mun¬
dane tasks. =
5* »
ft
RL2 I 40 Vcc
RL3 2 39 RL1
elk 3 38 RLO
IRQ 4 37 Ctrl/Stb
RL4 5 36 Shift
RL5 6 35 SL3
RL6 7 34 SL2
RL7 8 33 SL1
Reset 9 32 SLO
RD 10 31 B0
WR II 30 B,
DQ< — » 1312 29 B2
Dj < —* 28 B3
D2<— » 14 27 Ao
D2 <— > 15 26 A,
> 16 25 A2
D5 17 24 As
D«<— > 18 23 BD
Fig. 22.14
D7 <— > 19 22 cs
Physical pin diagram of
Gnd 20 21 C/D
Intel 8279
Following is the description of the pins of 8279. The reader is advised to read this portion once again
after the working of 8279 is explained.
Vcc and Gnd: Power supply and ground pins. 8279 uses +5-V power supply.
D7-0: Eight bi-directional data pins for communication with the processor.
C/D An address input pin. A logic 1 on this pin selects control/status buffer inside the
8279. Logic 0 on this pin selects data buffer inside the 8279. From the viewpoint of
the processor, these are the only two buffers inside the 8279. See Fig. 22.16 that pro¬
vides the internal architecture of 8279.
RD*: Active-low input pin that is activated by the processor to read data buffer or status
buffer from the 8279. The information comes to the processor from data buffer if
C/D is at logic 0. The data buffer could have received the data from FIFO/sensor
RAM or display RAM. The information comes to the processor from status buffer if
C/D is at logic 1.
386
Intel 8279
Vcc Shift
Gnd Ctrl/Stb
RL7-0
0
cs SL3-0
sE RD
A3-0
!
.2
WR
C/D
B3.0
s BD
w elk
§ Reset
CO
0 IRQ
H Fig. 22.15
Functional pin dia¬
gram of Intel 8279
WR*: Active-low input pin that is activated by the processor to write to data buffer or control buffer
inside the 8279. The processor writes to the data buffer if C/D is at logic 0. The content of
data buffer is sent to display RAM. The processor writes to the control buffer if C/D is at
logic 1.
CS*: Active-low input pin used for selecting the chip.
Clk: It is the clock input pin. The maximum clock frequency on this input is 2 MHz for 8279.
For 8279-5, the maximum clock frequency is 3.1 MHz. On the ALS kit, the 3-MHz clock
output of 8085 is divided by 2 and the resulting 1.5-MHz frequency is provided as the clock
input to 8279. This frequency is internally divided using a programmable divider by a max¬
imum value of 31. In fact, the default value of the programmable divider is 31. This results
in the internal clock frequency of about 50 kHz.
Reset: It is an active-high input pin. It is normally connected to the ‘reset out’ pin of 8085. As such,
when the 8085 is reset, it sends out a logic 1 pulse on the ‘reset out’ pin thus resetting the
8279. After reset, the 8279 will be placed in the following state. The new terms that we
come across are explained later.
D3 D2 D1 DO
B3 B2 B1 BO
d c b a
A3-<f These are the output pins of 8279. The MS hex digit of a display RAM location is sent out
on these pins. These pins can be used to drive a 4-bit binary to 7-segment code converter.
388
In such a case, it is possible to interface two sets of 7-segment displays, each having a
maximum of 16 digits display. More commonly, these pins directly drive four of the eight
segments of a 7-segment display. On the ALS kit, these pins are connected to dp, g, f, and
e-segments of the 7-segment display unit. The correspondence among D7.4, A3.0, and dp, g, f, e
segments is as shown in the following. With this scheme, the display unit can have a maximum
o of 16 digits display.
sE D7 D6 D5 D4
A3 A2 A1 AO
!
.2
dp g f e
s BD*: It is an active-low output status pin. This pin is activated by the 8279 when display is
w
§ blanked because of a clear command or a display blank command. It is also activated during
00 switching. This pin is left unconnected on the ALS kit.
0
H
Data port
Control/status port
Intel 8279
Control port
Selected (write only)
when
A0=l Status port
(read only)
FIFO/sensor RAM
size: 8x8
Read only
Selected
when
A0=0 Display RAM
Size: 16x8
Write as well as Fig. 22.16
read capable
Internal architecture
of 8279
The 8279 can be used to interface a matrix keyboard, like the one used in microprocessor kits or a
matrix of switch sensors. It can also be used as a strobed input port.
389
The ‘keyboard/display mode set’ command (to be discussed later) indicates whether 8279 is used
to interface a matrix keyboard, a matrix of switch sensors, or a strobed input port. However, very com¬
monly the 8279 is used for interfacing a matrix keyboard rather than a matrix of switch sensors. It is
very rarely, if at all, used as a strobed input port.
□□
+5 V
Shift Ctrl
SL3
SL2
SLt y-'
The 8279 performs a scan of the matrix keyboard in decoded mode of operation as follows. One
row at a time is made logic 0 and the information on the eight columns are read. The pattern sent out
on SL3_0 by the 8279 for scanning the keyboard is shown in the following table.
Because of the internal pull-up circuits, the information on a return line will be at logic 1 if the key
on that column is not pressed. Hence, if all the return lines are at logic 1 it means that no key is pressed
on the row scanned. If any key is pressed on the row being scanned, then logic 0 will be received on
the corresponding return line by the 8279.
As an example, assume 8279 to be presently scanning row 3 by sending 0111 on SL3_0 and the
information received on the return lines RL7_0 is 10111111, indicating that RL6 is at logic 0. Then in
the FIFO RAM the following information will be stored.
Ctrl Shift 0 1 1 1 1 0
The LS 3 bits indicate the column on which the key pressed is connected. Three bits are needed to
provide column information, as there can be eight columns in the matrix. The LS 3 bits are 110 in the
above example. It means that the key pressed is on column 6.
390
The next 3 bits indicate the row on which the key pressed is connected. Just 2 bits are enough to
provide row information when there are only four rows in the matrix. However, 3 bits are used to
provide row information, as there can be eight rows in the matrix in the encoded mode of operation.
These 3 bits are 011 in the above example. It means that the key pressed is on row 3.
The Ctrl bit = 1 and Shift bit = 1 when the Ctrl and Shift keys are not pressed. In the simple matrix
o keyboards of the type found in microprocessor kits, the Ctrl and Shift keys are not used. Even in such
cases, because of the internal pull-up circuits, these bits will be in logic 1 state.
sE The scan code with the pattern as described is stored in the FIFO RAM. Then the IRQ output is
activated by the 8279. The IRQ output is connected to an interrupt request pin of the processor. On
.2
I the ALS kit, the IRQ output is connected to RST 5.5 interrupt pin of 8085. The IRQ output becomes
S logic 0 when the processor reads the FIFO RAM. This is achieved by issuing ‘read from FIFO RAM’
w command and then reading the data port of 8279.
§
CO It is possible that the processor is in disable interrupt state when the IRQ output of 8279 is
0
activated. In such a case, the scan code of the key pressed remains in the FIFO RAM. If another key
H
is pressed, the scan code of this key is stored in the FIFO RAM behind the scan code of the earlier
key in a queue form.
Status register: There is a status register in 8279. In this the LS 3 bits, shown as NNN, provide a
3-bit field that indicates the number of characters present in the FIFO RAM. The format of the status
register is shown as follows.
Du S/E O U F N N N
To start with, this 3-bit field is 000. It is incremented by 1 whenever a scan code is entered into the
FIFO RAM. The IRQ output is activated whenever there is data in the FIFO RAM. The IRQ output
becomes logic 0 after a read operation of FIFO RAM. At this point, the 3-bit field is decremented
by 1. If the 3-bit field is non-zero, the IRQ output is activated again.
If the processor remains in disable interrupt state and the user presses seven keys one after another,
then the 3-bit field in the status register would change to 111. If one more key is pressed, the scan code
of this key is also stored in the FIFO RAM and the FIFO RAM becomes full. This is indicated in the
status register by incrementing the 3-bit field from 111 to 000 and setting the F bit to 1. Thus the LS
4 bits of status register become 1000. The F bit has the following meaning.
F = 1 means that the FIFO RAM is full.
F = 0 means that the FIFO RAM is not yet full.
If a key is pressed after the FIFO RAM is full, the scan code of the key pressed overwrites the last
character in the FIFO RAM and causes over-run error. Setting of O bit in the status register indicates
the over-run error.
Similarly, if the LS 4 bits of the status register are 0000, it means that the FIFO RAM is not full,
and there are no characters in the FIFO RAM. In such a case, if an attempt is made to read from the
FIFO RAM, it causes underflow error indicated by the setting of U flag in the status register. The
meaning of the MS 2 bits of the status register is described later.
Two-key lockout mode: The scanning of the keyboard can be either two-key lockout mode or N-key
rollover mode. The mode of operation is decided by keyboard/display mode set command. In the two-key
lockout mode, the de-bounce logic is set when a key is pressed. Other pressed keys are looked for during
the next two scans. If none are encountered, it is treated as pressing of a single key and its scan code is
entered into the FIFO RAM. If another key pressing is encountered, no entry is made to the FIFO RAM.
391
The keyboard scanning, and de-bouncing are done for some fixed periods. It is dependent on the
internal clock frequency, which is about 5 |xs for keyboard scan time and 10 p.s for de-bounce time
when the internal clock is 100 kHz. The internal clock is derived by suitably dividing the external
clock input frequency using the programmable divider present in 8279. For this purpose it is neces¬
sary to issue ‘program clock’ command to the 8279.
Program clock command: There are eight command words in the 8279. All of them are written to =
the same control port. The MS 3 bits of the control port identify the command as shown in the
following table.
3 aj »
ft
*5
l-s.
o
MS 3 bits of control port Command type </>
When the MS 3 bits in the control port are 001 it means that it is program clock command. The
format for the program clock command that is written to the control port of 8279 is as follows.
0 0 1 P P P P P
The LS 5 bits indicated as PPPPP provide a number in the range 2-31. Note that PPPPP should not
be made 00000 or 00001. If the external clock input is 1.5 MHz, PPPPP has to be 01111 = 15 so that
the internal clock frequency is 100 kHz. The default value of PPPPP after reset of 8279 is 11111 = 31.
Port addresses of the 8279 on the ALS kit: The Intel 8279 used on the ALS kit is connected in the
system as an I/O-mapped I/O port with the addresses as indicated in the following.
If it is desired to have the internal clock frequency as 1/15 of the external clock frequency, then the
execution of the following instructions achieves this objective on the ALS kit.
MVI A, 00101 11 IB
OUT D1H
N-key rollover mode: In the N-key rollover mode, pressing of each key is considered independent
of the pressing of other keys. When a key is pressed, the de-bounce circuit waits for two keyboard
scans and then checks to see if the key is still pressed. If it is, the scan code of the key is entered into
the FIFO RAM. It does not mind the simultaneous pressing of other keys. If several keys are pressed
simultaneously, the keys are recognized and entered into FIFO RAM according to the order the key¬
board scan found them.
392
Encoded mode of operation: In the decoded mode of operation the maximum number of rows pos¬
sible in the matrix keyboard are only four. Because of this limitation, the decoded mode is not the
common mode of operation. More commonly, the 8279 is used in encoded mode of operation.
It is possible to configure the 8279 to operate in encoded scan mode by issuing the appropriate
‘keyboard/display mode set’ command. The matrix keyboard can then have up to eight rows and eight
o columns, for a total of 8 X 8 = 64 keys. At the intersection of every row and column, a key of the
keyboard is connected. In addition to these 64 keys, Shift and Ctrl keys could be present for a total
sE of 66 keys. The Shift and Ctrl keys do not form a part of the matrix. Such an encoded scan matrix
.2
I keyboard is shown in Fig. 22.18.
S
w +5 V
§
oo
«
Row 7 I □□
H SL2 External Shift Ctrl
SL, 3 to 8
decoder Row 1
SL0
X7 A
y*
Fig. 22.18
Row 0
RL0 Encoded scan matrix
RL7 RL6
Note: Only 2 keys instead of 64 keys, are shown in the figure simplicity. keyboard
As can be seen from Fig. 22.18, an external three to eight decoder is used when an encoded
operation is desired. Row information is sent out on SL2.0 in coded form and SL3 is unused. The
decoder outputs are connected to the eight rows of the matrix. Thus the operation is referred to as
encoded mode. The decoder decodes the scan lines SL2_0. The 8279 performs a scan of the matrix key¬
board in encoded mode of operation as follows. One row at a time is made logic 0 and the information
on the eight columns is read. The pattern sent out on SL2_0 by the 8279 for scanning the keyboard in
encoded mode is shown in the following table.
Because of the internal pull-up circuits, the information on a return line will be at logic 1 if the key
on that column is not pressed. Hence, if all the return lines are at logic 1, it means that no key is
pressed on the row scanned. If any key is pressed on the row being scanned, then logic 0 will be
received on the corresponding return line by the 8279.
As an example, assume 8279 to be presently scanning row 5 by sending 101 on SL2_0 and the infor¬
mation received on the return lines RL7_0 is 10111111, indicating that RL6 is at logic 0. Then in a FIFO
RAM location the following information will be stored.
Ctrl Shift 1 0 1 1 1 0
393
The LS 3 bits are 110 in the above example. It means that the key pressed is on column 6. The next 3
bits are 101 in the above example. It means that the key pressed is on row 5. The MS 2 bits provide
the status of Ctrl and Shift keys. As there can be up to eight rows in the matrix keyboard when the
8279 operates in encoded mode, this mode is the preferred mode of operation in general.
Interfacing matrix of switch sensors: Intel 8279 can also be configured to scan a matrix of switch =
5*»
ft
sensors such as the metal strips and magnetic sensors found on large departmental store doors and
windows. In this mode, the 8279 scans all the switch sensors arranged in the form of a matrix. If it is
the decoded mode of operation, the matrix size is 4 X 8 = 32 switch sensors, and if it is the encoded
mode, the matrix size is 8 X 8 = 64 switch sensors. In the scanning of matrix of switch sensors, the
*5
l-s.
o
Shift and Ctrl pins in the 8279 have no role to play. This is because, the scanning of switches, and not </>
keys, is performed in this mode.
During the scanning of the matrix of switch sensors, the FIFO RAM works as sensor RAM. Each
row of the sensor RAM is loaded with the data present on return lines during the scan of the corre¬
sponding row in the matrix of switch sensors shown as follows.
Only four sensor RAM locations are used in decoded mode of operation and all the eight sensor
RAM locations are used in encoded mode of operation. If the eight-switch sensors on row 5 have the
logic value as 10010011, then the return lines receive this information during the scan of row 5, and
so row 5 of the sensor RAM is loaded with the value 10010011.
If a burglar tries to make a forceful entry to the departmental store by pressing a door or a window,
then the switch sensor on the door or window changes its logic state. This results in the activation of
IRQ output causing an interrupt to the processor. It also causes the S/E bit in the status register to be
set. The S/E bit acts as S bit when the 8279 is interfaced with a matrix of switch sensors. When the S
bit is set, it indicates at least one switch sensor closure. The interrupt service routine could then sound
an alarm, and unchain watchdogs thus thwarting the designs of the burglars. The IRQ output is
deactivated by the first data read operation of the sensor RAM if the 8279 is not in auto increment
mode. Auto increment mode of operation will be described very shortly. If working in auto increment
mode, the IRQ output is deactivated only by the execution of end interrupt command, which is
described later. The sensor matrix will not be altered by the changes in switch sensors untill the IRQ
output gets deactivated. The 8279 is rarely used for interfacing matrix of switch sensors.
Read from FIFO/sensor RAM command: To read from the FIFO/sensor RAM, the ‘read from
FIFO/sensor RAM’ command has to be issued to the 8279 control port. The format for the read from
FIFO/sensor RAM command that is written to the control port of the 8279 is as follows.
0 1 0 Ai X A A A
When the MS 3 bits in the control port are 010, it means that it is ‘read from FIFO/sensor RAM’ com¬
mand. All the LS 5 bits in this command are don’t-care bits, if the operation is reading from FIFO
RAM. This is because there is no need to specify the address when reading from a FIFO RAM.
If the operation is reading from sensor RAM, then the LS 3 bits, shown as AAA, select the sensor
RAM location. Three bits are needed, as there are eight locations in the sensor RAM. The bit denoted
394
as X is a don’t-care bit. Generally a 0 is entered for this bit value. The bit denoted as Ai is the auto
increment bit. The use of this bit is explained with an example.
Suppose the 8279 is interfaced with a matrix of switch sensors. Let us say ‘read from sensor RAM
location 3 in auto increment mode’ command is issued to the command port of 8279. Then the sensor
RAM pointer is loaded with address 3. Hence when the processor reads from the sensor RAM for the
0 first time, it will be from sensor RAM location 3. Then automatically the sensor RAM pointer value
is incremented to 4. So when the processor reads from the sensor RAM next time, it will be from sen¬
sE sor RAM location 4 automatically. Then the sensor RAM pointer value changes to 5. So when the
processor reads from the sensor RAM for the third time, it will be from sensor RAM location 5. The
I
.2 following program segment achieves this objective on the ALS kit.
S
w MVI A, 01010011B
§ OUT D1H ; 'Read from Sensor RAM location 3 in Ai mode' command
CO
« IN DOH ; Accumulator loaded with contents of row 3 of sensor RAM
H MOV B, A
IN DOH ; Accumulator loaded with contents of row 4 of sensor RAM
MOV C, A
IN DOH ; Accumulator loaded with contents of row 5 of sensor RAM
Without the auto increment facility, a read from FIFO/sensor RAM command would be needed for
every read operation from a sensor RAM location. Thus auto increment mode reduces programming
effort.
Interfacing strobed input port: In the strobed input mode of operation the contents on the return lines
of the 8279 are strobed into the FIFO RAM when the Ctrl input makes a 0 to 1 transition. The Ctrl pin
is now acting like a strobe input. As such, the Ctrl pin is generally indicated as Ctrl/Stb pin. Thus the
data that enters the FIFO RAM when the Ctrl/Stb makes a zero to one transition is indicated as follows.
This mode of operation is somewhat similar to the mode-1 operation of Port A or Port B of 8255 PPI
chip. The 8279 is rarely used in this mode of operation.
The 8279 can be simultaneously used for interfacing a matrix keyboard as well as for refreshing mul¬
tiplexed display consisting of as many as sixteen 7-segment LEDs. All that is needed to display a char¬
acter on a 7-segment display is just storing the corresponding 7-segment code in a display RAM loca¬
tion. The processor is not required to do anything else. The display RAM inside the 8279 is having a
size of 16 words, each of 8-bits width.
LED connection details on the ALS kit: On the ALS kit only six 7-segment LEDs are used. The LED
positions seen physically on the board and their corresponding display RAM addresses are indicated
in the following. Note that there are no LEDs corresponding to display RAM locations 0 and 1. For
convenience, the LEDs are numbered from 2 to 7 so that LED positions and display RAM locations
correspond.
395
Display RAM location 7 6 5 4 3 2 1 0
LED number 7 6 5 4 3 2
Display field Addr Addr Addr Addr Data Data
MS LS MS LS
If it is required to display ‘ABCD’ in the address field of the ALS kit, then the following 7-segment
codes have to be stored in display RAM locations 4 to 7.
Write to display RAM command: To write to the display RAM, the ‘write to display RAM’ com¬
mand has to be issued to the 8279 control port. The format for the write to display RAM command
that is written to the control port of the 8279 is as follows.
1 0 0 Ai A A A A
When the MS 3 bits in the control port are 100, it means that it is ‘write to display RAM’ com¬
mand. The LS 4 bits, shown as AAAA, select the display RAM location. Four bits are needed, as there
are 16 locations in the display RAM. The bit denoted as Ai is the auto increment bit. The use of this
bit is explained with an example.
Let us say ‘write to display RAM location 4 in auto increment mode’ command is issued to the
command port of 8279. Then the display RAM pointer is loaded with the address 4. So when the
processor writes to the display RAM for the first time, it will be to display RAM location 4. Then the
display RAM pointer value is automatically incremented to 5. So when the processor writes to the dis¬
play RAM next time, it will be to display RAM location 5 automatically, and so on. The following
program segment achieves the objective of displaying ‘ABCD’ in the address field on the ALS kit.
MVI A, 10010100B
OUT D1H ; 'Write to Display RAM location 4 in Ai mode' command
MVI A, A1H
OUT D0H ; Display RAM location 4 loaded with A1H
MVI A, C6H
OUT D0H ; Display RAM location 5 loaded with C6H
396
MVI A, 83H
OUT DOH ; Display RAM location 6 loaded with 83H
MVI A, 88H
OUT DOH ; Display RAM location 7 loaded with 88H
o At the end of the execution of the aforementioned program segment, the display RAM pointer will
have the value 8.
sE
Read from display RAM command: It is possible to read the contents of any display RAM location.
.2
I To read from a display RAM location, the ‘read from display RAM’ command has to be issued to the
S 8279 control port. If the data port of 8279 is read without issuing ‘read from display RAM’ or ‘read
in from FIFO/sensor RAM’ command, the 8279 is not clear about the information to supply to the
§ processor. The format for the read from display RAM command that is written to the control port of
co
0 the 8279 is as follows.
H
0 1 1 Ai A A A A
When the MS 3 bits in the control port are 011, it means that it is ‘read from display RAM’ com¬
mand. The LS 4 bits, shown as AAAA, select the display RAM location. Four bits are needed, as there
are 16 locations in the display RAM. The bit denoted as Ai is the auto increment bit. It is to be noted
that the display RAM pointer is the same for both read and write operations. The use of the Ai bit is
explained with an example.
MVI A, 01110100B
OUT D1H ;'Read from Display RAM location 4 in Ai mode' command
IN DOH ;Load A with contents of Display RAM location 4
;After this, Display RAM pointer value will be 5.
MVI A, A1H
OUT DOH /Display RAM location 5 loaded with A1H
/After this, Display RAM pointer value will be 6.
IN DOH /Load A with contents of Display RAM location 6
/After this, Display RAM pointer value will be 7.
When IN DOH is executed the second time in the above example, the 8279 remembers that IN DOH
refers to reading of display RAM and not reading of FIFO/sensor RAM. If our interest is to read from
FIFO/sensor RAM, ‘read from FIFO/sensor RAM’ command has to be issued. Then subsequent read
operations will be from FIFO/sensor RAM.
Refreshing of 7-segment display devices: The number of 7-segment LEDs, which can be connected
using the 8279 depends on the mode of operation of the 8279.
Decoded mode of operation: In the decoded mode of operation external decoder is not made use
of. One scan line at a time is made logic 0. This selects a particular LED position for the display
purpose. As such, there can be a maximum of only four 7-segment LEDs. Only the first four dis¬
play RAM locations are used in this mode. Hence the decoded mode of operation is rarely used. The
pattern sent out on SL3_0 by the 8279 for selecting an LED position for display is shown in the
following table.
397
SLj-o Selected LED
1110 LED-0
1101 LED-1
1011 LED-2
0111 LED-3
The interfacing of common-anode 7-segment LEDs in decoded mode is shown in Fig. 22.19. First of
all, the 8279 outputs 1110 on SL3.0. This results in LED-0 receiving the 5-V power supply to its anode,
_=
®
ft
j*
2.
and all other LEDs receiving 0-V power supply for their anodes. *5
l-s.
o
</>
SL, 1»
SL2 >ÿ
SL! -i»
SL0
1
I I I I
n □ □ n
Fig. 22.19
A3-0
Interfacing 7-segment LEDs
B3-0 in decoded mode
Meanwhile, the contents of display RAM location 0 is output on A3_0 and B3_0 by the 8279. All the
7-segment LEDs receive this information. However, a character is displayed only on LED-0 that
receives the 5-V power supply.
If the internal clock frequency is 100 kHz, the clock period will be 10 jxs. For 48 such clocks the
signals on SL3_0, A3.0, and B3_0 will be sent. Thus the character is displayed for 480 jxs duration. After
480 jxs, the 8279 outputs on A3_0 and B3_0 a blanking code for 16 clock periods (160 jxs) that turns off
all the segments. During this period, the BD* status output is activated. The display is blanked for this
duration to prevent ghosting of information from one character to the next when data on SL3_0 is
changed to display the next character. Thus in 640 jxs one character is displayed.
Next, the 8279 sends out 1101 on SL3_0. This results in LED-1 receiving the 5-V power supply to
its anode, and all other LEDs receiving 0-V power supply for their anodes. Meanwhile the contents of
display RAM location 1 is output on A3_0 and B3.0 by the 8279. All the 7-segment LEDs receive this
information. However, a character is displayed only on LED-1 that receives the 5-V power
supply. This way in the next 640 jxs a character on LED-1 is displayed.
Proceeding this way, all the four characters can be displayed in 4 X 640 jxs = 2.5 jxs. This is
known as the display scan time. After this the display process is repeated endlessly to provide flicker-
free display.
Encoded mode of operation: The encoded mode of operation is very commonly used as upto six¬
teen 7-segment display devices can be used. In the encoded mode of operation an external decoder is
made use of. This mode can be configured for 8-character or 16-character display. It is configured by
issuing ‘keyboard/display mode set’ command. If 8279 is configured for 8-character display, SL3 is
unused. Information about LED position is sent out on SL2_o in coded form. An external three to eight
decoder decodes the scan lines SL2_0. Thus the operation is referred to as encoded mode. Only the first
eight display RAM locations are used in this mode.
398
If 8279 is configured for 16-character display, SL3 is also used. Information about LED position is
sent out on SL3.0 in coded form. An external 4 to 16 decoder is used in this mode. All the 16 display
RAM locations are used in this mode.
The interfacing of common anode 7-segment LEDs in encoded mode is shown in Fig. 22.20. It is
assumed that the 8279 is configured for a 16-character display. First of all, the 8279 outputs 0000 on
o SL3_0. This results in LED-0 receiving the 5-V power supply to its anode, and all other LEDs receiv¬
ing 0-V power supply for their anodes.
sE
I
.2
S Extemal Out 15
in 4 to 16— H>-
§ SL3-o decoder
co
« (active low
H outputs)
OutO
n
W
n TTKLED-15
nn LED-0 Fig. 22.20
Interfacing
A3-0 7-segment LEDs in
B3-0 encoded mode
In the meanwhile the contents of display RAM location 0 is output on A3.0 and B3_0 by the 8279.
All the 7-segment LEDs receive this information. However, a character is displayed only on LED-0
that receives the 5-V power supply. If the internal clock frequency is 100 kHz, as described earlier,
one character is displayed in 640 pts.
Next, the 8279 sends out 0001 on SL3_0. This results in LED-1 receiving the 5-V power supply to
its anode, and all other LEDs receiving 0-V power supply for their anodes. Meanwhile, the contents
of display RAM location 1 is output on A3_0 and B3.0 by the 8279. All the 7-segment LEDs receive
this information. However, a character is displayed only on LED-1 that receives the 5-V power sup¬
ply. This way a character on LED-1 is displayed in the next 640 jxs.
Proceeding this way, all the 16 characters can be displayed in 16 X 640 p,s = 10 JJLS. This is known
as the display scan time. The process can be repeated 100 times per second. The display process is
repeated endlessly to provide flicker-free display.
Left entry mode of display: The 8279 can be configured to operate in left entry mode or right entry
mode of operation. Left entry mode is the simpler one. In this mode, the 7-segment code present at
display RAM location n is used to display a character on LEDn. Each 7-segment LED directly corre¬
sponds to a display RAM location.
As an example, if the 7-segment code for character 6 is entered to display RAM location 4,
then character 6 is displayed on LED-4, which is the LS digit position in the address field of ALS
Kit. Next, if the 7-segment code for character 7 is entered to display RAM location 5, then char¬
acter 7 is displayed on LED-5. Next, if the 7-segment code for character 8 is entered to display
RAM location 6, then character 8 is displayed on LED-6. Finally, if the 7-segment code for char¬
acter 9 is entered to display RAM location 7, then character 9 is displayed on LED-7, which is
the MS digit position in the address field. This operation is indicated in the following table. In
this table ‘6’ stands for 7-segment code for character 6, for simplicity. A blank at an LED posi¬
tion is indicated as b.
399
1 ‘6’ 4 b b b 6
2 ‘7’ 5 b b 7 6
3 ‘8’ 6 b 8 7 6
4 ‘9’ 7 9 8 7 6
=
As character codes are entered into successive display RAM locations, the characters are displayed 5 2. »
ft
on successive LED positions. If the LEDs were numbered from left to right, like 0, 1, ..., 7, then the O £
display on the LEDs in the above example would be ‘6789’. This is very much like the appearance 5. °
when characters are typed on a typewriter. Hence this mode of operation is called ‘typewriter mode
of entry’. On the ALS kit (like on most other kits), the LEDs are numbered from right to left. As a
•
result the entry does not appear like typewriter entry on the ALS kit.
Right entry mode of display: The 8279 can be configured to operate in left entry mode or right entry
mode of operation. Right entry mode is more complex than left entry mode. In this mode, the
7-segment code present at display RAM location n does not necessarily correspond to the character
displayed on LEDn.
As an example, if 8279 is configured for 8-character, right entry mode of operation and the
7-segment code for character 6 is entered to display RAM location 4, then character 6 is displayed on
LED-7, which is the MS digit position in the address field. Next, if the 7-segment code for character
7 is entered to display RAM location 5, then character 7 is displayed on LED-7 and character 6 on
LED-6. Next, if the 7-segment code for character 8 is entered to display RAM location 6, then
character 8 is displayed on LED-7, character 7 on LED-6, and character 6 on LED-5. Finally, if the 7-
segment code for character 9 is entered to display RAM location 7, then character 9 is displayed on
LED-7, which is the MS digit position in the address field, character 8 on LED-6, character 7 on LED-
5, and character 6 on LED-4. This operation is indicated in the following table. In this table ‘6’ stands
for 7-segment code for character 6, for simplicity. A blank at an LED position is indicated as b.
As character codes are entered into successive display RAM locations, the display on all the 7-segment
LEDs change. If the LEDs are numbered from left to right, like 0, 1, ..., 7, then the display on the LEDs
in the above example would change in the sequence bbb6, bb67, b678, and 6789. This is similar to the
appearance of the characters when typed on a calculator. Hence this mode of operation is called ‘calcula¬
tor mode of entry’. On the ALS kit (like on most other kits), the LEDs are numbered from right to left. As
a result the entry does not appear like calculator entry on the ALS kit.
Keyboard/display mode set command: This is the command that configures the 8279 keyboard
and display mode. The format for the keyboard/display mode set command that is written to the con¬
trol port of 8279 is as follows.
0 0 0 D D K K K
400
When the MS 3 bits in the control port are 000, it means that it is ‘keyboard/display mode set’
command. The LS 3 bits denoted as KKK are used to configure the keyboard as indicated in the
following table.
I
.2
Oil
100
Decoded scan, N-key rollover
Encoded scan sensor matrix
s 101 Decoded scan sensor matrix
10 110 Encoded strobed input
§ ill Decoded strobed input
co
0
H
The bits denoted as DD are used to configure the display as indicated in the following table. However,
if the KKK bits indicate that it is a decoded mode, the maximum number of characters in the display
is only four.
DD Display mode
00 8-character, left entry
01 16-character, left entry
10 8-character, right entry
11 16-character, right entry
Clear command: This command is used for clearing the display and the FIFO RAM. The format for
the clear command that is written to the control port of 8279 is as follows.
1 1 0 CD C c CF CA
When the MS 3 bits in the control port are 110, it means that it is clear command.
If CA = 1, it means the clear command is used for ‘clear all’. That is, clear both FIFO RAM and
display RAM. The CF and CD bits are used for clearing the FIFO RAM and display RAM, respec¬
tively.
Thus, if CF = 1 or CA = 1, the LS 4 bits of status register are reset to 0. This means that the FIFO
RAM is cleared. Similarly, if CD = 1 or CA = 1, the display RAM is cleared. The CC bits indicate the
blanking code that is stored in all the display RAM locations, as shown in the following table.
CC Blanking code
00 00H
01 00H
10 20H
11 FFH
Blanking code of 00H is generally used with common-cathode 7-segment display units. Blanking
code of FFH is generally used with common-anode 7-segment display units. When the blanking code
401
is 20H = 00100000, all the segments except segment f will glow. This is preferred on the display of
some measuring equipment, like electronic weighing machines. If the display is entirely blank, suspi¬
cion may arise about the working condition of the display.
‘Du’ bit of status register: The writing of the blank code in all the display RAM locations takes about
160 pis. During this period it is not possible to write to the display RAM locations. To indicate such a
‘display unavailable’ condition, the Du bit at the MS bit position of the status register will be set to 1 =
5*»
ft
for this duration.
Display write inhibit/blanking command: The information present in a display RAM location *5
comes out on A3.0 and B3.0. On A3_0 the MS nibble is sent out, while on B3_0 the LS nibble is sent out. l-s.
o
Generally the 7-segment code is directly stored in a display RAM location. However, it is possible to w
treat A3_0 as a hex digit and B3_0 as another hex digit that could be displayed on two separate 7-seg-
ment displays. If the 8279 is configured for a 16-character display, it is possible to connect thirty two
7-segment display units as shown in Fig. 22.21. It makes use of two separate sets of sixteen 7-segment
display units that could be called display unit A and display unit B, respectively.
External 0°
4 to 16 Out 15 Display unit A Display unit B
SL3-0 decoder
(active I
low
outputs)
OutO
nn .. n
n
n
n . . n
n
Hex to "'LED-15 db LED-0 LED15 LED0
A3-0 7 segment
converter
B3-0
Hex to
7 segment £
converter
If the 8279 is interfaced with display unit A and display unit B, sometimes it may be required to
change the information displayed on only one of the display units. Sometimes it may be required to
blank the display on only one of the display units. Such requirements are met using the display write
inhibit/blanking command.
The format for the ‘display write inhibit/blanking’ command that is written to the control port of
8279 is as follows.
When the MS 3 bits in the control port are 101, it means that it is display write inhibit/blanking com¬
mand. The bit shown as X in this command is a don’t-care bit.
The IW bits stand for ‘inhibit write’. If IWA = 1 and IWB = 0, the A3_0 portion of the display RAM
will not be written with any new value when a write operation to display RAM is performed. Only the
402
B3_O portion will be altered. Similarly, if IWA = 0 and IWB = 1, the B3.0 portion of the display RAM
will not be written with any new value when a write operation to display RAM is performed. Only the
A3_Q portion will be altered.
If the contents of display RAM location 4 is C3H, it results in characters C and 3 being on the dis¬
play units A and B, respectively. If it is desired to change the character 3 to character 5 on display unit
o B, it is achieved by making IWA = 1 and IWB = 0, and then writing X5H to display RAM location
4, where X is any hex digit. For example, even if the value F5FI is sent to display RAM location 4,
sE the contents change from C3 to C5, and not F5.
The BL bits stand for ‘blank’ display. If BLA = 1, the display unit A will be blanked. The CC bits
I
.2 in the previously used clear command provide the code to be used for the blanking. Flowever, the
S moment BLA becomes 0, the display reappears. Thus by making BLA = 1 or BLB = 1, the contents
w
§ of the display RAM are not changed to code for blank. If the display RAM contents are to be changed
CO to code for blank, the clear command has to be issued to the 8279.
0
H
End interrupt/error mode set command: The format for the ‘end interrupt/error mode set’ command
that is written to the control port of the 8279 is as follows.
1 1 1 E X X X X
When the MS 3 bits in the control port are 111, it means that it is end interrupt/error mode set com¬
mand. In this command the LS 4 bits are don’t care bits.
This command works as end interrupt command when the 8279 is configured for sensor matrix
mode. In the end interrupt command, the E bit is also a don’t care bit. When this command is exe¬
cuted, the IRQ output is deactivated by the 8279.
The E bit is meaningful only when the 8279 is configured for interfacing a matrix keyboard in N-
key rollover mode. In such a case, if the E bit is set to 1, the 8279 will operate in special error mode.
In this mode, the de-bounce cycle and key validity check are same as in normal N-key rollover mode.
However, if during a single de-bounce cycle, two keys are found pressed, it will be interpreted as an
error indicating simultaneous multiple key strokes. This causes S/E flag bit in the status register. The
S/E bit acts like E bit (stands for error) in this case. When the S/E bit is set, it prevents further writ¬
ing into the FIFO RAM and cause activation of IRQ, if not already set. The S/E bit is reset when a
clear command is issued with CF = 1 or CA = 1.
In the previous chapters, a number of monitor routines like RDKBD, UPDAD, UPDDT, CLEAR,
OUTPT were used at a number of places in the programs described. The reader need not be under the
impression that writing of such monitor routines is beyond his abilities. Developing routines for these
functions needs a clear knowledge of the 8279. Armed with such knowledge, routines for the above-
mentioned functions are developed in this section. The names given to the equivalent routines devel¬
oped in this section are shown in the following table.
403
ALS monitor routine Equivalent routine Starting address
RDKBD KBD RD C6E0H
OUTPT DISPLAY C640H
UPDAD ADRDISP C6B0H
UPDDT DATDISP C690H
CLEAR BLANK C6D0H
=
»
(P
The routines developed in this section make use of symbolic locations DATBUF and ADRBUF as
indicated in the following table.
°*5 -ÿ
l-s.
o
</>
Write an 8085 assembly language routine that returns the scan code of the key pressed on the key¬
board. In other words, develop a routine that is functionally equivalent to RDKBD monitor routine.
Program
ORG C6E0H
DAT79 EQU DOH
CTRL79 EQU D1H
KBD_RD: RIM ; Get pending interrupt status
ANI 00010000B; Check RST5.5 pending interrupt status
JZ KBD_RD ; Go to KBD_RD if RST 5.5 is not pending
MVI A, 01000000B;
OUT CTRL79 ; Issue command to read from FIFO RAM
IN DAT79 ; Read scancode from FIFO RAM
ANI 3FH ; Reset MS two bits of scancode
RET
In this routine the MS 2 bits of the scan code that correspond to Shift and Ctrl are reset to 0. This is
because Shift and Ctrl keys are not present on the keyboard of the kit. The KBD RD routine destroys
the contents of only register A. This routine does not call any other routine.
Write an 8085 assembly language routine that displays two hex digits in data field, four hex digits in
address field, or six hex digits on the complete six-digit display. The hex digits are stored as unpacked
hex bytes in memory. Address of the first byte is provided in HL. The display field is indicated by the
contents of C register shown as follows.
404
Contents of Display field
C register
sE
In addition, a dot is to be displayed at the end of the field if the content of B is 01H. For any other
.2
I value of B register, dot is not to be displayed at the end of the field.
S In other words, develop a routine that is limctionally somewhat superior to OUTPT monitor
in
§ routine. It is superior to OUTPT routine because OUTPT can only display in the address field or the
co data field. To display on all the six digits, OUTPT routine has to be called twice.
0
In the program that follows, DISPTBL contains a table of the 7-segment codes (without dot) for
H
the characters ‘0’ to ‘9’, and ‘A’ to ‘F’. The 7-segment codes for any character can easily be derived.
For example, the derivation of 7-segment code for ‘3’ is indicated in the following. Note that on the
ALS kit, the LS bit of 7-segment is responsible for the display of segment ‘a’. In other kits, the imple¬
mentation could be different.
dp g f e d c b a
1 0 1 1 0 0 0 0 = BOH
In the following program, the bytes 06, 05, 04, 03, 02, 01 are stored in six memory locations
starting from C150H.
C150H 06
C151H 05
C152H 04
C153H 03
C154H 02
C155H 01
Program
ORG C640H
DISPLAY: MOV A, C
CPI 00H
JZ DATA ; Display in data field if (C) = 00
CPI 01H
JZ ADDR ; Display in address field if (C) = 01
CPI 02H
JZ BOTH ; Display in complete field if (C) = 02
RET ; Return if (C) > 2
DATA: MVI C, 02H ; Counter to display 2 chars in Data field
MVI A, 92H ; Display RAM address as 2, auto increment
JMP DISP ; Jump to perform display
405
ADDR: MVI C, 04H ; Counter to display 4 chars in Addr field
MVI A, 94H ; Display RAM address as 4, auto increment
JMP DISP ; Jump to perform display
BOTH: MVI C, 06H ; Display 6 chars in Complete field
MVI A, 92H ; Display RAM address as 2, auto increment
a
DISP: OUT CTRL79 ; Issue 'Write to Display RAM' command
AGAIN: MOV A, M ; Get the character to be displayed in A a
ANI OFH ; Use only LS 4 bits 5 55'
XCHG ; Save HL in DE o
ar*
;The next 4 instructions obtain 7-segment code for the it
/character to be displayed from the display table (A
LXI H, DISPTBL;
ADD L
MOV L, A
MOV A, M ; Obtain 7-segment code in A
DCR B
JNZ NO DOT ; Check B value for 1. If <>1, go to NO_DOT
ANI 7FH ; Reset MS bit of displaycode (Display dot)
NO DOT: OUT DAT79 ; Display char present at RAM address
XCHG ; Restore HL from DE
INX H ; Point HL to next character
DCR C ; Decrement counter
JNZ AGAIN ; Repeat till all characters displayed
RET
DISPTBL DB COH, F9H, A4H, BOH, 99H, 92H, 82H, F8H, 80H, 98H
DB 88H, 83H, C6H, A1H, 86H, 8EH
END
This routine destroys the contents of A, B, C, D, E, H, and L and does not call any other routine.
Write an 8085 assembly language routine that expands a two-digit hex number present in A register
into two unpacked hex digits. The LS hex digit is stored in memory pointed by HL. The MS hex digit
is stored in the next higher memory location.
Program
ORG C6F0H
XPAND: MOV C, A ; Save A in C
ANI OFH ; Consider only LS nibble of A
MOV M, A ; Save in memory pointed by HL
MOV A, C
RRC
RRC
406
RRC
RRC
ANI OFH ; Consider only MS nibble of A
INX H
MOV M, A ; Save in next memory location
0 RET
s2 This routine destroys the contents of A, C, H, and L. It does not call any other routine.
2
.2 22.7.4 DATDISP ROUTINE
s
in
§ Write an 8085 assembly language routine that displays the contents of location DATBUF in data field.
co In addition, a dot is to be displayed at the end of the field if the content of B is 01H. For any other
jf value of B register, no dot is to be displayed at the end of the field. In other words, develop a routine
that is functionally the same as UPDDT monitor routine.
Program
ORG C690H
XPAND EQU C6F0H
DATDISP: LDA DATBUF
LXI H, DISPBUF
CALL XPAND ; Store LS and MS nibbles of DATBUF in
; DISPBUF and DISPBUF+1
LXI H, DISPBUF ; HL points to characters to display
MVI C, 00 ; Display in Data field
CALL DISPLAY;
RET
This routine destroys the contents of A, B, C, D, E, H, and L. It calls XPAND and DISPLAY routines.
Write an 8085 assembly language routine that displays the contents of location ADRBUF and ADR-
BUF + 1 in address field. In addition, a dot is to be displayed at the end of the field if the content of B
is 01H. For any other value of B register, no dot is to be displayed at the end of the field. In other
words, develop a routine that is functionally the same as UPDAD monitor routine.
Program
ORG C6B0H
ADRBUF EQU C610H
DISPBUF EQU C620H
XPAND EQU C6F0H
DISPLAY EQU C640H
ADRDISP: LDA ADRBUF
LXI H, DISPBUF+2
CALL XPAND ; Store LS and MS nibbles of ADRBUF
407
;in DISPBUF+2 and DISPBUF+3
LDA ADRBUF+1
LXI H, DISPBUF+4
CALL XPAND ; Store LS and MS nibbles of ADRBUF+1
;in DISPBUF+ 4 and DISPBUF+5
a
LXI H, DISPBUF+2; HL points to characters to display
MVI C, 01 ; Display in Address field a.
CALL DISPLAY ; §2.
RET
<, 0
®
This routine destroys the contents of A, B, C, D, E, H, and L. It calls the XPAND and DISPLAY n
(A
routines.
Write an 8085 assembly language program that blanks the entire six-digit display.
Program
ORG C000H
DAT79 EQU DOH
CTRL79 EQU D1H
MVI C, 06
MVI A, 92H
OUT CTRL79
MVI A, FFH ; FFH is 7-segment code for Blank character
AGAIN: OUT DAT79
DCR C
JNZ AGAIN
HLT
Write an 8085 assembly language routine that blanks the entire six-digit display. In other words,
develop a routine that is functionally the same as CLEAR monitor routine.
Program
ORG C6D0H
CTRL79 EQU D1H
BLANK: MVI A, 11011100B ;
OUT CTRL79 ; 'Clear command' with blank code = FFH
RET
This routine destroys the contents of register A only. It does not call any routines.
408
22.7.8 DISPLAY SCAN CODE OF KEY
Write an 8085 assembly language program that uses the KBD RD routine to display the scan code of
the key pressed in the data field of the kit.
o
Program
o
u ORG C000H
2
a UPDDT EQU 06D3H
o
CURDT EQU FFF9H
S KBD RD EQU C6E0H
U)
00 BEGIN: CALL KBD_RD ; returns scan code of key pressed in A
O
00 STA CURDT ;
o
CALL UPDDT ; Display scan code in data field
JMP BEGIN ; Be in an infinite loop till Reset key is pressed
Write an 8085 assembly language program that uses the DISPLAY routine to display ‘123456.’ on the
six-digit display of the kit.
Program
ORG C000H
DAT79 EQU D0H
CTRL79 EQU D1H
DISPLAY EQU C640H
LXI H, C150H ; Characters are stored from C150H
MVI C, 02H ; Display in the complete field
MVI B, 01H ; Display a dot at the end of the field
CALL DISPLAY ; Display the characters
HLT
In the above program, the characters to be displayed are stored in memory locations starting from
C150H shown as follows.
Write an 8085 assembly language program that uses DATDISP routine in a main program to display
‘56.’ in the data field of the kit.
409
Program
ORG C000H
DATBUF EQU C600H
DISPBUF EQU C620H
DATDISP EQU C690H
a
MVI A, 56H ;
STA DATBUF ; Store 56 in DATBUF a
MVI B, 01H ; Display a dot at the end of the field 55
o 5'
CALL DATDISP ; Display '56.' in Data field ar*
HLT
n
(A
Write an 8085 assembly language program that uses ADRDISP routine in a main program to display
‘1234.’ in the address field of the kit.
Program
ORG C000H
ADRBUF EQU C610H
DISPBUF EQU C620H
ADRDISP EQU C6B0H
LXI H, 1234H ;
SHLD ADRBUF ; Store 34 in ADRBUF and 12 in ADRBUF+ 1
MVI B, 01H ; Display a dot at the end of the field
CALL ADRDISP ; Display '1234.' in Address field
HLT
Write an 8085 assembly language program that uses BLANK routine in a main program to blank the
display on the kit.
Program
ORG C000H
BLANK EQU C6D0H
CALL BLANK; Blank the display
HLT
Write an 8085 assembly language program that changes the contents of a RAM location on the kit
without using any of the monitor routines supplied by the manufacturer of the kit. The following
actions should take place when the program is executed.
410
l. The entire display is blanked out to start with.
2. Keep displaying the characters input from the keyboard in the address field in ‘moving dis¬
play fashion’ till the ‘Next’ key on the kit is pressed.
3. Keep displaying the characters input from the keyboard in the data field in ‘moving display
fashion’ till the ‘Exec’ key on the kit is pressed.
o 4. Contents of memory whose address is provided in the address field should be changed to
the value displayed in the data field.
S
E The program, which follows makes use of KBD RD routine developed in this chapter. It does not
!
.2
make use of any of the monitor routines supplied along with the kit.
S ORG C010H
10
§ DAT79 EQU DOH
CO CTRL79 EQU D1H
0 EXEC EQU 10H ; Scancode of 'Exec' key is 10H
H NEXT EQU 11H ; Scancode of 'Next' key is 11H
DATBUF EQU C600H
ADRBUF EQU C610H
KBD RD EQU C6E0H
MVI A, 00H
OUT CTRL79 ; mode set for 8-char left entry
MVI A, DFH
OUT CTRL79 ; Clear command with FFH as blank code
LXI D, 0000
AGAIN: CALL KBD_RD ; Read from keyboard. Scan code in A
MOV B, A ; Save A in B
CPI NEXT
JZ DATFLD
RLC
RLC
RLC
RLC ;(A) =23H
MOV D,A ;(D) =23H
411
MOV A, E ;(A) = 34H
ANI OFH ;(A) = 04H
RLC
RLC
RLC
3
RLC ;(A) = 4 OH <P
ADD B
MOV E,A
;(A) = 45H
;(E) = 45H
-
0 2.
3,
tt
XCHG -o°~
(P
SHLD ADRBUF W
XCHG ; Save DE contents in ADRBUF and ADRBUF+1
MOV A, B ; Get scan code of key pressed into A from B
LXI H, TABLE
ADD L
MOV L,A
MOV B,M ; Get 7-segment code of key pressed into B
0 RLC
u RLC
2a RLC
o
RLC ;(A) = 4 OH
S ADD B ;(A) = 45H
U)
00 STA DATBUF
o
00 MOV C,A ;(C) = 45H
tl
X MOV A,B; Get scan code of key pressed into A from B
LXI H, TABLE
ADD L
MOV L,A
MOV B,M; Get 7-segment code of key pressed into B
MVI A, 72H
OUT CTRL79
IN DAT79
OUT DAT79 ; Read from display RAM location 2 and write to location 3
MVI A, 82H
OUT CTRL79 Write to Display RAM location 2' command
MOV A, B
OUT DAT79 ;Write 7-segment code of key pressed into display RAM
/location 2
JMP AGAIN1
EXIT: LHLD ADRBUF ; HL loaded with address present in the address field
LDA DATBUF ; A loaded with data present in data field
MOV M,A
HLT
TABLE DB C0H, F9H, A4H, BOH, 99H, 92H, 82H, F8H
DB 80H, 98H, 88H, 83H, C6H, A1H, 86H, 8EH
END
Write an 8085 assembly language program to display 123456 on the kit and then blank the kit when
a key is pressed. Again display 123456 when a key is pressed and repeat it again and again. Demon¬
strate the use of ‘display write inhibit/blanking’ command.
ORG C010H
DAT79 EQU D0H
CTRL79 EQU D1H
413
CUR AD EQU FFF7H
CURDT EQU FFF9H
UPDAD EQU 06BCH
UPDDT EQU 06D3H
RDKBD EQU 0634H
MVI A, 11011111B
OUT CTRL79 ; Clear display 0
3,
MVI A, 00001110B 5 2.
SIM U
El ; Unmask RST 5.5 and Enable Interrupts -o°~
<P
v>
CALL RDKBD ; Wait for pressing of a key
LXI H, 1234H
SHLD CURAD
MVI A, 56H
STA CURDT
CALL UPDAD ; Display 1234 in Address field
CALL UPDDT ; Display 56 in Data field
LOOP: CALL RDKBD ; Wait for pressing of a key
MVI A, 10100011B
OUT CTRL79 ; Blank display
CALL RDKBD ; Wait for pressing of a key
MVI A, 10100000B
OUT CTRL79 ; Resume display
JMP LOOP
END
ORG C010H
DAT79 EQU DOH
CTRL79 EQU DlH
MVI A, DFH; DFH = 11011111
OUT CTRL79; Clear display
LXI H, C300H
AGAIN: IN CTRL79 ; Read Status register
414
RLC
JC NOTAVAI; If MS bit = 1, Display RAM is not available
MVI M, FFH
HLT ; Store FF in memory and Halt if Display available
s NOTAVAI: MVI M, 0
INX H
u JMP AGAIN ; Store 00 in consecutive memory locations
2
a END
2
.2
2
in
os
S
1H
1. Write a circuit diagram to interface a 7-segment display unit using a simple latch. What are
the disadvantages of this scheme?
2. Write an 8085 assembly language program that displays ‘34567890’ continuously on the
eight 7-segment LEDs, assuming that the LEDs are interfaced with the microprocessor as in
Fig. 22.6.
3. Explain with a neat diagram the display interface that uses serial data transfer. Write an 8085
assembly language program that displays alternately ‘1234’ and ‘5678’ with a gap of 2 s
between them.
4. Write a circuit diagram to interface a simple keyboard using tri-state buffers. What are the
drawbacks of this method?
5. Explain with a neat diagram the matrix keyboard interface using 8255 ports. What are the
disadvantages of using 8255 ports for interfacing a matrix keyboard?
6. Write an 8085 assembly language program that uses 3x8 matrix keyboard interface, and
displays the scan code of the key that is pressed on the interface.
7. Provide a brief overview of the working of 8279 keyboard/display controller chip and
describe its functional pin diagram.
8. Compare the decoded and encoded modes of operation of 8279 with respect to interfacing
of matrix keyboards and interfacing 7-segment display devices.
9. What is FIFO/sensor RAM? Describe its use in 8279. Explain how a location of FIFO/sen¬
sor RAM can be read?
10. Describe the status register of 8279.
11 . Compare two-key lockout and N-key rollover modes of operation.
12. Explain how the 8279 could be used to interface a matrix of switch sensors?
13. What is display RAM? Describe its use in 8279. Explain how a location of display RAM
can be read or written?
14. Explain left entry and right entry modes of operation.
415
15. Explain keyboard/display mode set command of 8279.
16. Explain the following commands of 8279
a. Clear command;
b. Display write inhibit/blanking command;
c. End interrupt/eror mode set command.
=
5*»
ft
17. Develop your own routine that is functionally equivalent or superior to a monitor routine
provided on your kit.
*5
!ÿ a
Sw
23
m \
P:
Intel 8259A —
Programmable
Interrupt Controller
■ Need for an interrupt controller
■ Overview of the working of 8259
■ Pins of 8259
■ Registers used in 8259
•Interrupt request register
•Interrupt mask register
•In-service register
•Slave register
m Programming the 8259 with no slaves
•Initialization command word 2 (ICW2)
•Initialization command word 1 (ICW1)
•Initialization command word 3 (ICW3)
•Initialization command word 4 (ICW4)
•Operation command word 1 (OCW1)
•Operation command word 2 (OCW2)
•Operation command word 3 (OCW3)
■ Programming the 8259 with slaves
•Initialization command word 3 (ICW3)
•Initialization command word 4 (ICW4)
■ Use of 8259 in an 8086-based system
■ Architecture of 8259
■ Questions
In this chapter we deal with the above mentioned topics of programmable interrupt controller 8259A.
For simplicity, the 8259A is denoted as 8259 throughout this chapter.
417
■ 23.1 NEED FOR AN INTERRUPT CONTROLLER
In an 8085 microprocessor, there are five interrupt input pins. They are TRAP, RST 7.5, RST 6.5, RST
5.5, and INTR. If there are a maximum of five I/O devices that desire to perform interrupt driven data
transfer, they can be connected to the five interrupt input pins. Now consider the case where there are more
than five I/O devices that would like to perform interrupt driven data transfer. In such a case, on some
_=
interrupt pins more than one I/O device will have to interrupt. In fact, most microprocessors provide very 5. g>
few interrupt input pins. For example, Zilog Z-80, Motorola 6800, Intel 8086 have only two interrupt input 5 j£
pins. Thus an interrupt pin may be required to receive interrupt requests from several devices. Figure 23.1 ■o
shows a case where three I/O devices interrupt the 8085 using the RST 6.5 interrupt input.
8255-1
a!
33
INTB
’I
8255-2
RST6.5 OR
INTB
8255-3
Fig. 23.1
INTB Three devices interrupting 8085
8085 using RST6.5 pin
In Fig. 23.1, the inputs to the OR gate are from Port B of 8255 PPI chips. Port B in these 8255 chips
are assumed to be configured for mode 1 operation. The 8085 is interrupted when any one or more of
the three inputs to the OR gate is active. It then branches to the interrupt service subroutine (ISS) at
6.5 X 8 = 52 = 0034H. Thus the branch to ISS at 0034H takes place immaterial of the source of the
interrupt. In reality, the processor is required to branch to different subroutines based on the source of
the interrupt. Thus in the ISS for RST 6.5 the processor has to identify from among the I/O ports con¬
nected to the RST 6.5 pin the I/O port that needs service. This process of identifying the port that needs
service is called ‘polling’. The subroutine that starts at 0034H is now called the interrupt level sub¬
routine (ILS) for RST 6.5 instead of ISS for RST 6.5. Once the port that needs service is identified in
the ILS, the processor performs a branch to the appropriate ISS.
The identification of the port that needs service is quite simple. The status information about Port
B is obtained by reading Port C. If the LS bit of Port C is 1, it means that Port B interrupt request is
active. The format of the ILS is shown as follows.
PUSH PSW
PUSH B
PUSH D
PUSH H ; Save registers
IN PORT Cl
RRC
JC DEV 1 ; If LS bit of Port C in 8255-1 is 1, go to DEV_1
IN PORT C2
RRC
418
JC DEV_2 ; If LS bit of Port C in 8255-2 is 1, go to DEV_2
IN P0RT_C3
RRC
JC DEV_3 ; If LS bit of Port C in 8255-3 is 1, go to DEV_3
JMP EXIT
s
M DEV_1:CALL ISS_1 ; Branch to ISS_1
o JMP EXIT
Sa
o DEV 2:CALL ISS_2 ; Branch to ISS_2
S JMP EXIT
in
00 DEV_3:CALL ISS_3 ; Branch to ISS_3
s EXIT: POP H
Jj
H POP D
POP B
POP PSW ; Restore registers
El
RET ; Return to interrupted program
ISS li-
RET
ISS_2 : -
RET
ISS 3:-
RET
The disadvantages of the polling method are as follows.
1. The polling process as shown in the ILS earlier takes quite a lot of time. This results in slow
interrupt response.
2. If all the three inputs of the OR gate are active in Fig. 23.1, then in the ILS shown, devicel
is serviced first as device l is polled first. Thus the priority for the various devices
connected on a single interrupt input pin is decided by the order in which they are polled.
Similarly, if only device_3 is in need of service, the ILS first of all checks device l and
device_2, finds that they are not in need of service, and only then it checks device_3 and
starts servicing it. The priority thus gets fixed and cannot be altered.
The above mentioned disadvantages are eliminated if a programmable interrupt controller (PIC) is
used in the microcomputer system. The 8259 chip is such a PIC in the Intel family.
419
■ 23.2 OVERVIEW OF THE WORKING OF 8259
The 8259 accepts interrupt requests from as many as eight interrupting devices on IR0 to IR7 pins.
Then it identifies the highest priority interrupt request from among those inputs that are active. It is
possible to configure the 8259 for ‘fixed priority’ mode of operation. In such a case, IRo has the
highest and IR7 has the lowest priority. If IR2, IR4, and IR6 inputs are active, then IR2 is the highest
_=
priority interrupt request among the active requests. The details about the interrupt requests that are 5. g>
active are stored in the interrupt request register (IRR). |jg
It is possible to mask the interrupt requests by loading the interrupt mask register (IMR). If IR2 and ■u
IR3 interrupt requests are masked, then IR4 is the highest priority interrupt request among the active O 3
Information about the interrupt requests that are presently being serviced will be kept in 3 3
=
requests that are not masked. It is possible that the processor is already servicing IR5 interrupt request. j
■ An IR input is activated;
■ The IR input is not masked;
■ The processor is presently not servicing an IR request with a higher priority.
Data bus
8259
8085 IRo
[CALL] IR IR,
/
AD7_Q IR2
w z iRj
INTR INT
IR4
ISR IRR
0 0 IR5
INTA*
IRe
INTA* Priority
T' resolver IR?
7 7
IMR
0 7
Control bus
Data bus
S8259 Inta
IR0
M8259 Inta
8085
Int
Inta
IR2
Inta
IR7
Inta
IR6
S8259
■IR0
Int
IR7
WR*:
the 8259.
Active low-input pin that is activated by the processor to write control information to
H
the 8259.
CS*: Active low-input pin used for selecting the chip.
A0: An address input pin. It is used along with RD*, and WR* to identify the various
command words sent to the 8259 and the status read from the 8259.
IR0-IR7: Eight asynchronous interrupt request inputs. The interrupt requests can be pro¬
grammed for level-triggered mode or edge-triggered mode.
INT: It is an active high-output pin that interrupts the processor. It is always connected to INTR
interrupt input of 8085. The INT output is activated only when all the following condi¬
tions are satisfied.
"'CT
CS-? 1 28 f— Vcc
WR 2 27 S— AQ
RD -» 3 26 INTA
D7 O 4 25 «- IR7
D6« 5 24 IR6
D5 •$-> 6 23 IR5
D4 <r> 7 22 t- IR4
D3 « 8 21 «- IR3
D2 4-) 9 20 S— IR2
D] 10 19 (- IRj
DQ II 18 f- IR0
CASO 12 17 INT
CAS 1 13 16 «-> SP/EN
Gnd —5 14 15 CAS2 Fig. 23.4
Pin diagram of Intel 8259
■ An IR input is activated.
■ The IR input is not masked.
■ The processor is presently not servicing an IR request with a higher priority.
INTA*: It is an active low-input pin. The 8259 receives this signal from INTA* output of 8085.
When the 8085 sends three successive INTA* signals, the 8259 sends a 3-byte CALL
422
8259 PIC 28-pin DIP
Vcc IRo
Gnd IR,
D7-0 IR2
8
M
cs IR3
u WR IR4
Sa IR5
RD
s
.2 Ao IRe
S IR7
U)
Int
00 CAS2
s« Inta
SP/EN CAS,
Fig. 23.5
H CAS0 Functional pin diagram of 8259
instruction to the 8085 using the D7_0 pins. The second and third bytes of the CALL instruc¬
tion contain ISS address that depends on the IR input of 8259 that is being serviced.
CAS2_o: These are the cascade lines. They are used only when there are multiple 8259s in the
system. The interrupt control system can have a Master 8259 and a maximum of eight
Slave 8259s. The INT output of Master 8259 is connected to INTR input of 8085. The INT
output of a Slave 8259 is connected to an IR input of Master 8259.
For the Master 8259, CAS pins are output pins that are used to identify one of the Slave
8259s. For a Slave 8259, CAS pins are input pins on which the Slave 8259 receives slave iden¬
tification from the Master 8259. The CAS lines form a private 8259 bus. The information sent
out by Master 8259 on CAS lines is received by all the Slave 8259s on their CAS inputs.
SP*/EN*: It stands for ‘slave program/enable buffer’. It is a dual function pin which when used as
SP* pin is an active low-input pin. If SP* = 0, the 8259 is designated as a Slave 8259. If
SP* = 1, the 8259 is designated as Master 8259. When there is a single 8259 in the sys¬
tem, the SP* input is tied to logic 1. When used as EN* pin it is an active low-output pin
that controls buffer transceivers in the buffered mode.
From the point of view of a microprocessor, the 8259 is a specialized I/O port chip. It is never used
for interfacing I/O devices, but only for controlling the interrupt system in a microcomputer. The 8259
has A0 as the only address input pin. Thus only two addresses are possible for the 8259 ports as seen
from a microprocessor. The two ports can be designated as low port and high port.
The processor issues command words to these ports in order to configure the 8259 as per the
need. There are several command words which are classified as initialization command words and
operation command words. There are four initialization command words (ICW1, ICW2, ICW3, and
423
ICW4) and three operation command words (OCW1, OCW2, and OCW3). The processor also reads
the status of 8259 by reading the low port and the high port. There are several status words to be read.
The 8259 makes use of a number of 8-bit registers shown as follows for its working.
I
The processor writes command words, reads status words, or accesses registers using only the low u i
port and the high port. Identification of a command word, status word, or a register is based on A0
value, the bit values in certain bit positions, and in some cases by the context of the programming. The
v
-if
details about the identification of a command word, status word, or a register are provided later. o 3
A brief description of the registers is provided as follows.
H
23.4.1 INTERRUPT REQUEST REGISTER
It is an 8-bit register that keeps track of active interrupt requests. Whenever an interrupt request input
is activated, the corresponding bit in IRR register is set to 1. For example, if IR4 and IR6 inputs are
activated, bits 4 and 6 of IRR are set to 1 making the contents of IRR as 01010000. The processor can
only read the contents of this register but cannot write to IRR. To read the IRR contents, the proces¬
sor has to issue OCW3 command to the 8259, with the LS 3 bits of the OCW3 command as 010. This
results in 8259 storing the IRR status in low port of 8259. Then the processor has to read the low port
of 8259.
It is an 8-bit register that keeps track of the interrupt requests that are masked. If IR4 and IR6 requests
should not cause an interrupt to the processor, it is easily achieved by making the contents of IMR as
01010000 that sets bits 4 and 6 of IMR to 1. Then even if IR4 or IR6 request is activated, the 8259
does not activate INT output and hence the processor will not be interrupted. The IMR is written by
issuing the OCW1 command. The command uses high port of 8259. The processor can also read the
contents of IMR register. To do this, the processor has to read the high port of 8259.
It is an 8-bit register that keeps track of the interrupt requests that are currently being serviced. If IR6
request is currently being serviced, then the contents of ISR will be 01000000. If IR3 request becomes
active during the service of IR6, the 8259 sets bit 3 of ISR to 1 and activates INT output. But bit 6 of
ISR remains set at 1 as IR6 request is not fully serviced yet. Thus the contents of ISR will be
01001000. The following assumptions must hold good for this to happen.
■ 8259 is operating in fully nested mode, without rotating priority, so that IR3 has higher priority
over IR6.
424
■ The processor has enabled interrupts in the routine for IR6.
■ IR3 request has not been masked.
The processor can only read the contents of the ISR register but cannot write to ISR. To read the
ISR contents, the processor has to issue OCW3 command to the 8259, with the LS 3 bits of the OCW3
command as 011. This results in the 8259 storing the ISR status in low port of 8259. Then the proces¬
8 sor has to read the low port of 8259.
a>
0
sa
2 23.4.4 SLAVE REGISTER
s
in
00
It is an 8-bit register. The processor writes to SLR but cannot read it. The content of this register has
s different meanings for Master 8259 and a Slave 8259. For Master 8259, it provides information about
the IR inputs to which Slave 8259s are connected. If SLR of Master 8259 is loaded with the value
Jj
H 00001111, then it means that:
Before 8259 PIC could be used in a microcomputer system for interrupt control application, it has
to be properly configured to meet the needs of the system. By configuring the 8259, it is provided with
a variety of information like
IV for IR0 request;
Level-triggered or edge-triggered interrupts;
Single or multiple 8259s;
ICW4 needed or not;
Masking information for interrupt requests etc.
To provide this information to the 8259, the processor has to issue the following commands.
Two initialization command words (ICW1 and ICW2) must be issued to the 8259 before it could
be used, as they are compulsory. ICW3 is also compulsory provided there are Slave 8259s in the sys¬
tem. ICW4 is also compulsory provided the processor is 8086 or special modes of 8259 are desired. =
In contrast to this, operation command words are not compulsory. Hence operation command words
could also be called ‘optional command words’. =sr s
ci
I
The command words have to be written to the low port or the high port of 8259 depending on the
command.
ICWl, OCW2, and OCW3 commands are written to the low port of 8259.
-if
TJ I
TJ
ICW2, ICW3, ICW4, and OCW1 commands are written to the high port of 8259. o 3
The 8259 port addresses depend on the chip select used in the system. For the discussion that fol¬ H
lows, it is assumed that the port addresses of 8259 are as given in the following:
ICWl will be explained after ICW2. The ICW2 command is written to the high port of 8259. It indi¬
cates A15_8, which is the MS byte of IV address. The 8259 should supply this MS byte of IV address
to the 8085 processor when the processor activates INTA* for the third time. If it is desired that the
IV supplied by 8259 to 8085 processor is 2480H when IR0 request is active, then the ICW2 command
should indicate that 24H is the MS byte of IV address. This is achieved by executing the following
instructions.
MVI A, 24H
OUT 51H
The ICWl command is written to the low port of 8259. Several other commands are also written to
the same port. If bit 4 of the low port is 1, the command in the low port is identified as ICWl.
The ICWl command indicates the LS byte of IV address which the 8259 should supply to the 8085
processor when the processor activates INTA* for the second time. If it is desired that the IV supplied
by 8259 to 8085 processor is 2480H when IR0 request is active, then the ICWl command should indi¬
cate that 80H is the LS byte of IV address.
The ICWl command provides some additional information also to the 8259 detailed as follows.
The format of the ICWl command is indicated as follows. Note that bit 4 = 1, indicating it is ICWl
command.
426
4/8 * bit Suppose the 8259 is configured such that the IV address is 2480H when IRo request is activated.
If IR] request causes the activation of INT output, the 8259 automatically sends out 2484H (a distance of
4 bytes from 2480H) as the IV address when 4/8* bit = 1. For the same IRt request, the IV address
automatically sent out by 8259 is 2488H (a distance of 8 bytes from 2480H) when 4/8* = 0. Table 23.1
indicates the IV address sent out by 8259 for different activations IR inputs when the 4/8* bit is 1.
■ Only four locations are available for writing service routine for an IR input. For example, for IR2
request, the service routine should start at 2488H and should end by 248BH. Obviously, the
amount of memory space will be inadequate. Storing a 3-byte jump instruction say ‘JMP C100H’
at 2488H could solve this problem. In such a case, the service routine for IR2 starts at C100H. If
the service routine for IR2 ends at C3FFH, then the service routine for IR3 can be started at C400H.
Storing ‘JMP C400H’ at location 248CH achieves this.
■ The LS 2 bits (shown underlined) in the LS byte of address are always 00. So the 8259 will not
be informed about the value of these bits explicitly.
■ The value of the next 3 bits (shown in bold type) is dependent on the IR input that is active. For
IR0 it is 000, for IR5 it is 101 etc. The 8085 processor cannot foresee which IR request will be acti¬
vated. Only the 8259 should decide about the value of these bits depending on the IR request that
is active. As such, the value of these bits will not be supplied to 8259 by the 8085. Thus, out of
the 16-bit IV address, the 8085 is required to supply only the MS 11 bit address. The LS 5 bits
of the address will be decided by the 8259 itself. Out of the MS 11 bits of address, the MS 8 bits
427
of address is provided by ICW2 command, as already described. The remaining 3 bits (shown in
italics) that are the MS 3 bits of LS byte of address is provided by the ICW1 command. In the
ICW1 format, these 3 bits of address are indicated as A7, A6, and A5.
The LS 5 bits of IV address have to be 00000 for IR0 request. Hence the only possible IV addresses
for IR0 when 24H is the MS byte of address depends on A7, A6, and A5. They are as shown in
Table 23.2.
=
=sr sci
Table 23.2 Possible IV addresses for IR0 I
A7
0
A6
0 0
IV address for IR0
2400H
-if
■o I
V
0 0 1 2420H o 3
o l
0
1
1
0
0
1
0
2440H
2460H
2480H
H
1 0 1 24A0H
1 1 0 24C0H
1 1 1 24E0H
Table 23.3 indicates the IV address sent out by 8259 for different activation of IR inputs when the
4/8* bit is 0, assuming IV address for IR0 to be 2480H.
■ Only eight locations are available for writing service routine for an IR input. For example, for IR2
request, the service routine should start at 2490H and end by 2497H. Obviously, the amount of
memory space will be inadequate. Storing a 3-byte jump instruction say ‘JMP C100H’ at 2490H
could solve this problem. In such a case, the service routine for IR2 starts at C100H. If the
service routine for IR2 ends at C3FFH, then the service routine for IR3 can be started at C400H.
Storing ‘JMP C400H’ at location 2498H achieves this.
■ The LS 3 bits (shown underlined) in the LS byte of address are always 000. So the 8259 will not
be informed about the value of these bits explicitly.
■ The value of next 3 bits (shown in bold type) is dependent on the IR input that is active. For IR0 it
is 000, for IR5 it is 101 etc. The 8085 processor cannot foresee which IR request will be activated.
Only the 8259 should decide about the value of these bits depending on the IR request that is active.
As such, the value of these bits will not be supplied to 8259 by the 8085. Thus, out of the 16-bit IV
address, the 8085 is required to supply only the MS 10-bit address. The LS 6 bits of the address
428
will be decided by the 8259 itself. Out of the MS 10 bits of address, the MS 8 bits of address is
provided by ICW2 command, as already described. The remaining 2 bits (shown in italics) that are
the MS 2 bits of LS byte of address is provided by the ICW1 command. In the ICW1 format, these
2 bits of address are indicated as A7 and A6. The bit indicated as A5/x is used as X (don’t-care) bit
when 4/8* bit = 0. Generally a 0 is stored for the X bit.
8M The LS 6 bits of IV address have to be 000000 for IR0 request. Hence the only possible IV
0 addresses for IR0 when 24H is the MS byte of address depends on A7 and A6. They are as shown in
S
a Table 23.4.
s
.2
S
w Table 23.4 Possible IV addresses for IR0
oo
s A7 A6 IV address for IR0
Jj 0 0 2400H
H 0 1 2440H
1 0 2480H
1 1 24C0H
Example
Assume it is required to configure the 8259 using IC W1 for the following operation.
MVI A, 10010010B
OUT 51H
The MS 2 bits are 10 which means that A7_6 = 10. Thus the LS byte of IV address for IR0 will be
10 000000 = 80H as required.
ICW3 is not needed when there are no Slave 8259s in the system. As it is assumed that there are no
Slave 8259s in the system, the discussion about ICW3 is deferred for the present.
429
23.5.4 INITIALIZATION COMMAND WORD 4 (ICW4)
The ICW4 command is written to the high port of 8259 and is needed only if any of the following
conditions is satisfied.
?!
v
to the same port. For example ICW2, as described earlier, is written to the high port of 8259. Even g Sj
ICW3 and OCW1 commands are written to the same port. The 8259 identifies the command in the jf | u
high port based on the ICW 1 command issued to the low port of 8259 earlier. “
!T
The first time the high port is written, the command is always interpreted as ICW2. The second
time the high port is written, the command is interpreted as ICW3, if there are Slave 8259s in the
system. If there are no Slave 8259s, it will be interpreted as IC W4, if IC W4 is needed as indicated in
bit 0 of ICW1 command. If ICW4 is also not needed, it will be interpreted as OCW1. The third time
the high port is written, the command is interpreted as IC W4 if IC W4 is needed, else it is interpreted
as OCW1. If the high port is written for the fourth time and onwards, the command is interpreted as
OC W1. Thus if it is needed to load the high port of 8259 with IC W2 command again, it is necessary
to load the low port of 8259 with ICW1 command first.
Assuming that the processor writes to the high port of 8259 five times, the interpretation of the
contents of the high port will be as indicated in Table 23.5.
The format of the ICW4 command is indicated as follows. Note that only 5 bits are used in this com¬
mand. The MS 3 bits are always 000.
g AEOI bit: If the processor is servicing IR4, then bit 4 of ISR is set to 1 by the 8259. This bit has to be
M
reset to 0 sometime later. Otherwise, IR4 input cannot interrupt the processor anymore. The highest
o priority ISR bit that is set can be reset to 0 by the following two methods.
a
s ■ By issuing an EOI command before returning from service routine.
S ■ Automatically following the last INTA* pulse.
in
00
s« If the AEOI bit is 1, the 8259 is configured for AEOI mode, else it is configured for EOI mode.
H
The SFNM, BUF, M/S* bits of ICW4 command will be described when ‘programming the 8259
when there are slaves’ topic is discussed.
The OCW 1 command is written to the high port of 8259. It indicates the IR inputs that must be
masked. Basically, when the high port of 8259 is loaded with OCW 1 command, the content of the high
port is internally moved to the IMR. The format of the OCW 1 command is indicated as follows.
M7 M6 M5 M4 M3 M2 Ml MO
MVI A, 50H
OUT 51H
The OCW2 command is written to the low port of 8259. The ICW1 and OCW3 commands are also
written to the same port. If bit 4 and bit 3 of the low port are 00, the command in the low port is iden¬
tified as OCW2, which is used for issuing the following types of commands to the 8259.
o 3
EOI: 1 = Issue explicit EOI command.
SL:
0 = Not explicit EOI command. Generally considered as automatic EOI.
1 = Use specific level for rotate/EOI command.
H
0 = No specific level for rotate/EOI command.
L2-0: Indicates the level to be used in specific rotate/EOI command. These bits are
don’t-care bits when SL bit is 0.
The R, SL, and EOI bits of OCW2 command identify the configuration of 8259.
When R = 1, SL = 1, and EOI = 0 in the OCW2 command, it means that specific rotation has to be
performed in AEOI command. It simply rotates the priorities.
It may be noted that when R = 0, SL = 1, and EOI = 0 in the OCW2 command, it means that— no
rotation, AEOI, but specified level to be used. It is indeed a ‘no operation’ command that is never used.
Need for rotation of priorities: In some applications it is possible that all the devices that interrupt
on IR0_7 have equal priority. However, after initialization of 8259, IR0 will have the highest and IR7
the lowest priority unless rotating priority is forced on 8259. This will result in a poor response to
low-priority interrupt requests. For example, IR7 request may never be serviced, if other higher
priority requests keep occurring. This problem is solved using rotating priority. There are two types
of rotation of priorities. They are:
Non-specific rotation;
Specific rotation.
Non-specific rotation: In this mode, the priorities will be rotated such that the highest priority ISR
bit that is presently in the set state will get the lowest priority. If bit 4 of ISR is the highest-priority
432
ISR bit that is in the set state, then the priorities would be rotated so that IR4 will have the lowest
priority and IR5 the highest as indicated in the following.
Priority
8M IR5
IR6
0 IR7
a
s IRo
s
.2
IR,
IR2
S IR3
in
00 IR4
s«
H The EOI command is always combined with non-specific rotation command. Hence in this example,
bit 4 of ISR is reset to 0. Non-specific rotation with EOI is used, when fully nested structure is preserved.
Specific rotation: In this mode, the priorities will be rotated such that the specified level will get the
lowest priority. If level 4 were specified using specific rotation, then the priorities would be rotated so
that IR4 will have the lowest priority and IR5 the highest priority as indicated in the following.
Priority
IR5
IR6
IR7
IRo
IR,
IR,
IR3
IR4
The EOI command is generally combined with specific rotation. If EOI command is combined with
specific rotation in the above example shown, bit 4 of ISR will be reset to 0. Specific rotation with¬
out EOI command is not commonly used. Such a command disturbs the fully nested structure. ‘Spe¬
cific EOI command’ will have to be executed to reset the appropriate ISR bit in case ‘specific rotation
without EOI command’ is used earlier. An example illustrates this situation.
Consider the following situation. Presently IR0 has the highest and IR7 has the lowest priority. IR4
input is activated and has not been masked. The processor is busy servicing IR6 request. Then bit 4 of
ISR will be set to 1. However, bit 6 of ISR remains set at 1, as IR6 request has not been fully serviced.
The processor now branches to the IR4 service routine. Assume that the following instructions are exe¬
cuted within the IR4 service routine.
After the execution of the instructions shown, bit 4 and bit 6 of ISR remain set, as EOI bit is 0. How¬
ever priorities would be rotated so that IR4 will have the lowest priority and IR5 the highest priority
indicated as follows.
433
Priority ISR status
IR5 0
IR6 1
IR7 0
IR0 0
IR,
IR2
0
0
=
IR3
IR4
0
1 =sr sci
I
If non-specific EOI command is issued just before the end of the service routine for IR4, the 8259
resets the highest priority ISR bit that is in the set state. In this case, it resets bit 6 of ISR and not
-
■c
?!
3
I
V
-
bit 4 of ISR. The rotation of priorities without EOI command has disturbed the fully nested structure, g §j
It is necessary in such a case to issue a specific EOI command that resets bit 4 of the ISR. |
y
"*
Some examples for 0CW2 commands are provided in the following. It is assumed that the low port E
address is 50H.
Example 1: The 0CW2 needed to issue a non-specific EOI command to the 8259 is:
Example 2: The OCW2 needed to issue a specific EOI command to the 8259 that resets bit 5 of ISR is:
Example 3: The OCW2 needed to issue a rotate with non-specific EOI command to the 8259 is:
Example 4\ The OCW2 needed to issue a rotate with specific EOI command to the 8259 that resets
bit-5 of ISR is:
Example 5: If 8259 is already configured for AEOI mode using ICW4 command, then the OCW2
needed to issue a rotate in AEOI command to the 8259 is:
Example 6: If 8259 is already configured for AEOI mode using ICW4 command and the 8259 is
already configured for rotate in AEOI mode using OC W2 command, as in the previous example, then
the new OCW2 needed to issue a ‘stop rotate in automatic EOI command’ to the 8259 is:
The 0CW3 command is written to the low port of 8259. The ICW1 and OCW2 commands are
also written to the same port. If bit 4 and bit 3 of the low port are 01, the command in the low port is
identified as OCW3. The OCW3 command is used for issuing the following types of commands to the
8 8259.
M
o Polled mode of operation;
S
a Set up 8259 for reading IRR or ISR register;
s
.2
Enable/disable special mask mode.
S
in
oo The format of the OCW3 command is indicated as follows. Note that bit 4 = 0 and bit 3 = 1,
s indicating it is OCW3 command. The MS bit in this command is a don’t-care bit.
Jj
H
X ESMM SMM 0 1 P RR IS/IR*
Polled mode: When the P bit is set to 1, the 8259 works in polled mode. If P = 0, polling mode is not
used. When working in polled mode, the INT output will never be activated by the 8259. As such, the
INT output of 8259 need not be connected to the INTR input of 8085. In this mode, the processor has
to identify the IR input that needs service using software. The processor first of all issues an OCW3
command and configures the 8259 for polled mode. Then the 8259 automatically freezes the contents
of ISR based on IR inputs at that moment, till the low port of 8259 is read. Thus even if the IR inputs
were to change after poll command is issued, the ISR contents do not change. The processor then reads
the low port that provides information about the highest priority-level requesting service. The infor¬
mation is provided in the following format.
I X X X X W2 wt W0
Where W2_o indicates the highest priority-level requesting service and I indicates whether the corre¬
sponding IR input is still active.
The following instructions are to be executed by the 8259 to find out about the IR input that needs
service in polling mode.
The polled mode can be used to expand the number of priority levels indefinitely, unlike the limi¬
tation of 64 levels in non-polled mode using one Master and eight Slave 8259s. Figure 23.6 shows
three 8259s connected in cascade. The INT output of 8259-2 is connected to IR4 input of 8259-1 and
that of 8259-3 is connected to IR6 input of 8259-2.
Assume only IR0 input of 8259-3 is active. All the three 8259s are configured for polled mode. The
processor polls 8259-1 and comes to the conclusion that IR4 input of 8259-1 is active. As 8259-2 is
connected to IR4 input of 8259-1, the processor polls 8259-2 and comes to the conclusion that the IR6
input of 8259-2 is active. Finally, as 8259-3 is connected to IR6 input of 8259-2, the processor polls
435
Master 8259-1 8259-2 8259-3
IR4
Int
=
Int =s
T oi
IR6
I
Fig. 23.6
EN EN
o 3
8259-3 and comes to the conclusion that the IRo input of 8259-3 is active. Once the interrupting source
is identified, the processor branches to the appropriate service routine.
H
Read status of IRR or ISR: Sometimes we wish to know the contents of the IRR or ISR. For this pur¬
pose, the OCW3 command has to be executed first indicating whether it is desired to read IRR or ISR.
After this, the low port of 8259 has to be read to get the status of the desired register.
In case OCW3 command is issued with P = 1 and RR = 1, reading of the low port provides polled
information and not status of IRR or ISR.
Special masked mode (SMM): Sometimes it is necessary that in a service routine some of the lower
priority IR inputs should be capable of interrupting the processor. Under normal conditions, assuming
IR0 is the highest priority input, if the processor is servicing say IR4 request, IR5_7 inputs cannot activate
INT output of 8259. This is true even if none of IR5_7 is masked. This is where the SMM proves useful.
If IMR is loaded with 00010011 when the 8259 is configured for SMM in the service routine for
IR4, then it means that not only the higher priority inputs IR2 and IR3 but also the lower-priority inputs
IR5, IR6, and IR7 are capable of interrupting the IR4 service routine. For the same value in IMR, if the
command were normal mask mode, only the higher priority inputs IR2 and IR3 would be capable of
interrupting the IR4 service routine.
The SMM is set when ESMM = 1 and SMM = 1 in OCW3 command. It reverts to normal mask
mode when ESMM = 1 and SMM = 0. ESMM stands for enable special mask mode and SMM stands
for special mask mode. When ESMM = 0, the SMM bit is a don’t-care bit.
436
■ 23.6 PROGRAMMING THE 8259 WITH SLAVES
In this section, it is assumed that a Slave 8259 is connected to IR4 input of Master 8259 and the proces¬
sor used in the microcomputer system is 8085. This is shown in Fig. 23.7.
s The port addresses of 8259 depend on the chip select circuit used. For the discussion, the port
addresses are assumed as follows.
a>
0
sa
2 8085 M8259 S8259
.2
s
in
00
S
Jj Intr Int
H Int
Fig. 23.7
A Slave 8259
connected to IR4 input
of Master 8259
The Master 8259 and all Slave 8259s used in the system have to be individually programmed using
ICWs and OCWs. The ICW1 and ICW2 have the same meaning whether it is Master 8259 or a Slave
8259. Hence discussion about ICW1 and ICW2 is not repeated.
The ICW3 command is written to the high port of 8259 and is needed only if there are Slave 8259s in
the system. Bit 1 of ICW1 command indicates whether there are Slave 8259s or not. Several other
commands are also written to the same port, for example, ICW2, ICW4, and OCW1. The 8259
identifies the command in the high port based on the ICW1 command issued to the low port of 8259
earlier. This topic has already been dealt with.
The ICW3 command has different interpretations for Master 8259 and a Slave 8259. Basically,
when the high port of 8259 is loaded with ICW3 command, the content of the high port is internally
moved to the SLR.
For Master 8259, ICW3 provides information about the IR inputs to which Slave 8259s are
connected. Assume the following instructions to be executed after the ICW2 command, when there
are Slave 8259s in the system.
MVI A, 00010000B
OUT 51H
The SLR of Master 8259 is loaded with the value 00010000. Then it means that there is a Slave 8259
on IR4 input only of the Master 8259.
437
For a Slave 8259, ICW3 provides information about the IR input of Master 8259 to which the Slave
8259 is connected. In this case, only the LS 3 bits of SLR are meaningful. Assume SLR of Slave 8259
to be loaded with the value 00000100 using the following instructions.
MVI A, 0000100B
OUT 61 FI
=
Then it means that the Slave 8259 is connected to IR4 input of the Master 8259.
=sr s
ci
I
23.6.2 INITIALIZATION COMMAND WORD 4 (ICW4)
The ICW4 was partly described earlier. It is written to the high port of 8259 and is needed only if any
-
TJ I
?Ju
«
TJ
If any of these conditions are true, the processor has to issue an ICW1 command with IC4 bit,
present at the LS bit position, as 1. The LS bit of ICW 1 command indicates whether IC W4 command
is needed or not.
The format of the ICW4 command is indicated as follows. Note that only 5 bits are used in this
command. The MS 3 bits are always 000.
Buffered mode of 8259: If the 8259 is used in a large system, then bus-driving buffers are needed
on the data bus. This opens up the problem of enabling the buffers which is solved by configuring the
8259 for buffered mode of operation.
In non-buffered mode, SP*/EN* pin is used as the input pin SP*. This has been discussed earlier.
However, in the buffered mode, SP*/EN* pin is used as the output pin EN* that enables the buffers.
This output pin is activated whenever the 8259 data bus outputs are enabled. Then in the buffered
mode, as SP*/EN* is used as EN* output, the identification of an 8259 as master or a slave becomes
a problem. This is solved by software using BUF and M/S* bits of the ICW4 command.
Special fully nested mode: If the SFNM bit is 1, the 8259 will be configured for SFNM. This mode
is meaningful only for Master 8259. To understand SFNM, it is necessary to first understand FNM.
438
Fully nested mode: The 8259 is configured to function in FNM if SFNM bit = 0 in the ICW4
command. If ICW4 command is not used in the configuring of 8259, the 8259 is automatically
configured for FNM. In this mode IR0 will have the highest and IR7 will have the lowest priority to
start with. This priority changes dynamically as interrupt requests arrive, if the 8259 is configured for
rotating priority mode. Rotating priority is already described. If the 8259 is not configured for
s rotating priority, IR0 will continuously have the highest and IR7 the lowest priority.
The FNM of operation is normally used when there are no Slave 8259s. In FNM, IR0 will have the
a>
o
sa highest and IR7 the lowest priority if rotating priority is not used. Thus if IR4 causes the activation of
INT output, bit 4 of ISR will be set to 1. In such a case, IR4.7 cannot activate INT output till bit 4 of
2 ISR is reset to 0 using EOI command with the help of OCW2 or using AEOI command with the help
.2
s of IC W4 command.
in
oo Assume a Slave 8259 is connected to IR4 input of the Master 8259 and the IRfi input of Slave 8259
s is responsible for the activation of INT output of the Slave. This is shown in Fig. 23.8.
Jj
H
8085 M8259 S8259
As the INT output of Slave 8259 is connected to IR4 input of Master 8259, bit 4 of ISR in the
Master 8259 is set to 1. Now assume that the IR2 input of Slave 8259 becomes active in the service
routine for IR6 of Slave 8259. As IR2 has higher priority over IR6 that is currently being serviced, the
Slave 8259 again activates its INT output. But the Master 8259 does not activate its INT output
because it is only the IR4 input of Master 8259 that is active and bit 4 of ISR in the Master 8259 is not
yet reset to 0. This problem is solved using the SFNM for Master 8259 and configuring Slave 8259s
in FNM.
The SFNM is very similar to FNM with the following dilference. Assume the processor is servic¬
ing an IR input of a Slave 8259. If a higher priority IR input of the Slave 8259 becomes active, the
Master 8259 activates its INT output if the Master is configured for SFNM. This would not have hap¬
pened if the master were in FNM. When the Master 8259 is configured for SFNM, the procedure to
be adopted before exiting a service routine is as follows. It is explained with the situation described
here as an example.
First of all, non-specific EOI command using OCW2 has to be issued to the Slave 8259. This would
reset bit 2 of the ISR of Slave 8259. Then the ISR of Slave 8259 is read by issuing OCW3 command
to the Slave 8259. If the contents of ISR of Slave 8259 is 00H, it means none of the IR inputs of Slave
8259 are currently being serviced. Only in such a case, non-specific EOI command using OCW2 has
to be issued to the Master 8259. If the ISR contents of Slave 8259 is non zero, EOI command should
not be issued to the Master 8259.
The Master 8259 is configured for SFNM if SFNM bit is set to 1 in the IC W4 command. If this bit
is 0, the Master 8259 is configured for FNM only as indicated in the following.
439
SFNM: 1 = 8259 configured for SFNM.
0 = 8259 is not configured for SFNM. It is only FNM.
Default settings for ICW4: If the ICW4 command is not used in the system, the 8259 will automatically
be configured as if the LS 5 bits of ICW4 command were 00000. In other words when IC W4 command
is not used, the 8259 is automatically configured as follows. =
■ The processor used in the system is assumed to be 8085/8080. =£ sci
■ End of interrupt command needed before the end of the service routine. I
■
■
Non-buffered mode is used.
Fully nested mode is used. -I!
TJ I
TJ
33
Based on the description of the working of 8259, the simplified architecture of 8259 can be illustrated
as in Fig. 23.9.
440
+5 V
Gnd
\ Data bus -X
T" Low port
buffer \ \
RD
Hx
0 Read/
WR ICW1 OCW2 OCW3
« write
CS
control
£ A0
a
£
.2
2 X- Priority
in CAS2 Cascade ISR IRR IR0-7
CASI o/p resolver
CAS0 comparator
SP/EN
if
OCW1 (IMR)
Int
Inta
Control
logic r High port
f
ICW3
ICW2 ICW4
(SLR)
To conclude, the 8259 is a highly programmable chip that can be configured to exactly suit the
requirements of interrupt control in an application.
9. Name the command word and indicate the contents of the command words that are required
H
to configure three 8259s such that the Slave 8259s are connected on IR2 and IR5 inputs of
Master 8259.
10. Name the command word of 8259 that specifies the processor used in the system. What
should be the contents of this command word to configure the 8259 as buffered-mode slave,
in FNM, with highest priority ISR bit getting automatically reset when the second INTA*
pulse is received in an 8086-based system?
11. What is the change in the interpretation of bits in ICW1 and ICW2 commands of 8259 when
the processor used in the system is changed from 8085 to 8086?
12. How does the 8259 identify the command written to the high port of 8259?
13. Explain the need for SFNM. How do you configure 8259 for SFNM?
14. Explain OCW2 command with relevant examples.
15. Explain polled mode of operation in 8259.
16. Explain SMM of 8259 and its utility.
17. What is the default setting of ICW4 command?
18. Explain the architecture of 8259 with a neat diagram.
■ 1 Mu
24Intel 8257—
v
f:
Programmable
DMA Controller
This chapter deals with the programmable DMA controller— Intel 8257. It gives a detailed description
of the structure and working of 8257 with neat diagrams. The various aspects of 8257 are provided
under several sections as mentioned in the outline for this chapter.
In a microcomputer system there are basically three blocks: the microprocessor, memories such as
RAM and EPROM, and I/O ports to which I/O devices are connected. Then the possible data trans¬
fers as indicated in Fig. 24.1 are as follows:
■ Data transfer between microprocessor and memory (Ex. using LDA and STA instructions);
■ Data transfer between microprocessor and I/O ports (Ex. using IN and OUT instructions);
■ Data transfer between memory and I/O ports (DMA data transfer).
443
A data transfer
V ~7 =
Fig. 24.1
Possible data transfers in S tn
Memory M> I/O ports a microcomputer system
it
S3
I!
For data transfer between microprocessor and memory or between microprocessor and I/O ports,
instructions of the microprocessor are executed. As such, these data transfers are termed as u
programmed data transfer, which is generally used when a small amount of data transfer is involved.
Both microprocessor and memory are semiconductor chips that work at electronic speeds. Hence
data transfer between microprocessor and memory is generally not a problem. In case memory is a bit
slower than the microprocessor, the processor has to insert wait states. There is no problem other than
this.
The I/O ports like 8255, 8212, and so on are also semiconductor chips that work at electronic
speeds. However, they are used for interfacing I/O devices that are electromechanical in nature, and
hence very slow compared to the speed of the microprocessor. As a result data transfer between micro¬
processor and I/O ports becomes more complex. In the chapter on 8255 the basic I/O, status check
I/O, and the interrupt driven I/O schemes for data transfer between microprocessor and I/O ports are
already discussed.
Data transfer between memory and I/O port directly (without going through the microprocessor) is
called direct memory access (DMA). There are no instructions in the instruction set of the processor
to perform DMA data transfer. The need for DMA data transfer and methods to perform it are dis¬
cussed next.
If programmed data transfer is used for reading from memory location 3456H and writing to output
port number 50H, it takes 13 clocks for reading from memory location 3456H using LDA 3456H
instruction and ten clocks to write to output port number 50H. Thus it takes a total of 23 clocks. If the
processor is working at 3-MHz internal frequency with a clock period of 0.33 p,s, it takes 7.66 p,s.
Similarly, for reading from input port 40H and writing to memory location 2345H, it takes 7.66 p,s
using programmed data transfer.
If DMA data transfer is used, reading from memory location 3456H and writing to output port
number 50H requires only four clocks, which amounts to only 1.33 |xs. Similarly, for reading from
input port 40H and writing to memory location 2345H, it takes 1.33 p,s using DMA data transfer.
Some I/O devices like the hard disk and the floppy disk are capable of performing data transfers at
quite a fast rate. If we have a 1.44-MB floppy diskette rotating at 360 rpm, and has 18 sectors
per track, each sector storing 512 bytes, then the data transfer rate becomes 54K bytes per second, or
about 19 p,s per byte. Hard disks can easily transfer data at least ten times faster. Hence it turns out to
444
be 1.9 jxs per byte. This is the situation when DMA data transfer becomes a must. In programmed data
transfer, which needs about 7.66 p,s per byte of data transfer, 4 bytes would have come out of the hard
disk in the same time. Thus with fast I/O devices, DMA data transfer becomes a necessity.
Sometimes we may want to read from the A/D converter, say 1,000 times, and store the converted
values in 1,000 memory locations. This may be needed to obtain statistics like the average, the largest,
s and the smallest, of the converted values. Using programmed data transfer, it takes 1,000 X 7.66 = 7,660
p,s of processor time. Using DMA data transfer it takes only 1,000 X 1.33 = 1,330 p,s. Thus DMA data
a>
o transfer is desirable, if not essential, for data transfer between a slow I/O device (like A/D converter)
sa and memory, if large amount of data transfer is desired.
2 Thus it is to be noted that when large amount of data has to be transferred between memory and
.2
s I/O port, routing each byte via microprocessor becomes a time consuming operation. If the I/O port
in
oo can directly access memory for data transfer, without processor intervention, it will be more efficient.
s Such a scheme is known as DMA data transfer.
Jj However, as already mentioned, there are no instructions in the instruction set of a processor to per¬
H
form DMA data transfer. If DMA data transfer has to take place without processor intervention, there
must be a controller circuit on the I/O port that supervises DMA data transfer. Such a controller must
have the following features.
■ IOR, IOW, MR, and MW control signals generation capability.
■ Memory address register to generate memory address for data transfer.
■ Count register to indicate the number of bytes still to be transferred.
Generally I/O ports, like 8255 PPI chip, do not possess these features. Also, if more than one I/O
port needs to perform DMA data transfer, then all these I/O ports need to have such controller cir¬
cuitry. To solve this problem, Intel has developed programmable DMA controller chips like Intel 8257
and Intel 8237. Intel 8257 is described in this chapter.
Intel 8257 is a 40-pin programmable IC available as a DIP package. Its physical and functional pin
diagrams are indicated in Figs. 24.2 and 24.3, respectively.
The 8257 works in two modes— slave mode and the master mode. Similarly, the processor also
works in two modes— active mode and the HOLD mode. The processor is normally in the active
mode. In the active mode, the processor is the master of the computer system, including the 8257.
Only when DMA transfer is required to be performed, the processor goes to the HOLD state and gives
up control of the system bus. In such a state the processor is logically disconnected from the rest of
the computer system, and the 8257 becomes the master for the rest of the computer system.
The 8257 is in the slave mode when the processor is programming the 8257, or when the proces¬
sor is reading the contents of the internal registers of 8257. At this point of time the processor is in
active mode, and is the master of the computer system, including the 8257.
The 8257 is in the master mode when the 8257 is actually controlling DMA data transfer. At this
point of time the processor is in the HOLD state. Hence 8257 is the master of the computer system,
excluding the processor, which is logically disconnected in the computer system.
The 8257 can be used to control DMA data transfer of as many as four I/O ports. For each I/O port
there is a corresponding DMA channel. Each DMA channel has its DMA request (DRQ) input, and a
corresponding DMA acknowledge (DACK*) output. In addition, each DMA channel has a 16-bit
address register (AR), and a 16-bit count register (CR).
445
Intel 8257 40-pin DIP
IOW
—
IOR 4 > 1
2
40
39
A7
A6
MR 3 38 A5
MW 4 37 A4
Mark 5 36 TC =
Ready 6 35 (
— > A3
<— A SS
HLDA
ADSTB
7
8
34
33 <— >
2
Aj
ss
AEN
HRQ
9
10
32
31
4 >— Ao
Vcc S3
cs — » D0/A8 I!
11 30
n
(
Intel 8257
Vcc DRQO
Gnd DACKO
Al5-8/D7-0 DRQ1
DACK1
CS
DRQ2
IOR
DACK2
IOW
DRQ3
Clk
DACK3
Reset
MR
MW
Ready AEN
HRQ ADSTB
HLDA A7-4
TC Fig. 24.3
Functional pin dia¬
Mark
gram of Intel 8257
The 8085 processor has two lines dedicated for DMA data operation. They are HOLD and HLDA
(HOLD acknowledge). If an I/O port is in need of DMA service, it activates a DRQ input of the 8257,
which in turn sends out HRQ (HOLD request) on HOLD input of 8085. The 8085 then completes the
current machine cycle (note: not the current instruction cycle) and then goes to HOLD state. In the
HOLD state the address pins, data pins, RD*, WR*, and IO/M* pins are tristated. Thus the 8085
446
is effectively disconnected from the rest of the system, which is made known by activating HLDA
output of the 8085.
The 8057, which was so far a slave to the 8085, and was receiving commands from the 8085, now
becomes the master of the computer system. Of course, it does not become the master of 8085, which
is logically disconnected from the computer system. The 8257 resolves the priorities of the requesting
I/O ports (if there is more than one DMA request active), and sends DACK* signal to the highest
s priority I/O port needing DMA service.
a>
o
s
a
2 24.3.1 CONDITION WHEN PROCESSOR IS THE MASTER AND 8257 is IN SLAVE MODE
s
in D7_O/A15_8 are used as bi-directional data lines for communication between the processor and an inter¬
00
s nal register of 8257.
A3_0 are input lines of 8257, to select an internal register of 8257 for communication with the
Jj
H processor.
IOR* and IO W* are input lines of 8257 so that the processor can read from or write to the internal
registers of 8257.
MR*, MW*, and A7.4, which are the output pins of 8257, are tristated by 8257.
24.3.2 CONDITION WHEN PROCESSOR IS IN THE HOLD STATE AND 8257 is IN MASTER MODE
D7_O/A15_8 are used as uni-directional address output lines for sending out MS byte of address from an
AR in 8257.
A3_0 are output lines of 8257 and are used to send out the LS 4 bits of address from an AR in 8257.
A7_4 are output lines of 8257 and are used to send out the MS 4 bits of LS byte of address from an
AR in 8257.
IOR*, IOW*, MR*, and MW* are output pins of 8257. If the required operation is DMA read
machine cycle, MR* and IOW* signals will be activated by the 8257. The IOR* and MW* signals
will be in the inactive state. If the required operation is DMA write machine cycle, IOR* and MW*
signals will be activated by the 8257. The MR* and IOW* signals will be in the inactive state.
The programmer’s view of 8257 is provided in Fig. 24.4. From the microprocessor point of view, it is
an I/O port chip that is used exclusively for DMA control application. It is not used for interfacing I/O
devices for the purpose of data transfer with the processor. This chip can be used to control DMA data
transfer of as many as four I/O ports. For each I/O port there is a corresponding DMA channel. The
chip provides all the features needed for a DMA data transfer.
For each DMA channel there is an address register (AR) and a count register (CR). These registers are
16-bits wide. Thus, there are four ARs, AR3_0, each of 16 bits. Similarly there are four CRs, in addition
to control and status registers. The control and status registers are separate 8-bit registers, but having
447
16 bits
AR0[
CRo
2 bits 14 bits
ARi
=
CR,
3 o»
r tn
AR2[
CR2 [I
it
S3
I!
AR3[
CR3[
8 bits
n u
Control port f
Fig. 24.4
Status port f Programmer’s view of
Intel 8257
the same address. The control register can only be written, and the status register can only be read
by the processor.
To select one of these registers, four address pins are needed. A3.0 address pins of 8257 are used
for this purpose. The processor writes to an AR, CR, or control register by sending an appropriate
address on A3_0 pins when chip select (CS*) and I/O write (IOW*) signals are active. The processor
reads from an AR, CR, or status register by sending an appropriate address on A3_0 pins when CS* and
I/O read (IOR*) signals are active.
An AR contains the address of a memory location that is used in DMA data transfer. The AR used
in a DMA transfer is automatically incremented by 1 after every DMA access. As there are only eight
pins for communication with the processor, the LS and MS bytes of an AR are written in two steps,
starting with the LS byte. Thus, when the processor writes to an AR for the first time, the LS byte of
the address register is written, and when it is written the second time, the MS byte is written.
First/last flip-flop: The 8257 contains a first/last (F/L) flip-flop which toggles after each access of an
AR or CR. This flip-flop may be more appropriately called the MS/LS* (M/L*) flip-flop. The state of
this flip-flop decides whether MS or LS bytes of an AR/CR is accessed.
In the above case, when bit 15 is 1, the 8257 generates MR* and IOW* signals. When bit 14 is 1, it
generates MW* and IOR* signals.
If desired, 8257 can be connected as a memory-mapped I/O device in a system. In such a case, the
MR* and MW* signals generated by the processor are connected to IOR* and IOW* pins of 8257.
The meaning of the MS 2 bits of a CR are changed as indicated in the following, if the 8257 is con¬
nected as memory-mapped I/O device.
In the above case, when bit 15 is 1, the 8257 generates IOR* and MW* signals. When bit 14 is 1, it
generates IOW* and MR* signals.
In DMA verify mode, no control signal is activated by 8257. Hence actual data transfer does not
take place. It is normally used after some data have been transferred to an I/O port by memory using
DMA read operation. It verifies that the information transferred to the I/O port is a true copy of the
449
data in memory. For this purpose, the 8257 sends out DACK* signal for each DMA verify cycle. The
peripheral device may use these signals to verily the data it has acquired. It calculates the CRC (cyclic
redundancy check) character for the stored data on I/O device and compares with the stored value of
the CRC on the device to verify the data stored.
Enabling/disabling of DMA channels: The HRQ output of the 8257 is activated when a DRQ input
is activated, and the corresponding DMA channel has been enabled. Thus, if a DMA channel is dis¬
abled, the HRQ output is not going to be activated even if the corresponding DRQ input is active. The
contents of ECH3-0 bits in the control register decide the enabling or disabling of the DMA channels.
After reset of 8257, all the four DMA channels are disabled.
Rotating priority: The 8257 uses fixed priority for the servicing of DMA requests after reset input
of 8257 is activated. Channel 0 has the highest priority and Channel 3 has the lowest priority. Thus
the priority for the channels in descending order would be 0, 1, 2, 3. In such a case, if DMA
Channel 0 is enabled and the DRQ0 request is active, then the DMA channel 0 data transfer starts.
If the I/O device connected to DRQ0 does not withdraw its request till all bytes are transferred, no
other channel can be serviced till Channel 0 is fully serviced. Such a data transfer is called burst mode
data transfer.
If all the channels are to be serviced with equal priority, the rotating priority has to be selected by
setting to 1 the RTPR bit in the control register. In such a case, the DMA channel, which has just been
serviced, by transferring a byte using DMA, is allotted the lowest priority. Highest priority is allocated
to the channel with the next higher number. The channel numbers 0, 1, 2, 3 can be considered to form
a circle. For example, if Channel 2 is serviced just then, it gets the lowest priority and Channel 3 the
highest priority. Thus the priority for the channels in descending order would become 3, 0, 1, 2.
450
Similarly, if Channel 1 is serviced just then, it gets the lowest priority and Channel 2 the highest
priority. Thus the priority for the channels in descending order would become 2, 3, 0, 1. If all the
four DMAs are enabled, and all the four DRQ inputs are active, then each DMA channel gets its
chance to perform a DMA data transfer in four DMA cycles.
8 Extended write: A DMA machine cycle in which a byte is transferred directly between memory and
a> I/O port needs four clock cycles.
o
s
a ■ In the first clock cycle it will be in state S1. In this state, address is sent out from the relevant AR
s
.2 by 8257. MS byte is sent out on Al5.8/D7.0 and LS byte on A7.0.
S ■ In the second clock cycle it will be state S2. In this state, MR* (for DMA read machine cycle) or
in
00 IOR* (for DMA write machine cycle) is activated.
s ■ In the third clock cycle it will be state S3. In this state, 10W* (for DMA read machine cycle) or
Jj MW* (for DMA write machine cycle) is activated.
H
■ In the fourth clock cycle it will be state S4. In this state, a byte of data is transferred between
selected memory location and I/O port.
In the S3 state, the ready input is checked by the 8257. This ready input is similar in function to
the ready input in 8085. Only if this input is a 1, the 8257 goes to state S4 during the next clock. If
this input is a 0, the 8257 enters a wait state. It comes out of wait state and goes to S4 only after ready
input becomes 1.
In a DMA verify machine cycle there will not be any wait states and hence it needs only four clock
cycles. This is because there will not be any data transfer in a DMA verify machine cycle.
In a DMA read or write machine cycle, if the extended write bit is set, the IOW* (for DMA read
machine cycle) or the MW* (for DMA write machine cycle) is generated in the second clock cycle itself.
This provides for extended write times. This helps in avoiding wasteful wait states to a great extent.
Stop on terminal count (TCS bit): When the LS 14 bits of a DMA CR is decremented to zero value,
the TC output of 8257 is activated. If the TCS bit in the Control register is set to 1, then the correspon¬
ding DMA channel is automatically disabled (corresponding ECH bit of control port reset to 0) on the
activation of the TC output. This prevents further DMA operation on that channel. The ECH bit for
the channel must be set to 1 to begin another DMA operation.
If the TCS bit in the control port is reset to 0, the activation of TC output has no effect on the DMA
channel. It is then the responsibility of the I/O port to withdraw DMA request to terminate the DMA
operation.
Auto load: Auto load feature is useful for repeat block operations and block chaining operations. This
feature makes use of Channel 2 only, with help always from Channel 3, described as follows. Thus,
in auto load mode Channel 3 is not available to the user for DMA data transfer. Hence the ECH3 bit
is reset to 0 to disable Channel 3.
Repeat block operation: Assume that we want to display a page of information on the CRT. The CRT
uses raster scan to display the information, which has to be refreshed about 50 times per second to get
a flicker-free display of the page. The information in a page of display RAM can be sent to CRT con¬
troller using DMA read machine cycles.
Let us say, we want to transfer 1000H bytes starting from display RAM location 6000H to CRT
controller. Then AR2 can be loaded with 6000H and the MS 2 bits of CR2 with 10 to signify DMA
read machine cycle. The LS 14 bits of CR2 are loaded with 1000H that is 01 0000 0000 0000. Thus
CR2 is loaded with 9000H. When the CRT controller activates DRQ2 input of 8257, DMA read
451
machine cycles start. TC output is activated when CR2 contents are decremented to 8000H (when all
the LS 14 bits of CR2 become Os). At this point of time, the AR2 contents would have become 7000H.
Now it is necessary to reload AR2 with 6000H and CR2 with 9000H to repeat the sequence, and
this has to be done 50 times per second to get flicker-free display. This operation is automated using
the auto load feature.
If the AULD bit in the control port is set to 1, then the values stored in Channel-2 registers are auto¬
matically loaded into Channel-3 registers. Note that the auto load feature makes use of only Channel 2
=
with help from Channel-3 registers. Thus when AR2 and CR2 are loaded with 6000H and 9000H, AR3 ®
and CR3 are also automatically loaded with 6000H and 9000H. When the DRQ2 request input is acti- >
“
vated by the CRT controller, the DMA read machine cycles start. After 1000H bytes are transferred, o TI
AR2 and CR2 would have become 7000H and 8000H, and TC output would be activated. At this point J
of time, the Channel-2 registers are automatically reloaded from Channel-3 registers. This happens
during an ‘update’ cycle, when the UD (update) flag in the status register is set to 1 by the 8257. Then
=
u
*1
the values in Channel-3 registers are copied to Channel-2 registers. The UD bit is reset to 0 by the u
8257 only after DMA transfer starts again on Channel 2.
Even if the TCS bit is set to 1, it will not disable Channel 2 when TC output is activated, if AULD
bit is set to 1. Hence the block operations are automatically repeated if DRQ2 input remains active.
Block chaining operation: The maximum number of bytes that can be transferred using DMA data
transfer is 214 = 16K = 16,384. What if we want to transfer the contents of memory from 6000H to
7400H, which is 17K bytes, to an I/O port? In such a situation block chaining operation can be used
as described in the following.
First of all, AULD bit is set to 1, ECH2 bit to 1, and ECH3 bit is reset to 0 in the control port. Then
load AR2 with 6000H and CR2 with 9000H, as described for block repeat operation. These values are
automatically loaded into Channel-3 registers during a UD cycle. The UD bit in the status register is
monitored till it becomes 0. Then the AR3 is loaded with 7000H and CR3 with 10 00 0100 0000 0000 =
8400H. Now the DMA transfer for Channel 2 takes place and the TC output is activated when AR2 has
become 7000H and CR2 8000H. This would have transferred 1000H bytes out of our requirement of
1400H. At this point, the channel registers are reloaded from Channel-3 registers. Thus AR2 becomes
7000H and CR2 becomes 8400H. Now the next block of DMA transfer for Channel 2 results in the trans¬
fer of 400H bytes. The TC output is activated when the AR2 is 7400H and CR2 8000H. With the block
chaining scheme, any amount of information can be transferred using DMA data transfer.
The status register is 8-bits wide, and can only be read but not written by the processor. It is selected
when CS* = 0, A3_0 = 1000, and IOR* = 0.
The status register of 8257 provides status information about the present state of 8257. Only the LS
5 bits are meaningful in the status register as indicated in Fig. 24.6.
The description of the pins of 8257 is given in Fig. 24.3. The reader is advised to read this portion
once again after the programming of 8257 is explained.
Vcc and Gnd: Power supply and ground pins. 8257 uses +5V power supply.
Dy.f/A |5_8: Used as eight bi-directional data pins for communication with the processor, when the
processor is active and the 8257 is in the slave mode. When the processor is in the FIOLD
state and the 8257 is the master, they are used to send out the MS 8 bits of memory
address from an AR of 8257.
A3-o: When the processor is active, they are used as address input pins of 8257 to select one of
the registers inside the 8257. There are ARs, CRs, control and status registers as shown
in the following table. When the processor is in the HOLD state, these pins are used to
output the LS 4 bits of memory address by the 8257.
A7_4: When the processor is in the HOLD state, these pins are used to output the MS 4 bits of the
LS byte of memory address by the 8257. These pins are tristated, when the processor is active.
RESET: This is an active high input pin which is normally connected to the RESET OUT pin of 8085.
As such, when the 8085 is reset, it sends out a logic 1 pulse on the RESET OUT pin thus reset¬
ting the 8257. After the reset of 8257, the control register contents becomes 00H. This means:
DRQ3-0: These are active high DMA request input pins, one for each of the four DMA channels.
They are activated by special-purpose I/O port chips like Intel 8272— floppy disk con¬
troller, and Intel 8275— CRT controller. In fixed priority mode (when RTPR bit is 0 in
=
the control register), DRQO has the highest, and DRQ3 the lowest priority. 25
g 01
DACK3-0*: These are active low DMA acknowledgement output pins, one for each of the four ** f
DMA channels. Logical OR of chip select circuit output and DACK* output of 8257 is g T>
given as chip select input to special purpose I/O ports like Intel 8272 and Intel 8275. A **
DACK* output becomes 0 and then 1 for each byte of DMA data transfer. I!
IOR*: It is an active low input pin that is activated by the processor to read an AR, CR, or the *1 u
status register, when the 8257 is in the slave mode. The IOR*, generated using IO/M*,
RD*, and WR* signals of 8085, is connected to this pin when 8257 is connected as I/O-
mapped I/O device. If the 8257 is connected as memory-mapped I/O device, the MR*,
which is generated using IO/M*, RD*, and WR* signals of 8085, is connected to this pin.
When the processor is in the HOLD state, this becomes an output pin, and is acti¬
vated for a DMA write machine cycle.
IOW*: It is an active low input pin that is activated by the processor to write to an AR, CR, or the
control register, when the 8257 is in the slave mode. The IOW*, generated using IO/M*,
RD*, and WR* signals of 8085, is connected to this pin when 8257 is connected as I/O-
mapped I/O device. If the 8257 is connected as memory-mapped I/O device, the MW*,
which is generated using IO/M*, RD*, and WR* signals of 8085, is connected to this pin.
When the processor is in the HOLD state, this becomes an output pin, and is acti¬
vated for a DMA read machine cycle.
MR*: It is an active low output pin that is in tristate when the 8257 is in the slave mode. When
the processor is in the HOLD state, the 8257 drives this pin. It is activated for a DMA
read machine cycle and is inactive for a DMA write machine cycle.
MW*: It is an active low output pin that is in tristate when the 8257 is in the slave mode. When
the processor is in the HOLD state, the 8257 drives this pin. It is activated for a DMA
write machine cycle and is inactive for a DMA read machine cycle.
CS*: Active low input pin used for selecting the chip.
Clk: It is the clock input pin. The maximum allowed clock frequency on this input is about
3 MHz. The clock input is connected to the ClkOut pin of 8085 in an 8085-based system.
Ready: This is an active high input pin. It is similar in function to the ready input of 8085. Devices
with slow access times can use this input to insert wait states during DMA read or write
machine cycles. The wait states are never inserted in the case of DMA verify machine cycles.
HRQ: HRQ stands for HOLD request. This is an active high output pin. This is connected to
the HOLD input of 8085. Whenever a DRQ input is active, and the corresponding
DMA channel is enabled, the HRQ output is activated by the 8257. It then essentially
requests the processor to grant control of the system bus.
HLDA: HLDA stands for HOLD acknowledge. This is an active high input pin, which is con¬
nected to the HLDA output of 8085. When the HLDA input becomes active, it means
that the processor has gone to HOLD state and has relinquished the system bus. Now
the 8257 becomes the master in the microcomputer system.
454
TC: TC stands for terminal count. This is an active high output pin. This output is activated when
all the LS 14 bits of the CR become 0 for the DMA channel being serviced. There is a single
TC output pin although there are four DMA channels. If the I/O ports connected to the DMA
channels have TC input pin, then the TC output of 8257 is connected to the TC input pin of
the I/O port. For example, the TC output of 8257 is connected to the TC input of Intel 8272—
floppy disk controller chip. Thus when the TC output is activated, the I/O port for which
8 DACK* is active, receives the TC input. If the TCS bit in the control port of 8257 is reset to
a>
o 0, it will be the responsibility of the I/O port to withdraw the DRQ. If the TCS bit in the con¬
s
a trol port of 8257 is set to 1, the DMA channel being serviced is automatically disabled. How-
s
.2
ever, if Channel 2 is being serviced, and AULD bit in the control port is set to 1, the Channel
S 2 remains enabled irrespective of the TCS bit value.
in
00 MARK: This is an active high output pin. This output is activated when the LS 7 bits of the CR
S become 0 for the DMA channel being serviced. In other words, whenever the LS byte of the
jj CR becomes 80H or 00H. This happens whenever the number of bytes still to be transferred
H
is an integral multiple of 128 (80H). There is a single MARK output pin although there are
four DMA channels. This output is not normally used in a microcomputer system, because,
the chips that use DMA data transfer, like 8272 and 8275, do not have MARK input.
AEN: AEN stands for address enable. This is an active high output pin. Intel 8257 outputs a 0 on
AEN whenever 8085 is the master of the computer system. When the 8085 goes to the
FIOLD state, the 8257 outputs a 1 on AEN, indicating that 8257 is the master now.
ADSTB: ADSTB stands for address strobe. This is an active high output pin which is similar in
function to the ALE output of 8085. Intel 8257 outputs a 0 on this pin as long as it is in
slave mode. When the 8257 becomes the master, it outputs 1 on ADSTB only during the
first of the four clock cycles of a DMA machine cycle.
Fig. 24.7 illustrates the interfacing of 8257 in an 8085-based system. It uses two Intel 8212
non-programmable I/O port chips and a quad 2-to-l multiplexer. Both 8212s are used in
mode 0. At any instant of time only one of the two 8212s is selected. This is because the
AEN output of the 8257 is connected to DS1* input of 8212-1 and DS2 input of 8212-2.
Thus 8212-1 chip is selected when 8257 outputs a 0 on AEN in the slave mode. The 8212-2 chip
is selected when the 8257 outputs a 1 on AEN in the master mode. It is easy to note from Fig. 24.7
that the 8085 generates the address and control signals MR*, MW*, IOR*, and IOW* when the AEN
output of 8257 is 0. These control signals are generated by the 2-to-l multiplexer using RD*, WR*,
and IO/M* outputs of 8085.
Intel 8257 generates the address and control signals MR*, MW*, IOR*, and IOW* when the AEN
output of 8257 is 1. These control signals are directly generated by the 8257 on its pins. The ADSTB
output is pulsed to latch the MS byte of address sent out by the 8257.
455
8212-1 o»
ALE -r STB DS2 ■r N
3 in
>
A7-0 o I
ADy-0 0 TJ
7 7 S3
S'?0)
MD DS, ■AEN
(=0 if DMAC is slave)
=
Si3
01
7 Z
It
7
8257 8212-2
A 1 5-8
A 15-8/D7-0
\ 7
DS|
ADSTB STB
AEN MD
DS2
MR A3-0
\
MW
7
TOW IOR A7-4
Control bus
MR IOR MW IOW
Quad 2 to 1 Mux
-
RD Ao
1 A,
1 B0 -
RD B,
-
WR Co
1 Ci -*
1 Do
WR °1 SEL OE
IO/M T
AEN
First, the 8257 is programmed by the processor. At this point, the processor is the master and the 8257
works in the slave mode. The processor programs each channel by writing to the AR the starting
8 address of memory for data transfer, and writing to the CR the number of bytes to be transferred using
a> the DMA. The number of bytes information is specified using LS 14 bits of the CR. The MS 2 bits
o
sa indicate the type of DMA data transfer. Then the processor writes to the control port (referred to as
mode set register by Intel). This specifies the DMA channels that are enabled, whether it is fixed or
s
.2 rotating priority, and the like.
S Whenever an I/O port is in need of DMA service, it activates the DRQ input of a channel. The 8257
in
00 in turn activates the FIRQ. The FIRQ output of 8257 is connected to the FIOLD input of 8085. Thus a
s DMA request activates the FIOLD input of 8085. The 8085 then completes the current machine cycle
Jj (not the current instruction cycle) and then goes to the FIOLD state. In the FIOLD state the address
H
pins, data pins, RD*, WR*, and IO/M* pins are tristated. Thus the 8085 is effectively disconnected
from the rest of the system. It announces that it is in the FIOLD state by activating the FILDA output.
The 8057, which was so far a slave to the 8085, and was receiving commands from the 8085, now
becomes the master of the computer system. Of course, it does not become the master of 8085, which
is logically disconnected from the computer system. The 8257 resolves the priorities of the requesting
I/O ports (if there is more than one DMA request active), and sends DACK* signal to the highest pri¬
ority I/O port needing DMA service. This results in chip selection of the requesting I/O device. The
8257 sends out the 16-bit memory address present in AR using the pins A15.8/D7.0, A7.4, and A3.0.
If the required operation is DMA read machine cycle, then the MR* and IOW* signals will be acti¬
vated by the 8257 and the IOR* and MW* signals will be in the inactive state. If the required opera¬
tion is DMA write machine cycle, then the IOR* and MW* signals will be activated by the 8257 and
the MR* and IOW* signals will be in the inactive state. The outputting of memory address and the
activation of control signals results in the transfer of a byte of data between the memory and I/O port
directly. This DMA machine cycle takes four clock cycles. At the end of each DMA machine cycle,
the CR is decremented by 1, the AR is incremented by 1, and DACK is deactivated.
There are three types of DMA data transfers:
■ Single-byte transfer;
■ Short-burst mode;
■ Long-burst mode.
Single-byte transfer: Some I/O ports like Intel 8272 generate a DMA request for each byte of DMA
data transfer. The following steps are performed for each byte of data transfer.
By now we have fully discussed about the pins, the instruction set, and the various machine cycles of
8085. Now we study the state diagram of 8085, as illustrated in Fig. 24.8.
Intel 8085 starts in Treset state at power on. At this point the following flip-flops will have the sta¬
tus as shown in the following.
The halt flip-flop is set to 1 when the 8085 decodes the instruction in IR as HLT instruction.
Intel 8085 checks the value of the HOLD input pin at the end of T2 in a machine cycle consisting
of three or four clock cycles. It is checked at the end of T4 also, if the machine cycle consists of six
clock cycles, like the first machine cycle of a CALL instruction. If it senses a 1 on the HOLD input, the
HLDA flip-flop is set to 1. In such a case, at the end of the current machine cycle, not necessarily at
the end of the instruction cycle, the 8085 activates the HLDA output and enters rhold state.
Intel 8085 checks all the interrupt request pins at the end of the last clock cycle in an instruction
cycle. If it senses any valid interrupt request the INTE flip-flop is reset to 0. This results in disabling
of all interrupts except TRAP so that the ISS can be executed without further interrupts. If the 8085
has been interrupted because of INTR and none of the higher priority interrupt requests are active,
then the INTA flip-flop is set to 1. This results in the activation of INTA* output of 8085.
When reset-in becomes inactive, the 8085 enters the state Tl. This is the first clock cycle of a
machine cycle. At the end of Tl, if the HALT flip-flop status is 0, it enters state T2. At the end of T2,
if the ready input of 8085 is 0 and the machine cycle is not a BI machine cycle, then the 8085 enters
rwait state. (Intel 8085 executes a BI machine cycle only for DAD instruction or when it is required to
respond to a vector interrupt.) As long as the ready input remains 0, the 8085 remains in Tmlt state. If
the ready input becomes 1, then the 8085 comes out of 7'walt state.
Preset
Reset In*
4
Ti
Halt* Ready* Hold*, Valid Int*
Ready*. BI*
0 T2 M
tfi Ready
w Ready
0) +BI Hold
O
0
a
0
Hold i/p= 1SX Set HLDA f/f
Valid Int
o
N Set HLDA f/f
w <- J Reset Halt f/f
oo
o
00
0)
£
<$> — —- 4 elk
6 elk
H
OF*
N
- HLT in IR Hold i/p= 1ÿ>X
Y N Set HLDA f/f
Set Halt f/f
- 4 4-
HLDA \ Y
>-.4
f/f=l I
N Activate
HLDA o/p
N Last m/c\
f <\cycle of instn. Hold
Y
Hold*
N Valid
f
interrupt
Yk-
Reset INTE Ef
Reset HLDA f/f
Deactivate
Valid HLDA o/p
4
Y
vector interrupt
N
Set INTA f/f
Notes
■ No wait states in BI m/c cycle.
■ It is a valid interrupt case when
■ Trap is activated;
■ INTR is activated when INTE f/f = 1 ;
■ RST 1.516.515.5 activated when INTE f/f = 1 and the interrupt pin is not masked.
■ 8085 comes out of rhalt if there is a valid interrupt.
■ 8085 responds to Hold i/p even in 7’halt.
■ In such a case, it returns to 7hau after the DMA data transfer.
Fig. 24.8 State diagram of 8085
459
At the end of T2, if the ready input of 8085 is 1 or the machine cycle is a BI machine cycle, then
the 8085 checks the HOLD input. If the HOLD input is not active, then the 8085 enters T3 state. If the
HOLD input is active, then the 8085 sets the HLDA flip-flop, and enters the T3 state.
If it is the first machine cycle of an instruction (OF machine cycle), the 8085 enters T4 state from
T3. At the end of T4, if it is the last clock cycle for the machine cycle, 8085 checks if the code for HLT
instruction is in the IR. If yes, the 8085 sets the HALT flip-flop, and then checks the status of the
HLDA flip-flop. If the IR is not having the code for HLT, the 8085 directly checks the status of the
=
HLDA flip-flop. S tn
At the end of T4, if it is a six-clock machine cycle, the 8085 again checks the HOLD input. If the
HOLD input is not active, the 8085 enters T5 state. If the HOLD input is active, the 8085 sets the
HLDA flip-flop, and then enters T5 state. From state T5 it enters state T6. At the end of T6 the HLDA
it
S3
flip-flop status is checked. I!
If it is not the first machine cycle of an instruction (like MR, MW, IOR, IOW, INA, BI machine
cycles), the HLDA flip-flop status is checked at the end of the T3 state. Thus the HLDA flip-flop
*1
status is checked at the end of:
■ T3 for MR, MW, IOR, IOW, INA, and BI machine cycles;
■ T4 for OF machine cycle with four-clock cycles;
■ T6 for OF machine cycle with six-clock cycles.
If the HLDA flip-flop is set, the 8085 activates the HLDA output pin and enters state. It
remains in the state as long as the HOLD input is active. When the HOLD input is deactivated,
the 8085 comes out of the HOLD state and resets the HLDA flip-flop. Then the 8085 goes ahead with
the first clock cycle for the next machine cycle in the instruction by entering state Tj.
If at the end of T3 (T4 or T6 as the case may be) the HLDA flip-flop is in reset condition, then the
8085 finds out if it is the last machine cycle for the current instruction. If it is not the last machine cycle,
the 8085 begins the next machine cycle by entering T x state again. If it is the last machine cycle, it means
the current instruction execution is complete. Then it checks for any valid interrupts. If there are no valid
interrupt requests, the 8085 goes ahead with the next instruction cycle by entering Tt state again.
If there is any valid interrupt at the end of an instruction cycle, the 8085 resets the INTE flip-flop.
This ensures that the execution of ISS can proceed without any further interrupts. Then 8085 checks to
find out if INTR is the only valid interrupt. If yes, it sets the INTA flip-flop that activates INTA* output
pin. Then the 8085 goes ahead with receiving a 3-byte CALL instruction, or a 1-byte RSTrc instruction
from an I/O device, by entering T x state of an INA machine cycle. If an interrupt other than INTR is a
valid interrupt, the 8085 directly goes ahead with BI machine cycle, by entering T [ state again.
At the end of Tl, if HALT flip-flop status is 1, the 8085 enters state Tb?Av If the HOLD input
remains inactive and there is no valid interrupt request, the 8085 remains in Tbait state. It will wait in
this state for the activation of the HOLD input or of a valid interrupt request.
If the HOLD input becomes active during rha|„the 8085 sets the HLDA flip-flop, and activates the
HLDA output pin and enters 7ÿ state. As discussed earlier, the 8085 remains in the Tho\A state as long
as the HOLD input is active. When the HOLD input is deactivated, it comes out of the HOLD state
and resets the HLDA flip-flop. Then the 8085 re-enters state from T { state.
If valid interrupt becomes active during rhalt, the 8085 resets the HALT, and INTE flip-flops. This
ensures that the 8085 has come out of halt state, and the execution of ISS can proceed without any
further interrupts. Then, as discussed earlier, the 8085 checks to find out if INTR is the only valid
interrupt. If yes, it sets the INTA flip-flop that activates the INTA* output pin. Then the 8085 goes
ahead with receiving a 3-byte CALL instruction, or a 1-byte RSTn instruction from an I/O device, by
entering T x state of an INA machine cycle. If an interrupt other than INTR is a valid interrupt, the 8085
directly goes ahead with BI machine cycle, by entering T i state again.
460
1. What is meant by DMA? What is the need for DMA data transfer?
o 2. Describe the need for a DMA controller in a microcomputer system.
« 3. Provide a brief overview of the working of 8257 DMA controller.
E 4. Briefly describe the functions of the pins of 8257.
a 5. Explain the function of A3.0, MR*, MW*, IOR*, IOW*, and D7.o/Ai5.8 pins of 8257 when
E it is working as a slave to the processor.
.2
2 6. Explain the function of A3.0, MR*, MW*, IOR*, IOW*, and D7.o/Ai5.8 pins of 8257 when
in
it is working as a master controlling DMA data transfer.
7. Describe the meaning of the MS 2 bits of a count register, when 8257 is connected as a
j; memory-mapped I/O device.
8. Describe the meaning of the MS 2 bits of a CR, when 8257 is connected as an I/O-mapped
I/O device.
9. Explain auto load mechanism feature of 8257.
10. With a neat diagram explain the interfacing of 8257 in an 8085-based system.
11. Describe the function of the important registers available in 8257.
12. With an example, describe the meaning of every bit in the control port of 8257.
13. With an example, describe the meaning of every bit in the status port of 8257.
14. With a neat diagram describe the state transition diagram of 8085 processor.
15. Write the control word needed to perform the following:
a. Enable Channels 0, 3 and disable Channels 1, 2.
b. Enable rotating priority and extended write, disable auto load and TC stop.
16. Assume 8257 control port contains 53H. How does 8257 react to this?
17. Assume 8257 status port contains 53H. What does it mean?
18. Explain single byte transfer, short burst mode, and long burst mode data transfers.
25Intel 8253—
m \
P: Programmable
Interval Timer
■Need for programmable interval timer
■ Description of 8253 timer
■ Programming the 8253
• Read on the fly
• Internal architecture of a counter
■ Mode 0— interrupt on terminal count
■ Mode 1 — re-triggerable mono-stable multi
■ Mode 2 — rate generator
■ Mode 3 — square wave generator
■ Mode 4— software-triggered strobe
■ Mode 5 — hardware-triggered strobe
■ Use of 8253 in ALS-SDA-85 kit
■ Questions
This chapter deals with Intel 8253, which is a programmable interval timer. The various sections
mentioned in the outline of this chapter provide a detailed description of Intel 8253.
There are many situations where accurate time delays are required to be generated in a microcomputer sys¬
tem. For example, if we are implementing a real time clock, the time has to be updated once every second.
Accurate time delays can be generated using a few instructions in a loop as explained in
Sect. 17.13. This is the software method, in which the 8085 does not do any useful work except gen¬
erating time delay.
Time delays could be generated by hardware also. For example, a timer chip like 555 could be used
to generate time delays. In this case, the time delay generated will be dependent on the resistor and
capacitor component values. These R and C components will typically have 10% tolerance. Thus, the
time delay generated will not be very precise, but the processor is now free to do other processing.
462
In order to get accurate time delays by hardware method, programmable timer chips like Intel 8253
are used. In this method, there is very little software overhead and the processor is available for other
processing.
8
2o ■ 25.2 DESCRIPTION OF 8253 TIMER
S
a
E
Intel 8253 is a 24-pin programmable IC available as a DIP package. It has three independent counters
S each of 16-bits width. In addition, there is a control port to decide the mode of working of the three
in
00 counters. Its physical and functional pin diagrams are indicated in Figs. 25.1 and 25.2, respectively.
s«
H Intel 8253 24-pin DIP
Intel 8253
Vcc Clk0
Gnd Gateo
D7-0' Ouÿ
Clkj
CS*
Gate,
RD*
Out.
WR*
Clk2
Ai Fig. 25.2
Gate2
AQ Functional pin diagram
Out2 of Intel 8253
Vcc and Gnd: Power supply and ground pins. 8253 uses +5V power supply.
fÿ7-0* Eight bi-directional data pins for communication with the processor.
RD*: Active low input pin that is activated by the processor to read counter information from the 8253.
WR*: Active low input pin that is activated by the processor to write counter and control
information to the 8253.
CS*: Active low input pin used for selecting the chip.
463
Ab A0: Address input pins. They are used along with RD*, WR*, and CS* to select one of the coun¬
ters or the control port. The control port can be written only from the processor. The proces¬
sor cannot read it. The counters can be read or written. Table 25.1 provides the details.
From the point of view of a microprocessor, the 8253 is a specialized I/O port chip. It is never used
for interfacing I/O devices. It is only used for timing applications in a microcomputer. The 8253 has
A! and A0 as the address input pins. Thus only four addresses are possible for the 8253 ports as seen
from a microprocessor, as was shown in Table 25.1.
The counters are however 16 bits in width. This is because, if they were 8-bits wide, the time delay
generated would have been very small. The LS byte and the MS byte of a counter is selected using
the same port address. When it is desired to load both the LS and MS bytes of a counter: the first time
the port is written, it is stored in the LS byte, and the next time the port is written, it is stored in the
MS byte. This way, the same port address is used to access both the LS and the MS bytes of a counter.
464
The processor writes to the control port to configure the working of the three timers. Actually, the
processor has to write three times to the control port to configure the working of the three counters.
The contents of the control port convey the following information to the 8253.
The interpretation of the contents of the control port is as shown in Fig. 25.3.
For the discussion in this chapter, the chip select circuit is assumed to be such that the port
addresses are as follows.
465
Address Port
80H CounterO
81H Counter1
82H Counter2
83H Control port
=
25.3.1 READ ON THE FLY =s
n W
n—
There are times when it is needed to read the counter value when the count down is in progress and take TJ
a decision based on the current counter contents. If it is desired to read the 16-bit contents of Counter2 H3
and store in DE pair for later processing, then it is enough to execute the following instructions. 5 13
IN 82H; Read LS byte of Counter2 B
MOV E, A; Move it to E register
IN 82H; Read MS byte of Counter2
MOV D, A; Move it to D register
However, when these instructions are being executed, the counter goes ahead with the count-down
operation too. This could lead to wrong reading of the counter value. For example, let us say the count
value was 3400H when reading of the counter is attempted. Then, the E register gets the value 00H.
But by the time the IN 82H instruction is executed the second time, the counter could have counted
down to 33FFH. In such a case, the D register gets the value 33H. In other words, the counter value
is wrongly interpreted to be 3300H instead of the correct 3400H.
To avoid the above mentioned problem, the count-down operation of the counter must be inhibited
temporarily which could be done by inhibiting the clock input. In some modes, the count-down oper¬
ation can be inhibited by making the corresponding gate input 0.
However, if it is desired to read the contents of Counter2 without affecting its normal count-down
operation, the ‘read on the fly’ command is used. In such a case, the control port is written with the
value 10 00 XXXX. It selects Counter2 for read on the fly operation. The Counter2 value, say 3400H
is latched in a temporary register in 8253. Figure 25.4 provides the internal architecture of 8253,
showing the presence of the temporary register. The count-down operation for Counter2 continues as
usual. Thus DE now gets the correct 3400H. It is to be noted that the programmer always has to read
the LS and MS bytes in the ‘read on the fly’ operation.
MVI A, 1O00XXXXB;
OUT 83H; Set up Counter2 for read on the fly operation
IN 82H; Read LS byte of Counter2
MOV E, A; Move it to E register. E value is 00H
IN 82H; Read MS byte of Counted
MOV D, A; Move it to D register. D value is 34H
The internal architecture of a counter is extracted from Fig. 25.4 and is shown in Fig. 25.5.
It is to be noted that to each counter, there is a corresponding background counter, denoted as
BCounter. The counter loads the BCounter at the appropriate moment depending on the mode of
operation of the counter. The counter value remains unchanged even when clock pulses occur. It is
466
16 bits
Co
0
w BC0
w
0)
o 16 bits
0
a D7-0<d\ \
s
2
.2
s Ci
in
oo
o XK
00
tl BCj
x /
H
C2
XX
BC2
Ctrl 0
Ctrl 1
u
-a
■
x
Q
Ctrl 2
-> \
6 bits
Ctrl 8 bits
16 bits
\
Temp Fig. 25.4
Internal architecture of 8253
NI'J/
Clk Fig. 25.5
BCounter Gate
Out Internal architecture of a counter
467
only the BCounter that is decremented for every clock pulse. BCounter is affected by the gate input
depending on the mode of operation of the counter.
MVI A, 10//0001B
OUT 83H; Set up Counter2 for Mode 0. Load LS and then MS bytes. Decimal count down
MVI A, 12H
OUT 82H; Load LS byte of Counted with 12.
MVI A, 34H
OUT 82H; Load MS byte of Counter2 with 34. Thus load Counter2 with 3412 decimal.
Figure 25.6 provides the output waveform when the gate input is at logic 1 throughout the count¬
down operation. If the Clock2 frequency is 1 MHz, the Out2 pin becomes logic 1 after 3,412 |xs.
Figure 25.7 provides the output waveform when the gate input becomes logic 0, when count down
has reached a value of 2,400. When the gate input becomes logic 1 again a little later, the count down
continues from 2,400. Thus, if the gate2 input is in logic-0 state for 1,000 jxs, the Out2 pin goes to logic 1
after 4,412 p,s.
N-O®
Waveform
on Outj
T2 in M0 Z1
1
T2 loaded
Duration of
decrement
TC Fig. 25.6
Counter output waveform
‘1’ in mode 0 when gate input
Gate2 is at logic 1
468
Waveform
on Out2 HI
T2 in MO
i
T2 loaded
TC
Fig. 25.7
‘l’
Effect of gate input on counter
8 Gate2
output waveform in mode 0
a>
o
sa
E
| «25.5 MODE 1-RE-TRIGGERABLE MONO-STABLE MULTI
w
oo
s« The stable state of the multi-vibrator in this mode is logic 1. When a trigger pulse occurs on the gate
H input, the multi-vibrator output goes to logic 0 state, which is the quasi-stable state. It remains in this
state for a duration dependent on the loaded counter value and the clock frequency. Then it reverts to
the stable state of logic 1.
Before a counter is set up for mode 1 (or any other mode) operation, the corresponding output pin
will be in tristate. As soon as a counter is set up for mode 1, the output will become logic 1, the
stable state. The processor loads the counter a little later. However, the counter value is still not copied
into the corresponding BCounter. The value in the counter is copied to the BCounter only when the
trigger pulse occurs, in the form of gate input making a 0 to 1 transition. It results in the output chang¬
ing to the quasi-stable state of logic 0. From then on, the count down of BCounter takes place for every
occurrence of clock pulse, immaterial of the logic state of the gate input. In other words, 1 to 0
transition of the gate input has no effect on the count-down operation. The count down of BCounter
is complete when the BCounter value reaches the TC value of 0000H. Then the output reverts to the
stable state of logic 1.
If the gate input makes a 0 to 1 transition during the count-down operation, the count-down operation
is restarted from the beginning. This is because the 0 to 1 transition on the gate input causes reload¬
ing of the BCounter from the counter. This re-triggering increases the duration of the quasi-stable
state. Hence this mode is known as ‘re-triggerable mono-stable multi’.
Assume the counter is loaded with a new value, say 5,234, when the count down of BCounter is in
progress. This will have no effect on the output as long as no 0 to 1 transition occurs on the gate input
during the count-down operation. If there is a 0 to 1 transition on the gate input during the count down
after the new value 5,234 is loaded, the count down restarts from the value 5,234. This re-triggering
increases the duration of the quasi-stable state.
Let us say, it is desired to have Counter2 operate in mode 1 with a count value of 3,412 decimal.
Then it is necessary to execute the following instructions.
MVI A, 10770011B
OUT 83H; Set up Counter2 for Mode 1. Load LS and then MS bytes. Decimal count down
MVI A, 12H
OUT 82H; Load LS byte of Counted with 12.
MVI A, 34H
OUT 82H; Load MS byte of Counter2 with 34. Thus load Counter2 with 3412 decimal.
Figure 25.8 provides the output waveform when there is no 0 to 1 transition on the gate input
during the count-down operation. If the Clock2 frequency is 1 MHz, the Out2 pin becomes logic 1
after remaining in the quasi-stable state for 3412 p,s.
469
T2 loaded Stable
T2 in Ml
\i Stable
Waveform
on Out2 ■J
<N
223
(N <N
' III ■ ■
Unstable
TC
Gate2 T
Fig. 25.8 =
* effect on
trigger no
Out2
Counter output waveform in
mode 1 (without re-triggering) =s
n W
Figure 25.9 provides the output waveform when the gate input provides another trigger pulse when l!
the count down has reached a value of 2,412. The count down then restarts from 3,412. Thus, the |u
—JTJ
quasi-stable state becomes longer, and the Out2 pin goes to logic 1 after 4,412 p,s. 3
B
T2 loaded
k- 1000 clock A
T2 in Ml
u Stable
Waveform
on Out2 Iff77!!! 111 --
TC
Fig. 25.9
Gate, Effect of re-triggering on counter
trigger re-trigger output waveform in mode 1
Before a counter is set up for mode 2 (or any other mode) operation, the corresponding output pin will
be in tristate. As soon as a counter is set up for mode 2, the output becomes logic 1.
The BCounter is loaded from the corresponding counter when any of the following three condi¬
tions is satisfied.
In mode-2 operation, the output will remain high during the count-down process. The output
becomes logic 0 for one input clock period only for the last count down. The BCounter is then
reloaded from counter value, and the count-down operation continues with the output at logic 1. If the
counter is loaded with a value of 1234H and the clock input has a period of 1 p,s, then the output wave¬
form in mode 2 will be an infinite series of pulses with a pulse width of 1 p,s and the interval between
pulses will be equal to 1,234 p,s. Thus the output is the same as divide by 1234H of the input clock
frequency. Hence mode 2 is called rate generator or frequency divider. In mode-2 operation it is
possible to have the operation synchronized by software or by hardware.
In software synchronization, the gate input is held at logic 1 throughout. The moment the counter
is loaded by the processor, the BCounter is loaded from the counter and the count-down operation
starts. During the last count, the output becomes logic 0 for one input clock period. Then the BCounter
is reloaded from counter value, and the count-down operation continues with the output at logic 1.
470
T2 loaded "fr o
T2 in M2 m o
Waveform
vl ■1233 Clk
(N
s
VO
on Out2 ] R S R J-ÿlk 1
C-4L Fig. 25.10
TC, reload TC, reload TC, no reload
‘1’ Software synchronization
8 Gate2 in mode 2
s
0
sa
E Later, even if the gate input is made logic 0, the count down continues and generates a pulse during
.2 the last count and the operation stops. This is shown in Fig. 25. 10 where a count value of 1234H and
S
in clock input period of 1 |xs are assumed.
00
s« In hardware synchronization, the gate input is held at logic 0 initially. Then, the BCounter is not
loaded from the counter even when the processor loads the counter. Only when the gate input makes
H a 0 to 1 transition, the BCounter is loaded from counter and the count-down operation begins. During
the last count, the output becomes logic 0 for one input clock period. Then the BCounter is reloaded
from counter value, and the count-down operation continues with the output at logic 1. Later, even if
the gate input is made logic 0, the count down continues and generates a pulse during the last count
and the operation stops. This is shown in Fig. 25.11 where a count value of 1234H and clock input
period of 1 |xs are assumed.
T2 loaded
T2 in M2
\i 1233 Clk— > <—1233 Clk— >
Waveform
on Out2 J
■'t ■'t ■'t
m
co ro (N
(N <N i'§§ iclidF 8
TC, reload
rTC, reload8 \
TC, no reload
Gate2 J
Fig. 25.1 1 Flardware synchronization in mode 2
Assume that the counter is presently operating in mode 2 with a count value of 1234H. Let us
say the counter is loaded with a new value, 5,234 when the count down is in progress. This will not
affect the count-down process. Thus after 1,233 clock pulses, the output goes to logic 0 for one clock
period. The BCounter is then loaded from the counter with the new value of 5,234 and the count down
starts again.
Let us say, it is desired to have Counter2 operate in mode 2 with a count value of 3,412 decimal.
Then it is necessary to execute the following instructions.
MVI A, 10770101B
OUT 83H; Set up Counter2 for Mode 2. Load LS and then MS bytes. Decimal count down
MVI A, 12H
OUT 82H; Load LS byte of Counter2 with 12.
MVI A, 34H
OUT 82H; Load MS byte of Counter2 with 34. Thus load Counter2 with 3412 decimal.
471
■ 25.7 MODE 3 -SQUARE WAVE GENERATOR
The mode-3 operation is similar to mode 2, except that square waves are generated instead of pulses.
Before a counter is set up for mode-3 (or any other mode) operation, the corresponding output pin will
be in tristate. As soon as a counter is set up for mode 3, the output becomes logic 1.
The BCounter is loaded from the corresponding counter when any of the following three condi¬
=
tions is satisfied. =s
n W
n—
■ Gate = 1, counter is not presently running, but is loaded; TJ
■ Gate makes a 0 to 1 transition; H3
■ Gate = 1, counter is presently running, and TC is reached. 51
3
In mode-3 operation, the output will remain high for half the time during the count-down process.
For the remaining half-time of count-down process, the output will be in logic 0. If the count value
loaded is an odd number, say 4261FI, then the output will be in logic 1 state for 2131H clock periods
and in logic-0 state for 2130H clock periods. Thus, when the count value is odd, the output will be in
logic-1 state for one clock period more than in logic 0 state.
When the TC is reached the BCounter is reloaded from counter value, and the count-down opera¬
tion continues with the output at logic 1. If the counter is loaded with a value of 4264FI and the clock
input has a period of 1 ps, then the output waveform in mode 3 will be an infinite series of square
waves with 50% duty cycle and a period of 4,264 ps. Thus the output is the same as divide by 4264FI
of the input clock frequency. Flence mode 3 is called square wave rate generator or simply square
wave generator. In mode-3 operation it is possible to have the operation synchronized by software or
by hardware.
In software synchronization, the gate input is held at logic 1 throughout. The moment the counter
is loaded by the processor, the BCounter is loaded from the counter and the count-down operation
begins. The output will be in logic 1 for 50% of the time and logic 0 for the remaining 50% of count¬
down time. The BCounter is then reloaded from counter value, and the count-down operation contin¬
ues with the output at logic 1. Later, even if the gate input is made logic 0, the count down continues
and generates the last square wave and the operation stops. This is shown in Fig. 25. 12 where a count
value of 4264H and clock input period of 1 ps are assumed.
In hardware synchronization, the gate input is held at logic 0 initially. Then, even when the proces¬
sor loads the counter, the BCounter is not loaded from the counter. It is loaded from the counter and
the count-down operation begins only when the gate input makes a 0 to 1 transition. The output will
be in logic 1 for 50% of the time and logic 0 for the remaining 50% of count down time. The BCounter
T2 loaded
T2 in M3 2132 Clk 2132 Clk 2132Clk
Waveform
V 1
on Out2 ■J"I S3
33
TC, reload TC, reload TC, no reload
•r
Gate?
8
a>
o
Gate2
sa Fig. 25.13 Hardware synchronization in mode 3
E
.2
S
in
00 is then reloaded from counter value, and the count-down operation continues with the output at logic 1.
s« Later, even if the gate input is made logic 0, the count down continues and generates the last square
wave and the operation stops. This is shown in Fig. 25.13 where a count value of 4264FI and clock
H
input period of 1 p.s are assumed.
Assume that the counter is presently operating in mode 3 with a count value of 4264FI. Let us say
the counter is loaded with a new value, 5234 when the count down is in progress. This will not affect
the count-down process. Thus after 4,264 clock pulses, the TC is reached. The BCounter is then
loaded from the counter with the new value of 5,234 and the count down starts again.
Let us say, it is desired to have Counter2 operate in mode 3 with a count value of 3,412 decimal.
Then it is necessary to execute the following instructions.
MVI A, 10//0111B
OUT 83H; Set up Counter2 for Mode 3. Load LS and then MS bytes. Decimal count down
MVI A, 12H
OUT 82H; Load LS byte of Counted with 12.
MVI A, 34H
OUT 82H; Load MS byte of Counter2 with 34. Thus load Counter2 with 3412 decimal.
Mode 4 is similar to mode 2 except that the operation does not repeat after TC is reached. Before a
counter is set up for mode 4 (or any other mode) operation, the corresponding output pin will be in
tristate. As soon as a counter is set up for mode 4, the output will become logic 1. The BCounter is
loaded from the corresponding counter whenever the processor loads the counter.
In mode-4 operation, the output will remain high during the count-down process. The output
becomes logic 0 for one input clock period and then the output becomes logic 1 only for the last count
down. Then the BCounter is not reloaded from the counter. If the counter is loaded with a value of
1,234 and the clock input has a period of 1 p,s, then the output waveform in mode 4 will be logic-1
level for 1,233 p,s and logic 0 for 1 p,s, after which the output stays at logic 1. Thus the output
is a strobe pulse of one clock period that is generated after 1,233 input clock periods, as shown in
Fig. 25.14. Hence mode 4 is called software-triggered strobe.
Let us say, it is desired to have Counter2 operate in mode 4 with a count value of 1,234 decimal.
Then it is necessary to execute the following instructions.
473
T2 loaded
T2 in M4
\ I <— 1233 Clk— >
Waveform 1 Clk
on Out2
TC, no reload
‘l’ Fig. 25.14
Gate2 Output waveform in mode 4 =
MVI A, 10//J_001B
s
ff= in
—
» Cl
OUT 83H; Set up Counter2 for Mode 4. Load LS and then MS bytes. Decimal count down 2 t*
H3
MVI A, 34H
OUT 82H; Load LS byte of Counter2 with 34. n
51
MVI A, 12H 3
B
OUT 82H; Load MS byte of Counter2 with 12. Thus load Counter2 with 1234 decimal.
If the gate input is made logic 0 during the count down, the count down is inhibited. When the gate
input becomes logic 1, the count down continues from where it was stopped. Thus, the generation of
strobe pulse is delayed by the time for which the gate input is at logic 0.
This is shown in Fig. 25.15 where a count value of 1,234 and a clock input period of 1 p,s are assumed.
Let us say the gate input becomes logic 0 when the count down has reached a value of 0800. When the
gate input becomes logic 1 again a little later, the count down continues from 0800. Thus, if the Gate2
input is in logic 0 state for 1,000 p,s, the strobe signal is generated by Out2 pin after 2,234 p,s.
T2 loaded
T2 in M4,
Waveform
1
41 fO N "o’ "o’
fO frl frl 1 Clk
on Out2 J <N <N (N
\ Fig. 25.15
TC, no reload Effect of gate input on counter
T’
Gate, output waveform in mode 4
If a new value is loaded into the counter while the count down is in progress, it restarts from the
new loaded value, thus altering the time at which the strobe pulse is generated.
Mode 5 is similar to mode 4 except that the triggering is by hardware instead of software. Before a
counter is set up for mode 5 (or any other mode) operation, the corresponding output pin will be in
tristate. As soon as a counter is set up for mode 5, the output will become logic 1. The BCounter is
loaded from the corresponding counter whenever the corresponding gate input makes a 0 to 1 transi¬
tion. From then on, the count down of BCounter takes place for every occurrence of clock pulse,
immaterial of the logic state of the gate input. In other words, the 1 to 0 transition of the gate input
has no effect on the count-down operation.
In mode-5 operation, the output will remain high during the count-down process. The output
becomes logic 0 for one input clock period only for the last count down, and then becomes logic 1.
474
Then the BCounter is not reloaded from the counter. If the counter is loaded with a value of 1,234
and the clock input has a period of 1 |JLS, then the output waveform in mode 5 will be logic 1 level for
1,233 |JLS and logic 0 for 1 p,s, after which the output stays at logic 1. Thus the output is a strobe pulse
of one clock period that is generated after 1,233 input clock periods, after the occurrence of trigger
pulse as shown in Fig. 25.16. Flence mode 5 is called hardware-triggered strobe.
8
a>
o T2 loaded
2 T2 in M5
a
2
1
.2
S
Waveform
on Out2 J
m m m
M M
(N
•<t CO
fN rN rd Hh 1 Clk
w
oo
s«
H
‘0’
Gate2
trigger
T
n \
n effect on Out2
no
TC, no reload
Fig. 25.16
Output waveform in mode 5
Let us say, it is desired to have Counter2 operate in mode 5 with a count value of 1,234 decimal.
Then it is necessary to execute the following instructions.
If the gate input makes a 0 to 1 transition during the count-down operation, the count-down oper¬
ation is restarted from the beginning. This is because the 0 to 1 transition on the gate input causes
reloading of the BCounter from the counter. This re-triggering delays the generation of the strobe
pulse.
This is shown in Fig. 25.17 where a count value of 1,234 and clock input period of 1 p.s are
assumed. Let us say the gate input makes a 0 to 1 transition when the count down has reached a value
of 0834. Then the count down restarts from 1,234, and so the strobe signal is generated by Out2 pin
after 1,634 p.s.
T2 loaded
T2 in M5
1
Waveform
on Out2 J aa
—
i
— Tf CO
m m
<N *
■'t r*i
o S 8-
S • •
rn rn
Gate2
_ TC, no reload
trigger re-trigger
7. Explain the need for Read on the fly operation. Describe its implementation in 8253.
8. Explain with neat waveforms Mode 0 operation of 8253. Describe the role of Gate input in
this mode.
9. Explain with neat waveforms Mode 1 operation of 8253. Describe the role of Gate input in
this mode.
10. Explain with neat waveforms Mode 2 operation of 8253. Describe the role of Gate input in
this mode.
476
11. Explain with neat waveforms Mode 3 operation of 8253. Describe the role of Gate input in
this mode.
12. Explain with neat waveforms Mode 4 operation of 8253. Describe the role of Gate input
in this mode.
13. Explain with neat waveforms Mode 5 operation of 8253. Describe the role of Gate input in
8 this mode.
a>
o
sa
E
.2
S
in
00
s«
H
26 Intel 8251 A — Universal
m \
P:
Synchronous
Asynchronous Receiver
Transmitter (USART)
■
Need for a USART
■ Asynchronous transmission
■ Asynchronous reception
■ Synchronous transmission
■ Synchronous reception
■ Description of 8251 USART
■ Programming the 8251
•Mode instruction
•Command instruction
• Identifying the commandof in the control port
• Status port 825 1
■ Use of SOD pin of 8085 for serial transfer
■ Questions
This chapter deals in depth with Intel 8251A, which is a universal synchronous asynchronous receiver
transmitter (USART). The 8251A is given the name USART for its capability to virtually support any
serial data format. This chapter gives a detailed description of the topics mentioned in the chapter
outline with neat diagrams.
As the 8251 is used for communication purpose, it is necessary to study the various types of commu¬
nications. In asynchronous transmission mode, the transmission of characters is not at regular inter¬
vals. The transmission is not synchronized with a clock.
The parallel data, to be transmitted in serial format, is sent by the processor to the transmit buffer
of 825 1. The transmit buffer is an 8-bit port that can only be written, but not read, by the processor.
The processor writes to the transmit buffer by activating the CS* input and WR* inputs of 825 1, when
C/D* (control/data*) input is at 0. The information in the transmit buffer is automatically transferred
to transmit shift register. This register acts like a parallel-in serial-out shift register.
When the 8251 chip is selected, the RD*, WR*, and C/D* inputs decide which register is going to
be accessed by the processor, as indicated in the following.
For the chip select circuit shown in Fig. 26.1, the 8251 is connected as an I/O-mapped I/O device.
The transmit buffer and receive buffer have the same address 5OH and the control and status registers
have the same address 51H, as per this figure.
Intel 825 1 is a programmable chip. It can be configured to suit our requirement by writing to the
control port of 8251. The control port is an 8-bit port that can only be written, but not read, by the
processor. The processor writes to the control port by activating the CS* and WR* inputs of 8251,
when C/D* input is at 1. The control port of 8251 is used to supply the following types of informations
to the 825 1.
479
8251
D7-0 / Ns Tx
0 Ay Tx buffer
RD* Shift
1 A6 ~7
Control register
0 A5 WR* TxD
1 A4
0 Ay
Ay
CS* Rx buffer Rx
RxD =
2.
0
0 A| A0
Status Shift
register rs
1 IO/M C/D'
Is
h
Fig. 26.1
■
Chip select circuit for connecting 8251 as l/O-mapped I/O
fi
■ Synchronization character or characters; ||
■ Command instruction (Cl). S J
gg
Using the MI (to be described later), the 8251 can be configured as indicated in the following list
in the asynchronous mode for transmission as well as reception. n
1y
■ 5, 6, 7, or 8 bits per character;
■ Even, odd, or no parity;
■ 1, 1.5, or 2 stop bits;
■ XI, X16, or X64 mode.
Number of Bits per Character: ASCII code is normally used for representing a character. It uses a
7-bit code. Extended ASCII code that uses 8 bits are used when some special characters like graphic
characters are to be represented. Intel 8251 allows the user to specify the character length as 5, 6, 7,
or 8 bits by writing appropriate information in MI.
Parity Bit: Serial communication is basically used for long distance communication. During transit, the
data may get corrupted because of noise on the communication medium. The receiver of the data needs
to be sure that he has received the correct data. This can be achieved to a great extent by appending a par¬
ity bit at the end of the character data. There are two types of parity bits— Odd parity and Even parity.
The bit appended at the end of the character is called odd parity bit if the number of Is in the data
including the parity bit is an odd number.
Example1: Assume the data to be 01010010. In this, the number of Is is 3, which is already an odd
number. Thus the odd parity to be appended at the end of this character will be a 0. The character with
appended odd parity bit will then be 01010010 0.
Example 2: Assume the data to be 01110010. In this, the number of 1s is 4, which is an even number. Thus
the odd parity to be appended at the end of this character will be a 1, so that there may be odd number of
Is after appending the parity bit. The character with appended odd parity bit will then be 01110010 1.
The bit appended at the end of a character is called even parity bit if the number of Is in the data
including the parity bit is an even number.
Example1: Assume the data to be 01010010. In this, the number of Is is 3, which is an odd number.
Thus the even parity to be appended at the end of this character will be a 1, so that there may be even
number of Is after appending the parity bit. The character with appended even parity bit will then be
01010010 1.
480
Example 2: Assume the data to be 01110010. In this, the number of Is is 4, which is already an even
number. Thus the even parity to be appended at the end of this character will be a 0, so that there may
be even number of Is after appending the parity bit. The character with appended even parity bit will
then be 01110010 0.
Sometimes the data is transferred in serial form over only a small distance. An examle is the
8 downloading of the machine code of 8085 program from a personal computer to the 8085 kit using
a> the serial port of the PC. In such a case, it may not be necessary to append a parity bit at the end of
o each character. This improves the speed of transmission. Intel 825 1 can be programmed for append¬
s
a ing an odd parity bit, even parity bit, or not appending any parity bit by writing appropriate informa¬
E tion in the MI.
.2
S
in Start and Stop Bits: In the asynchronous mode of transmission, there can be any amount of time
00
s« gap between the transmissions of two characters. Hence there is a need for the receiver to be informed
about the beginning and end of the character. This is done using the start bit that is appended at the
H beginning of the character and stop bits that are appended at the end of the character.
When there is nothing to transmit, the TxD output of 8251will be in the mark state, which is a
logical 1. The receiver then comes to know that the transmitter is active, but has nothing to transmit.
Hence the moment the transmitter has a character to send, it sends the start bit, which is always a
logical 0. This is followed by the data bits of the character, with LS bit transmitted first and MS bit
last. The parity bit, as discussed here, is computed by the 8251 and is sent next. Finally, stop bits are
sent. The number of stop bits to be transmitted at the end of a character can be programmed to be 1,
1.5, or 2 bits. The stop bit value is always a logical 1.
In asynchronous mode, the receiver frequency can be slightly off from the transmitting frequency
without causing any problems in receiving. This is because the start bit ensures synchronization at the
beginning of every character. If the number of stop bits is programmed for 1.5 or 2 bits there is that
much extra time for the receiver to catch up with the transmitter frequency.
Number of Clocks for Transmitting or Receiving a Bit: Intel 8251 uses transmit clock (TxC*)
input to send out the information in transmit shift register. For every falling edge of TxC*, a bit of
transmit shift register is sent out on TxD output if 825 1 is programmed for X 1 mode. In X 16 mode,
a bit is sent out for every 16 clock transitions on TxC*. In X64 mode, a bit is sent out for every
64 clock transitions on TxC*. This is true only for asynchronous operation. In synchronous mode of
operation, a bit is sent out for every falling edge of TxC*.
Example: Let us say, 8251 is configured for asynchronous data transmission with character length of
5 bits, even parity, 1.5 stop bits, and X 1 mode.
Assume that the 8251 transmit buffer is loaded with 35H using the instructions
MVI A, 35H
OUT 50H
Then the waveform on TxD output pin of 825 1 will be as shown in Fig. 26.2. The 8251 TxD output will
be in mark state (logic 1) initially. When transmit buffer is loaded with 35H, it is automatically moved
to transmit shift register. The contents of transmit shift register are sent out in serial on TxD pin only if
CTS* (clear to send) input pin is activated and TxEn (transmitter enable) bit is set to 1 in the Cl.
The data is shifted out on the falling edge of TxC* for every 1, 16, or 64 clock pulses depending
on whether the 825 1 is programmed for X 1, X 16, or X 64 modes respectively.
If conditions for transmission are met, the 8251 sends out the start bit (a logic 0) on TxD pin. Then
the data bits of the character are sent out on TxD, starting with the LS bit. In the aforementioned
example, only the LS 5 bits of 35H that is 10101 are sent out followed by the even parity bit which is
481
logic 1 in this case. This is finally followed by 1.5 stop bits that are in the logic 1 state. At the end of
all this, the TxD output will again be in the mark state.
It is to be noted that the transmit buffer and transmit shift registers are only 8-bit long. The start
bit, calculated parity bit (if any), and stop bits are sent out automatically from internal registers that
are not accessible to the user.
The data transmission takes place at the rate of 1 bit per clock cycle, as 8251 is programmed for
X 1 mode. If the TxC* frequency is 1 kHz, it needs 8.5 jxs for the transmission of the 5-bit character
=
including start bit, parity bit, and the stop bits. The baud rate can be a maximum of 19.2 kHz for
asynchronous transmission.
The TxRdy output can be used to interrupt the processor. The processor can respond by sending a
character to 825 1 in the ISS. Assume that the processor is in disable interrupt state, or it does not send
a character when it executes the ISS. A short while later the transmit shift register would send out its
contents in serial form on TxD pin. Then both the transmit buffer and the transmit shift register are
empty. In such a case, the TxE (transmitter empty) output is activated by the 8251.
The TxE output can also be used to interrupt the processor. It is deactivated by the 8251 when a
character is written to the transmit buffer by the processor. This output indicates to the processor that
the transmitter section has become completely empty and it is time to switch over from transmit mode
to receive mode in the half duplex mode of operation.
In asynchronous receive mode, the receiving of characters is not at regular intervals. The reception is
not synchronized with a clock. The 825 1 reveives serial data on RxD input pin and stores it in receive
shift register, which acts like a serial-in parallel-out shift register. The information in the receive shift
register is automatically transferred to receive buffer. The receive buffer is an 8-bit port that can only
be read, but not written, by the processor. The processor reads from the receive buffer by activating
the CS* and RD* inputs of 8251.
482
Another important point to be noted is that when the transmitter section of 8251 is configured, the
receiver section is also configured with the same features. Thus, as mentioned earlier, the 8251 can be
configured using MI in the asynchronous receive/transmit mode for:
■ 5, 6, 7, or 8 bits per character;
■ Even, odd, or no parity;
8 ■ 1, 1.5, or 2 stop bits;
a>
o ■ XI, X16, orX 64 mode.
sa During asynchronous reception, the 8251 receives a character of 5- to 8- bits length on RxD pin, along
E with start bit (logic 0) at the beginning, parity bit and stop bits (logic 1) at the end. A bit is received by
.2
S 8251 for every 1, 16, or 64 clock pulses on RxC* input for X 1, X 16, and X64 modes, respectively.
in
00 The RxD pin will normally be in the mark state (logic 1). A 1— >0 transition on this line is treated
s« as the beginning of the start bit, if the 8251 is programmed for X 1 mode. The RxD pin is sensed at
every 0— >1 transition of RxC* in the X 1 mode.
H
In the X 16 and X64 modes there is ‘false start bit’ detection. A 1— >0 transition on RxD pin triggers
the detection of the start bit, if the 8251 is programmed for X 16 or X64 mode. If it is X 16 mode, the
RxD pin value is again checked at the end of eight RxC* clocks on the rising edge of RxC*. It is treated
as a valid start bit only if logic 0 is detected again. From then on, the RxD pin is sensed at the end of
every 16 RxC* clock pulses. This ensures that a bit value is sensed at its nominal mid position.
Similarly, in X64 mode, the RxD pin is again checked at the end of 32 RxC* clocks on the rising
edge of RxC*. Only if logic 0 is detected again, it is treated as a valid start bit. From then on, the RxD
pin is sensed at the end of every 64 RxC* clock pulses. This ensures that a bit value is sensed at its
nominal mid polistion.
If incorrect parity bit is received by the 8251, it sets the parity error (PE) bit in the status register.
If logic 0 is detected, when the 825 1 is expecting the occurrence of a stop bit, it sets the framing error
(FE) bit in the status register. The 825 1 is satisfied with receiving one stop bit— it does not matter if
it was programmed for 1, 1.5, or 2 stop bits.
Once a character is received in the receive shift register without PE and FE, it is automatically
moved to the receive buffer. Then the RxRdy (receiver ready) output pin of 825 1 is activated to indi¬
cate that there is data in the receive buffer for the processor to receive it. This happens assuming that
RxEn (receiver enable) bit is set to 1 in the Cl. The RxRdy pin is deactivated when the processor reads
the receive buffer.
The receiver is double buffered, which means that when the data in the receive buffer still remains
to be read by the processor, the receive shift register is ready to receive another character.
The RxRdy output can be used to interrupt the processor. The processor can respond by reading a
character from 8251 in the ISS. Assume that the processor is in disable interrupt state, or it does not read
a character when it executes the ISS. A short while later the receive shift register may receive another
character and send it to the receive buffer. Then the earlier character in the receive buffer that is yet to
be read by the processor is overwritten. This error sets the over-run error (OE) bit in the status register.
Although one or more of the three error bits are set, the 825 1 continues with the receive operation
without any effect. It is to be noted that the receive buffer and receive shift register are only 8-bit long.
The start bit, parity bit (if any), and the stop bits are automatically received in internal registers that
are not accessible to the user.
Example: Let us say, the 825 1 is configured for asynchronous data reception with character length of
5 bits, even parity, 1.5 stop bits, and X 1 mode.
If the serial input is as shown in Fig. 26.3 on RxD input pin of 825 1, then the data shifted into the
receive shift register will be 10110, with the MS 3 bits as 000. This is the value that is then transferred
to the receive buffer.
483
1.5 Stop bits
Mark
RxD
Start 0 1 1 0 1 1
Fig. 26.3
bit LS
bit
MS
bit
Even
parity
— > time Waveform on RxD in =
2.
bit asynchronous reception
rs
If the processor executes IN 5 OH instruction, then the 8251 chip is selected, RD* input is acti- Is
§. |
vated, and C/D* input becomes 0. The accumulator contents become 000 10110. The processor now o C
ignores the MS 3 bits, and recognizes the character data as 10110. The RxRdy pin is now deactivated. §
ft
r
||
■ 26.4 SYNCHRONOUS TRANSMISSION S|
=ÿ3
If
In synchronous transmission, characters are sent one after another without any gap, synchronized by i3
clock pulses. Before the actual synchronous transmission, the processor writes to 8251 control port S’
one or two synchronization characters, depending on the way the 8251 is configured. Thus the 8251
is aware of the sync (synchronization) characters to be used in the synchronous mode.
First of all, the 8251 sends out the programmed number of sync characters on the TxD pin. This is
followed by the assembled data characters. The assembled data characters will not have any start or
stop bits. The start and stop bits are not needed any more as one data character follows another with¬
out any time gap between them. However, the assembled data characters may have the optional par¬
ity bit. Since there are no start and stop bits for each character, the synchronous mode of transmission
is faster. It is to be noted that in synchronous mode, a bit is sent out of TxD pin for every TxC* clock.
It does not support X 16 and X64 modes.
The 8251 expects a steady stream of data characters from the processor for transmission on TxD
output pin. In case the transmitter becomes empty, indicated by TxE signal becoming active, the 825 1
automatically sends out programmed number of sync characters on TxD pin to avoid losing synchro¬
nization. The waveform on TxD in synchronous transmission mode is shown in Fig. 26.4.
In synchronous mode, the receiver frequency must match the transmitting frequency. Else, it causes
problems in receiving. This is because there is no start bit that ensures synchronization at the begin¬
ning of every character.
TxE activated
Mark
TxD SCI SC2 DC1 DC2 DC3 SCI SC2 DC4
Fig. 26.4 Waveform on TxD in synchronous transmission assuming two sync characters
484
■ 26.5 SYNCHRONOUS RECEPTION
Waveform on RxD in synchronous receive mode is shown in Fig. 26.5. In synchronous mode, the
receiver frequency must match the transmitting frequency. Else, it causes problems in receiving. This
8 is because there is no start bit that ensures synchronization at the beginning of every character.
M
0
2 RxD
SCI SC2 DC1 DC2 DC 3 DC4 SCI SC2 DC 5
a
2
time
s SynDet activated Discarded by
U) processor
00
s« Fig. 26.5 Waveform on RxD in synchronous reception assuming two sync characters
H
In this mode, character synchronization can be achieved either internally or externally. It depends
on bit 6 of the MI, when programmed for synchronous operation. This bit may be called ESD (exter¬
nal sync detect) bit, when programmed for synchronous operation.
Before any data is received by 8251 in internal sync detect mode, the EH (enter hunt) bit must be
set to 1 in the CL This results in 8251 hunting for programmed number of sync characters on RxD
input. The characters received are stored in internal registers that are not accessible to the user. They
are compared with the programmed sync characters. Once it detects the sync characters, the 825 1
comes out of the hunt mode and activates the SynDet (SD) output. Once the 8251 comes out of the
hunt mode it starts receiving characters in the receiver buffer. It is to be noted that in synchronous
mode, a bit is received on RxD pin for every RxC* clock. It does not support X 16 and X64 modes.
SD pin is an output pin in internal sync mode. This output is automatically deactivated when the
processor reads the status register.
When in external sync mode, the SD pin is an input pin. In this case, external circuit checks for
sync characters. Once sync characters are detected, logic 1 is input on the SD input. The 825 1 then
comes out of the hunt mode and starts receiving characters. The logic 1 on SD pin can be removed
after one RxC* cycle.
Once sync characters are detected, the 825 1 starts receiving characters in the receive shift register
and moves it to the receive buffer for the processor to read. After some data characters are received,
if sync characters are again received on the RxD pin, they are received in the receive buffer for the
processor to read. It now becomes the responsibility of the processor to identify them as sync charac¬
ters and discard them, as they do not form part of the data.
Intel 8251 is a 28-pin programmable IC available as a DIP package. Its physical and functional pin
diagrams are indicated in Figs. 26.6 and 26.7, respectively.
485
8251 28-pin DIP
D2 I 28 Dj
D3 2 27 <— * D0
RxD 3 26 Vcc
Gnd 4 25 RxC*
—
D4 < > 5 24 DTR* =
—
D5 1 > 6 23 RTS*
DSR* rs
2.
D6 7 22
D- 8 21 Reset
Is
TxC*
WR*
9 20 Clk
4
r
10 19 TxD
§
cs* 11 18 TxE
C/D* 12 17 CTS*
RD* 13 16 <— > SD/BD Fig. 26.6
ft
RxRdy 14 15 TxRdy
Pin diagram of Intel 8251 ||
S|
=ÿ3
8251 If
Vcc TxD
i
§
Gnd TxC* i?
TxRdy
TxE
CS*
RxD
RD*
RxC*
WR*
RxRdy
C/D*
SD/BD
Reset
CTS*
Clk
RTS*
DSR*
DTR*
Fig. 26.7
Functional pin diagram of 8251
Vcc and Gnd: Power supply and ground pins. 8251 uses +5V power supply.
D7-0: Eight bi-directional data pins for communication with the processor.
RD*: Active low input pin that is activated by the processor to read status information and
receive buffer information from the 825 1.
WR*: Active low input pin that is activated by the processor to write to control register and
to transmit buffer of the 825 1.
CS*: Active low input pin used for selecting the chip.
TxD: It stands for Transmit Data Output pin, on which parallel data received from the
processor is sent out in serial fashion.
TxRdy: It is the abbreviation for Transmitter Ready. Active high output pin, which indicates
to the processor that the 825 1 is ready to receive a character into the transmit buffer.
This output can be used to interrupt the processor. The TxRdy output is activated
when the following conditions are satisfied.
486
■ Transmit buffer is empty;
■ TxEn bit is set to 1 in the Cl;
■ CTS* input is active.
It is deactivated by the 8251 when a character is written to the transmit buffer by the processor.
TxE: It is the abbreviation for Transmitter Empty. Active high output pin, which indicates to the
8 processor that the transmitter section has become completely empty— both transmit buffer
a> and transmit shift register are now empty. It is time then to switch over from transmit mode
o to receive mode in the half duplex mode of operation. The TxE output can also be used to
s
a interrupt the processor. It is deactivated by the 8251 when a character is written to the trans¬
E mit buffer by the processor.
.2
E TxC*: It is the abbreviation for Transmitter clock. Clock input to the transmit shift register. The
in
00 data is shifted out of transmit shift register in serial form on the falling edge of TxC*, for
s« every 1, 16, or 64 clock pulses in asynchronous mode. This depends on whether the 8251 is
programmed for Xl, X16, or X 64 modes. In synchronous mode, data is shifted out for
H
every clock pulse.
Clock: The clock input is used to generate internal timing for the 8251. No external inputs or out¬
puts are referenced to the clock input. This frequency must be at least 30 times greater than
the transmitter or receiver bit rates.
RxD: It is abbreviation for Receive data. Input pin on which data is received in serial fashion. This
is converted to parallel form by the 8251, which is read by the processor.
RxC*: It is abbreviation for Receiver clock. Clock input to the receiver Shift register. The data is
shifted into the receiver shift register in serial form on the rising edge of RxC*, for every 1,
16, or 64 clock pulses in asynchronous mode. This depends on whether the 8251 is pro¬
grammed for Xl, X16, or X64 modes. In synchronous mode, data is shifted into the
receiver shift register for every clock pulse.
Intel 825 1 is normally required to handle the transmission and receive operations of a
single link. In such situations, the transmission and reception baud rates are required to be
the same. The TxC* and RxC* pins are then connected to the same frequency source.
RxRdy: It is abbreviation for Receiver Ready. Active high output pin, which indicates to the processor
that the 825 1 has received a character in the receiver buffer. This output can be used to inter¬
rupt the processor. The RxRdy output is activated when the following conditions are met.
the processor. It is deactivated by the 8251 when the RxD pin receives a logic 1, or the 8251 is reset. S' |
In the synchronous mode of operation, the same pin is used for synchronization detection. The §3
synchronization detection can be internal or external to 8251. This is a programmable feature of c|
8251. It is decided by bit 6 of MI, when the 8251 is programmed for synchronous operation. If it is JJ g
external synchronization detection, this pin acts as an input pin to the 8251, informing the 8251 about 0
the detection of synchronization. The logic 1 on this pin can be removed after one RxC* cycle. < 3
If it is internal synchronization detection, this pin acts as an output pin to the 8251. This * §ÿ
output can be used to interrupt the processor. It is deactivated by the 8251 when the proces-
sor reads the status register of 8251.
-If 0
Modem control pins: The use of 8251 relieves the processor from the time consuming par- i;
allel to serial conversion during transmission, and serial to parallel conversion during receive ®
operation. However, sending information over a long distance on a single line will be expen¬
sive, unless the existing cable network, like telephone network, is used for communication.
In such a case we have to use audio frequencies. So a MoDem (modulator-demodulator)
working as a modulator is used for converting a logic 0 to an audio frequency of 2200 Hz and
a logic 1 to another audio frequency of 1200 Hz, and transmitted over a carrier on the tele¬
phone network. At the receiving end, another modem, working as a demodulator, converts
these frequencies back to 0s and Is, as shown in Fig. 26.8.
Processor Processor
8251 8251
TxD Modu¬ Telephone line Demod¬
A 0s lator 0s as 2200 Hz ulator RxD y
and Is DCE Is as 1200 Hz DCE
DTE Long distance DTE
Sender Receiver
Modems are generally called data communication equipment (DCE). The originators and
terminators of data are called data terminal equipment (DTE). Thus, 825 1 is a DTE. There
are instances when serial communication is used even for short distance communication with
a peripheral. In such cases, the peripheral is called the DCE.
The interaction between DTE and DCE is handled by the following four modem control
signals.
DTR*: It is the abbreviation for Data Terminal Ready. This is an active low output pin. It informs
the modem about the readiness of the 825 1 to receive or transmit serial data. This pin is acti¬
vated when the DTR bit in the Cl is set to 1. The DTR bit information is inverted and sent
out on DTR* pin.
488
DSR*: It is the abbreviation for Data Set Ready. This is an active low input pin. It is used by the
modem to inform the 8251 that it is ready to accept data from the 8251 for transmission. The
processor can obtain the logic value on this pin by reading the MS bit of the status register
of 8251. The DSR* pin information is inverted and stored in the MS bit of the status regis¬
ter. Thus if the MS bit of status register is 1, it means that the DSR* pin is active.
RTS: It is the abbreviation for Request To Send. This is an active low output pin. It informs the
8
a> modem that the 8251 has data to be transmitted. This pin is activated when the RTS bit in
o the Cl is set to 1. The RTS bit information is inverted and sent out on RTS* pin.
sa CTS*: It is the abbreviation for Clear to Send. This is an active low input pin. It is used by the modem
E to inform the 8251 that the line is clear for the 8251 to send the data. The processor has no
.2
S means of reading the logic value on this pin, as the status register of 8251 does not provide
in
00 this information. The information comes out on TxD pin of 8251 only if CTS* is active and
s« the TxEn bit in Cl is set to 1. When CTS* is deactivated or TxEn bit is reset to 0, the trans¬
mission is disabled after the transmission of the character in progress is completed.
H
Prior to data transmission or reception, the 8251 must be loaded with a set of control words. This must
follow a reset of 8251. The reset of 8251 can be done in the following two ways.
■ By sending a logic 1 pulse on the external reset input pin of 8251. This is hardware reset.
■ By setting the IR (internal reset) bit of Cl to 1. This is software reset.
The control port of 825 1 is used to supply to the 825 1 the following types of information.
■ Mode instruction;
■ Synchronization character or characters;
■ Command instruction.
55H = 01 01 01 01
Dy D6 D5 D4 D3 D2 D! DO
EvP EnP
ft
D5 = 1 means even parity (EvP), D4 = 1 means enable parity (EnP)
00 = No parity ||
01 = Odd parity S|
=ÿ3
10 = No parity
If
11 = Even parity
i
§
D7, D6: Provides different information depending on whether it is asynchronous mode or #
synchronous mode. Configures the number of stop bits in asynchronous mode (Db D0 ¥= 00)
00 = Invalid
01 = 1 stop bit
10 = 1.5 stop bits
11=2 stop bits
Configures the number of sync characters, external/internal sync detection in sync mode (Db D0 = 00).
In this case D7 = 1 means single sync char, D6 = 1 means external sync detect
00 = Double sync character, internal sync detect
01 = Double sync character, external sync detect
10 = Single sync character, internal sync detect
11 = Single sync character, external sync detect
Similarly, if 64H is the content of the control port when it contains MI, it means the following:
64H = 01 10 01 00
Command instruction controls the actual operation of the 8251 as indicated in Fig. 26.10. The Cl may
have to be altered several times during the course of communication. For example, the transmitter
might be needed to be disabled, the error bits might have to be reset, and so on.
8
a>
u D7 D6 D5 D4 D3 D2 D, Do
Sa
E EH IR RTS ER SBRK RxEn DTR TxEn
.2
S
in
00 TxEn: 1 = Transmitter enabled
s« 0 = Transmitter disabled
The control port contains MI, Cl, or sync characters. The 825 1 interprets just by the context the
contents of the control port as indicated in Fig. 26.11. After a reset operation, the 825 1 expects the MI
to be written to the control port by the processor. This reset operation could be due to hardware reset
using the reset input pin, or software reset using the IR bit of CL The LS 2 bits of MI clearly indicate
whether the 825 1 is configured for synchronous or asynchronous mode.
If it is asynchronous mode, the next value written to the control port is interpreted as CI. Any sub¬
sequent value written to the control port is interpreted to be a new CI that overrides the previous CI.
If it is in synchronous mode, the MS bit of the MI decides whether the 825 1 is configured for one
or two sync characters. If it is configured for single sync character the next value written to the
control port is interpreted as the sync character. When the control port is written the third time, it is
interpreted to be CI. Any subsequent value written to the control port is interpreted to be a new CI that
overrides the previous CI.
491
If it is configured for double sync character the next value written to the control port is interpreted as
the first sync character. When the control port is written the third time, it is interpreted to be the second
sync character. When the control port is written the fourth time, it is interpreted to be Cl. Any subse¬
quent value written to the control port is interpreted to be a new Cl that overrides the previous Cl.
Thus to enter a new MI, the 8251 has to be reset first before writing to the control port.
=
Reset
srs
MI
fi
if
Sync
mode
N
is
fl
Y CI
||
Y Single I S|
=ÿ3
I sync
If
sc 1
I SCI
N
CI i
§
CI
I I i?
T SC2
I
1 CI
CI
I
T
1 Fig. 26.11
Cl
Interpreting the con¬
T tents of the control port
If the processor writes to the control port five times after the reset of 825 1, then the interpretation
of control port contents is as follows.
Async mode Sync mode— single sync char Sync mode— double sync char
MI MI MI
CI SC (sync character) SCI (sync char 1)
CI CI SC2 (sync char 2)
CI CI CI
CI CI CI
The status information of 825 1 is read by the processor when CS* = 0, C/D* = 1, and RD* =0. Both
control and status ports have the same address, but control port is written by the processor, while the
status port is read by the processor. The contents of the status Port Provide Status information about
8251 as indicated in Fig. 26.12.
492
D7 D6 D5 D4 D3 D2 D, Do
DSR SD/BD FE OE PE TxE RxRdy TxRdy
The RxRdy pin of 8251 need not be connected to any interrupt pin of the processor. The RxRdy bit
of status register can be read by the processor to find the status of the RxRdy pin. If this bit is set to 1,
the processor can read from the receiver buffer of 825 1. This allows status check data transfer as an
alternative to interrupt driven data transfer. In a similar way, the TxE and SD/BD bits of the status
register can be read by the processor to find out about the status of TxE and SD/BD pins.
The TxRdy status bit only indicates whether the transmit buffer is empy or not. The TxRdy output
is activated only when the following are true.
When information has to be sent by 8085 over long distances, it is economical to send it on a single
line. The SOD pin can be used for this purpose in 8085. In that case the 8085 has to convert an 8-bit
493
parallel data to serial data and then output it on SOD pin. Thus a lot of CPU time is required for such
a conversion. The program to send out serial data on SOD pin is provided in the following. It is
assumed that:
■ Register B contains the parallel data to be transmitted in serial form on SOD pin of 8085.
■ The baud rate required is 300. Hence the time for sending a bit is 1/300 s=3.33 |xs.
■ No parity bit used.
■ Two stop bits to be used. >s
12
If
Program
MVI A, 01000000B
if5 5
fi
u
SIM ; Send a 0 (Start bit) on SOD pin.
CALL BITTIME; subroutine for a time delay of 3.33 mSec
MVI C, 08; C used as counter to transmit 8 data bits S o
AGAIN: MOV A, B
RAR 35
MOV B,A;The 3 instructions move the LS bit of B to Cy flag
ll (A
3
x
MVI A, 10000000B 2
RAR; Move a bit of B which is in Cy flag to MS bit of A
SIM
CALL BITTIME
DCR C
JNZ AGAIN; Come out when 8 data bits sent out on SOD pin
MVI A, 11000000B
SIM
CALL BITTIME ; Send a 1 (Stop bit) on SOD pin
CALL BITTIME ; Send a 1 (Second Stop bit) on SOD pin
HLT
If 8251 is available in the system, the user has to just send the parallel data into the transmit buffer
of 8251. The rest would be taken care of by the 8251. This justifies the need for 8251 in a micro¬
computer system.
P: Microprocessor
In the previous chapters Intel 8085 microprocessor was dealt with in depth. This chapter and the next
deal with two other very popular 8-bit microprocessors. They are Z-80 microprocessor from Zilog
Corporation, and M-6800 microprocessor from Motorola Corporation. This will provide the reader
with a comparison of the most popular 8-bit microprocessors.
496
■ 27.1 COMPARISON OF INTEL 8080 WITH INTEL 8085
Intel 8080 is the predecessor of both Intel 8085 and Zilog Z-80. In fact, the main people responsible
for the development of Intel 8080 left Intel Company and started their own company by the name
o Zilog. Thus, a brief study of Intel 8080 will enable the reader to appreciate the improvements that have
o taken place in Intel 8085 and Zilog Z-80.
0
The functional pin diagrams of Intel 8080 and Intel 8085 are partly provided in Fig. 27.1. The figure
a
o also shows the clock generator chip— Intel 8224 and system controller chip— 8228 used with 8080.
The Intel 8224 clock generator provides two-phase clock to the 8080 as shown in Fig. 27.2. The
S
in
following observations could be made from Fig. 27.1.
00
o
00
O 8080 40-pin DIP 8085 40-pin DIP
Figure. 27.1 Functional pin diagram of Intel 8080 and Intel 8085
Qi
Q2
The programmer’s view of Z-80 is provided in Fig. 27.3a and the simplified architecture of Z-80 in
Fig. 27.3b.
The Z-80 consists of the following registers, the details of which will be dealt with later.
The alternate set of secondary 8-bit registers cannot be used in Z-80 instructions directly. Thus
there are no instructions like ‘move data from B' register to D' register’ and ‘move data from C reg¬
ister to B' register’. The only instructions that make use of these alternate set of registers are EXX and
EX AF, AF'.
The EXX exchanges B, C, D, E, H, L with B', C', D', E', H', L'. It is an 1-byte instruction with
the code D9H. It is executed in just four clock cycles. Thus, it is a very fast way of saving B, C, D, E,
H, L registers inside the Z-80 itself.
The EX AF, AF' exchanges AF with A'F'. It is an 1-byte instruction with the code 08H. It is exe¬
cuted in just four clock cycles. Thus, it is a very fast way of saving A and F registers inside the Z-80
itself.
Instruction Operation Opcode
EX AF, AF' Exchange AF with A'F' 08H
EXX Exchange B, C, D, E, H, L with B', C', D', E', H', L' D9H
16 bits
A F A' F' SP
B C B' C' PC
D E D' E' IX
H L H' L' 1Y
8 bits 8 bits 8 bits 8 bits
8 Kite 8 Kite
Fig. 27.3a Programmer’s view of Z-80
498
\ / \ y
A Temp IR
00 Mux/
demux
A' F'
B' C'
8
M
\ Cy w z D' E'
F 8-bit ALU Instruction
U B C H' V
decoder
Sa y
D E
/
E H L
.2 IX
s 3,
IY
U) 2
00 Vcc SP
s« Gnd PC
H \ /
Data
Reset* buffer
Incr/decr
Wait* Control unit Address latch
Clk
Address
buffer
/ \
All the instructions of Intel 8080 are present in Z-80 and many new ones are provided. Only the RIM
and SIM of 8085 are not in Z-80. The mnemonic is different in Z-80 and 8080 for the same instruc¬
tion in a majority of the cases. For example, ‘DAD B’ instruction of 8080/8085 is the same as ‘ADD
HL, BC’ of Z-80 with the same opcode 09H. Thus the programs written for 8080/8085 can be directly
run on Z-80, only after the assembly source code of 8080/8085 is translated to machine language. In
the following table, the Z-80 mnemonics and the corresponding 8080/8085 mnemonics are provided.
In this table, only the instructions that have different mnemonics in Z-80 and 8085 are included. The
convention followed in the table is indicated as follows.
dl6 = 16-bit immediate data;
al6 = 16-bit address;
a8 = 8-bit address;
r = A, B, C, D, E, H, or L.
‘ADD/ADC A, d8’ stands for ‘ADD A, d8’ and ‘ADC A, d8’;
‘ADD HL, BC/DE/HL/SP’ stands for ‘ADD HL, BC’, ‘ADD HL, DE’ etc.;
‘LD (HL) | r’ stands for ‘LD (HL), r’ and ‘LD r, (HL)’.
ADD/ADC A, d8 ADI/ACI d8
ADD/ADC A, (HL) ADD/ADC M
ADD/ADC A, r ADD/ADC r
ADD HL, BC/DE/HL/SP DAD B/D/H/SP
SUB d8 SUI d8
SUB (HL) SUB M
SBC A, d8 SBI d8
SBC A, (HL) SBBM
SBC A, r SBBr
CALL C/NC/Z/NZ/P/M/PO/PE al6 CNC/CC/CZ/CNZ/CP/CM/CPO/CPE al6
RET C/NC/Z/NZ/P/M/PO/PE RNC/RC/RZ/RNZ/RP/RM/RPO/RPE
JP al6 JMP al6
JP C/NC/Z/NZ/P/M/PO/PE al6 JNC/JC/JZ/JNZ/JP/JM/JPO/JPE al6
JP (HL) PCHL
RST 0/8/ 1OH/ 18H/20H/28H/30H/38H RST 0/1/2/3/4/5/6/7
INC/DEC r/(HL) INR/DCR r/M
INC/DEC BC/DE/HL/SP INX/DCX B/D/H/SP
CCF, SCF CMC, STC
500
CP r/(HL) CMP r/M
CPd8 CPI d8
CPL CMA
EX DE, HL XCHG
EX (SP), HL XTHL
HALT HLT
0 IN A, (a8) IN a8
M
M OUT (a8), A OUT a8
U
0 LD A, (a 16) LDA al6
o LD (a 16), A STA al6
a
2 LD A, (BC)/(DE) LDAXB/D
.2 LD (BC)/(DE), A STAX B/D
S LD HL, (a 16) LHLD al6
in LD (a 16), HL SHLD al6
00
o LDrl,r2 MOV rl, r2
00
tl LD (HL) | r MOV M | r
x LD r/(HL), d8 MVI r/M, d8
LD BC/DE/HL/SP, dl6 LXI B/D/H/SP, dl6
LD SP, HL SPHL
PUSH/POP BC/DE/HL/AF PUSH/POP B/D/H/PSW
RLA/RLCA/RRA/RRCA RAL/RLC/RAR/RRC
As the Z-80 instruction set is a superset of the 8080 instruction set, it has all the addressing modes
of 8080/8085. In addition, there are a number of new addressing modes in Z-80. They are: relative
addressing, indexed addressing, and bit addressing. These new addressing modes of Z-80 are
described as follows.
‘JR EXIT’ is an example for an instruction that uses relative addressing. Here JR stands for ‘jump
relative’. It is a 2-byte instruction. EXIT is a symbolic memory location to which the branch occurs
when ‘JR EXIT’ is executed. Assume EXIT is actual memory location 2357H, and ‘JR EXIT’ instruction
is at location 2345H as shown in Fig. 27.4.
By the time the Z-80 fetches the ‘JR EXIT’ instruction, the PC would have been incremented to
2347H. Location EXIT is 10H locations further from location 2347H. Thus the assembler generates
an 8-bit signed relative displacement value of 10H. Hence the machine code for the instruction would
be ‘18 10’. Here 18H is the opcode for the mnemonic JR, and 10H is the signed displacement corre¬
sponding to location EXIT. This 18H happens to be one of the unused opcodes of Intel 8080. The fol¬
lowing 12 opcodes were unused in Intel 8080, as only 244 opcodes were implemented: 08H, 10H,
18H, 20H, 28H, 30H, 38H, CBH, D9H, DDH, EDH, and FDH.
501
Program starting at 8900H Same program shifted to start at 8C00H
8900H 8900H
8920H JP 8C00H
8921H 42
8922H 89 8C20H JP Ifi
3
o 5*
42
8C co
3
■o °
8942H next instr.
I
8C42H next instr. 5
0
If EXIT were to be memory location 2337H, the assembler would have generated an 8-bit signed
relative displacement value of FOH, which is nothing but — 10H when interpreted as 2’s complement
signed number. Hence the machine code for the instruction would have been generated as ‘18 FO’. In
general, the JR is coded as ‘18 r8\ where r8 is the 8-bit signed displacement.
Advantages of relative addressing: Let us say we have used absolute jump instructions in our pro¬
gram, as shown in Fig. 27.5a. It may be noted that the JMP instruction of 8085 has the mnemonic
JP in Z-80. Thus JP stands for absolute jump in Z-80. Assume that our program is stored in memory
starting from 8900H and for some reason, we want to shift our program to memory starting from loca¬
tion 8C00H. If the program is simply moved to a new starting location as 8C00H, it will not work
properly in that location. It is necessary to modify the addresses that appear in the absolute jump
instructions in order that the program works fine in the relocated region. For example, at location
8C20H the instruction should be changed from ‘JP 8942H’ to ‘JP 8C42H’.
Let us suppose we have used relative jump instructions in our program, as shown in Fig. 27.5b.
Assume that our program is stored in memory starting from 8900H and for some reason, we want to
shift our program to memory starting from location 8C00H. If the program is simply moved to a new
starting location as 8C00H, it will work properly in that location. It is not necessary to modify the
8900H 8900H
8920H JR
892 1H 20 8C00H
8922H
8C20H JR
8C21H 20
8C22H
8922H + 20H = 8942H next instr.
After the execution of these instructions the P/O flag is set to 1 if there are even number of 1’s in the
result, otherwise it is reset to 0. The P/O flag works as O flag when the Z-80 executes any of the arith¬
metic instructions (Ex. ADC D).
Overflow flag: The O flag is meaningful only while working with signed numbers. We just ignore
this flag with unsigned numbers. Overflow indicates a wrong answer. The processor sometimes gives
a wrong answer when it is performing signed number arithmetic. With unsigned number arithmetic,
we always get the correct answer. To drive home the aforementioned points, the meaning of O flag is
explained as follows with examples.
Addition of unsigned numbers: If we add 42H and 43H. The result will be 85H with Cy flag value
as 0, as shown in the following. If we interpret 42H and 43H as unsigned numbers, the answer is
correct. In fact, a microprocessor never gives a wrong answer with unsigned arithmetic.
42H
+ 43H
Notice that there are no relative jump instructions that perform a jump based on the value of S flag
and P flag which are not commonly needed. However, if conditional jump based on S flag or P flag is
desired, one has to use conditional absolute jump instructions.
Finally, there is one more very useful relative jump instruction which is ‘DJNZ r8\ It is also a
2-byte instruction whose opcode is 10H. Its working is as follows.
It is very useful in executing a program segment repetitively for a specified number of times. Thus
‘DJNZ BACK’ instruction is functionally equivalent to the execution of the following two instruc¬
tions, which occupy a total of 4 bytes.
DEC B
JP NZ BACK
Thus six of the 12 unused opcodes of 8080 are used for the six relative jump instructions in Z-80.
Opcode size for Z-80 instructions: As Z-80 has about 700 opcodes, it may seem that opcodes have
to be 10-bits long (as 29 = 512 and 2 10 = 1,024). This problem is ingeniously solved as follows.
The Z-80 instructions that are common with 8080 (a total of 244) will have the same 8-bit opcode
as that of 8080. With 8 bits, a maximum of 28 = 256 opcodes are theoretically possible. So another
12 opcodes are possible. These were not used in 8080. Out of them eight opcodes are used in Z-80 to
provide eight new instructions. Six of these new instructions come under relative addressing, dis¬
cussed earlier. The other two are EXX and EX AF, AF' which are also discussed earlier.
There are still four opcodes remaining which are CBH, DDH, FDH, and EDH. Whenever a Z-80
instruction starts with one of these opcodes it means that it is a pure Z-80 instruction with no parallel
in 8080/8085. To exactly find out the new Z-80 instruction, the Z-80 performs second opcode fetch.
504
Instruction Operation Opcode
The second byte of opcode now indicates the actual new Z-80 instruction. For example, the decoding
8 of 23H by itself, and when preceded by one of the four aforementioned opcodes is shown in Table 27.2.
a>
o
2
a
Table 27.2 Interpretation of Opcode 23H in Z-80
2 Opcode Decoded instruction
s 23H INC HL
U)
00 DD23H INC IX
s« FD 23H
CB23H
INC IY
SLA E (Shift Left Arithmetic E register)
H ED 23H Not a valid instruction
With this scheme, 4 X 256 = 1,024 additional instructions are possible. Flowever only 446
additional instructions are implemented using this scheme.
The new Z-80 instructions that start with the aforementioned four opcodes are briefly indicated in
Table 27.3. Instructions that start with the four opcodes as shown in Table 27.3 will have a length of
the opcode as 2 bytes.
‘ADD A, (IX+20H)’ is an example for an instruction that uses indexed addressing. The execution of
this instruction results in the addition of A contents with the contents of a memory location. The
address of the memory location is partly provided in IX register. The other part of the memory address
is provided in the instruction itself. In the aforementioned instruction, the other part of the address is
20H. The 20H in this instruction is commonly called the displacement. It is 8 bits in size, and is an
unsigned number. It is denoted as ‘d8’ in general to indicate displacement of 8 bits size. Indexed
addressing is a very powerful addressing mode and is very convenient to access data values in a table.
Example: Assume that A has 30H before the execution of this instruction and the IX content is 8900H.
This is interpreted as part of the memory address. The other part of the address provided in the instruc¬
tion is 20H. Thus the complete memory address is 8920H. If the contents of this location are 35H, then
35H is added to A register contents. The result will be stored in A register. Thus, A contents are changed
to 65H after the execution of the instruction. It is indicated diagrammatically as follows.
Before After
(A) 30H 65H
(IX) 8900H
(8920H) 35H
If the instruction set of Z-80 does not provide bit addressing, then the following five instructions are
to be executed for setting bit 3 of B register. In 8085 this method is used for setting bit 3 of B register,
2
g N
o CO
PUSH AF; It is PUSH PSW of 8085. Save A and F on stack top 3
■B
°
LD A, B; It is MOV A, B of 8085. Move B contents to A
OR 08H; It is ORI 08H of 8085. Set bit 3 of A register I
LD B, A; It is MOV B, A of 8085. Save result in B register
5
O
POP AF; It is POP PSW of 8085. Restore A and F values
Bit SET instruction: The Z-80 provides a powerful bit addressing mode. An example for an instruc¬
tion that uses bit addressing is ‘SET 3, B’. The execution of this single instruction results in setting
bit 3 of B register to the 1 state. This amply demonstrates the power of bit addressing mode.
‘SET 3, B’ is a 2-byte instruction with the opcode CB D8H.
CBH indicates that it is a pure Z-80 instruction involving shift, rotate, or bit processing.
D8H = 11 Oil 000 indicates that the instruction is ‘SET 3, B’ because of the following.
11 indicates that it is a SET instruction.
N = nnn = 011 indicates that bit 3 is to be set.
R = rrr = 000 indicates that register B is to be used in the instruction.
Similarly, the execution of ‘SET 3, (IY +3OH)’ instruction results in setting bit 3 of a memory loca¬
tion. The address of the memory location is partly provided in IY register. The other part of the mem¬
ory address is 30H, which is provided in the instruction itself.
If IY content is 8900H, it is interpreted as part of the memory address. The other part of the address
provided in the instruction is 30H. Thus the complete memory address is 8930H. Assume that the con¬
tents of this location is 35H = 0011 0101, with bit 3 value as 0. After the execution of this instruc¬
tion, the contents of memory location 893OH change to 3DH = 0011 1101, indicating that bit 3 has
been set to 1.
‘SET 3, (IY +30H)’ is a 4-byte instruction with the opcode FD CB 30 DEH.
FDH indicates that it is a pure Z-80 instruction involving IY register.
CBH indicates that it is a pure Z-80 instruction involving shift, rotate, or bit processing.
3OH provides the displacement value d8 in the indexed addressing mode.
DEH = 11 Oil 110 indicates that the instruction is ‘SET 3, (IY +30H)’ because of the following.
11 nnn 110 indicates that it is ‘SET N, (Iz+d8)’ instruction, where Iz = IX or IY
N = nnn = 011 indicates that bit 3 is to be set.
From these examples, it is clear that there are two general formats for the SET instruction. They are
‘SET N, R’ and ‘SET N, (Iz+d8)\ where
N = bit number, a value in the range 0-7
R = A, B, C, D, E, H, L, or (HL) that stands for memory pointed by HL.
Iz = IX or IY
d8 = 8-bit unsigned displacement.
Bit RES instruction: The other bit processing instructions provided in the instruction set of Z-80 are
the RES and BIT instructions. RES stands for reset a specified bit. BIT stands for test a specified bit.
506
The execution of ‘RES 4, C’ results in resetting bit 4 of register C to the 0 state. It is a 2-byte
instruction. Similarly, the execution of ‘RES 3, (IY +3OH)’ instruction results in resetting bit 3 of a
memory location. The address of the memory location is partly provided in IY register. The other part
of the memory address is 30H. ‘RES 3, (IY+30H)’ is a 4-byte instruction. The two general formats
for the RES instruction are ‘RES N, R’ and ‘RES N, (Iz+d8)’.
8 BIT instruction: The execution of ‘BIT 4, C’ results in testing bit 4 of register C. If the bit value is 0,
a> then the Z flag is set to 1, otherwise it is reset to 0. The instruction execution does not alter the bit
o
sa value. The BIT instruction is generally followed by ‘JR NZ r8’ or ‘JR Z r8’ instruction. ‘BIT 4, C’ is
E a 2-byte instruction.
.2 Similarly, the execution of ‘BIT 3, (IY +30H)’ instruction results in testing bit 3 of a memory loca¬
S
in tion. The address of the memory location is partly provided in IY register. The other part of the mem¬
00
s« ory address is 30H. ‘BIT 3, (IY+30H)’ is a 4-byte instruction. The two general formats for the BIT
instruction are ‘BIT N, R’ and ‘BIT N, (Iz+d8)’.
H
DAA Instruction: The DAA instruction of 8085 always adds 00H, 06H, 60H, or 66H depending on
the contents of A register and the status of Cy and ACy flags. In other words, it is functionally same
as ADI 00H/06H/60H/66H. Intel gave the expansion for ‘DAA’ instruction of 8085 as ‘Decimal Adjust
Accumulator’. But a more appropriate expansion would have been ‘Decimal Adjust after Addition’,
because, the DAA instruction of 8085 is supposed to be used for arriving at correct decimal answer
only after addition of two 2-digit BCD numbers. It should not be used for arriving at correct decimal
answer after subtraction of two 2-digit BCD numbers. In fact, there is no single instruction in 8085 to
arrive at correct decimal answer after subtraction of two 2-digit BCD numbers.
As an example, assume that we want to compute 45 —28. The required answer is 17. If the sub¬
traction were done treating the numbers as hexadecimal, the accumulator contents would be 1DH.
Then the execution of DAA instruction of 8085 results in addition of 06H, to get the BCD answer as
23. This is an incorrect result. To get the correct result of 17, 06H would have to be subtracted from
1DH, but there is no single instruction in 8085 to do this.
Now consider Z-80, which has DAA instruction standing for ‘Decimal Adjust Accumulator’. It is
indeed a very apt expansion in the case of Z-80 because, the DAA instruction of Z-80 can be used for
arriving at correct decimal answer immaterial of addition or subtraction of two 2-digit BCD numbers.
The DAA instruction of Z-80 is functionally the same as
■ value of N flag;
■ contents of the accumulator;
■ contents of Cy and AC flags.
To summarize, the DAA instruction of Z-80 is functionally the same as
ADD A 00H/06H/60H/66H if N flag = 0 H co
SUB 00H/06H/60H/66H if N flag = 1 o 5*
v
3 °
LDI Instruction: Assume that we want to move the contents of memory location C200H to memory o
location C100H. It could be done using the following program segment without altering the contents %
w
of register A.
LD HL, C200H; Same as LXI H, C200H of 8085
LD DE, C 100H; Same as LXI D, C 100H of 8085
PUSH AF; Same as PUSH PSW of 8085
LD A, (HL); Same as MOV A, M of 8085
LD (DE), A; Same as STAX D of 8085
POP AF; Same as POP PSW of 8085
An easier and more efficient method in Z-80 would be to use the LDI instruction. The execution
of this instruction results in the following:
‘LDI’ stands for LoaD (from memory pointed by HL to memory pointed by DE) and then Increment
HL and DE (BC is always decremented). It is a 2-byte instruction with the opcode ED A0H where:
■ EDH indicates that it is a pure Z-80 instruction.
■ A0H indicates that it is LDI instruction.
Thus, to move the contents of memory location C200H to memory location C100H, the easier
option would be:
LD HL, C200H
LD DE, C100H
LDI
A more powerful and useful instruction is the LDIR instruction.
LDIR Instruction: Assume that we want to move a block of information of size 6 bytes starting at
location C200H to the block at location C100H. If the processor is 8085, we are required to execute
a program to achieve this. Such a program was provided in Sect. 15.4 of Chap. 15. A similar program
could be written in Z-80 also, but it can be made more simple and efficient using the block move
instruction LDIR provided in Z-80.
The execution of the LDIR instruction results in the following:
1. Move contents of memory pointed by HL to memory pointed by DE.
2. Increment HL and DE contents by 1.
3. Decrement BC contents by 1.
4. Repeat steps 1, 2, and 3 if BC content is not 0000H.
508
‘LDIR’ stands for LoaD (from memory pointed by HL to memory pointed by DE), then Increment
HL and DE (BC is always decremented), and Repeat the operation (till BC becomes zero). It is a
2-byte instruction with the opcode ED BOH where:
■ EDH indicates that it is a pure Z-80 instruction.
■ BOH indicates that it is LDIR instruction.
8
M Thus, to move the contents of a block of 6 bytes of memory starting at location C200H to memory
o location C100H, the easier option would be:
a
E LD HL, C200H
.2 LD DE, C100H
S
in LD BC, 0006H
00
s LDIR
i! LDD Instruction: An alternative method to move the contents of memory location C200H to mem¬
ory location C100H would be to use the LDD instruction. The execution of this instruction results in
the following:
‘LDD’ stands for LoaD (from memory pointed by HL to memory pointed by DE) and then
Decrement HL and DE (BC is always decremented). It is a 2-byte instruction.
Thus, to move the contents of memory location C200H to memory location C100H, an alternative
easier option would be:
LD HL, C200H
LD DE, C100H
LDD
A more powerful and useful instruction is the LDDR instruction.
LDDR Instruction: Assume that we want to move a block of information of size 6 bytes starting at
location C200H to the block at location C100H. An alternative to LDIR instruction is the equally sim¬
ple and efficient instruction LDDR provided in Z-80.
The execution of the LDDR instruction results in the following:
‘LDDR’ stands for LoaD (from memory pointed by HL to memory pointed by DE), then Decre¬
ment HL and DE (BC is always decremented), and Repeat the operation (till BC becomes zero). It is
a 2-byte instruction.
Thus, to move the contents of a block of 6 bytes of memory starting at location C200H to memory
location C100H, the equally easy option using LDDR would be:
LD HL, C205H
LD DE, C105H
LD BC, 0006H
LDDR
509
CPI Instruction: The execution of CPI instruction results in the following:
‘CPI’ stands for ComPare (register A contents with contents of memory pointed by HL) and then
Increment HL (BC is always decremented). It is a 2-byte instruction with the opcode ED A1H where:
2
o co
■ EDH indicates that it is a pure Z-80 instruction. 3 °
■B
■ A1H indicates that it is CPI instruction.
A more powerful and useful instruction is the CPIR instruction.
I
5w
CPIR Instruction: Assume that we want to search for a specific byte value of 23H in a block of "*
information of size 6 bytes starting at location C200H. If the processor is 8085, we are required to
execute a program to perform this linear search operation. Such a program is provided in Sect. 16.1
of Chap. 16. A similar program could be written in Z-80 also, but it can be made more simple and effi¬
cient using the block search instruction CPIR provided in Z-80.
The execution of the CPIR instruction results in the following:
‘CPIR’ stands for ComPare (A register contents with contents of memory pointed by HL), then
Increment HL (BC is always decremented), and Repeat the operation (till BC becomes zero). It is a
2-byte instruction with the opcode ED B1H where:
■ EDH indicates that it is a pure Z-80 instruction.
■ B1H indicates that it is CPIR instruction.
Thus, to search for a specific byte value of 23H in a block of information of size 6 bytes starting
at location C200H, the easier option would be:
LD HL, C200H
LD A, 23H
LD BC, 0006H
CPIR
The Z flag is set to 1 if the byte value 23H is found anywhere in the block. The location in the block
where the byte is found is pointed by HL. If the byte were not found in the block, then the Z flag would
be reset to 0. Thus ‘JR NZ r8’ or ‘JR Z r8’ instruction normally follows the CPIR instruction.
‘CPD’ stands for ComPare (A register contents with contents of memory pointed by HL) and then
Decrement HL (BC is always decremented). It is a 2-byte instruction. A more powerful and useful
instruction is the CPDR instruction.
510
CPDR Instruction: Assume that we want to search for a specific byte value of 23H in a block of
information of size 6 bytes starting at location C200H. For this purpose the CPIR instruction could be
used. An equally simple and efficient method is using the block search instruction CPDR in Z-80.
The execution of the CPDR instruction results in the following:
1. Move data to memory location pointed by HL from input port whose address is provided in
C register.
2. Increment HL contents by 1.
3. Decrement B contents by 1.
511
‘INI’ stands for IN put (from input port whose address is provided in C register to memory pointed
by HL) and then Increment HL (B is always decremented). It is a 2-byte instruction. A more power¬
ful and useful instruction is the INIR instruction.
INIR Instruction: The execution of INIR instruction results in the following:
1. Move data to memory location pointed by HL from input port whose address is provided in
C register.
2. Increment HL contents by 1. H co
o 5*
3. Decrement B contents by 1. 3
■B
°
4. Repeat steps 1, 2, and 3 if B content is not 00H.
I
‘INIR’ stands for INput (from input port whose address is provided in C register to memory pointed 5w
by HL), then Increment HL (B is always decremented), and Repeat the operation (till B becomes "*
zero). It is a 2-byte instruction, which is used for reading a string of bytes from an input port and stor¬
ing in consecutive memory locations. It is a DMA type of instruction, as the port directly communi¬
cates with memory in this case.
IND Instruction: The execution of IND instruction results in the following:
1. Move data to memory location pointed by HL from input port whose address is provided in
C register.
2. Decrement HL contents by 1.
3. Decrement B contents by 1.
‘IND’ stands for INput (from input port whose address is provided in C register to memory pointed
by HL) and then Decrement HL (B is always decremented). It is a 2-byte instruction. A more power¬
ful and useful instruction is the INDR instruction.
INDR Instruction: The execution of INDR instruction results in the following:
1. Move data to memory location pointed by HL from input port whose address is provided in
C register.
2. Decrement HL contents by 1.
3. Decrement B contents by 1.
4. Repeat steps 1, 2, and 3 if B content is not 00H.
‘INDR’ stands for INput (from input port whose address is provided in C register to memory
pointed by HL), then Decrement HL (B is always decremented), and Repeat the operation (till B
becomes zero). It is a 2-byte instruction, which is used for reading a string of bytes from an input port
and storing in consecutive memory locations. It is a DMA type of instruction, as the port directly com¬
municates with memory in this case.
OUT (C), reg Instruction: Assume that we want data to be moved from B register to output port
number 56H. This could be done using the following program segment.
PUSH AF
LD A, B
OUT (56H), A ; Same as OUT 56H of 8085
POP AF
In an 8085-based system, this method was the only way to move data from B register to an I/O port.
However, in a Z-80-based system there is a simpler and more efficient method using ‘OUT (C), reg’
512
instruction. Here, ‘reg’ refers to any of the registers A, B, C, D, E, H, or L. For example, the execu¬
tion of ‘OUT (C), B’ instruction results in the following:
■ Move data from B register to output port whose address is provided in C register.
■ Only C register is allowed to have the 8-bit address of the I/O port.
8 Thus to move data from B register to output port number 56H, it is enough to execute the follow¬
a>
o ing two instructions.
sa LD C, 56H
E OUT (C), B
.2
S ‘OUT (C), B’ is a 2-byte instruction.
in
00
s« OUTI Instruction: The execution of OUTI instruction results in the following:
H 1. Move data from memory location pointed by HL to output port whose address is provided in
C register.
2. Increment HL contents by 1.
3. Decrement B contents by 1.
‘OUTI’ stands for OUTput (from memory pointed by HL to output port whose address is provided
in C register) and then Increment HL (B is always decremented). It is a 2-byte instruction. A more
powerful and useful instruction is the OTIR instruction.
1. Move data from memory location pointed by HL to output port whose address is provided in
C register.
2. Increment HL contents by 1.
3. Decrement B contents by 1.
4. Repeat steps 1, 2, and 3 if B content is not 00H.
‘OTIR’ stands for OuTput (from memory pointed by HL to output port whose address is provided in C
register), then Increment HL (B is always decremented), and Repeat the operation (till B becomes zero). It
is a 2-byte instruction, which is used for sending a string of bytes from consecutive memory locations to
an output port. It is a DMA type of instruction, as memory communicates directly with the port in this case.
1. Move data from memory location pointed by HL to output port whose address is provided in
C register.
2. Decrement HL contents by 1.
3. Decrement B contents by 1.
‘OUTD’ stands for OUTput (from memory pointed by HL to output port whose address is provided
in C register) and then Decrement HL (B is always decremented). It is a 2-byte instruction. A more
powerful and useful instruction is the OTDR instruction.
1. Move data from memory location pointed by HL to output port whose address is provided in
C register.
513
2. Decrement HL contents by 1.
3. Decrement B contents by 1.
4. Repeat steps 1, 2, and 3 if B content is not 00H.
‘OTDR’ stands for OuTput (from memory pointed by HL to output port whose address is provided
in C register), then Decrement HL (B is always decremented), and Repeat the operation (till B
becomes zero). It is a 2-byte instruction.
This instruction is used for sending a string of bytes from consecutive memory locations to an out- 3N
put port. It is a DMA type of instruction, as memory communicates directly with the port in this case. !o co
3°
■B
I
27.5.1 ROTATE AND SHIFT INSTRUCTIONS 5w
Rotate instructions: The rotate instructions of Z-80 that were available in 8085 also are shown in
Table 27.4.
In 8085 the rotate operations are possible only on A register contents. In Z-80 the rotate operations
could be performed on the contents of
These new rotate instructions of Z-80 are shown in Table 27.5. The following convention is used in
this table.
R=A,B, C, D, E, H, L, or (HL);
Iz = IX or IY;
d8 = 8-bit unsigned displacement.
RLD and RRD Instructions: In addition to the powerful rotate instructions described, there are two
more instructions under the rotate group of instructions. They are RLD and RRD.
514
RLD instruction: RLD stands for ‘rotate left digit’. It is a 2-byte instruction. The first byte indicates
that it is a pure Z-80 instruction, and the second byte indicates that it is RLD instruction. It works on
the contents of A register and the contents of memory pointed by HL. There are a total of four hex
digits in A register and the memory contents pointed by HL. However, rotate left operation is per¬
formed only on three hex digits, leaving the MS hex digit in register A unchanged. It is shown dia-
8 grammatically in Fig. 27.6.
a>
o
sa
E A I 2 3 4 (HL)
Fig. 27.6
S
in
Illustration of RLD instruction
00
s« Example:
H Before After
A = 12H (HL) = 34H A = 13H (HL) = 42H
RRD instruction: RRD stands for ‘rotate right digit’. It is a 2-byte instruction. The first byte indicates that
it is a pure Z-80 instruction, and the second one indicates that it is RRD instruction. It works on the con¬
tents of A register and the contents of memory pointed by HL. There are a total of four hex digits in A reg¬
ister and the memory contents pointed by HL. However, rotate right operation is performed only on three
hex digits, leaving the MS hex digit in A register unchanged. It is shown diagrammatically in Fig. 27.7.
A I 2 3 4 (HL)
Fig. 27.7
Illustration of RRD instruction
Example
Before After
A = 12H (HL) = 34H A = 14H (HL) = 23H
Shift instructions: Intel 8085 did not have shift instructions. These are the additions in Z-80. A shift
instruction just shifts the number in a register or a memory location to the left or to the right by one
bit position. There will not be any rotation of the bits in this case. In Z-80 the shift operations could
be performed on the contents of
The shift instructions are of two types: arithmetic shift and logical shift.
Arithmetic Shift Instructions: Arithmetic shift instructions are used when the number to be shifted
is interpreted by the programmer to be a signed number. In arithmetic shift operation the sign of the
number remains unchanged after the operation.
SRA instruction: SRA stands for ‘shift right arithmetic’. It shifts all the bits to the right by one position.
The LS bit that comes out is moved to Cy flag. In the vacancy created at the MS bit position, the earlier
MS bit value is retained. The effect of the execution of SRA instruction is indicated in Fig. 27.8.
515
MS bit Cy
Fig. 27.8
Effect of execution of SRA instruction
A right shift operation divides the signed number by 2, if it is an even number. When an odd num- M
her is right shifted, the result is truncated to the nearest integer. For example, when SRA instruction 2
operates on number 5, the result will be 2. Similarly, when SRA instruction operates on number FBFI 3N
(interpreted as —5), the result will be FDFI (interpreted as —3). o co
The two general formats for the SRA instructions are:
3
■B
°
I
■ SRA R of size 2 bytes; 5w
■ SRA (Iz+d8) of size 4 bytes
where
R = A, B, C, D, E, H, L, or (HL)
Iz = IX or IY
d8 = 8-bit unsigned displacement.
SLA instruction: SLA stands for ‘shift left arithmetic’. It shifts all the bits to the left by one position.
The MS bit that comes out is moved to Cy flag. In the vacancy created at the LS bit position,
a 0 value is moved. The effect of the execution of SLA instruction is indicated in Fig. 27.9.
Cy o Fig. 27.9
Effect of execution of SLA instruction
The left shift operation has the same effect immaterial of the number to be shifted being treated as
signed or unsigned. The condition for correct multiplication by 2 depends on whether the number
before shift is treated as unsigned or signed, as shown in the following table with an example each.
Logical Shift Instructions: Logical shift instructions are used when the number to be shifted is inter¬
preted by the programmer to be an unsigned number.
SRL instruction: SRL stands for ‘shift right logical’. It shifts all the bits to the right by one position.
The LS bit that comes out is moved to Cy flag. A 0 value is moved in the vacancy created at the
MS bit position. The effect of the execution of SRL instruction is indicated in Fig. 27.10.
Cy
o- Fig. 27.10
Effect of the execution of SRL instruction
516
A right shift operation divides the signed number by 2, if it is an even number. When an odd num¬
ber is right shifted the result is truncated to the nearest integer. For example, when SRL instruction
operates on number 5, the result will be 2. Similarly, when SRL instruction operates on number FBFI,
the result will be 7DFI.
The two general formats for the SRL instructions are:
8
M ■ SRL R of size 2 bytes;
§ ■ SRL (Iz+d8) of size 4 bytes.
a
E Similarly, the ‘shift left logical’ instruction shifts all the bits to the left by one position. The MS bit
S that comes out is moved to Cy flag. A 0 value is moved in the vacancy created at the LS bit position.
in
00 This same action is performed when SLA instruction is executed. As such, there is no ‘shift left logi¬
s« cal’ instruction in the instruction set of Z-80. Whenever ‘shift left logical’ operation is desired, an SLA
instruction is executed.
H
Other Instructions: The remaining pure Z-80 instructions are provided in Table 27.6. The following
convention is used in describing the instructions.
Iz = IX or IY;
zp = BC, DE, Iz, or SP (note that FIL is not allowed);
dl6 = 16-bit immediate data;
al6 = 16-bit address;
a8 = 8-bit address;
r = A, B, C, D, E, H, or L;
rp = BC, DE, HL, or SP.
INC/DEC Iz stands for INC Iz and DEC Iz;
LD Iz | (al6) stands for LD Iz, (al6) and LD (al6), Iz;
IM 0/1/2 stands for IM 0, IM 1, and IM 2.
The devices normally used along with Z-80 to provide the required functions in a microcomputer are:
Z-80 PIO (similar to 8255 PPI) and Z-80 CTC (similar to 8253 timer).
The actual pin diagram and the functional pin diagram of Z-80 are provided in Figs. 27.11 and
27.12, respectively.
H co
o 5*
Z-80 40-pin DIP 3°
■B
A11
Ai2
4- 1
2
40 -4 A10
a9
I
A]3 <—
4— 39 -4 5w
3 38 -4 a8
A14 4— 4 37 -> A7
A15 4- 5 36 -4 A6
Clk -4 6 35 ■4 A5
D4 o 7
D3 <4 8
34
33
— a4
-4 A3
D5 <4 9 32 -4 a2
D6
—
<4
Vcc | 1 1
Dj <4 12
D7 <4 13
10 31
30
29
28
-4 Ai
-4 Ao
t- Gnd*
-4 Rfsh*
Do 44 14 27 -4 Ml*
D, <4 15 26 t— Reset*
INI* 16 25 4— BusRq*
NMI* -4 17 24 t- Wait*
Halt* 4- 18 23 BusAk*
MREQ* 4- 19 22 -4 WR*
IORQ* 4- 20 21 RD* Fig. 27.11
Pin diagram of Z-80
A15-0: Unidirectional 16 address pins on which address is sent out by Z-80. Thus, Z-80 is capa¬
ble of addressing 2 16 = 64K memory locations.
D7-0: Bi-directional 8 data pins on which data is sent out or received by Z-80. Thus, Z-80 is an
8-bit microprocessor.
Vcc, Gnd: Power supply pins. They use a dc power supply of +5 V.
Clk: The Z-80 receives the clock input on this pin. An external clock generator generates the
clock.
518
MREQ*: Active low output pin. The Z-80 sends out a 0 on this pin when communication with
memory is desired.
IORQ*: Active low output pin. The Z-80 sends out a 0 on this pin when communication with I/O
ports is desired.
RD*: Active low output pin. The Z-80 sends out a 0 on this pin when a read operation from
8 memory or an input port is desired.
a> WR*: Active low output pin. The Z-80 sends out a 0 on this pin when a write operation to mem¬
o ory or an output port is desired.
s& Reset*: Active low input pin. When momentarily brought down to 0 state, the Z-80 is reset.
| Wait*: Active low input pin. It is used to insert wait states when working with slow memory chips.
S BusRq*, BusRq* is an active low input pin, which is similar in function to HOLD
in
00 BusAk*: input of 8085. BusAk* is an active low output pin. It is similar in function to Hlda output
s of 8085. These two pins are used for facilitating DMA.
i NMI*, These are active low interrupt input pins. NMI stands for non-maskable interrupt
INT*: and INT stands for interrupt that could be masked or disabled. More details about these
interrupts are provided little later.
Halt*: Active low output pin. It is activated by the Z-80 when the HALT instruction is
executed.
Ml*: Active low output pin. Here Ml stands for Machine cycle number1. In other words, the Z-
80 activates this output whenever the first machine cycle of an instruction (opcode fetch
machine cycle) is in progress.
Rfsh*: Active low output pin. Here RFSH stands for refresh. This pin is used for refreshing dynamic
RAM chips, if any are used in the system. RAM is available in two types: static RAM
(SRAM) and dynamic RAM (DRAM). In SRAM, the chip information is stored in flip-flops.
It requires about six transistors to implement a flip-flop on the average in a memory chip.
The advantage of SRAM is that the information remains intact as long as the power is on. In
a DRAM, information is stored on a tiny capacitor as a charge (1 state) or no charge (0 state).
It needs only one transistor to write a bit to the DRAM cell or read the bit on the DRAM cell.
Thus DRAMs require much less power per bit of storage. Hence more number of bits can be
stored in a given size of chip compared to SRAM, which results in lower storage cost per bit.
The disadvantage of DRAM is that the charge on the tiny capacitor tends to change due to
leakage. Thus, periodic refreshing of information must be done to avoid losing the informa¬
tion. This refresh cycle is essentially a memory access operation and must be performed in
a time less than about 2 jxs for each of the rows of the DRAM. The R register (refresh reg¬
ister) of Z-80 eases the burden (as briefly described in the following) of dealing with this
constant refresh requirement associated with DRAMs.
The contents of R register is incremented automatically by 1 after each opcode fetch.
As soon as the opcode byte enters the instruction register, decoding of the instruction starts.
This is done in the fourth clock cycle of the opcode fetch machine cycle. The decoding
operation is entirely internal to the processor. During this period, the Z-80 outputs the con¬
tents of the R register on the LS byte of address bus. Simultaneously, the Z-80 activates
Rfsh* output. This results in the refreshing of the entire row of DRAM specified by R reg¬
ister. A few microseconds later, when the next opcode byte is being decoded, the new
incremented value of R register and Rfsh* are output. This refreshes the next row of
DRAM memory. A typical DRAM could be totally refreshed using this method in about
300 |xs, which is well within the limit of 2 p.s.
The Z-80 is probably the only 8-bit microprocessor to provide refresh facility on chip for DRAMs.
However, it is not normally made use of. This is because of the fact that, SRAMs are used mostly in
Z-80-based systems, as the maximum memory addressing capacity is a meager 64K bytes.
519
■ 27.7 INTERRUPT STRUCTURE IN Z-80
The Z-80 has two external interrupt pins, which are NMI* and INT*. Although there are only two
interrupt pins, compared with the five in 8085, the Z-80 has a superior interrupt structure than the 8085.
The following description illustrates the superiority of Z-80 interrupt structure compared to 8085.
H co
o 5*
27.7.1 INT* INTERRUPT 3 °
■B
INT* is an active low, level-triggered interrupt input pin. This is an interrupt that could be masked or I
disabled. The execution of DI instruction disables this interrupt. In such a case, even if an I/O port 5w
activates the INT* pin, the Z-80 will not be interrupted. Even after a reset of Z-80 the INT* interrupt %
is disabled. The Z-80 is always started with activation of reset. Thus, in a Z-80 program, if it is desired
that the Z-80 should be interrupted by INT* pin, then the El instruction must be present in the pro¬
gram. The INT* interrupt is enabled only after the execution of the El instruction.
The action performed by the Z-80 when INT* pin is activated depends on the interrupt mode of Z-80.
There are three interrupt modes that are: interrupt mode 0, interrupt mode 1, and interrupt mode 2.
Interrupt mode 0 (IM 0): This is actually a 2-byte instruction of Z-80 with the opcode ED 46H. After
the execution of this instruction the INT* input of Z-80 works in IM 0. In fact, this is the default mode
of operation after reset of Z-80. Thus if the interrupt mode type is not explicitly specified by execut¬
ing one of IM 0, IM 1, or IM 2 instructions, the Z-80 works in IM 0.
INT* in IM 0 mode of operation is very similar to INTR interrupt of 8085. INT* in IM 0 mode of
operation acts like a non-vectored interrupt. In this mode the Z-80 will respond to the interrupt request
by activating IORQ* and Ml* signals. The simultaneous activation of IORQ* and Ml* is the Z-80 way
of informing the peripherals that it is issuing an interrupt acknowledgement. The Z-80 expects the inter¬
rupting peripheral to respond (as IORQ* = 0) with an opcode byte (as Ml * = 0). The peripheral should
send to the Z-80 an RST instruction or a 3-byte CALL instruction using the data lines of Z-80. The Z-
80 will automatically disable the interrupt system. The user is not required to explicitly use DI instruc¬
tion. The Z-80 will save the address of the next instruction (same as PC contents) above the stack top.
Then the Z-80 branches to the ISS indicated by the peripheral using RST or CALL instruction.
Thus the action taken by Z-80 when interrupted half way through an instruction because of INT*
in IM 0 mode could be summarized as follows. It is assumed that NMI* is not active and the interrupt
system is enabled using El instruction.
Interrupt mode t (IM 7 ): This is actually a 2-byte instruction of Z-80 with the opcode ED 56H. After
the execution of this instruction the INT* input of Z-80 works in interrupt mode 1. INT* in IM 1 mode
of operation is very similar to RST 5.5, RST 6.5, or RST 7.5 which are the vectored interrupts of 8085.
521
The Z-80 knows the location to branch in this case that is understood to be 0038H. Hence there is no
activation of IORQ* and Ml* in IM 1 mode.
The Z-80 will automatically disable the interrupt system. The user is not required to explicitly use
DI instruction. The Z-80 will save the address of the next instruction (same as PC contents) above the
stack top. The Z-80 then branches to the ISS at the specific location 0038H. Hence the action taken
by Z-80 when interrupted half way through an instruction because of INT* in IM 1 mode could be
summarized as follows. It is assumed that NMI* is not active and interrupt system was enabled using
El instruction. H co
o 5*
■ Completes the execution of the instruction under progress.
3
■B
°
■ Disables the interrupt system. I
■ Saves PC value (address of next instruction) on the stack top. 5w
■ Branches to the ISS at 0038H.
In Z-80 kit, there could be an unconditional jump to a system RAM location as part of the
monitor program at location 0038H. For example, the instruction ‘JP 8FF3H’ could be present in the
three locations starting from 0038H. Again at location 8FF3H there could be an unconditional jump
to a user RAM location as part of the user program. For example, the instruction ‘JP 9C00H’ could be
present in the three locations starting from 8FF3H. In such a case, the INT* service routine in IM 1
mode starts from location 9C00H, as indicated in Fig. 27.14.
This mode is functionally the same as IM 0 mode if the peripheral sends RST 38H instruction
or CALL 0038H instruction. The advantage of IM 1 mode is that no external hardware is needed to
supply the service routine address. It is useful in applications that have a single interrupt source. The
disadvantage is that if several interrupt sources are present, there is no direct way in this mode to
identify the source of interrupt.
Interrupt mode 2 (IM 2): This is actually a 2-byte instruction of Z-80 with the opcode ED 5EH. After
the execution of this instruction the INT* input of Z-80 works in interrupt mode 2. INT* in IM 2 mode
of operation has no parallel in 8085. The advantage of IM 2 mode is that as many as 128 interrupting
sources could be present in the system. Each interrupting source sends a unique 8-bit address to the
Z-80 in response to simultaneous activation of IORQ* and Ml*. Based on the 8-bit address received
the Z-80 branches to one of the 128 possible service routines. It is a very powerful mode indeed.
The IM 2 mode resembles both IM 0 and IM 1 modes to some extent. The response of Z-80 for
activation of INT* in IM 2 mode is explained with an example.
522
If INT* is activated when Z-80 is in IM 2 mode, then the Z-80 will complete the execution of the
instruction in progress. The Z-80 will then respond to the interrupt request by activating IORQ* and
Ml* signals. The Z-80 expects the interrupting peripheral to respond with an 8-bit address. Hence, the
peripheral should send an 8-bit address to the Z-80 using the data lines of Z-80. Let us say 09H =
00001001 is received by the Z-80. The Z-80 always treats the LS bit of this address as 0 immaterial of
what is received. Thus, the 8-bit address received is treated as 00001000 = 08H. This is interpreted as
8
a> the LS byte of a 16-bit address pointer. The MS byte of this 16-bit address pointer is understood to be
o provided by I register contents. I register stands for ‘interrupt vector register’ which is loaded using
sa the instruction ‘LD I, A’. If the I register content is 05H, then 0508H is taken as a 16-bit address
E pointer. If I register contains 05H, the Z-80 treats memory locations 0500H to 05FFH to provide an
.2
S interrupt vector table. In this table, which contains 256 memory locations, 128 ISS addresses, each of
in
00 2 bytes, are stored. Assume that in locations 0508H and 0509H the bytes 12H and 34H, respectively
s« are stored as shown in Fig. 27.15. In such a case, the Z-80 branches to the service routine at location
3412H. In this manner, the Z-80 can branch to any one of the 128 interrupt service routines based on
H
the 8-bit address pointer it receives from the peripheral.
1
0500H
0501H
Thus, the action taken by Z-80 when interrupted half way through an instruction because of INT*
in IM 2 mode could be summarized as follows. It is assumed that NMI* is not active and the interrupt
system is enabled using El instruction.
The NMI* is negative edge triggered interrupt input. It has higher priority over INT*. Also, whenever
the NMI* pin is activated, the Z-80 always responds by executing the appropriate ISS. In other words,
523
this interrupt is non maskable. It is a vectored interrupt, very similar to the TRAP interrupt input of
8085. It always branches to the service routine at location 0066H. NMI* interrupt is normally used for
high priority events such as power failure, and real time clock. After return from the NMI service rou¬
tine the interrupt system will be restored to the earlier enabled or disabled status.
The action performed by Z-80 when interrupted half way through an instruction because of NMI*
could be summarized as follows.
part of the monitor program. For example, the instruction ‘JP 8FF0H’ could be present in the three
locations starting from 0066H. Again at location 8FF0H there could be an unconditional jump to a
user RAM location as part of the user program. For example, the instruction ‘JP 8C00H’ could be
present in the three locations starting from 8FF0H. In such a case, the NMI* service routine starts
from location 8C00H.
The format of the NMI* service routine will generally be as follows.
RETN stands for ‘RETum from NMI service routine’. The RETN instruction does the following.
■ Pop from the stack top to PC. This ensures return to main program.
■ Get back to old value of interrupt enable/disable status.
Executing the following instructions could perform the saving of all the registers. It is to be noted that the
registers are being saved on the stack instead of alternate registers. This is because, the NMI* can get activated
when the Z-80 is half way through INT* service routine. In such a case, the Z-80 should return from NMI*
service routine to INT* service routine with the contents of main and alternate set of registers unchanged.
PUSH AF
PUSH BC
PUSH DE
PUSH HL
PUSH IX
PUSH IY
EX AF, AF'
EXX
PUSH AF
PUSH BC
PUSH DE
PUSH HL
524
Executing the following instructions would perform restoring of all the registers by popping in
reverse order.
POP HL
POP DE
POP BC
o
M
M
POP AF
0
o EX AF, AF'
a EXX
2
.2 POP IY
s
in POP IX
oo
a POP HL
o POP DE
POP BC
POP AF
Write an Z-80 assembly language program to add two multi-byte numbers. The size in bytes of the
two multi-byte numbers is provided in location 891OH. The first multi-byte number starts from loca¬
tion 8920H and the second one from location 8940H. The result is to be stored from location 8970H.
For the result, one extra location is provided to account for the carry generated.
A typical input and the expected output are indicated in Fig. 27.16.
i
8910H 03 4E 3D 25H
+ D5 F2 ABH
LD HL, 8910H
LD IX, 8920H
*
IS
AGAIN: LD A, (IX + 0)
ADC A, (IX + 2 OH ) o co
LD (IX + 50H) , A 3
■B
°
INC IX 8
DEC (HL) 5w
JP NZ AGAIN
JP NC EXIT
LD (IX + 50H) , 01
HALT
The reader is urged to compare this program with the one in Sect. 15.2 that used 8085 processor.
A shortage of registers was felt while using the 8085 processor. The usage of indexed addressing in
Z-80 has resulted in a more compact program, and a number of registers of Z-80 are still available
for use. This amply demonstrates the power of indexed addressing mode.
Write an Z-80 assembly language program to exchange the contents of two blocks in memory. The
number of elements in the block is provided in location 891OH. The two blocks start at locations
8920H and 8940H, respectively.
A typical input and the expected output are indicated in Fig. 27.17.
Before After
8910H 04 04
H The reader is urged to compare this program with the one in Sect. 15.1 that used 8085 processor.
The usage of indexed addressing in Z-80 has resulted in a more compact program, once again demon¬
strating the power of indexed addressing mode.
Write an Z-80 assembly language program to perform block movement. The blocks are assumed to be
non overlapping. The block starting at location 8920H is to be moved to that starting at location
8940H. The block size is provided in location 891OH.
A typical input and the expected output are indicated in Fig. 27.18.
Before After
8910H 03 03
Program
LD HL, 8910H
LD IX, 8920H
AGAIN: LD A, (IX + 0)
LD (IX + 20H), A
527
INC IX
DEC (HL)
JP NZ AGAIN
HALT
The reader is urged to compare this program with the one in Sect. 15.4 that used 8085 processor.
Once again, the use of indexed addressing in Z-80 has resulted in a more compact program. o
IS
0 00
so
■o
■ 27.9 INSTRUCTION SET SUMMARY 0
0
0
The Z-80 instruction set summary is provided in Table 27.7. The convention used in this table is as "*
follows.
o r, rl, r2 = A, B, C, D, E, H, or L;
« Iz = IX or IY;
£ rp = BC, DE, HL, or SP;
a Rp = BC, DE, HL, SP, IX, or IY;
£ zp = BC, DE, Iz, SP
2 that is, if Iz = IX then zp = BC, DE, IX, SP and if Iz = IY then zp = BC, DE, IY, SP;
in
« = 0,1, 2, ..., 7;
cond = C/NC/Z/NZ/P/M/PO/PE.
j;
P:
Motorola
M6800
Microprocessor
■ Pin description of 6800
■ Programmer's view of 6800
■ Addressing modes of 6800
• Immediate addressing
• Implied or inherent addressing
• Direct addressing
• Extended addressing
• Indexed addressing
• Relative addressing
■ Instruction set of 6800
• Data transfer group
•Arithmetic group
• Logical group
• Branch group
• Miscellaneous instructions
■ Interrupts of 6800
■ Programming examples
Addition of multi-byte numbers
•
• Exchange of blocks
• Block movement without overlap
m Questions
In the previous chapters Intel 8085 and Zilog Z-80 microprocessors were dealt with in depth. In this
chapter Motorola M6800, another very popular 8-bit microprocessor will be discussed. This will
provide the reader with a comparison of the most popular 8-bit microprocessors.
530
■ 28.1 PIN DESCRIPTION OF 6800
Motorola 6800 is another popular 8-bit microprocessor, with which, the following chips are normally
used to provide the required functions in a microcomputer.
sM
— 6870 Clock generator;
o —6830 ROM or 68708 EPROM;
Sa
—6810 RAM;
2
.2 — 6820 Peripheral interface adapter (similar to 8255 PPI in 8085 system);
s — 6850 Asynchronous communications interface adapter (similar to 8251 USART);
in
— 6828 Priotity interrupt controller (similar to 8259 PIC).
soo
Jj The 6800 is available as a 40-pin DIP and its actual pin diagram is shown in Fig. 28.1. The func¬
H
tional pin diagram is shown in Fig. 28.2.
Gnd 1 40 Reset*
Halt* 2 39 TSC
<Pl 3 38 NC
IRQ* 4 37 <p2
VMA 5 36 DBE
NMI* 6 35 NC
BA 7 34 R/W*
Vcc 8 33 Do
A0 9 32 t > Dj
A, 10 31 —
< > D2
A: 30 —
< > D3
A3 12 29 t D4
A4 13 28 —« >Dg
t > D5
As 14 27 —
<— > D7
A6 15 26
A7 16 25 A15
As 17 24 A14
A9 18 23 A13
A10 19 22 Al2
A] i 20 21 Gnd Fig. 28.1
Pin diagram of 6800
6800
+5 V
(2 pins) Gnd =>AI5_O
Qi DBE
0: BA
Reset* VMA
NMI* R/W*
IRQ* TSC Fig. 28.2
Halt* Functional pin
2-pins NC diagram of 6800
531
Ai5-o: Unidirectional 16-address pins on which address is sent out by 6800. Thus, 6800 is capable
of addressing 216 = 64K memory locations.
D7-<f Bi-directional 8-data pins on which data is sent out or received by 6800. Thus, 6800 is an
8-bit microprocessor.
V, Power supply pins. 6800 uses a dc power supply of + 5 V. There are actually two Gnd pins
Gnd: and both of them have to be connected to Gnd. 3
cpl, cp2: Motorola 6800 receives two-phase clock inputs on these pins. A clock generator chip M6870 o
is used to provide the two-phase clock. The typical frequency of operation is 1 MHz, while H 2.
the minimum is 100 kHz. If operated at a frequency lower than 100 kHz, the clock duration g
would be greater than 10 ps, and some of the dynamic internal registers would lose their data. 73 01
R/W*: Active-low output pin. Motorola 6800 sends out a 1 on this pin when reading of data from mem¬
ory or input port is desired. It sends out a 0 on this pin when a write operation to memory or an 5w
output port is desired and uses memory mapped I/O for addressing of I/O ports. As such, there S
is no pin like IO/M*.
Reset*: Active-low input pin. When brought down to 0 state momentarily, the 6800 is reset.
TSC, TSC is an active-high input pin. TSC stands for tristate control. It is similar in function to hold
BA: input of 8085. BA is an active-high output pin. BA stands for bus available. It is similar in func¬
tion to Hlda output of 8085. These two pins are used for facilitating direct memory access.
NMI*, These are active low interrupt input pins. NMI stands for non-maskable interrupt. IRQ
IRQ*: stands for interrupt request that could be masked or disabled. These interrupts are discussed
in detail later.
Halt*: Active-low input pin. When this pin is activated the 6800 goes to the halt state. In this state
A15_0, D7-O» an<l R/W* go to their high impedance state and the processor halts. This pin
could be used for single stepping through a program.
VMA: Active-high output pin. VMA stands for valid memory address. When this pin is activated
by the 6800, it means that the address on A15.0 is a stable valid address. It is activated a lit¬
tle after the address is sent out on A15_0.
DBE: Active-high input pin. DBE stands for data bus enable. When this pin is activated, the data
pins D7_0 are enabled. In such a case, the data present on D7_0 enters the 6800 if it is a read
operation, or data comes out on D7.0 if it is a write operation. When the pin is in the 0 state,
the data pins D7_0 are tristated. It is generally connected to cp2 clock.
NC: There are two NC pins. NC stands for no connection. Thus, the required functions of 6800
are implemented using only 38 pins.
From a programmer’s point of view the 6800 is a very simple microprocessor. The programmer’s view
is indicated in Fig. 28.3a. The simplified internal architecture of 6800 is provided in Fig. 28.3b.
From a programmer’s viewpoint the 6800 has:
Acc A Acc B
8 bits 8 bits
IX
s
a>
0 ]SP
Sa
2
.2
]PC
S 16 bits
w
oo
s« ]CCR Fig. 28.3a
Programmer’s
8 bits
H view of 6800
8-bit bus
|lncr/Decr|
|Temp IR A B / y \ /
IX SP PC
MUX \
Instruction 16-bit adder
decoder
8-bit ALU Ik 16-bit bus
CCR
Address buffer
Both A and B registers have almost the same prominence in the instruction set. This was not the case
with 8085 where A register was more important than other 8-bit registers. In 6800 there are only a few
operations that can be done with A, but not by B. The following list shows the exceptions.
ABA (add B and A, store result in A) is implemented, but not AAB;
SBA (subtract B from A, store result in A) is implemented, but not SAB;
533
CBA (compare B with A, store result in a temporary register) is implemented, but not CAB;
TAP (transfer from A to CCR) is implemented, but not TBP;
TPA (transfer from CCR to A) is implemented, but not TPB.
Flags register: The flags register in 6800 is termed CCR that stands for condition code register. The
flags register contents are indicated in the following. It contains only six flags in the LS position, of 3
which five are status flags and the remaining one is a control flag. The other two bits in the MS bit a
0
positions of the CCR are always in the 1 state.
n a
1 1 H IM N |Z |V |Cy| li
The status flags are affected based on the result of execution of an instruction. The five status flags
are:
5w
FI flag: It is half carry flag and is the same as the auxiliary carry of 8085.
N flag: It is negative flag and is the same as the sign flag of 8085.
Z flag: It is zero flag.
Cy flag: It is carry flag.
V flag: It is overflow flag and is described in Z-80.
Note that the P flag of 8085 is omitted here, as it is not a useful flag.
The control flag in CCR is the IM flag. IM stands for interrupt mask. If this flag is set to 1, then
IRQ* interrupt will be masked or disabled. It implies that, the 6800 will not be interrupted even if the
IRQ* pin is activated by a peripheral. If reset to 0, the IRQ* interrupt will be unmasked or enabled.
That is, if the IRQ* pin is activated by a peripheral, the 6800 will be interrupted, provided of course,
the higher priority NMI* is not active at the same time.
The IM bit is reset to 0 by the execution of the instruction CLI that stands for CLear Interrupt mask
bit. The CLI instruction is functionally the same as the El instruction of 8085. The IM bit is set to 1
by the execution of the instruction SEI that stands for SEt Interrupt mask bit. The SEI instruction is
functionally the same as the DI instruction of 8085. The CCR register is indicated as P in an instruc¬
tion mnemonic for brevity. For example, the mnemonic TAP stands for transfer from A register to P
(CCR) register.
Immediate addressing;
Implied or inherent addressing;
Direct addressing or page 0 addressing;
Extended addressing or absolute addressing;
Indexed addressing;
Relative addressing.
The following conventions are used in the assembly language programming of 6800.
A value 3FH is indicated as $3F. Thus, $ stands for an hexadecimal number. With $ not indicated,
the number is taken to be a decimal number.
534
Immediate data in an instruction follows ‘#’ symbol. A value that is not preceded by ‘#’ symbol
is taken to be a memory address.
If there are two operands for an instruction, the source operand appears first, and the destination
operand appears later in the instruction. For example, the mnemonic SBA stands for ‘subtract B
from A, and store the result in A’. This is exactly the opposite of the situation in 8085 or Z-80.
s The 16-bit data or memory address in a 3-byte instruction is stored in memory with MS byte at a
a> lower memory address, and LS byte at a higher address. It is not stored in byte reversal form that
o
sa was prevalent in 8085 and Z-80. For example, the instruction STA A $1234 is stored in memory
2 as B7 12 34, where B7 is the opcode for STA A.
.2
s
in
oo 28.3.1 IMMEDIATE ADDRESSING
s
J: In this addressing, the data to be used in the execution of the instruction is provided in the instruction
itself, immediately after the opcode of the instruction. Some examples follow.
If the operands used in the instruction are within the 6800, the addressing mode is called implied or
inherent addressing mode in 6800 terminology. Thus in this addressing mode, the operands will not
be in memory. Some examples follow.
In this mode the operand will be in memory. This mode is sometimes called page 0 addressing because
the operand in memory is restricted to be in the address range of 0000H to 00FFH that happens to be
page 0 of memory. Memory in 6800 system can be thought of as made up of pages of size 100H = 256
bytes. Some examples follow.
Direct addressing is a very powerful addressing mode provided by 6800. This was not present in
8085 or Z-80. Instructions that use direct addressing will be only 2-byte long compared to the 3-byte
length of instructions that use extended addressing. As such, direct addressing instruction occupies
less space in memory. And more importantly, it takes less time to fetch and execute an instruction that
uses direct addressing.
535
In a 6800 system it is advantageous to have the frequently accessed data stored in the memory
range 0000H-00FFH. The data can hence be accessed very fast using direct addressing. To facilitate
this, RAM generally starts from location 0000H and grows towards higher addresses in the memory
space of 6800. ROM or EPROM is provided the last part of the memory space, and ends with the
address FFFFH. This is shown in Fig. 28.4.
3
8 bits a
0
0000H
Range O U
000 1H
for
direct li
addressing
VRAM
OOFFH 5w
ROM
or Fig. 28.4
FFFEH EPROM
Memory space
FFFFH in 6800
It is the same as the absolute addressing of 8085. However, it is much more powerful here. In this
mode the operand will be in memory. The address of the operand is provided after the opcode in the
instruction using 16 bits. Because of the 16-bit address, the operand could be anywhere in the mem¬
ory space in the range 0000H to FFFFH. Some examples follow.
SBC B $2345; Subtract from B with carry the contents of location 2345H. Store result in B.
CLR $ABCD; Clear the contents of memory location ABCDH.
TST $AABB; Test contents of location AABBH for 00H value.
NEG $6677; Negate the contents of location 6677H. Contents changed to FEH (= -02) if the
original contents of 6677H was 02H.
COM $7788; Complement (l’s complement) the contents of location 7788H. Contents
changed to FDH if the original contents of 7788H was 02H.
It may be noted that none of these instructions were present in the absolute addressing mode of 8085.
This demonstrates the power of extended addressing in 6800.
If the user writes in assembly language an instruction like COM $0056, the 6800 assembler automat¬
ically generates a 2-byte instruction code treating the instruction as COM $56 in direct addressing mode.
As such, the practical addressing range for extended addressing could be indicated as 0100H to FFFFH.
In this mode the operand will be in memory. A part of the address is provided in the IX register and
the other part of size 8 bits is provided directly in the instruction. The sum of these two parts provides
536
the complete memory address for the operand. This mode is already described in Z-80. However, in
6800 there is a single IX register compared to the two present in Z-80. Some examples follow. In these
instructions, the IX content is assumed to be 1234H.
ADC B $10, X; Add with carry to B register the contents of memory location 1244H.
8 STS $30, X; Store SP contents in memory locations 1264H and 1265H.
a> BIT B $25, X; AND contents of B register with contents of memory location 1259H. Contents
o
sa of B remain unchanged. Only flags are affected.
CPX $20, X; Compare IX contents with contents of memory locations 1254H and 1255H.
2
.2
s
in
oo
s •28.3.6 RELATIVE ADDRESSING
Jj
H The relative addressing mode is used in the branch group of instructions. The branch group of instruc¬
tions that use relative addressing will be only 2-byte long and will be executed faster compared to those
that use extended addressing. In relative addressing, a signed 8-bit number will follow the opcode byte.
This number indicates whether the branch is to a forward location or to a backward location. The range
for branching is 127 locations forward or 128 locations backward from the next instruction. This mode
is already described in Z-80. Some examples follow.
BRA$F0; Branch unconditionally 10H locations backward (F0H = -10H) from the next
instruction after BRA $F0.
BEQ $F0; Branch if equal to zero (Z flag = 1) 10H locations backward (F0H = -10H) from
the next instruction after BEQ $F0.
BVS $20; Branch if V flag is set to 20H locations forward from the next instruction after
BVS $20.
BSR $20; Branch to subroutine that is 20H locations forward from the next instruction after
BSR $20.
Some statistics about the instruction set of 6800 are shown in the following.
The following convention is used in the description of the instruction set of 6800.
d8 = 8-bit data;
a8 = 8-bit address;
al6 = 16-bit address;
IX+a8 = Indexed addressing using IX and 8-bit displacement;
r8 = 8-bit relative (signed) displacement.
There are 23 opcodes in this group under 21 instruction types. The conditional branch instructions of
even Z-80 performed branch based on the value of a single flag, except V flag. For example, ‘JR NC,
r8’ of Z-80 performed a branch only if Cy flag were 0. In 6800, conditional branch instructions that
test V flag are also provided. They are very useful when working with signed numbers and are shown
as follows:
In addition, there are conditional branch instructions that test more than one flag also in 6800. Such
instructions are useful in the comparison of two unsigned or two signed numbers.
In 6800 terminology, ‘higher’ and ‘lower’ are used when comparing unsigned numbers. ‘Greater
than’ and ‘less than’ are used when comparing signed numbers. Consider the numbers 85H and 45H
as an example. 85H is a negative number (as the MS bit is 1) and 45H is a positive number (as the MS
bit is 0) when interpreted as signed numbers. Thus, all the following statements are perfectly true in
6800 terminology.
BHI (branch if higher) and BLS (branch if lower or same) are used with unsigned numbers. BHI
and BLS are generally used after a compare operation like ‘CMP B, #$25’. These instructions perform
3
a branch based on the value of Cy flag and Z flag.
BGT (branch if greater than), BGE (branch if greater or equal), BLT (branch if less than) and BLE
a
0
(branch if less than or equal) are used with signed numbers. They are generally used after a compare n a
operation like ‘CMP B, #$25’. These instructions perform a branch based on the value of sign flag, V 33
flag, and Z flag. ~
°*
The other instructions under the branch group are:
5w
BRA— unconditional relative branch
JMP— unconditional branch using extended or indexed addressing
BSR— unconditional relative branch to subroutine
JSR— unconditional branch to subroutine using extended or indexed addressing
RTS— return from subroutine
RTI (return from ISS) and SWI (software interrupt) will be described later.
Note that there are no conditional branches to a subroutine and conditional returns from a subroutine.
They are not implemented, as it is possible to manage without such instructions. If it is required to
branch to subroutine at symbolic memory location SUBR only if Cy flag is 0, it is managed as follows.
BCS NEXT
BSR SUBR
NEXT: next instruction
BVS SKIP
RTS
SKIP: next instruction
There are eight opcodes in this group under eight instruction types.
540
Mnemonic No. of opcodes No. of instruction types
WAI 1 1
NOP 1 1
CLC/SEC/CLI/SEI/CLV/SEV 6 6
8M WAI (wait for interrupt) is functionally the same as the HLT instruction of 8085. There are instructions
o to clear or set the Cy, IM, and V flags. CLC stands for clear carry, SEC stands for set carry and so on.
S
o 6800 instruction set summary
.2
S
w Mnemonic No. of opcodes
oo
s« BIT/CMP/EOR/ORA/AND/SBC/SUB/ADC/ADD/LDAA/B, d8/a8/al6/IX+a8
CPX/LDS/LDX d 16/a8/a 16/IX + a8
10 * 2 * 4 = 80
3 *4 = 12
H STAA/B a8/al6/IX+a8 2*3 = 6
STS/STX a8/al6/IX+a8 2*3 = 6
ROL/ROR/LSR/ASR/ASL/COM/TST/NEG/CLR/INC/DEC A/B/al6/Ix+a8 11 *4 = 44
PSH/PUL A/B 2*2 = 4
TAB/TBA/TSX/TXS/TAP/TPA 6
INS/INX/DES/DEX 4
CBA/ABA/SBA 3
CLC/SEC/CLI/SEI/CLV/SEV 6
BCC/BCS/BEQ/BNE/BMI/BPL/BVC/BVS r8 8
BHI/BLS/BGT/BGE/BLT/BLE r8 6
BRA/BSR r8 2
JMP/JSR al6/IX+a8 2*2 = 4
RTS/RTI 2
SWI/WAI/NOP/DAA 4
Note-. 'ROL/ROR/LSR/ASR/ASL/COM/TST/NEG/CLR/INC/DEC a8’ and CAB/AAB/SAB/TBP/TPB
instructions are not implemented.
Speed of instruction execution: The typical clock frequency used in 6800 is 1 MHz. The one used
in 8085 is 3 MHz. This gives a wrong impression that 8085 is three times faster than 6800. A simple
example is provided to prove it to be a wrong impression.
Consider the execution of the 6800 instruction ‘LDA A, #$F3\ Its equivalent in 8085 is ‘MVI A,
F3H’. In 8085 this instruction takes a total of seven clock cycles for the opcode fetch, decode and exe¬
cute portion. It works out to 2.33 JJLS if the 8085 is operating at 3 MHz. In 6800 this instruction takes
a total of two clock cycles only— one for fetching and decoding, and the other for execution. It works
out to 2 |xs if the 6800 is operating at 1MHz. Thus 6800 working at 1 MHz is slightly faster than 8085
processor working at 3 MHz.
Processor Instruction Clock speed (MHz) No. of T states Instruction time (p,s)
6800 LDA A, #$F3 1 2 2
8085 MVI A, F3H 2 7 2.33
There are two hardware interrupt pins in 6800 which are NMI* and IRQ*. These are active low inter¬
rupt input pins of which NMI* is non maskable. Thus the 6800 gets interrupted whenever the NMI*
pin is activated. IRQ* is maskable. It is masked or disabled if the IM flag of CCR register is set to 1
541
using the SEI instruction. It is also automatically masked whenever the 6800 enters an ISS. In such a
case, the 6800 is not interrupted even if the IRQ* pin is activated.
IRQ* is unmasked or enabled if the IM flag of CCR register is reset to 0 using the CLI instruction.
In such a case, whenever the IRQ* pin is activated, the 6800 is interrupted, provided NMI* is not
active at the same time. This is because, NMI* has a higher priority over IRQ*. When NMI* pin is
activated half way through an instruction, the action performed by 6800 is as follows: 3
a
The instruction under execution is completed.
PC value saved on the stack.
■I
n a
All the other registers saved on the stack in a particular order as shown in figure 28.5. IS
IM flag is set to 1.
Jumps to the ISS whose address is provided in locations FFFCH and FFFDH. 5w
If the contents of locations FFFCH and FFFDH are 56H and 78H as shown in Fig. 28.5, then the
NMI* ISS starts from 5678H and ends with RTI instruction. The execution of this instruction results
in the following.
8 bits Stack
FFF8H 9 AH = MSB Interrupt CCR
request
FFF9H BCH = LSB vector B
FFFAH 22H = MSB Software A
interrupt
FFFBII 33H = LSB vector IX High
FFFCH 56H = MSB NMI IX Low
Fig. 28.5
vector PC High
FFFDII 78H = LSB Interrupt vectors
FFFEH E0H = MSB Reset PC Low of 6800 and saving
interrupt of registers on the
FFFFH 00H = LSB vector
stack
As there is no explicit need for saving and restoring registers in the ISS, the interrupt response is much
faster than in 8085. In the ISS of 8085, the user had to explicitly save and restore the registers in the
ISS, thereby slowing down the interrupt response.
When IRQ* pin is activated half way through an instruction, the action performed by 6800 is very
much similar to the activation of NMI*. In this case the 6800 jumps to the ISS whose address is provided
in locations FFF8H and FFF9H. If the contents of locations FFF8H and FFF9H are 9AH and BCH as
shown in Fig. 28.5, then the IRQ* ISS starts from 9ABCH and the ISS ends with RTI instruction.
In addition to the two hardware interrupts, there is also an SWI (software interrupt) in 6800. This
instruction has the mnemonic SWI. When this instruction is executed by the 6800 in a program, the
action taken is as follows.
Write a 6800 assembly language program to add two multi-byte numbers. The size in bytes of the two
multi-byte numbers is provided in location 891OH. The first multi-byte number starts from location
8920H with the LS byte and the second one from location 8940H with the LS byte. The result is to be
stored from location 8970H starting with the LS byte. For the result, one extra location is provided to
account for the carry generated.
A typical input and the expected output are indicated in Fig. 28.6.
l cy
8910H 03 4E 3D 25H
+ D5 F2 ABH
24 2F D0H
8920FI 25 LS byte -N
8921 FI 3D
8922FT 4E MS byte
> Input
8940FI AB LS byte
8941 FT F2
8942FI D5 MS byte J
8970FI DO LS byte
8971 FI 2F
Output
8978FI 24
Fig. 28.6
8973H 01 MS byte
Addition of two
multi-byte numbers
543
Program
CLC
LDX #$8920
I NX
■I
n a
DEC $8910 is
BNE AGAIN
5w
CLR $50, X
BCC EXIT
INC $50, X
EXIT: WAI
The reader is urged to compare this program with the one in Section 14.2 that used 8085 proces¬
sor. A shortage of registers was felt while using 8085 processor. The number of registers in 6800 is
much less than in 8085. In spite of this, in the above program, register B of 6800 is still available for
use. This is because, the usage of indexed addressing in 6800 has resulted in a more compact program,
which amply demonstrates the power of indexed addressing mode.
Write a 6800 assembly language program to exchange contents of two blocks in memory. The num¬
ber of elements in the block is provided in location 891OH. The two blocks start at locations 8920H
and 8940H, respectively.
Typical memory contents before and after program execution are indicated in Fig. 28.7.
Before After
891OH 04 04
8M STA A $20, X
STA B 0, X
0
S
a INX
s
.2
DEC $8910
BNE REPEAT
s
in WAI
oo
s« The reader is urged to compare this program with the one in Sect. 14.1 that used 8085 processor. The
H usage of indexed addressing in 6800 has resulted in a more compact program, which once again
demonstrates the power of indexed addressing mode.
Write a 6800 assembly language program to perform block movement. The blocks are assumed to be
non overlapping. The block starting at location 8920H is to be moved to the block starting at location
8940H. The block size is provided in location 891OH.
Typical memory contents before and after program execution are indicated in Fig. 28.8.
Before After
8910IT 03 03
Program
LDX #$8920
AGAIN: LDA A 0, X
STA A $20, X
INX
DEC $8910
BNE AGAIN
WAI
The reader is urged to compare this program with the one in Sect. 14.4 that used 8085 processor. Once
again, the use of indexed addressing in 6800 has resulted in a more compact program.
545
The main features of Intel 8051 are listed as follows and details about these features are provided in
the rest of this chapter and the next.
The 8051 is available in various versions as 8051H, 8051BH, and 80C51. In this book they are
referred to in general as 8051. The 8051, 8031, and 8751 are available in 40-pin DIP package as well
8 as 44-pin LCC (leadless chip carrier package with pins on all four sides of the square chip). In the
a> LCC package four pins would be unused pins. The functional pin diagram of 8051 is provided in
o
sa Fig. 29.1, and the actual pin diagram in Fig. 29.2.
E The 8051 works on a power supply of +5-V dc. It has an on-chip clock circuit. All that is needed
.2 to complete the clock circuit is to connect a quartz crystal between the pins XTAL1 and XTAL2, and
S two capacitors as shown in Fig. 29.1. The maximum crystal frequency is typically 12 MFIz. There are
in
00 versions of 8051 that allow maximum crystal frequency of 20 MFIz. There is a flip-flop inside 8051
s« that divides the clock frequency by 2, to provide the internal clock. Thus with a 12-MFIz crystal, the
H internal clock frequency is 6 MFIz. An internal clock period constitutes one state. A machine cycle of
8051 constitutes six such states, SI to S6. Thus if the crystal frequency is 12 MFIz, the duration of a
machine cycle is 1 |JLS. Most of the instructions in the instruction set of 8051 are executed in just one
machine cycle.
P1.0 1 40 Vcc(+5 V)
Pl.l > 2 39 PO.O(ADQ)
PI. 2 3 38 —
< > PO.l(AD))
PI.3 > 4 37 > P0.2(AD2)
PI.4 5 36 P0.3(AD3)
PI.5 > 6 35 —
< > P0.4(AD4)
PI.6 4— > 7 34 > P0.5(AD5)
PI.7 4— > 8 33 <— >• P0.6(AD6)
Reset 9 32 > P0.7(AD7)
P3.0(RXD) > 10 31 EA*
P3.1(TXD) 4— > II 30 ALE
P3.2(INT0*) 12 29 PSEN*
P3.3(INT1*) 13 28 ►
P2.7(AI5)
P3.5(Counterl)
—
P3.4(CounterO) < > 14
15
27
26
<— >
P2.6(AI4)
<->
P2.5(AI3)
P3.6(WR*) 4-ÿ 16 25 »
P2.4(AI2)
P3.7(RD*) 17 24 «— ►
P2.3(A„)
XTAL2 18 23 P2.2(AI0)
XTAL1 19 22 > P2.1(A9) Fig. 29.2
Vss(Gnd) 20 21 *-» P2.0(A8)
Pin diagram of 8051
549
It is also possible to drive the 8051 from an external clock source. In such a case, the clock output of
the external source is connected to XTAL2 input of 8051 and XTAL1 to ground. Reset is an active high
input. Logic 1 on this input for atleast two machine cycles, when the clock is running, resets the 8051.
The 8051 has four 8-bit I/O ports. The ports are called Port 0, Port 1, Port 2, and Port 3. These
four ports account for 32 pins of 8051. When the internal data and/or program memory is not adequate
for an application, external data and/or program memory will have to be used. In such a case, Port 2 g
(n
(P2) pins provide the MS byte of address A15.0 to external memory, and P0 pins provide the LS byte
of address. P0 pins are also used for data/code transfer between 8051 and external data/program 2
0
memory. Thus P0 pins are used as multiplexed address data pins AD7_0. The ALE output from 8051 3
indicates whether address or data is present on AD7.0 pins. ALE stands for ‘address latch enable’. o
If logic 1 is sent out on ALE, it implies that 8051 is sending out LS byte of address on P0 pins. z
An external latch is used to latch this LS byte of address. l
Two pins of P3 will be used for sending out read (RD*) and write (WR*) signals to external data 5
memory. A signal with a ‘*’ after the signal name indicates that it is an active low signal. Thus RD*
and WR* are active low signals. Also, if it is desired to use the internally provided timers, UART, and
interrupt features, then the remaining pins of P3 will be used up for this purpose. In such a case, CO
and Cl pins are used as two counter inputs (Intel refers to them as TO and Tl), RXD and TXD pins
are used by the UART for receiving and sending serial data, INTO* and INTI* are used as two exter¬
nal interrupt pins. Thus, only PI is available for I/O operations when external memory is being used.
It is possible to use individually every port pin as input or output. A bit value written to a port bit
comes out on the port pin. But if a port pin is to be used as an input pin, the corresponding port bit must
be written with a 1 first. Only then must the port pin be read. The reason for this will become clear in
the next chapter, where a detailed description of port structures is provided. In fact, after reset of 8051
all the port bits are written with Is. Figure 29.3 provides the simplified block diagram of 8051.
Counted i/p
INTI* INTO* (AIF) CounteiO i/p
(AIF) (AIF) (AIF)
Vcc
1-4
Interrupt Timed
control TimetO
Gnd
~Z\
Reset
4KX8 128X8
CPU
ROM RAM
\
\ /
/ \ \
6 MHz \ / XTITIRI
Serial
Osc. ckt. Bus control 4 i/o ports
port
XTALl
12MHz
XTAL2
—
H
PSEN WR P3/AF RXD
I I EA| |(AOF) JL (AIF)
ALE RD PO/AD7_O PI TXD
(AOF) W (AOF)
P2/A15.S
Fig. 29.3
AF
AIF
Alternate function
Alternate input function
TI
RI
—— »» Transmitter interrupt
Receiver interrupt
Simplified block
AOF Alternate output function diagram of 8051
550
■ 29.3 PROGRAM MEMORY STRUCTURE
The logic level on EA* pin indicates whether the 8051 makes use of the internal program memory or
not. EA* stands for ‘external access’. If this pin is connected to logic 0, the internal on-chip ROM of
8 4K byte capacity will not be used. The 8051 in this case accesses only external ROM in the range
a> 0000H-FFFFH. It is to be noted that in 8031, which is ROM less version of 8051, the EA* pin must
o
sa be connected to ground.
RD* and WR* signals output by 8051 on P3 pins are used for accessing the external data memory
E
.2 only. So in order to facilitate reading from the external ROM, 8051 provides PSEN* output. It is an
S active low-output pin, which stands for ‘program strobe enable’. When PSEN* = 0, the 8051 reads
in
00 from the external ROM. The 16-bit address of the external ROM location is output on P0 (LS byte)
s« and P2 (MS byte). The program code is received on P0 pins.
If EA* is tied to logic 1, the internal on-chip ROM is used for address range 0000FI-0FFFFI. Exter¬
H
nal ROM is accessed for addresses in the range 1000FI-FFFFFI. Thus it can be concluded that the total
amount of program memory can be a maximum of 64K bytes including the internal program memory.
The external program memory is accessed in the following cases.
Figures 29.4 and 29.5 provide program memory structure and interfacing of external program
memory, respectively.
\
P07_0/AD7_o D7-0
\
Address \
7
A7-0
ALE latch
A15-8
P27_o/Ai5_8 Fig. 29.5
/
PSEN* OE* Interfacing of external
8051 EPROM program memory
551
The external program memory address is always sent out as a 16-bit quantity, even if the amount
of external memory is much less than 64K bytes. Thus whenever external program memory is used,
PO and P2 cannot be used for I/O purposes. The interesting aspect in the design of 8051 is that the
instruction execution times do not depend on whether the instruction is in on-chip ROM or external
EPROM.
g
(n
2
o
■ 29.4 DATA MEMORY STRUCTURE 3
o
The 8051 has 128 bytes of internal on-chip RAM in the address range 00H-7FH for data storage. If l
»
more data memory is needed, it is necessary to use external data memory. The data memory structure
of 8051 is shown in Fig. 29.6.
7FH
64 K bytes
maximum
Fig. 29.6
FFFFH Data memory structure
As can be seen from Fig. 29.6, it is possible to have an external data memory location with address
as 50H, when there is already an internal RAM location with the same address 50H. Thus it can be
concluded that the total amount of data memory can be a maximum of 64 K bytes excluding the inter¬
nal data memory.
RD* and WR* signals output by 805 1 on P3 pins are used for accessing the external data memory.
External data memory address can be either 1- or 2-bytes wide. One-byte addresses are commonly
used, but then only 256 bytes of external memory can be addressed. For most applications this should
be adequate. In fact for many applications the internal program memory of 4 K bytes and internal data
memory of 128 bytes should suffice. If only a little extra data memory is required, PO pins can be used
to provide an 8-bit address. Then upto 256 bytes of external data memory can be accessed. In such a
case P2 is free for use as an I/O port, provided external program memory is not present.
If a little more than 256 bytes of external data memory are required, then a few lines of P2 can be
used to page the external RAM. A scheme for providing 2K bytes of external data memory using eight
pages of 256 bytes each is presented in Fig. 29.7.
In this scheme only three lines of P2 are used to select a page of 256 bytes. The remaining five lines
of P2 can be used for I/O purposes. It is assumed that there is no external program memory. This is
because if there is external program memory, 16 bits of address would have to be sent out, always on PO
and P2. In an instruction like ‘MOVX A, @R1’ (to be discussed later) only 8-bit address is sent out by
805 1 on P0 pins, and it is the responsibility of the programmer to send out the three page bits using P2.
However, if a substantial amount of external data memory is required, then a full 16-bit address
has to be sent out on P0 and P2. In such a case, the programmer is allowed to use instructions like
552
8051 RAM2KX8
I EA* PO7.0/AD7.0 D7-0
/ Address A7-0
ALE latch
8
M P22/A10 A10
u P2 [/A9 A9
Sa P20/A8 As
E P36/WR* WR*
.2 P37/RD* OE
S
w Fig. 29.7 Implementing 2K bytes of external RAM
00
s«
‘MOVX A, @DPTR’ (the instruction will be explained later). External program memory and external
H
data memory may be combined if desired by connecting RD* and PSEN* outputs of 8051 as inputs
to an AND gate, and using the output of AND gate as the read strobe for the external program/data
memory. This scheme is shown in Fig. 29.8. In such a case it is the responsibility of the programmer
to ensure that program and data occupy non-overlapping memory locations.
P37/RD*
RD*
PSEN *
P36/WR* WR*
Fig. 29.8
8751 RAM
used as external Circuit for common external
program and data memory program and data memory
Internal data memory is addressed using 8 bits. Thus 28 = 256 memory locations are possible, but
8051 contains only 128 bytes of internal data memory. They occupy the addresses 00H to 7FH. The
address range 80H-FFH is allocated to accommodate a number of registers like A (accumulator), B
register, DPTR (data pointer) and so on. These registers are called special function registers or SFRs.
These SFRs are implemented in the internal RAM address space 80H to FFH, called the SFR space.
Only 2 1 locations in the SFR space are used in 805 1 for implementing the SFRs. The other locations
in the SFR space do not exist physically. As such, the results will not be predictable if such addresses
are used in an instruction. The two parts of the internal RAM are illustrated in Fig. 29.9.
The internal data memory of 128 bytes is divided into two groups— a set of eight registers and scratch
pad memory. The eight registers are R0 to R7, and are implemented in the address range 00H-07H.
The rest is scratch pad memory. In fact, 8051 provides the facility of four register banks, but only one
553
OOH
Internal
data
memory
7FH
SFR = Special function register
80H
SFR Fig. 29.9
g
w
space Data memory and SFR
FFH space in internal RAM o
3
o
bank of registers can be in use at any point of time. Two bits in PSW (program status word), which is
an SFR, selects the register bank for use. The PSW will be described later. The register banks have the l
following addresses. »
OOH to 07H Register bank 0;
08H to OFH Register bank 1;
10H to 17H Register bank 2;
18H to 1FH Register bank 3.
The availability of four register banks is a very useful feature for faster servicing of interrupts. The
interrupted program may use one register bank, whereas the ISS may use another register bank. Hence
there is no need to save and restore registers in the ISS, thus speeding up interrupt service. Even if
there are nested interrupts this scheme can be used because there are four register banks.
If the user desires to use all the four register banks then only locations 20H to 7FH are available for
scratch pad memory. Out of this range, memory locations 20H to 2FH can be used as bit-addressable
RAM also in addition to being addressed as the usual byte locations. There are 16 bytes in this range
for a total of 16 X 8 = 128 bits. These bits have the addresses in the range OOH to 7FH. The details are
as provided in Fig. 29.10. These bits are addressed using 8 bits in an instruction like ‘CLR 78H’, which
clears bit with address 78H (LS bit of location 2FH). Actually by using 8 bits it is possible to address
256-bit locations, but there are only 128 bits that are bit addressable in the internal data memory.
Another 11 memory locations in the SFR area are also bit addressable. This constitutes another 88 bits
that are bit addressable. They have bit addresses in the range 80H-FFH. These bit-addressable SFRs
are described later. The remaining locations of the RAM in the range 30H-7FH can be used for storing
variable data and implementing stack.
805 1 stack: The stack in an 805 1-based system is always implemented in the internal data memory.
As the internal data memory range is only OOH to 7FH, the SP register is only 8 bits wide, and is one
of the SFRs of 805 1 with address 81 H. If the user desires to use all the four register banks, and uses
locations 20H to 2FH as bit-addressable memory, then the stack can start from location 30H. In such
a case the SP is initialized with 2FH as is shown in Fig. 29.11.
Note that when SP contains 2FH, it means locations 2FH, 2EH, and so on upto OOH are having use¬
ful information and locations 30H, 31H, and so on upto 7FH are having useless information. Thus SP
points to the highest addressed memory location having useful data. The SP is incremented by one
before a push operation and is decremented by 1 after a pop operation. This is exactly the opposite of
what happens to SP in 8085 microprocessor. The stack grows towards higher addresses, and variable
data should be made to grow towards lower addresses. It is the programmer’s responsibility to ensure
that the stack and variable data do not overlap and destroy each other. As the stack is required to be
implemented only in the internal data memory, only a small amount of stack is possible. This is one
of the limitations of 805 1.
554
Internal data memory Register bank area
organization 00H
OOH Register bank 0
Register bank area 07H
1 FI I
0811
2011 Bit-addressable area Register bank 1
0 2F1I OF! I
W 3011 1011
0 Register bank 2
Rest of RAM area 17H
0
a 18H
Register bank 3
s 7FH IFH
.2
2
w
oo Bit-addressable area
§ Byte address
0
A register bank
RO
I
20H 07 06 05 04 03 02 01 00
R1 21H OF OE OD OC OB OA 09 08 Bit
R2 address
R3
R4
R5
Fig. 29.10
R6
Organization of internal
R7 2FH 7F 7E 7D 7C 7B 7A 79 78
data memory
OOH
After reset of 8051, the SP is initialized to 07H. Thus locations 08H to 7FH can be used as stack
area. In other words, it is assumed that only register bank 0 will be used and locations 20H to 2FH are
not going to be used as bit-addressable area. However, if it is intended to use locations 20H to 2FH as
bit-addressable area, then SP is initialized with 2FH.
The internal RAM of 8051 has two portions, as indicated in Fig. 29.9. Locations OOH to 7FH are used
for register banks, bit-addressable memory locations, stack and variable data. This has been described
in the previous section. Locations 80H to FFH is reserved for SFR area. Only 2 1 locations in the SFR
555
space are used in 8051 for implementing the SFRs. It is illegal to access other locations in the
SFR space. Out of the 21 locations, 11 locations are bit-addressable SFR locations as are shown in
Table 29.1. It may be noted that an SFR whose address has a OFI or 8H in the LS hex digit is a bit-
addressable SFR.
Only PSW register is described below. The other SFRs are explained later.
PSW register: The PSW register is nothing but the conventional flags register of a typical
microprocessor. It is a bit-addressable SFR whose byte address is DOFI. The bits in the PSW have the
addresses DOH to D7H of which D7H is the address of the MS bit of PSW. Only 7 bits in the PSW
are meaningful bits. The details of the PSW register are shown in Fig. 29.12.
D7H D6H D5H D4H D3H D2H D1H DOH bit address
cy AC F0 RSI RSO Ov X p
Y Overflow
00 => Register bank 0
01 => Register bank 1 P (Parity)
10 => Register bank 2 1 => Odd parity
11 => Register bank 3 0 => Even parity
The Cy flag has the usual meaning as in the case of 8085 processor. It is affected by the execution
of add, subtract, and rotate through carry instructions. The Cy flag is also used as a 1-bit accumulator
in bit manipulation instructions. Intel refers to bit manipulation as Boolean operations. Bit manipula¬
tion instructions are described later. The AC flag has the usual meaning as in the case of 8085 proces¬
sor. The programmer does not use it, but the 8051 uses the value of this flag during the execution of
DAA (decimal adjust accumulator). The V flag is useful in signed number arithmetic. If we are work¬
ing with unsigned numbers there is no significance for this flag.
The P flag is set to 1 or cleared to 0 by the 805 1 at the end of each instruction cycle depending on
the number of Is in the accumulator. The instruction executed need not be an arithmetic instruction.
556
If there are even number of Is the P flag is cleared to 0. For odd number of Is the P flag is set to 1.
Note that this is exactly the opposite of the meaning of P flag in 8085 processor.
F0 is a general purpose flag bit, which is uncommitted and may be used as a general-purpose
status flag. In fact, even bit D1H in the PSW can be used as a general-purpose flag. RSI and RSO bits
in the PSW are used to select the current register bank as shown below.
s RSI RSO Selected register bank
a>
o
sa o 0 RBO (register bank 0)
E o l RBI
.2 l 0 RB2
s 1 1 RB3
U)
00
s« Note that Z flag and sign flags are not implemented in 8051. The F0 bit, for example, can be used as
sign flag. The user will have to set this bit to 1 when the MS bit of result is 1. The Z flag is not much
H
needed because the 8051 checks for a zero or non-zero condition by testing the contents of the accu¬
mulator directly.
Of the 21 SFR locations, ten contain byte information, which are not bit addressable, as that would
not make any sense for these SFRs. These non-bit-addressable SFRs are shown as follows. These
SFRs (excluding SP, which is already discussed) will be discussed later.
After reset of 805 1, the SFRs will be initialized to the following values.
■ SP contains 07H.
■ Ports 0 to 3 will contain FFH.
■ SBUF will have indeterminate (garbage) value.
■ All other SFRs will have 00H.
R5 8 bits
R6 o
R7
DPTR[ 3o
8 bits 16 bits
8 bits
■8s 0
Program memory p
4K X 8 bits ] PI
] P2
7FH OFFFH ] P3
The length of an 8051 instruction is 1, 2, or 3 bytes. However, except for a few instructions, the
others are just 1- or 2-byte instructions. The opcode for an instruction is always 8 bits long. Hence
28 = 256 opcodes are possible, of which 255 opcodes have been implemented. They can be classified
into 111 types of instructions. If the crystal frequency is 12 MHz, then 64 instruction types are
executed in just 1 jxs, and the rest in just 2 jxs (except for multiply and divide instructions that need
4p,s). Intel 8051 provides the following addressing modes for accessing data from internal data
memory or external data memory.
■ Immediate addressing;
■ Register addressing;
■ Direct addressing;
558
■ Register indirect addressing;
■ Indexed addressing;
■ Implied addressing.
s
M
29.6.1 IMMEDIATE ADDRESSING
0
S In this type of addressing the source operand value is provided in the byte or bytes following the
a opcode, examples of which are shown in the following.
E
.2 MOV A, #64H; Moves 64H to A. 2-byte instruction.
S
in MOV R6, #0E3H; Moves E3H to R6. 2-byte instruction.
00
s« MOV DPTR, # 1234 H; Moves 1234H to DPTR. 3-byte instruction.
The ‘#’ symbol stands for immediate data. DPIR stands for Data Pointer. It is a 16-bit SFR. The DPTR
H
points to an external data memory location. If a hexadecimal value starts with A to F, it should be pre¬
ceded by 0, as in 0E3. The code for the 3-byte instruction ‘MOV DPTR,#1234H’ is stored in memory
as ‘90 12 34’, where 90H is the opcode for ‘MOV DPTR’. It is to be noted that 12 34H is not stored
in byte reversal form.
In this type of addressing the source or destination operand is the contents of Rn (n = 0 to 7). Rn is
specified in the opcode using 3 bits, examples of which are shown in the following.
MOV A, R3; Moves contents of R3 to A. 1 byte instruction.
MOV R6, #30H; Moves 30H to R6. 2 byte instruction.
MOV R5, A; Moves contents of A to R5. 1 byte instruction.
Note that there is no instruction like ‘MOV R5,R3’. The same effect is achieved using the instruction
‘MOV R5,03H’ or ‘MOV 05H,R3’. This is because, in order to provide instructions like ‘MOV R5,R3’
an additional 64 opcodes are required, and already 255 out of the possible 256 opcodes are implemented.
It should be noted that ‘MOV R5,03’ works fine in place of ‘MOV R5,R3’ only if the selected register
bank is 0. If we are using register bank 1, ‘MOV R5,0BH’ has to be used for the same effect.
In this type of addressing, the source or destination operand address is specified using 8 bits in the
instruction. Only internal data memory and SFRs can be directly addressed. In fact, SFRs are accessed
using only direct addressing mode. External memory cannot be accessed using direct addressing mode
in 8051. The following are some examples.
MOV 80H, R3 ; Moves contents of R3 to Port 0. P0 address is 80H.
MOV R6, 30H ; Moves contents of internal RAM location 30H to R6.
MOV R5, 03H ; Moves contents of R3 to R5. (Assuming Register bank is 0)
MOV 05H, R3 ; Moves contents of R3 to R5. (Assuming Register bank is 0)
Note that ‘80H’ without ‘#’ preceding it, refers to an address. All the above instructions are 2 bytes in
length. Many assemblers allow ‘MOV 80H,R3’ to be written as ‘MOV P0,R3’.
559
Advantage of SFR: There is a definite advantage in treating SFR as an internal RAM location instead
of just treating it as a register. If a few more SFRs are added to the microcontroller in its improved
version, then there is no need to add any more instruction codes to access these added SFRs. It is
something like if the memory in the computer system is increased, there is no need to have additional
instruction codes to access these added memory locations. To justify the above, it may be noted that
8752 is an improvement over 8051, as it has one more timer/counter. But the instruction set of 8051 g
(n
and 8752 remain the same. Thus further (128-21) = 107 SFRs may be added to 8051 architecture
without the need for additional instruction codes. On the other hand, if one more register, say G reg¬ o
ister, is added to 8085 architecture, then we must have additional opcodes for instructions like ‘MOV 3o
A, G’ and so on.
l
29.6.4 REGISTER INDIRECT ADDRESSING
In this type of addressing the source or destination operand address is provided in a register. In this
mode, both internal and external data memory can be accessed. Only R0 and R1 can be used as 8-bit
address registers. Only DPTR can be used as 16-bit address register. The following are some
examples.
MOV 0F0H, @R1 /Suppose (Rl) = 40H. Then contents of internal RAM
/location 40H is moved to B. B address is F0H.
MOV @R0, 90H /Suppose (R0) = 30H. Then contents of Port 1 is moved
/to internal RAM location 30H. Port 1 has address 90H.
The ‘@’ symbol stands for register indirect addressing. The above instructions are 2 bytes in length.
Note that in the assembly language R0 to R7 and all the SFRs except A, and DPTR are expressed
using their 8-bit SFR address. Exceptions to this are the push and pop instructions where even A, and
DPTR are expressed using their 8-bit SFR address. However, many standard assemblers provide the
facility of using the SFR name in assembly language programming, although in machine language the
SFR address will be used (except for A, DPTR, and R0 to R7 in general). But some assemblers expect
the SFR address, instead of SFR name, to be used in assembly language also.
Thus in many assemblers ‘MOV 0F0H, @R1’ can also be written as ‘MOV B, @R1’ which is more
easily understandable. Similarly, ‘MOV @R0, 90H’ can be written as ‘MOV @R0, PI’.
The ‘X’ in ‘MOVX’ refers to eXtemal data memory. The above instructions are 1-byte long. Note that
external data memory can be accessed using register indirect addressing only. Also only A register
communicates with external memory.
In this type of addressing the source operand is accessed from program memory only. It is intended
for reading look-up tables present in program memory. The destination operand is always A register.
Some examples follow.
560
MOVC A, @A+ PC /Suppose (A) = 20H, (PC) = 1231H. Then contents
;of program memory location 1251H is moved to A.
MOVC A, 0A+DPTR /Suppose (A) = 20H, (DPTR) = 2230H. Then contents
/of program memory location 2250H is moved to A.
The ‘C’ in ‘MOVC’ refers to code byte. The above instructions are 1 byte in length.
8M
o
Sa 29.6.6 IMPLIED ADDRESSING
E
.2
S In this type of addressing there will be a single operand, and this happens to be a specific register only,
in
00 like the accumulator. Intel refers to this type of addressing as ‘register specific instructions’. The
s« following are some examples.
In an instruction that has two operands, the first operand specifies the destination and the second spec¬
ifies the source. This is the same as in the Intel 8085 microprocessor.
There are 28 instruction types in the data transfer type of operations. They account for 79 opcodes,
which are indicated in Table 29.2. The table indicates the mnemonic, length of the instruction, execu¬
tion time in terms of machine cycles, and number of opcodes for the type of instruction. Thus, if the
execution time is shown as one machine cycle, it takes 1 jxs for execution if the crystal frequency is
12 MHz. None of the flags are affected by the execution of data transfer instructions. However, if A
value is altered because of the execution of a data transfer instruction, the P flag is set to 1 if there are
odd number of Is, else it is reset to 0. Also if data is moved to PSW register all the flags are obviously
affected. In the description of instructions, the following convention is used.
1. MOV A, Rn 1 1 8
2. MOV A, a8 2 1 1
3.
4.
MOV A, @Ri
MOV A, #d8
1
2
1
1
2
1
g
(n
5. MOV Rn, A 1 1 8
6. MOV Rn, a8 2 2 8
o
7. MOV Rn, #d8 2 1 8 3o
8. MOV a8, A 2 1 1
9. MOV a8, Rn 2 2 8
10. MOV a8, a8 3 2 1 3
li. MOV a8, @Ri 2 2 2
12. MOV a8, #d8 3 2 1 5
13. MOV @Ri, A 1 1 2
14. MOV @Ri, a8 2 2 2
15. MOV @Ri, #d8 2 1 2
16. MOV DPTR, #dl6 3 2 1
17. MOVC A, @A+DPTR 1 2 1
18. MOVC A, @A+PC 1 2 1
19. MOVX A, @Ri 1 2 2
20. MOVX A, @DPTR 1 2 1
21. MOVX @Ri, A 1 2 2
22. MOVX @DPTR, A 1 2 1
23. PUSH a8 2 2 1
24. POP a8 2 2 1
25. XCH A, Rn 1 1 8
26. XCH A, a8 2 1 1
27. XCH A, @Ri 1 1 2
28. XCHD A, @Ri 1 1 2
Examples
‘MOV R3, 25 H’ is a typical example for the general type of instruction ‘MOV Rn, a8\ The contents
of internal RAM location 25H are moved to R3. It is a 2-byte instruction, and the execution time is
two machine cycles.
‘MOV 0F0H, #25H’ is a typical example for the general type of instruction ‘MOV a8, #d8\ The
immediate data 25H is moved to register B, which is an SFR with address FOH. It is a 3-byte instruc¬
tion, and the execution time is two machine cycles. The instruction can also be written as ‘MOV B,
#25H\
‘PUSH 0E0H’ is a typical example for the general type of instruction ‘PUSH a8\ The contents of
A register (SFR address EOH) are pushed on the stack. The SP is incremented by 1 in this operation.
It is a 2-byte instruction, and the execution time is two machine cycles. The instruction can also be
written as ‘PUSH A’.
‘POP 3OH’ is a typical example for the general type of instruction ‘POP a8\ The contents of stack
top is moved to internal RAM location 30H. The SP is then decremented by 1. It is a 2-byte instruc¬
tion, and the execution time is two machine cycles.
‘XCH A, R3’ is a typical example for the general type of instruction ‘XCH A, Rn’. The contents of
A and R3 are exchanged. It is a 1-byte instruction, and the execution time is one machine cycle.
562
‘XCHD A, @R1’ is a typical example for the general type of instruction ‘XCHD A, @Ri’. The contents
of LS digit of A and LS digit of internal RAM location pointed by R1 are exchanged. If A contents are
35H, R1 contains 45H, and internal RAM location 45H contains 67H, then A contents are changed to 37H
and internal RAM location 45H contents are changed to 65H. It is a 1-byte instruction, and the execution
time is one machine cycle. The effect of execution of this instruction is represented as shown below.
s Before After
a>
o
sa (A) 35H 37H
(Rl) 45H
E (45H) 67H 65H
.2
S
in
00
s« 29.7.2 ARITHMETIC GROUP
H
There are 24 instruction types in the arithmetic group of instructions, which account for 64 opcodes.
They are indicated in Table 29.3. The Cy, AC, and V flags are affected based on the result in the case
of ADD, ADDC, and SUBB instructions. The multiply and divide instructions clear the Cy flag, and
do not affect the AC flag. The V flag is set to 1 after multiply instruction, if the result is greater than
FFH, else it is cleared. Similarly after divide instruction, the V flag is set to 1 if the B content was 00
before division, else it is cleared. DA A instruction affects the Cy flag only. Other instructions do not
affect any flags. However, when A value is altered because of execution of an instruction, the P flag
is affected.
1. ADD A, Rn 1 1 8
2. ADD A, a8 2 1 1
3. ADD A, @Ri 1 1 2
4. ADD A, MS. 2 1 1
5. ADDC A, Rn 1 1 8
6. ADDC A, a8 2 1 1
7. ADDC A, @Ri 1 1 2
8. ADDC A, #d8 2 1 1
9. SUBB A, Rn 1 1 8
10. SUBB A, a8 2 I 1
11. SUBB A, @Ri 1 I 2
12. SUBB A, #d8 2 I I
13. INCA I I I
14. INC Rn 1 I 8
15. INC a8 2 I 1
16. INC @Ri 1 I 2
17. DEC A 1 1 1
18. DEC Rn 1 1 8
19. DEC a8 2 1 1
20. DEC @Ri 1 1 2
21. INC DPTR 1 2 1
22. MULAB 1 4 1
23. DIVAB 1 4 1
24. DA A 1 1 1
563
Examples
‘ADD A, #25H’ is a typical example for the general type of instruction ‘ADD A, #d8’. The immediate
data 25H is added to A, and the result is stored in A. It is a 2-byte instruction, and the execution time
is one machine cycle. Note that for add and subtract instructions the result is always stored in A.
‘ADDC A, @R0’ is a typical example for the general type of instruction ‘ADDC A, @Ri’. The con¬
tents of internal RAM location pointed by RO is added to A. It is a 1-byte instruction, and the execu¬ g
(n
tion time is one machine cycle.
‘SUBB A, R3’ is a typical example for the general type of instruction ‘SUBB A, Rn’. SUBB stands ¥
0
for subtract with borrow. The contents of R3 is subtracted from A contents along with borrow, and the 3
o
result is stored in A. ‘SUBB A, R3’ is a 1-byte instruction, and the execution time is one machine
cycle. Note that there is no ‘SUB A, R3’ which subtracts R3 contents from A. This was due to paucity Z
of opcodes. If this operation is desired, then it is necessary to execute ‘CLR C’ and ‘SUBB A, R3’. i
<r
‘CLR C’ clears the Cy flag to 0.
‘INC 30H’ is a typical example for the general type of instruction ‘INC a8’. The contents of inter¬
nal RAM location 3OH is incremented by one. It is a 2-byte instruction, and the execution time is one
machine cycle.
‘INC DPTR’ increment contents of DPTR by one. It is a 1-byte instruction, and the execution time
is two machine cycles. ‘DEC DPTR’ instruction is strangely not implemented in 8051.
‘MUL AB’ multiplies the contents of A and B registers, treating them as unsigned numbers. The
16-bit result is stored in B (MS byte) and A (LS byte). If the result is larger than FFH, then the V flag
is set to one. The Cy flag is always cleared after the multiply instruction. It is a 1-byte instruction that
needs four machine cycles for execution.
‘DIV AB’ divides the contents of A register by the contents of B register, treating them as unsigned
numbers. The 8-bit quotient is stored in A and the 8-bit remainder in B register. The V flag is set to 1
only if B content is 00H before division. Also A and B values are undefined after divide operation.
The Cy flag is always cleared after the divide instruction. It is a 1-byte instruction that needs four
machine cycles for execution.
There are 25 instruction types in the logical group of instructions. They account for 49 opcodes which
are indicated in Table 29.4. Only RRC and RLC instructions affect the Cy flag. The other instructions
do not affect any flag.
Examples
‘ANL A,R3’ is a typical example for the general type of instruction ‘ANL A,Rn’. The contents of R3
is ANDed with A contents and result stored in A. It is a 1-byte instruction, and the execution time is one
machine cycle. This is similar to ‘ANA’ instruction of Intel 8085. Similarly OR and Ex-OR operations
can also be performed. Note that in these operations the destination operand is always A register.
‘CLR A’ instruction results in A contents becoming 00H. It is not possible to clear the contents of
any other register or memory location. It is a 1-byte instruction, and the execution time is one machine
cycle.
‘CPL A’ instruction results in complementing every bit of A register. It is not possible to comple¬
ment the contents of any other register or memory location. It is a 1-byte instruction, and the execu¬
tion time is one machine cycle.
‘RL A’ rotates left contents of A by 1-bit position. It is similar to ‘RLC’ instruction of Intel 8085,
except that none of the flags are affected. Contents of no other register or memory location can be
rotated left or right. ‘RL A’ is a 1-byte instruction that is executed in one machine cycle.
564
Table 29.4 Logical Group of Instructions
SI. no. Mnemonic No. of bytes Exec, time Opcodes
1. ANL A, Rn 1 I 8
2. ANL A, a8 2 I 1
3. ANL A, @Ri 1 I 2
8
M
4. ANL A, #d8 2 I 1
5. ANL a8, A 2 I 1
0
sa 6.
7.
ANL a8, #d8
ORL A, Rn
3
1
2
I
1
8
E 8. ORLA, a8 2 I 1
.2 I
s 9. ORL A, @Ri 1 2
U) 10. ORL A, #d8 2 1 1
00
s« 11.
12.
ORL a8, A
ORL a8, #d8
2
3 2
1 1
1
13. XRL A, Rn 1 1 8
H 14. XRL A, a8 2 1 1
15. XRL A, @Ri 1 1 2
16. XRL A, #d8 2 1 1
17. XRL a8, A 2 1 1
18. XRL a8, #d8 3 2 1
19. CLR A 1 1 1
20. CPL A 1 1 1
21. RL A 1 1 1
22. RLC A 1 1 1
23. RR A 1 1 1
24. RRC A 1 1 1
25. SWAP A 1 1 1
‘RLC A’ rotates left contents of A together with Cy flag by 1-bit position. It is the same as the
‘RAL’ instruction of Intel 8085. Only Cy flag is affected. ‘RLC A’ is a 1-byte instruction that is
executed in one machine cycle.
‘RR A’ rotates right contents of A by 1-bit position. It is similar to ‘RRC’ instruction of Intel 8085,
except that none of the flags are affected. ‘RR A’ is a 1-byte instruction that is executed in one machine
cycle.
‘RRC A’ rotates right contents of A together with Cy flag by 1-bit position. It is the same as the
‘RAR’ instruction of Intel 8085. Only Cy flag is affected. ‘RRC A’ is a 1-byte instruction that is exe¬
cuted in one machine cycle.
‘SWAP A’ instruction exchanges the LS hex digit with the MS hex digit in A. It is functionally the
same as executing RLA/RRA four times. The flags are not affected and it is a 1-byte instruction that
is executed in one machine cycle.
There are 17 instruction types that come under bit processing of instructions. They account for 17
opcodes. The carry bit acts like ‘single-bit accumulator’ in many bit-processing instructions. The bit¬
processing instructions are indicated in Table 29.5. These instructions do not affect any flags,
however, instructions like ‘ANL C, bit’ will obviously affect the Cy flag. In the description of these
instructions, the following convention is used.
565
1. CLRC 1
2. CLR bit 2
3. SETBC 1 n
4. SETB bit 2 (n
5. CPLC 1
6. CPL bit 2
O
7. ANL C, bit 2 2
3o
8. ANL C, /bit 2 2
9. ORL C, bit 2 2
10. ORL C, /bit 2 2 0
11. MOV C, bit 2 1
12. MOV bit, C 2 2
13. JCrel 2 2
14. JNCrel 2 2
15. JB bit, rel 3 2
16. JNB bit,rel 3 2
17. JBC bit,rel 3 2
Examples
‘CLR C’ instruction results in Cy flag becoming 0. It is a 1-byte instruction, and the execution time is
one machine cycle.
‘SETB 0D5H’ is a typical example for the general type of instruction ‘SETB bit’. It sets to 1 bit
D5 (which is F0 flag in PSW). It is a 2-byte instruction, and the execution time is one machine cycle.
The instruction could also be written as ‘SETB PSW.5’ or ‘SETB F0\
‘CPL C’ instruction results in complementing C bit. Similarly using ‘CPL bit’ instruction a bit can
be complemented.
‘ANL C, 07’ is a typical example for the general type of instruction ‘ANL C, bit’. It ANDs C bit
with bit 7 (which is MS bit in internal RAM location 20H) and stores the result in C. It is a 2-byte
instruction, and the execution time is two machine cycles.
‘ORL C, /07’ is a typical example for the general type of instruction ‘ORL C, /bit’. It ORs C bit
with complement of bit 7 (bit 7 is MS bit in internal RAM location 20H) and stores the result in C. It
is a 2-byte instruction, and the execution time is two machine cycles.
‘MOV C, 07’ is a typical example for the general type of instruction ‘MOV C, bit’. It moves the
contents of bit 7 (which is MS bit in internal RAM location 20H) to C. It is a 2-byte instruction, and
the execution time is one machine cycle.
Thus if it is required to set the Cy flag if and only if LS bit of P0 is 1, MS bit of A register is 1, and
V flag is 0, the following instructions are to be executed.
MOV C, PO.O
ANL C, ACC. 7
ANL C, /OV ; or ANL C, / PSW . 2
8M ‘JC LOOP’ is a typical example for the general type of instruction ‘JC reT. If the Cy flag is set
o to 1, then a branch to location LOOP takes place. If Cy flag = 0, execution proceeds with the next
S
a instruction. LOOP is a symbolic location, but the assembler generates an 8-bit signed displacement
E from the instruction following ‘JC LOOP’. Thus branch can take place to a location which is 128 bytes
.2
S behind or 127 bytes ahead of the instruction following JC. It is a 2-byte instruction, and the execution
in
00
time is two machine cycles.
s« ‘JNB 80H, BACK’ is a typical example for the general type of instruction ‘JNB bit, rel’. If LS bit
of Port 0 (bit address 80H) is reset to 0, then a branch to location BACK takes place. If this bit is set
H to 1, execution proceeds with the next instruction. BACK is a symbolic location, but the assembler
generates an 8-bit signed displacement from the instruction following ‘JNB 80H,BACK’. It is a 3-byte
instruction, and the execution time is two machine cycles. The instruction could also be written as
‘JNB PO.O, BACK’.
‘JBC 80H, BACK’ is a typical example for the general type of instruction ‘JBC bit, reT, where JBC
stands for ‘jump if bit is set and then clear bit’. If LS bit of P0 (bit address 80H) is set to 1, then a
branch to location BACK takes place and the bit will be cleared to 0. If this bit was reset to 0, execu¬
tion proceeds with the next instruction. BACK is a symbolic location, but the assembler generates an
8-bit signed displacement from the instruction following ‘JNB 80H, BACK’. It is a 3-byte instruction,
and the execution time is two machine cycles. The instruction could also be written as ‘JBC PO.O,
BACK’. In reality, instructions like JC, JNB, and JBC could be discussed under program branch
instructions.
There are 17 instruction types that come under program branch instructions. They account for 46
opcodes. The program branch instructions are indicated in Table 29.6. Only CJNE instruction affects
the Cy flag, while other instructions do not affect any flag. In the description of these instructions, the
following convention is used.
Examples
‘LJMP LOOP’ is a typical example for the general type of instruction ‘LJMP addrl6’. LJMP stands
for long jump. It performs a branch to symbolic location LOOP. The assembler generates a 16-bit
address corresponding to location LOOP. A branch to any location in program memory is possible
using LJMP instruction. It is a 3-byte instruction, and the execution time is two machine cycles.
‘ACALL SUBR’ is a typical example for the general type of instruction ‘ACALL addrll’. It is a
2-byte instruction, and the execution time is two machine cycles. ACALL stands for absolute call. It
causes a branch to subroutine at symbolic location SUBR after saving address of the next instruction
on the stack top. For the purpose of ACALL and AJMP instructions, the program memory can be
thought of as being divided into the following blocks of 2K bytes— 0000H to 07FFH, 0800H to
567
Table 29.6 Program Branch Instructions
SI. no. Mnemonic No. of bytes Exec, time Opcodes
1. ACALL addrll 2 2 8
2. LCALL addrl6 3 2 1
3. RET 1 2 1 n
4. RETI 1 2 1 (n
5. AJMP addrll 2 2 8
6. LJMP addrl6 3 2 1
0
7. SJMP rel 2 2 1 3o
8. JMP @A+DPTR 1 2 1
9. JZ rel 2 2 1
10. JNZrel 2 2 1 0
11. CJNE A,a8,rel 3 2 1
12. CJNE A,#d8,rel 3 2 1
13. CJNE Rn,#d8,rel 3 2 8
14. CJNE @Ri,#d8,rel 3 2 2
15. DJNZ Rn.rel 2 2 8
16. DJNZ a8,rel 3 2 1
17. NOP 1 1 1
OFFFH, and so on, and finally F800FI to FFFFFI. As an example, if the address of the instruction after
AC ALL is in the block 1800H to 1FFFFI, then the starting address of the subroutine using ACALL is
restricted to be within this block of 1800H to 1FFFFI. For any address within such a block, the MS
5 bits remain the same. As an example, the MS 5 bits of 1800H and 1FFFH are 00011. This 00011 is
called the block address. It provides MS 5 bits of subroutine address. Hence just 11 bits of address is
enough to specify the address of subroutine within the block.
Further, the block of 2K = 0800H bytes of memory can be thought of as being divided into eight
pages each of 256 = 100H bytes. Hence to select a location in a block of 2K bytes, we have to select
a page within the block using 3 bits, and a location within the page using 8 bits. The ACALL instruc¬
tion is implemented using only 2 bytes. The first byte will be PPP 10001, where PPP indicates page
number within the block, and 10001 is the code for ACALL. The second byte contains the address
within the page.
As an example, consider ‘ACALL SUBR’ being at location 1834H in external program memory. If
SUBR is location 1C85H, ‘ACALL SUBR’ is coded as follows. First byte will be 100 10001B = 91H,
where 100 indicates select page 4 within the block and 10001 is the opcode for ACALL. The second
byte will be 85H that provides the LS byte of subroutine address.
The MS 5 bits of subroutine address will be the block address. This is 00011 as described earlier.
The next 3 bits provide page address. This will be 100 to indicate page 4 within block 00011. Thus,
branch takes place to the subroutine at address 00011 100 85H = 1C85H. If SUBR is at location
1D85H, then ‘ACALL SUBR’ is coded as follows. First byte will be 101 1000 IB = B1H, where 101
indicates select page 5 within the block and 10001 is the opcode for ACALL. The second byte will be
85H that provides the LS byte of subroutine address.
The MS 5 bits of subroutine address will be 00011 that gives the block address. The next 3 bits will
be 101 to indicate page 5. Thus, branch takes place to the subroutine at address 00011 101 85H =
1D85H. From the above it should be clear that there are eight opcodes for ACALL instruction depend¬
ing on the page number within the block where the subroutine starts. As can be seen, two of the eight
possible opcodes for ACALL are 91H and B1H.
‘SJMP LOOP’ is a typical example for the general type of instruction ‘SJMP rel\ SJMP stands for
short jump. ‘SJMP LOOP’ causes a branch to location LOOP. LOOP is a symbolic location, but the
568
assembler generates an 8-bit signed displacement from the instruction following ‘SJMP LOOP’. It is
a 2-byte instruction, and the execution time is two machine cycles. In the instruction set of 8051 there
is no HALT instruction. Thus, to terminate a program ‘LOOP: SJMP LOOP’ is used.
It can be noted from Table 29.6 that there are three kinds of unconditional jumps— SJMP, AJMP,
and LJMP. However, JMP is a generic name that can be used if the programmer does not care which
s way the jump is encoded. However, some assemblers expect atleast one of SJMP, AJMP, or LJMP to
be used. Similarly, it can be noted that there are two kinds of unconditional calls— ACALL and
a>
o
sa LCALL. However, CALL is a generic name that can be used if the programmer does not care which
way the call is encoded. However, some assemblers expect ACALL or LCALL to be used. There is
E no SC ALL instruction in 8051.
.2
S ‘JZ BACK’ is a typical example for the general type of instruction ‘JZ rel’. If A register contents
in
00 is 00H, then a branch to location BACK takes place. If A register contents are unequal to 00H,
s« execution proceeds with the next instruction. BACK is a symbolic location, but the assembler
generates an 8-bit signed displacement from the instruction following ‘JZ BACK’. It is a 2-byte
H
instruction, and the execution time is two machine cycles. It is to be noted that JZ instruction tests the
contents of A register for a 00H value, and there is no Z flag in 8051.
The RET instruction functions as pop from stack top to PC. It is similar to the RET instruction of
8085 (except that after the RET instruction of 8051, the SP value is decremented by 2). It is an l-byte
instruction that is executed in two machine cycles.
The RETI instruction also functions as pop from stack top to PC. In addition, it restores the
interrupt logic to accept additional interrupts at the same priority level as the one just processed.
RETI stands for return from interrupt. An interrupt service routine ends with RETI, and an ordinary
subroutine ends with RET instruction. RETI is a 1-byte instruction that is executed in two
machine cycles. The use of RETI would become clear after 8051 interrupt system is described in
detail later.
‘DJNZ 80H, BACK’ is a typical example for the general type of instruction ‘DJNZ a8, rel’. DJNZ
stands for ‘decrement and jump if not zero’. The P0 (address 80H) contents are decremented by 1. If
it is not 00H after the decrement, then a branch to location BACK takes place. Otherwise execution
proceeds with the next instruction. BACK is a symbolic location, but the assembler generates an 8-bit
signed displacement from the instruction following ‘DJNZ 80H, BACK’. It is a 3-byte instruction, and
the execution time is two machine cycles. The instruction could also be written as ‘DJNZ P0, BACK’.
‘CJNE R3,#80H,BACK’ is a typical example for the general type of instruction ‘CJNE Rn,#d8,rel’.
CJNE stands for ‘compare and jump if not equal’. The CJNE instruction combines compare and jump
into a single compact instruction. In this case, R3 contents are compared with 80H. If R3 contents are
different from 80H then a branch to location BACK takes place. Otherwise execution proceeds with
the next instruction. BACK is a symbolic location, but the assembler generates an 8-bit signed
displacement from the instruction following ‘CJNE R3,#80H, BACK’. It is a 3-byte instruction, and
the execution time is two machine cycles. However, 8051 does not have instructions like ‘compare
and jump if above’ or ‘compare and jump if below’.
The programming examples provided here are only to familiarize the reader with the instruction set
of 805 1, especially with those that are new to 805 1 compared to 8085. These programs were tested on
SDA 31 kit manufactured by M/s Advanced Electronic Systems, Bangalore— 560 055.
569
29.8.1 SHIFT A MULTI-BYTE BCD NUMBER TO THE RIGHT
Write an 8051 assembly language program to shift an 8-digit BCD number by two digits to the right.
The 8-digit BCD number is assumed to be in internal data memory locations 20H, 21H, 22H, and 23H.
Location 20H is having the MS byte.
For the trace that follows, it is assumed that the BCD number is 12 34 56 78, with 12 at location g
(n
20FI. After the program is executed, contents of location 20FI is cleared to 00FI, and the LS byte
shifted out will be available in A. o
CLR A (A) = 00 3
o
XCH A,20H (20H) = 00, (A) = 12,
XCH A,21H (21H) = 12, (A) = 34,
XCH A,22H (22H) = 34, (A) = 56,
l
XCH A,23H (23H) = 56, (A) = 78,
STOP: SJMP STOP
Write an 8051 assembly language program to convert an 8-bit binary number to its equivalent BCD
value. The 8-bit binary number is at external RAM location 30FI. The result is to be stored in loca¬
tions 31 FI and 32FI, with 31 FI having the MS portion of result.
In the trace that follows it is assumed that the data at location 30FI is FEFI. The program converts
binary value FEFI to BCD value of 0254, and stores it in locations 31 FI and 32FI.
MOV P2, #00H Set MS byte of external data address as 00
MOV R0, #30H Load R0 with address of data
MOVX A, @R0 (A) = FEH (254 decimal)
MOV B, #0AH (B) = 0AH (10 decimal)
DIV AB (A) = 19H (25 decimal), (B) = 04
MOV R2, B (R2) = 04
MOV B, #0AH (B) = 0AH
DIV AB (A) = 02, (B) = 05
INC R0 (R0) = 31H
MOVX @R0, A (0031) = 02 (Store MS portion of result)
MOV A, B (A) = 05
SWAP A (A) = 50
ADD A, R2 (A) = 54
INC R0 (R0) = 32H
MOVX @R0, A (0032) = 54 (Store LS portion of result)
EXIT: SJMP EXIT Stop
Write an 805 1 assembly language program to convert a two-digit BCD number to its equivalent binary
value. The two-digit BCD number is at external RAM location 200H. The result is to be stored in
location 201H.
570
In the program that follows, two software up counters are made use of. They start from 00 value
initially. One counter counts up in decimal using A register, while the other counter counts up in binary
using R2 register. When the decimal up counter reaches the desired BCD value, the count up is
stopped. At that point the binary counter will have the equivalent binary value.
In the trace that follows it is assumed that the data at location 200H is 15. The program converts
8M BCD value 15 to binary value of OFH, and stores it in location 0201H.
Write an 805 1 assembly language program to convert a two-digit BCD number to its equivalent binary
value. The two-digit BCD number is at external RAM location 200H. The result is to be stored in loca¬
tion 201H. In the program shown as follows, the MS digit of the BCD number is multiplied by 10
(0AH). For example, if the BCD number is 95, 9 is multiplied by 0AH, to get 5AH. Then the LS digit
of the BCD number, which is five, is added to get 5FH which is the binary equivalent of the BCD
number 95.
In the following trace it is assumed that the data at location 200H is 95. The program converts BCD
value 95 to binary value of 5FH, and stores it in location 0201H.
SWAP A / (A) = 59
ANL A, #0FH ; (A) = 09H
Write an 8051 assembly language program to convert a single-digit hexadecimal number to its
equivalent ASCII value. The single-digit hexadecimal number is at external RAM location 300H. The
result is to be stored in location 301H.
In the following trace it is assumed that the data at location 300H is 00. The program converts hexa¬ g
(n
decimal value 00 to ASCII value of 30H, and stores it in location 0301H. The program uses look-up
table approach. Immediately after the RET instruction, the look-up table containing the ASCII codes 2
0
for ‘0’ to ‘F’ is stored. 3
0
In the CONVRT subroutine when ‘MOVC A, @A+PC’ is executed, the PC value taken for calcula¬
tion is the address of RET instruction. So to move past the 1-byte RET instruction and fetch result from z
look up table, A value is incremented by 1 in the main program before branching to CONVRT subroutine. l
MOV DPTR, #0300H (DPTR) = 0300H
MOVX A, @DPTR (A) = 00H
INC A (A) = 01H
ACALL CONVRT (A) = 30H
INC DPTR (DPTR) = 0301H
MOVX @DPTR, A (0301) = 30H
STOP: SJMP STOP Halt
CONVRT: MOVC A, @A+PC (A) = 30H
RET Return to main program
30H ;ASCII code for '0'
31H
32H
33H
34H
35H
36H
37H
38H
39H ; ASCII code for '9'
4 1H ; ASCII code for 'A'
42H
4 3H
44H
4 5H
4 6H ; ASCII code for 'F'
Write an 805 1 assembly language program to modify a given byte at internal RAM location 40H as
detailed below, and store the modified byte at internal RAM location 41H.
Bit 0 is to be set to 1;
Bit 2 is to be reset to 0;
Bit 4 is to be complemented;
Bit 6 value should become the same as bit 5;
Bit 7 value should become AND of bit 1 and complement of bit 3.
572
In the trace that follows, it is assumed that the data at internal RAM location 40H is 25H. The pro¬
gram modifies it to 71H and stores it in internal RAM location 41H.
In the program that follows, contents of internal RAM location 40H is moved to internal RAM
location 20H, which is a bit-addressable RAM location. This is done in order to carry out bit manip¬
ulation on this byte. In this program ‘MOV C, 20.5’ stands for ‘move to Cy flag bit 5 of byte at inter¬
8M nal RAM location 20H’. It could as well have been written as ‘MOV C, 5’.
Write an 8051 assembly language program to convert a four-digit hexadecimal number to equivalent
ASCII. The four-digit hex number is at internal RAM locations 30H and 31H. The equivalent ASCII
is to be stored in four internal RAM locations starting from 50H.
MOV R0, #30H ;(R0) = 30H
MOV Rl, #4 OH ;(Rl) = 40H
MOV R2, #02H ;(R2) = 02H. R2 is used as loop counter.
;The 'REPT' loop converts a 4 digit hexadecimal number, say,
;12CD at internal RAM locations 30H and 31H to 01, 02, 0C, 0D, and
/stores them in locations starting from internal RAM location 40H.
REPT: MOV A, @R0 ;
ANL A, #0F0H ;
SWAP A
MOV @R1, A
MOV A, @R0
ANL A, #0FH ;
INC Rl
MOV @R1, A
INC R0
INC Rl
DJNZ R2,REPT
MOV R0, #40H;
MOV Rl, #50H;
MOV R2, #04H; R2 is used as loop counter
573
;The 'AGAIN' loop converts the hex number pointed by RO to its
,-equivalent ASCII, and stores in location pointed by R1. This
;is done by adding 30H to hex number, if the number is in the
;range 0 to 9. If the number is in the range A to F, 37H is
;added. The loop is repeated 4 times.
AGAIN: MOV A,@R0 §
ADD A, #30H w
CJNE A, #3AH,FRWD
0
FRWD: JC NUMB 3o
ADD A, #07H
NUMB: MOV @R1, A s
INC RO 3
INC R1 2
DJNZ R2, AGAIN
EXIT: SJMP EXIT
In the previous program it should be noted that the following two instructions of 805 1
CJNE A, #3AH,FRWD
FRWD: JC NUMB
are equivalent to the following 8085 instructions:
CPI 3AH
JC NUMB
sI
m 30. 1 INTERRUPT STRUCTURE OF 805 1
5' n
The 8051 can be interrupted from the following five sources.
2?
2.
Two external interrupts INTO* and INTI*; o
Two internal timer interrupts TO and Tl;
One internal serial port interrupt.
The interrupt vectors (ISS address) for these interrupt sources are shown as follows.
INTO* 0003H
TimerO 000BH
INTI* 0013H
Timer1 001BH
Serial port 0023H
Each of the interrupt sources can be individually enabled or disabled by programming the IE register.
The IE register is an SFR with the address A8H, and is bit addressable, which is shown in Fig. 30.1. It
is programmed by the user. The EA (enable all) bit is actually a global disable bit as described below.
AF AC AB AA A9 A8 Bit address
Fig. 30.1
EA X X ES ET1 EX1 ET0 EX0 Bit details of
IE register
Thus ‘CLR EA’ (or equivalently ‘CLR IE.7’ or ‘CLR AF’) is functionally the same as the DI
(disable interrupts) instruction of 8085. If EA = 0, the LS 5 bits are don’t-care bits. The meaning of
the other bits is as follows when EA = 1.
BC BB BA B9 B8 Bit address
Fig. 30.2
x x x PS PTl PX1 PT0 PX0 Bit details of
IP register
The external interrupts INTO* and INTI* can be individually programmed to be edge-triggered
or level-triggered. This is done by programming the TCON register. TCON is the abbreviation for
577
Timer Control. Although named TCON, it also provides control and status information about the
external interrupts. The bit details of TCON register are provided in Fig. 30.3.
—
TCON register 5' n
TCON is a bit-addressable register, with SFR address as 88H. Discussion about the LS 4 bits of 1H?
TCON that provide control and status information about external interrupts, ITO and ITI (IT for inter¬ 2.
o
rupt type) control bits decide whether INTO* and INTI* will be edge-triggered or level triggered
inputs as shown in the following. These bits are programmed by the user.
ITO: 1 = INTO* will be negative edge-triggered input
0 = INTO* will be active low level-triggered input
The IEO and IE1 bits indicate the status of INTO* and INTI* pins. These bits are set or cleared by
8051. IE stands for ‘Interrupt from External source’. For example, IEO bit is set to 1 by the 8051 when
INTO* activation is detected. If INTO* is programmed for edge triggering, IEO bit is automatically
reset to 0 by 8051 when a branch to the ISS at 0003H takes place. If INTO* is programmed for level
triggering, IEO bit is reset to 0 by 8051 only when INTO* input is deactivated. This can be done, for
example, by setting to 1 an external flip-flop in the ISS, which deactivates the INTO* request. If this
deactivation is not done, the 8051 will again be interrupted after the completion of the ISS.
The MS4 bits of TCON register shown in Fig. 30.3 provide control and status information about the
timers as discussed in the following.
TRO and TR1 (TR for timer run) bits decide the running of these timers. They provide software
control over the running of the timers. There can also be hardware control over the running of timers.
Thus, even if a timer is set for run mode by software, it will be in run mode only if some hardware
conditions are satisfied, as will be discussed later. In other words, TR bit = 1 is a necessary but not
sufficient condition for running the timer. TRO and TR1 bits are programmed by the user.
TFO and TFl bits indicate the overflow status of TimerO and Timerl. These bits are set or cleared
by 8051. For example, TFO bit is set to 1 by the 8051 when timerO overflow occurs. TFO bit is auto¬
matically reset to 0 by 805 1 when a branch to the ISS at 000BH takes place. More details about the
working of timers is provided later.
578
30.1 .5 SERIAL PORT INTERRUPT
The serial port is used for both transmission and reception. The interrupt status for transmission
and reception is provided by TI and RI bits in SCON register. SCON is the abbreviation for serial con¬
trol. The bit details of SCON register are provided in Fig. 30.4.
8M
0 9FH 9EH 9DH 9CH 9BH 9AH 99H 98H Bit address
S
a Fig. 30.4
s SMO SMI SM2 REN TB8 RB8 TI RI Bit details of
s SCON register
in
00
s SCON is a bit-addressable register, with SFR address as 98H. The LS 2 bits of SCON provide sta¬
tus information about serial port interrupts as discussed in the following. TI indicates the transmitter
Jj
H interrupt status, and RI indicates the receiver interrupt status. If any of these 2 bits is a 1, then the 8051
will be interrupted if all the following conditions are met.
In such a case, the 8051 branches to the ISS at 0023FI. In the ISS, the programmer has to reset the
TI and/or RI bit by software. These bits are not automatically reset upon entering the ISS. Further
details about the working of serial port and other bits of SCON are provided later. From this discus¬
sion on interrupt structure, the 8051 interrupt control system can be visualized as shown in Fig. 30.5.
ofTCON
PI" < Polling sequence
within the selected
0 05 0,
INT -
priority level
mbit/
ofTCON'':
IEl
Ixii*-EA‘:T PX1
<"1*
t setlJI ofTCON
o <L 0
□llK EÿIÿf
1 P , ,
PT1
ofTCON
of SCON
TI
RI
0.
mi— sh
0>
>
PS "=v
A machine cycle consists of six states, SI to S6. The interrupt flags IE0, IEl, TF0, TF1 of TCON reg¬
ister and RI and TI of SCON register are sampled at the end of S5 in each machine cycle. This is so,
579
even if an instruction needs more than one machine cycle for execution. The samples are polled dur¬
ing the next machine cycle. If one of the interrupt flags is set at the end of S5 of the preceding machine
cycle, the polling cycle will find it and the interrupt system will generate an LCALL to the appropri¬
ate ISS. This happens provided none of the following is true.
sI
(a) An interrupt of equal or higher priority level is already in progress.
(b) The polling cycle is not the final machine cycle in the execution of the instruction in
progress. 5' n
(c) The instruction in progress is one of the following:
RETI 2?
2.
Write to IP register o
Write to IE register
If any of these conditions is true, LCALL to ISS will not be generated. Condition (b) ensures that the
instruction in progress is completed before branching to an ISS. Condition (c) ensures that one more
instruction is executed after RETI, or any write to IE and/or IP register, before branching to an ISS.
The hardware generated LCALL pushes the contents of the PC on the stack top, and branches to
the appropriate ISS. During this ISS, the 8051 can only be interrupted by a higher priority level inter¬
rupt. Thus, if the priority level is already 1, it cannot be interrupted till the ISS is completed. The ISS
ends with RETI instruction. The RETI instruction pops to PC from the stack top and additionally
informs the 8051 that the ISS is over, which causes the interrupt logic to accept additional interrupt at
the same priority level as the one just processed. If the ISS is ended with RET instead of RETI, it
would have left the interrupt control system thinking that the interrupt was still in progress.
Single-step operation of 8051: The interrupt structure of 8051 allows single-step execution of a
program in a simple way as described in the following. Let us say a key is connected to INTO* pin
such that when the key is not pressed, the INTO* pin will be in 0 state. Hence on pressing the key,
INTO* becomes 1, and on releasing the key later, it becomes 0 again. The key can be designated as
the ‘single-step’ key. Subsequently INTO* interrupt is enabled, INTO* is programmed to be level sen¬
sitive, and the ISS for INTO* is terminated with the following code to implement single-step facility.
Note that P3.2 bit corresponds to INTO* pin.
Although the INTO* pin is at 0 for most part of the single-step routine, it does not interrupt 805 1
because the same priority interrupt service (in fact, same interrupt) is in progress. Then at the end of
the ISS, it waits for pressing and releasing of ‘single step’ key. After execution of RETI at the end of
an ISS, one instruction in the interrupted program will be executed before the 805 1 again branches to
single step ISS.
There are two 16-bit timer registers in 8051 called TimerO and Timerl. These timers can be config¬
ured to work as times or event counters. In the timer mode, internal machine cycles are counted,
580
whereas in counter mode external events are counted. Thus the only difference between timer and
counter is the source for incrementing the timer register.
In the Timer mode the timer register is incremented for every machine cycle consisting of 12 crys¬
tal clock cycles. Thus if the 8051 uses 12-MHz crystal, the timer register is incremented once every
microsecond in the timer mode. This mode does not use the external timer input pin. The timer input
pin is more appropriately called as counter input pin in this book, although Intel refers to it as timer
8 input pin.
a>
o In the counter mode, the timer register is incremented for every 1 to 0 transition at the external
sa counter input pin. Every 1 to 0 transition at the external counter input pin is treated as an event. The
s
.2 external counter input pin is sampled once every machine cycle. Hence, to recognize a 1 to 0 transi¬
S tion, atleast two machine cycles are needed. Thus if the crystal frequency is 12 MHz, the maximum
in
00 count frequency will be 12 MHz/24 = 500 kHz. In other words, for event counting the time duration
s between events must be atleast 2 p.s.
Jj In addition to selecting as timer or counter, there are four different modes of operation for these
H
timers/counters. Modes 0, 1, and 2 are the same for both the timers/counters, while mode 3 has differ¬
ent meanings for the two timers/counters. The TMOD register is programmed to configure these
timers/counters. Whenever serial port is used for serial communication, specifically in mode 1 and
mode 3, Timer1 is used for generating the baud rate. In such a case only TimerO is available for
timer/counter operation. Details about serial port are provided later in this chapter.
TMOD is an SFR with address 89H, and is non-bit addressable. TMOD is abbreviation for Timer
Mode. The bit details of TMOD register and the circuit that controls the running of a timer are pro¬
vided in Fig. 30.6.
Decides Timerl mode Decides TimerO mode
Gate C/T* Gate
Ml M0 C/T* Ml M0
(G) (G)
Crystal 4- 12
osc. [C/T* = 0
To timer increment input
Counter input pin TC/T* = 1 Run control
1 => Run
TR bit (in TCON1
0 => Stop
Fig. 30.6
Gbit Details of TMOD
register and run
1NT* input pin control of a timer
The user programs all the 8 bits of TMOD, as they are meant for control and not for obtaining sta¬
tus information. The LS 4 bits of TMOD are provided for control of TimerO, and the MS 4 bits of
TMOD for control of Timerl, discussed as follows.
sI
Gate: 0 = TimerX (X = 0 or 1) will be in run mode only if
TRX bit (in TCON register) = 1 (software control)
1 = TimerX will be in run mode only if TRX = 1 5' n
and INTX* pin = 1 (hardware control)
2?
2.
This meaning for gate bit is true only when the timer/counter is in mode 0, 1, or 2. The meaning of o
the gate bit for TimerO and Timerl when they are in mode 3 is described later. It is to be noted that in
the hardware control of running the timer/counter, external INTX* pin is used. As such, INTX* pin
will not be used as an interrupt pin and hence INTX* interrupt should be disabled using IE register.
Example 1
Assume it is desired to use TimerO as a 16-bit event counter, and Timerl as an 8-bit auto reload timer.
Further, running of these timers is to be controlled by software, then we have to execute the follow¬
ing instruction.
MOV TMOD, #25H; Load TMOD with binary 0 0 10 0 1 01
Timerl is configured with gate = 0, C/T* = 0, and Ml M0 = 10 indicating that Timerl works as a
timer in mode 2 (8-bit auto reload mode). Also as gate = 0, the timer starts running when TR1 is set
to 1 by software. TimerO is configured with gate = 0, C/T* = 1, and Ml M0 = 01 indicating that TimerO
works as a counter in mode 1 (16-bit counter mode). Also as gate = 0, the timer starts running when
TR0 is set to 1 by software.
Example 2
Suppose it is desired to use TimerO as a 16-bit event counter, and Timerl as an 8-bit auto reload timer.
Further, running of these timers is to be controlled by hardware, then we have to execute the follow¬
ing instruction.
MOV TMOD, #0ADH; Load TMOD with binary 1 0 10 1 1 01
Also execute instruction to make TR1 = 1 and TR0 = 1 in TCON register. Now Timerl is configured
with gate = 1, C/T* = 0, and Ml M0 = 10 indicating that Timerl works as a timer in mode 2 (8-bit
auto reload mode). Also as gate = 1, the timer starts running when external INTI* input is at logic 1,
providing hardware control.
TimerO is configured with gate = 1, C/T* = 1, and Ml M0 = 01 indicating that TimerO works as a
counter in mode 1 (16-bit counter mode). Also as gate = 1, the timer starts running when external
INTO* input is at logic 1, providing hardware control. In this example, INTI* and INTO* inputs are
not used as external interrupt input pins but used to control the running of Timerl and TimerO by an
external hardware.
In this case for every 25 = 32 events (in the case of counter operation) or for every 32 machine cycles
8 (in the case of timer operation), the TH1 register is incremented by 1. Finally when TH1 register
a> overflows from FFH to 00, TF1 flag in TCON register is set to 1, and the timer/counter stops. As an
o example, if TH1 is loaded with FOH, the TF1 flag will be set to 1 after 10H X 32 = 512 machine
sa cycles in the timer mode 0. If the crystal frequency is 12 MHz, this happens after 512 p.s. The execu¬
s
.2 tion of the following instructions generate an interrupt 512 p.s after Timerl starts running.
S
in
00 MOV TMOD, #00H ; Timerl in Timer mode 0, with Gate = 0
s MOV TH1, #0F0H ; TH1 (MS byte of Timerl) loaded with FOH
Jj MOV IE, #88H ; Enable Timerl interrupt
H SETB TR1 ; Set TR1 to 1 in TCON register. Start timer.
In this case, for every event (in the case of counter operation) or for every machine cycle (in the case
of timer operation), the TH0-TL0 register pair is incremented by 1. Finally when TH0-TL0 register
pair overflows from FFFFH to 0000, TF0 flag in TCON register is set to 1, and the timer/counter stops.
As an example, if TH0-TL0 pair is loaded with FFF0H, the TF0 flag will be set to 1 after 10H = 16
machine cycles in the timer mode 1. If the crystal frequency is 12 MHz, this happens after 16 p.s. The
execution of the following instructions generate an interrupt 16 JJLS after TimerO starts running.
Timerl in mode 2 is used for generating the desired baud rate for communication when the serial port
is working in mode 1 or mode 3. This aspect will be discussed later.
Crystal
-t-12 THO Set TF1 Cause Timerl
osc.
interrupt if
Run control conditions are met Fig. 30.10
TR1 1 => Run Mode 3 opera¬
0=> Stop tion of TimerO
When Timerl is operated in mode 3, it simply holds count and stops running. So when TimerO is
in mode 3, Timerl is generally configured to work in mode 0, 1, or 2. However, Timerl will not be in
a position to interrupt the 8051, as the TF1 flag (Timerl overflow) is used by THO timer. Timerl is
used as Baud rate generator by the serial port very commonly.
584
The meaning of the gate bit for TimerO and Timerl when they are in mode 3 is as follows. Gate bit
for TimerO in mode 3 controls the running of the 8-bit timer/counter TLO, as in the case of mode 0, 1,
or 2. The running of the 8-bit timer THO is controlled by TR1 bit only. The gate bit for Timerl in mode
3 does not have any role, as Timerl in mode 3 only holds count and stops running.
Mode 3 is provided for applications requiring an extra 8-bit timer/counter. When TimerO is in
mode 3, the 8051 has one 8-bit timer using THO, one 8-bit timer/counter using TLO, and one 16-bit
8 timer/counter using Timerl. It is to be noted that with TimerO in mode 3, whenever Timerl operates
a>
o in mode 0, 1, and 2 the run control for Timerl is activated when gate bit = 0 or INTI* external input
sa is a logic 1. The run control is deactivated when gate = 1 and INTI* = 0. It is also deactivated when¬
s
.2 ever Timerl mode is set to 3.
S
in
00
s
J!H ■ 30.3 SERIAL INTERFACE
The serial port of 8051 can operate in four modes and the operational mode is decided by SM0 and
SM 1 bits of SCON register. SCON is the abbreviation for serial control. SCON is an SFR with address
98H, and is bit addressable. The bit details of SCON register, which is provided in Fig. 30.4, is repro¬
duced again in Fig. 30.11.
Fig. 30.11
SM0 SMI SM2 REN TB8 RB8 TI RI Bit details of SCON register
In this mode the serial data bit stream coming into the 8051 on RXD input pin (P3.0 pin) is received.
The data is received treating every 8 bit as one unit. A TXD output pin (P3.1 pin) is used to provide
the receive clock. The clock frequency sent out by 805 1 on this pin is a fixed frequency of fill, where
/is the crystal frequency. Thus, if the crystal frequency is 12 MHz, a bit is received on RXD pin once
every microsecond.
The 805 1 has SBUF (serial buffer) register, which is used for serial communication. It is an SFR
with address 99H, and is not bit addressable. The SBUF is connected internally to serial port receive
register and serial port transmit register, which are not directly accessible by the programmer. The
SBUF register is shown in Fig. 30.12. Whenever the 8051 reads the SBUF register, it actually reads
from serial port receive register. Similarly, when 805 1 writes to SBUF, it will actually be written to
serial port transmit buffer.
Reception is initiated when REN (receive enable) bit in SCON = 1, and RI flag bit in SCON = 0.
The bits received are accumulated in the serial port receive register, starting with the LS bit. From the
point of view of the programmer there is SBUF register, and not serial port receive register. Whenever
SBUF is read, it is actually done from serial port receive buffer. Once all the 8 bits are received, the
585
In this program, a RAM buffer from location 20H is used to store the data to be output to the expan¬
sion ports. An advantage of the RAM buffer approach is that if the RAM buffer is chosen to reside in
on-chip bit-addressable data RAM, then the user can set, clear or test each bit of an expansion port by
simply setting, clearing, or testing the corresponding bit in the RAM buffer.
Similarly, parallel to serial shift registers are used for realizing additional input ports. The input
mode uses RXD as the serial data input, and TXD as the bit-shifting clock for the parallel to serial
shift register. Also an additional output line of 805 1 is used to strobe data from the parallel inputs of
the external shift register. Figure 30.14 depicts the implementation of an external input port.
To implement more input ports, the ‘serial in’ of the first input port must be connected to the ‘serial
out’ of the second input port and so on. A typical subroutine to input data from the expansion input
port is left as an exercise to the reader.
587
30.3.3 MODE 1 OF UART
Mode 1 is used for asynchronous data transfer. It is a full duplex mode, which means that it can trans¬
mit and receive simultaneously. RXD input pin is used for receiving the serial data and accumulating
it in the SBUF (actually in serial port receive register). It receives the start bit (which is logic 0), then
sI
the 8-data bits (bits 0 to 7) starting from the LS bit, and finally the stop bit (which is logic 1). The
received start bit is ignored, the data bits 0 to 7 are loaded in SBUF, and the stop bit is loaded into
RB8 (bit 8 of received data) of SCON register. 5' n
In this mode the serial data bit stream coming into the 8051 on RXD input pin at the programmed
baud rate is received. Its value is determined by the Timer1 (never TimerO) overflow rate. Generally 2?
2.
mode 2 of Timer1 (auto reload mode) is used for baud rate generation. The Timer1 interrupt should O
be disabled in this application. The Timerl can be configured for ‘timer’ or ‘counter’ operation, but
generally it is configured for timer mode. The baud rate is given by the formula
2SMOD X f/[32 X 12 X (256 — TH1)]
where TH1 is the content of MS byte of Timerl register and SMOD is the MS bit in PCON (power
control) register. PCON is an SFR with address 87H, and is not bit addressable. Whenever SMOD
bit = 1, the baud rate is doubled if the serial port is configured for mode 1, 2, or 3. The SMOD bit has
no effect on the baud rate of serial port when it is configured for mode 0. More details about PCON
register is provided later. Table 30. 1 provides the values of baud rates obtained using the above
formula when MS nibble of TMOD = 0010 (Timerl as ‘timer’ in mode 2).
12 1 FFH 62,500
11.059 1 FDH 19,200
11.059 0 FDH 9600
11.059 0 FAH 4800
11.059 0 F4H 2400
11.059 0 E8H 1200
Reception is initiated by the incoming start bit if REN bit = 1. Once all the 8-data bits and the stop
bit are received, the RI flag is set to 1 by the 8051. In mode 1 operation of UART, if SM2 bit of SCON
register is set to 1, then RI bit will not be set to 1, if RB8 = 0. As RB8 holds the stop bit in this mode, it
implies that RI bit will not be set to 1 when SM2 = 1 unless a valid stop bit is received by the UART.
The user can perform a status check of the RI bit to perform the read operation from SBUF. In such
a case, the user has to disable the serial port interrupt. Alternatively, the RI bit can be allowed to inter¬
rupt the 805 1, which results in a branch to ISS at 0023H provided that the usual conditions for branch¬
ing to an ISS are statisfied. The RI flag must be cleared by software at the end of the ISS.
The TXD output pin is used for transmitting the data available in SBUF (actually in serial
port transmit register) in serial fashion. It transmits the start bit (which is logic 0), then the 8-data bits
(bits 0 to 7) starting from the LS bit, and finally the stop bit (which is logic 1). Transmission is
initiated by any instruction that uses SBUF as a destination register.
Once all the 8-data bits and the stop bit are transmitted, the TI flag is set to 1 by the 8051. The user
can perform a status check of this bit to perform the next write operation to SBUF. In such a case, the
user has to disable the serial port interrupt. Alternatively, the TI bit can be allowed to interrupt the
588
8051 which results in a branch to ISS at 0023H provided the usual conditions for branching to serial
port ISS are satisfied. The TI flag must be cleared by software at the end of the ISS.
Transmission is initiated by any instruction which uses SBUF as a destination register, for example,
‘MOV SBUF, A’. In this mode it transmits the serial data on TXD output pin at the programmed baud rate.
Its value is determined by the Timer1 (never TimerO) overflow rate, as was described earlier for receive
s operation.
a>
o
sa 30.3.4 MODE 2 OF UART
2
.2
s Mode 2 is also used for asynchrononus data transfer in full duplex mode. RXD input pin is used for
in
oo receiving the serial data and accumulating it in the SBUF (actually in serial port receive register). It
s receives the start bit (which is logic 0), then the 8-data bits (bits 0 to 7) starting from the LS bit, a pro¬
Jj grammed bit 8, and finally the stop bit (which is logic 1). The received start bit is ignored, the data
H
bits 0 to 7 are loaded in SBUF, the programmed bit 8 is loaded into RB8 (bit 8 of received data) of
SCON register and the stop bit is ignored.
In this mode it receives the serial data bit stream coming into the 8051 on RXD input pin at the
baud rate of f/32 or f/64, where f is the crystal frequency. When SMOD bit in PCON register is 0, the
baud rate will be f/64. When SMOD = 1, the baud rate is doubled to f/32.
Reception is initiated by the incoming start bit if REN bit = 1. Once all the 8-data bits, RB8 bit,
and stop bit are received, the RI flag is set to 1 by the 8051. In mode 2 operation of UART, if SM2 bit
of SCON register is set to 1, then the RI bit will not be set to 1, if RB8 = 0. This feature is useful in
multiprocessor communication, as will be described later.
The user can perform a status check of the RI bit to perform the read operation from SBUF. In such
a case, the user has to disable the serial port interrupt. Alternatively, the RI bit can be allowed to inter¬
rupt the 805 1 which results in a branch to ISS at 0023H provided that the usual conditions for branch¬
ing to an ISS are satisfied. The RI flag must be cleared by software at the end of the ISS.
The TXD output pin is used for transmitting the data available in SBUF (actually in serial port trans¬
mit register) in serial fashion. It transmits the start bit (which is logic 0), the 8-data bits (bits 0 to 7)
starting from the LS bit, then TB8 bit of SCON register, and finally the stop bit (which is logic 1).
Transmission is initiated by any instruction that uses SBUF as a destination register.
Once all the 8-data bits, TB8 bit and the stop bit are transmitted, the TI flag is set to 1 by the 805 1.
The user can perform a status check of this bit to carry out the next write operation to SBUF. In such
a case, the user has to disable the serial port interrupt. Alternatively, the TI bit can be allowed to inter¬
rupt the 805 1 which results in a branch to ISS at 0023H provided the usual conditions for branching
to serial port ISS are satisfied. The TI flag must be cleared by software at the end of the ISS.
The TB8 bit can be loaded by the programmer with a value of 0 or 1, which will be useful in
multiprocessor communication, and will be described later. Alternatively, the P flag bit in PSW register
can be moved to TB8 bit, which is useful for transmitting data with even parity. In mode 2, the UART
transmits the serial data on TXD output pin at the baud rate of f/32 or f/64, based on SMOD bit value.
Mode 3 is also used for asynchronous data transfer in full duplex mode. It is the same as mode 2,
except the baud rate. The baud rate in mode 3 is variable, like in mode 1.
The RXD input pin is used for receiving the serial data and accumulating in the SBUF (actually
in serial port receive register). It receives the start bit (which is logic 0), then the 8-data bits (bits 0 to 7)
starting from the LS bit, a programmed bit 8, and finally the stop bit (which is logic 1). The received
589
start bit is ignored, the data bits 0 to 7 are loaded in SBUF, the programmed bit 8 is loaded into RB8
of SCON register, and the stop bit is ignored.
In this mode it receives the serial data bit stream coming into the 8051 on RXD input pin at the
programmed baud rate. Its value is determined by the Timerl (never TimerO) overflow rate. Generally
mode 2 of Timerl (auto reload mode) is used for baud rate generation. The Timerl interrupt should
be disabled in this application. The Timerl can be configured for ‘timer’ or ‘counter’ operation, but
generally it is configured for timer mode. The baud rate is given by the formula s
5' n
2SMOD X f/[32 X 12 X (256 — TH1)]
where TH1 is the content of MS byte of Timerl register and SMOD is the MS bit in PCON register. 2
Reception is initiated by the incoming start bit if REN bit = 1. Once all the 8-data bits, RB8 bit, and 2.
o
stop bit are received, the RI flag is set to 1 by the 8051. In mode 3 operation of UART, if SM2 bit of
SCON register is set to 1, then RI bit will not be set to 1, if RB8 = 0. This feature is useful in multi¬
processor communication, as will be described later.
The user can perform a status check of the RI bit to perform the read operation from SBUF. In such
a case, the user has to disable the serial port interrupt. Alternatively, the RI bit can be allowed to inter¬
rupt the 8051 which results in a branch to ISS at 0023H provided that the usual conditions for branch¬
ing to an ISS are satisfied. The RI flag must be cleared by software at the end of the ISS.
The TXD output pin is used for transmitting the data available in SBUF (actually in serial port trans¬
mit register) in serial fashion. It transmits the start bit (which is logic 0), the 8-data bits (bits 0 to 7)
starting from the LS bit, then TB8 bit of SCON register, and finally the stop bit (which is logic 1).
Transmission is initiated by any instruction that uses SBUF as a destination register.
Once all the 8-data bits, TB8 bit, and the stop bit are transmitted, the TI flag is set to 1 by the 8051.
The user can perform a status check of this bit to perform the next write operation to SBUF. In such
a case, the user has to disable the serial port interrupt. Alternatively, the TI bit can be allowed to inter¬
rupt the 8051 which results in a branch to ISS at 0023H provided the usual conditions for branching
to serial port ISS are satisfied. The TI flag must be cleared by software at the end of the ISS.
The TB8 bit can be loaded by the programmer with a value of 0 or 1, which will be useful in
multiprocessor communication, and will be described later. Alternatively, the P flag bit in PSW
register can be moved to TB8 bit, which is useful for transmitting data with even parity. In mode 3,
serial data is transmitted on TXD output pin at the programmed baud rate. Its value is determined by
the Timerl (never TimerO) overflow rate, as was described earlier for receive operation.
Multiprocessor communication: Modes 2 and 3 of UART provide facility for multiprocessor commu¬
nications. In these modes data bits 0 to 7 are received in SBUF, and one more bit, called RB8, is received
in RB8 bit of SCON register. The UART can be programmed so that RI bit of SCON will be set to 1,
causing an interupt to the 8051, only if RB8 = 1. This is done by setting to 1 the SM2 bit in SCON.
When the master 805 1 wants to transmit a block of data to one of the several 805 1 slaves, it first
sends out an address byte that identifies the target slave. For an address byte RB8 bit will be a 1. For a
data byte the RB8 bit will be a 0. With SM2 = 1 in the SCON register of each of the slave 8051s, no
slave 8051 will be interrupted by a data byte. An address byte will interrupt all slave 8051s. Now each
of the slaves can examine the received address byte, and check whether the address is meant for that
particular slave. The addressed slave will clear its SM2 bit to 0. The other slaves do not clear the SM2
bit to 0. Thus from now on only the selected slave will receive the data bytes sent by the master.
So far we have discussed in detail the various blocks of 805 1. Armed with this knowledge, the detailed
architecture of 8051 as shown in Fig. 30.15(a) can be understood easily.
590
P0.7-0/AD7_0 P2.7-0/A15.8
(a)
\y Xy
Port 0 Port 2
Vcc = +5 V *-
y: drivers drivers
GndiT •2 /X 4N '
TA7-0 'Th If A15.8
■« .2 D7-0
-O
O
w
w RAM 128
XJ/
Port 0 latch
IE
Port 2 latch 4K bytes
0)
o
< bytes (SFR) (SFR) EPROM
0 A
a / \
o / \JJ/ w \y \y
.2
2 / \
w Register A \y xy MAR
00 (SFR)
O Stack
00 Temp 1
0) / pointer (SFR)
\ /
£ Addr.
Register B
buffer reg. <=>
H Temp 2
(SFR)
I N /
XTAL1 — !ÿ XTAL2
\>P1.7-0 \ J/P3.7-0/AF
‘Read latch’
(b)
Internal
bus ——
< <> >D
Pn.x
Q
—
4 >
Out put
driver
Pn.x n=0...3
Latch bit pin x=0...7
‘Write to circuit
Clk Q
latch’
Fig. 30.15 (a) Detailed architecture of 8051; (b) Internal structure common to all ports
591
■ 30.4 STRUCTURE AND OPERATION OF PORTS
All the four ports— PAO, PA1, PA2, and PA3 are bi-directional. Each port circuit consists of a latch,
an output driver, and an input buffer. The latches are bit-addressable SFRs with addresses 80H, 90H,
sI
AOH, and BOH for the ports PO, PI, P2, and P3 respectively.
The PI driver receives the data to be sent out from PI latch. The PO and P2 drivers receive the
information to be sent out either from the port latch or from the memory address register (MAR). 5' n
The P3 driver receives information from P3 latch or alternate functions. This can be seen from
Fig. 30.15(a), which provides the architecture of 8051. 2?
2.
If external program memory is being accessed to fetch a byte of instruction code, the program o
counter supplies the address to MAR. The MAR register in turn supplies the address to be sent out to
the PO and P2 drivers. Let us say external program memory is being accessed to fetch a byte from a
look up table, using indexed addressing. Then the address buffer register will have the computed value
of desired memory address. Hence the address buffer register supplies the address to MAR, which in
turn supplies the address to be sent out to the PO and P2 drivers.
If external data memory is being accessed, using Ri (i = 0 or 1) in register indirect addressing mode,
then Ri supplies address buffer register (ABR) with LS byte of address to be sent out. From ABR it is
passed on to MAR, which in turn sends it to PO driver. The P2 latch supplies the MS byte of address
to P2 driver. If external data memory is being accessed, using DPTR in register indirect addressing
mode, then DPTR supplies MAR with address to be sent out, which in turn sends it to PO and P2
drivers.
In all the above cases, the PO pins are used for both sending out LS byte of address and
sending/receiving 8-bit data. So these pins are being used as AD7_0 pins in time multiplexed mode. The
ALE output from 805 1 indicates whether address or data is present on AD7_0 pins. ALE stands for
‘address latch enable’. If logic 1 is sent out on ALE, it means 805 1 is sending out LS byte of address
on P0 pins. An external latch is used to latch this LS byte of address.
In the case of external program memory access, the 8-bit code is received on P0 pins. This infor¬
mation comes to internal bus, and from there is routed to the instruction register (IR), if it is the first
or only byte of the instruction. If the data received is the second or third byte of an instruction, they
are received in TMP1 and TMP2 registers. IR TMP1, and TMP2 registers are not accessible to the
user. In the case of external data memory access, the 8-bit data is received on P0 pins. This informa¬
tion comes to internal bus, and from there is routed to A register.
All the four port structures are very similar. They differ only in the output driver circuit portion. The
internal structure common to all the ports is shown in Fig. 30.15b. The output driver circuit portion
for the four ports are shown in Figs. 30.16 to 30.19. The internal structure common to all the ports is
discussed in this section. The latch bit, which is a bit of the port SFR, is implemented using D-type
flip-flop. When the internal ‘write to latch’ signal is activated, the latch bit is written with the data bit
present on D input, which is connected to the internal bus. The output of the flip-flop will be placed
on the internal bus in response to internal ‘read latch’ signal. The level of the port pin itself is placed
on the internal bus in response to internal ‘read pin’ signal.
In some instructions the port latch information is read, and in some others the port pin information
is read. When just reading of the port is desired, it is done from the port pins. If it is desired to read,
then modify, and finally write, the information will be read from the port latch. Such instructions will
592
have the destination operand as a port or a port bit. The list of such ‘read-modify-write’ (RMW)
instructions is provided as follows.
ANL (EX. ANL PI, A)
ORL (EX. ORL PI, A)
XRL (EX. XRL PI, A)
s
M
JBC (EX. JBC Pl.l, LOCN)
CPL (EX. CPL PI.5)
0
sa INC
DEC
(EX.
(EX.
INC PI)
DEC PI)
2 DJNZ (EX. DJNZ PI, BACK)
s MOV (EX. MOV PI.5, C)
U)
00 CLR (EX. CLR PI.5)
s SETB (EX. SETB PI.5)
Jj
H For example, in ‘ANL PI, A’ instruction, the 8051 CPU has to read Plvalue, then modify it by
ANDing it with A contents, and finally write it back to PI. Thus it is a RMW instruction, and so the
port latch is read in this instruction. In the ‘Mov PI.5, C’ instruction, the RMW feature is not obvi¬
ous. Flowever, the instruction reads all the 8 bits of PI first, then modifies bit 5 of the port with C
value, and then writes the 8 bits back to the port.
The instruction ‘MOV A, PI’ is not an RMW instruction, as the destination is not a port. So in this
case, A register is loaded with PI pin information. The reason that RMW instructions access the latch
rather than the pin is to avoid a possible misinterpretation of the voltage level at the pin. For example,
a port bit might be used to drive the base of a transistor. When a 1 is written to the latch bit, the tran¬
sistor is turned ‘on’. Now if the 8051 reads at the pin rather than the latch, it will read the base volt¬
age of the transistor and misinterpret it as a 0. But reading the latch will return the correct value of 1.
f
Mux
tj-FL (Lower FET)
Q
Fig. 30.16
Mux O/P Output driver circuit
X = a value between 0 to 7
AD = 1 =$ÿ Addr/Data of a pin of P0
= 0=> Q
There is a 2 to 1 multiplexer in P0. One of the inputs is from the complement of a bit in the port
latch, while the other input is from an Addr/Data bit of MAR after inversion. If the AD internal con¬
trol signal shown in the figure is a logic 1, the multiplexer outputs the Addr/Data. If P0 is being used
as Addr/Data pins, the internal AD signal will be logic 1. If the Addr/Data bit is at logic 1, the upper
FET is on, and the lower FET is off. Thus a 1 is sent out as Addr/Data on the port pin. If the Addr/Data
593
bit is at logic 0, the upper FET off, and the lower FET is on. Thus a 0 is sent out as Addr/Data on the port
pin. It is to be noted that there is no need for any external pull up when P0 is being used as Addr/Data pins.
While reading the code byte from external program memory, or reading a data byte from external
data memory, the internal AD control signal will be a 0. With the port latch bit at logic 1, both the
upper and lower FETs are off. Flence the port pin floats. Now the port pin will be used to receive the
sI
code or data byte and the ‘read pin’ will be activated instead of ‘read latch’, so that the read will be
from the port pin. To facilitate this read operation, the P0 latch will always be written with Is during
access to external program or data memory. 5' n
The P0 pins can be used for I/O purposes only if the external program memory or external data
memory is not present in the system. In such a case the internal AD control signal will be at logic 0. 2?
Then the multiplexer outputs the complement of port latch bit. If the latch bit is at logic 1, the upper 2.
o
FET is off, and the lower FET is also off. Flence the port pin floats. Thus it is seen that P0 does not
have internal pull ups when it is used for I/O purposes. So external pull-up resistors have to be used
for output operation. Similarly, when the latch bit is at logic 0, the upper FET remains off, and the
lower FET is turned on. So the port pin outputs logic 0. If it is desired to use P0 pin as an input pin,
the port latch should be written with a 1. Then both the upper and lower FETs are off. Flence the port
pin floats, and now it can be used as an input pin.
ADDR AD Vcc
1
Internal
pull up.
Q
—t »|P2.X|
'
FL
Fig. 30.17
AD = 1 •ADDR is Mux o/p Output driver circuit
0 • Q is Mux o/p of a pin of P2
This port uses an active internal pull-up circuit using FETs. The details of this active pull up are
not shown for simplicity. There is a 2 to 1 multiplexer in P2. One of the inputs is from a bit of the port
latch. The other input is from an Addr bit of MAR. If the AD internal control signal shown in the
figure is at logic 1, the multiplexer outputs the Addr. If P2 is being used as Addr pins the internal AD
signal will be logic 1. If the Addr bit is a logic 1, the FET is off and the internal active pull up circuit
will pull up the port pin to logic 1. Thus a 1 is sent out as Addr on the port pin. If the Addr bit is at
logic 0, the internal pull-up circuit will not be active, and the FET is on. Thus a 0 is sent out as address
on the port pin. During external memory access, the P2 latch value remains unchanged.
The P2 pins can be used for I/O purposes only if the external program memory or external data
memory is not present in the system. In such a case the internal AD control signal will be at logic 0.
Then the multiplexer outputs the port latch bit. If the latch bit is at logic 1, the FET is off, and the inter¬
nal active pull up circuit pulls up the pin output to logic 1. Similarly, when the latch bit is at logic 0, the
FET is turned on, and the internal active pull up will not be active. Hence the port pin outputs logic
0. Thus no external pull-up resistors are needed for output operation, whereas for output operation
with P0 an external pull-up resistor is needed.
594
If it is desired to use P2 pin as an input pin, the port latch should be written with a 1. Then the FET
is off, and the internal pull-up circuit pulls up the port pin to logic 1. Now the port pin can be used as
an input pin. If the external input is at logic 0, the port pin is externally pulled low by this operation.
This port also uses an active internal pull-up circuit using FETs. The details of this active pull up are
not shown for simplicity. This port is always available for I/O operations immaterial of the existence
of external program memory and data memories.
If the latch bit is at logic 1, the lower FET is ‘off’, and the internal active pulls up circuit pulls up the
pin output to logic 1. Similarly, when the latch bit is at logic 0, the lower FET is turned ‘on’, and the inter¬
nal active pull up will not be active. So the port pin outputs logic 0. Thus no external pull-up resistors are
needed for output operation, whereas for those with port 0, external pull-up resistors are needed.
If it is desired to use PI pin as an input pin, the port latch should be written with a 1. Then the lower
FET is ‘off’, and the internal pull-up circuit pulls up the port pin to logic 1. Now the port pin can be used
as an input pin. If the external input is at logic 0, the port pin is externally pulled low by this operation.
This port also uses an active internal pull-up circuit using FETs. The details of this active pull up are
not shown for simplicity. A pin of P3 can be used for I/O operation, if it is not needed for an alternate
function. The alternate functions of the P3 pins are listed as follows.
595
P3.7 RD* (RD* output for reading from external data memory);
P3.6 WR* (WR* output for writing to external data memory);
P3.5 Cl (counterl external input);
P3.4 CO (counterO external input);
P3.3 INTI * (external interrupt 1 input);
P3.2 INTO* (external interrupt 0 input);
P3. 1 TXD (transmit data output from serial port); s
P3.0 RXD (receive data input for serial port). 5' n
In P3, pins 7 and 6 are used as the alternate output function (AOF) signals RD* and WR* for exter¬
2?
2.
nal data memory, whenever external data memory access is performed. For a P3 pin to be used for o
generating AOF signal, the corresponding port latch bit must be set to 1. Then the AOF internal sig¬
nal comes out on the port pin. In case the latch bit is at logic 0, the port pin will be stuck at logic 0,
which can be easily seen from the figure for P3 structure.
Assume we want a P3 pin to be used as an alternate input function (AIF) signal. For example,
we may want pin 2 of P3 to be used as external interrupt 0 input. Then the corresponding port latch
bit must be set to 1. Also the internal AOF signal must be at logic 1. Then the lower FET is ‘off’, and
the internal pull-up circuit pulls up the port pin to logic 1. Now the port pin can be used as an AIF pin.
If the external input is a logic 0, the port pin is externally pulled low by this operation. The 8051
senses the value of this external AIF signal by sensing the internal AIF signal, as shown in the figure
for P3 structure.
If a P3 pin is not needed for alternate function, the port pin can be used for I/O purposes.
In such a case the internal AOF signal will be at logic 1. If the latch bit is at logic 1, the lower FET
is ‘off’, and the internal active pull-up circuit pulls up the pin output to logic 1. Similarly, when
the latch bit is at logic 0, the lower FET is turned ‘on’, and the internal active pull up will not be
active. Hence the port pin outputs logic 0. Thus no external pull-up resistors are needed for output
operation.
If it is desired to use P3 pin as an input pin, the port latch should be written with a 1. The internal
AOF signal will be at logic 1. Then the lower FET is ‘off’, and the internal pull-up circuit pulls up the
port pin to logic 1. Now the port pin can be used as an input pin. If the external input is at logic 0, the
port pin is externally pulled low by this operation.
8051 is typically implemented using HMOS technology, and is available as 805 1H. However, the
CHMOS version is also available as 80C5 1. The CHMOS version is used in applications where power
consumption is critical, like data collection in a remote place. The CHMOS version provides two
power-reducing modes called the idle mode and the power down mode. The typical current drain from
the power supply for 80C51 in the various modes are indicated as follows.
The chip is programmed to work in idle mode by setting IDL bit of PCON register to 1. PCON is
abbreviation for power control. PCON is an SFR with address 87H and is not bit addressable. The bit
details of PCON register is provided in Fig. 30.20. Note that in FIMOS devices, only SMOD bit, which
s is abeady discussed, is implemented in the PCON register.
a>
o
sa
2 SMOD X X X GF1 GF0 PD IDL Fig. 30.20
s Bit details of PCON register
in
00
s« Immediately after the IDL bit is set to 1 by an instruction, the device goes to the idle mode. In the
idle mode the clock signal is not provided to the CPU core portion. Flowever, the oscillator continues
H
to run, providing the clock to the interrupts, serial port, and timers. This can be seen from Fig. 30.21.
Thus the power consumption is greatly reduced, although supply voltage has to be maintained at
+5 V. Typically the current drain from the power supply will be only 2.5 mA in idle mode as against
18 mA in normal working mode.
Oscillator
•To serial port, timers
— o®-
PD bit
and interrupt logic of 805 1
•To 8051 core portion (CPU) Fig. 30.21
of PCON register
Circuit details of
IDL bit of PCON register power saving modes
In this mode the program counter, SP, PSW, accumulator, and all other register maintain their data.
In other words, the CPU status is preserved fully. The port pins hold the logic values they had at the
time the 805 1 went to idle mode. The ALE and PSEN* will be held at logic 1. There are two ways to
come out of the idle mode. If a valid interrupt occurs, the IDL bit is reset to 0 by the 805 1 automati¬
cally. Hence the 805 1 comes out of the idle mode. It first services the interrupt and then proceeds with
the next instruction after the one that put the 805 1 into idle mode.
The action to be taken in the ISS may depend on whether the interrupt occurred during normal
operation, or in idle mode. This can be taken care of by the two general-purpose flag bits GF1 and
GF0 in PCON register. They can be used as truly general-purpose flags, if desired. However, one of
these bits is commonly used to indicate if an interrupt occurred during normal operation, or in idle
mode. For example, the instruction that sets IDL bit to 1, can also set GF0 to l.When the idle mode
is terminated by an interrupt, the ISS can check the GF0 bit to take appropriate action. The second
method of coming out of idle mode is with a hardware reset. The IDL bit is cleared to 0 when reset.
The reset redefines all the SFRs, but does not change the on-chip RAM values.
The chip is programmed to work in power down mode by setting PD bit of PCON register to 1. Imme¬
diately after PD bit is set to 1 by an instruction, the device goes to the power down mode. In the power
597
down mode the clock is not provided to any portion of the 8051. Thus all functions within the 8051
are halted. This can be seen from Fig. 30.21. Flence the power consumption becomes extremely low.
But the on-chip RAM and SFR values are held. The port pins output the values held by their respec¬
tive SFRs. The ALE and PSEN* will be at logic 0. Typically the current drain from the power supply
will be only 3 |xA in power down mode as against 2.5 mA in idle mode.
sI
There is only one way to come out of the power down mode. It is by hardware reset of 8051. The
PD bit is cleared to 0 when reset. The reset redefines all the SFRs, but does not change the on-chip
RAM values. In the power down mode, the power supply can be reduced to as low as 2 V, but care 5' n
must be taken to see that power supply is not reduced before going to power down mode. Also power
supply should be restored to 5 V before coming out of power down mode. 2?2.
o
As noted in the previous chapter, 8751 is the EPROM version of 8051. To program the EPROM in
8751, the chip must be running with a 4-6-MFIz oscillator. This is because, the internal bus will be
used to transfer address and program data to appropriate internal registers. The details of programming
of the EPROM vary depending on the 8751 version. They differ with regard to programming voltage
value, the width of program pulse, EPROM security features and so on. As such, only programming
of the EPROM in 8751BFI is described here as a typical example. For other versions of 8751, the
reader is advised to go through the manuals of Intel.
The 12-bit address of an EPROM location to be programmed is applied to LS 4 pins of P2 and 8
pins of PI. The data to be written to that location is applied to P0. The connection details for EPROM
programming is shown in Fig. 30.22.
8751 BH
Data to be
A7-0 PI P0 i
programmed
All-8 P2.3-0 EA*/VPP +12.75 V
ALE/PROG* Programming pulses (25 pulses
Reset*
of 100|xs duration, to program a byte)
1 P3.6 PSEN* 0
P3.7 P2.7 1
XTAL2 P2.6 0
4-6 MHz □ #
Fig. 30.22
XTALl Vcc +5 V
Connection details
Vss for EPROM
programming
The ALE pin, which is used as an output pin in normal operation, is used as PROG* (program
pulse) input during the programming of any part of EPROM, like programming code data, encryption
array or lock bits. The encryption array and lock bits are described a little later. The EA* pin, which
is tied to 5 V or Gnd during normal operation, is connected to the programming voltage Vpp during
programming any part of the EPROM. The programming voltage source should be well regulated and
free from glitches. Even a narrow glitch above the specified maximum for programming voltage can
cause permanent damage to the device.
598
The 8751BH uses programming voltage of 12.75 V with a margin of 0.25 V on either side. It uses
the faster ‘quick pulse’ programming algorithm. Programming of each byte is done using 25 PROG*
pulses, each of 100 p,s duration. The gap between each of these 25 PROG* pulses must be atleast
10 pis, as shown in Fig. 30.23. This results in a total programming time of about 13 s for the 4K bytes,
taking into account other set up times for programming a byte.
s 25 pulses
a>
o
sa
2
.2
ALE/PROG*
In many applications it is desirable that the program memory is secured from software piracy. To facil¬
itate such security 875 1BH is provided with two program memory lock schemes, which are encryption
array and program lock bits.
Encryption array: The EPROM of 875 1BH contains a 32-byte array that can be used for the encryp¬
tion of code data when verification of code data is attempted. The array is initially unprogrammed
(all Is). The user can program the array and for this purpose the address to be sent will be in the range
00 to 1FH to select an encryption byte. The other pins should be held as indicated in ‘program encryp¬
tion array’ row of Table 30.2.
The verification procedure sequentially XNORs each byte of the encryption array with a byte
of the code. When the last byte in the array is reached, the verification routine restarts with the
first byte of the array for the next code byte. Thus the verified code byte will be an encrypted version
of the programmed code byte. With the array programmed, the verified bytes can be properly
599
deciphered to obtain the actual programmed code bytes only if all the 32 bytes of encryption array are
correctly known.
If the encryption array is not programmed, the XNOR operation produces a verified code byte that
is the same as programmed code byte. Another thing to note is that if the programmed code byte is
FFH, the verify operation reveals the encryption byte. Thus, if a large block of EPROM is left unpro¬
grammed, the verify operation will reveal the complete encryption array. Flence it is necessary to
program unwanted portion of EPROM also with some random values, to ensure protection of the s
program. It is recommended that whenever encryption array is programmed, atleast one of the program 5' n
lock bits is also programmed to ensure stiffer protection. It is to be noted that if both the program lock § °-
bits are programmed, the on-chip program memory cannot be read out for the purpose of verification. 2o
2.
o
Program lock bits: Two program lock bits are provided in the EPROM of 8751BFI called LB1 and
LB2. If both these bits are unprogrammed, the program lock features are not enabled. Flowever, if
encryption array is programmed, verification process provides the program bytes in coded form.
To program the LB1 bit, the signals on the various pins should be held at the levels as indicated in
‘program lock bit 1’ row of Table 30.2. If only LB1 bit is programmed, MOVC instructions executed
from external program memory are disabled from fetching code byte from EPROM of 8751BFI. In
this mode, EA* is sampled and latched on reset. Thus, if the device is powered up without a reset, the
latch initializes to a random value, and holds that value until reset is activated. To ensure proper func¬
tionality of the chip, the internally latched value of EA* must tally with its external input. Also, fur¬
ther programming of EPROM in 8751BFI is disabled. In this mode, the verification of program code
is possible, but unauthorized users cannot easily decipher the program code, as it will be in encrypted
form, if the user programs the encryption array. To program the LB2 bit, the signals on the various
pins should be held at the levels as indicated in ‘program lock bit 2’ row of Table 30.2. If both LB1
and LB2 are programmed, the function is the same as in the case where only LB 1 is programmed,
except that even verification of program code is disabled.
Some versions of 8751, like 87C51, provide three program lock bits— LB1, LB2, and LB3. The
function of LB1 and LB2 is the same as in 8751BH. If all the 3 bits are programmed, it denies elec¬
trical access by any external means to the on-chip program memory. The device cannot be pro¬
grammed further and it cannot execute instructions in external program memory. Also, in 87C51, the
encryption array is 64 bytes long, double that of 875 1 BH. Erasing the EPROM deactivates all the lock
bits and the device can then be programmed again.
If both the program lock bits are programmed, the on-chip program memory cannot be read out for
the purpose of verification. In other cases it is possible to read out program memory contents for the
purpose of verification of code. The verification can be done after the programming of every byte, or
after the completion of the entire programming operation.
The address of the program memory location is applied to LS 4 pins of P2 and 8 pins of PI. The
PROG* pin should be held at logic 1 and the other pins as indicated in ‘verify code data’ row of
Table 30.2. Note that EA* needs to be held at logic 1. The contents of the addressed EPROM location
will come out on P0 pins.
Read signature: Read signature function indicates the manufacturer code and the device code
for the chip. The method of reading the signature is similar to verifying code data. The PROG* input
is held at logic 1, and the other signals are held as shown in the ‘read signature’ row of Table 30.2. The
600
address to be sent for reading the signature is 30H and 31H. The signature bytes read will be 89H from
location 30H, indicating that the manufacturer is Intel and 51H from location 31H, indicating that the
chip is 8751BH.
o The 8751BH comes in a plastic package without a quartz window. As such, its EPROM contents can
a only be programmed once and cannot be erased. In other words, the 8751BH contains a PROM. How-
o ever, most other 8751 versions come with a quartz window, and the EPROM contents in these chips can
E be erased. Exposing the EPROM to an ultraviolet lamp of 12,000 |xW/cm2 rating for about 30 min, with
in
the EPROM at a distance of about 2 cm is enough to erase the EPROM. This will erase the complete
program, encryption array, and the program lock bits. After the erasure, all bits will be in 1 state.
j;
I-
M P
Main features of intel 805 1 547 Power saving modes of 8051 595
3
Main memory 9 Power down mode 596
Programming examples 524, 542, 568 £
Random access memory (RAM) 9 X