Microprocessors and Introduction To Microcontroller - Text
Microprocessors and Introduction To Microcontroller - Text
Introduction to Microcontroller
(8085, 8086, 8051 - Architecture, Interfacing and Programming)
(i)
About the Author
A. P. Godse
· Completed M.S in Software Systems with distinction from Birla Institute of Technology.
· Completed B.E. in Industrial Electronics with distinction from University of Pune in 1990.
· Worked as a Professor at Vishwakarma Institute of Technology, Pune.
· Worked as a Technical Director at Noble Institute of Technology, Pune.
· Worked as selection Committee member for M. S. admission for West Virginia University,
Washington D.C.
· Developed Microprocessor Based Instruments in co-ordination with Anna Hazare for
Environmental Studies Laboratory, at Ralegan Siddhi.
· Developed Microprocessor Lab in-house for Vishwakarma Institute of Technology.
· Worked as Subject Expert for a State Level Technical Paper Presentation Competition, Pune.
· Awarded on 26th Jan 2001 by Pune Municipal Corporation for contributing in education field
and technical writing.
· Awarded as a “Parvati Bhushan Puraskar” for contributing in the education field.
· Since 1996, writing books on various engineering subjects. Over the years, many of books are
recommended as the reference books and text books in various national and international
engineering universities.
D. A. Godse
· Completed M.E and pursuing Ph.D. in Computer Engineering from Bharati Vidyapeeth’s
University Pune.
· Completed B.E. in Industrial Electronics from University of Pune in 1992.
· Working as a Professor and Head of Information Technology Department in B.V.C.O.E.W, Pune.
· Subject Expert for syllabus setting of Computer Engineering and Information Technology branches
at the faculty of Engineering of Pune University.
· Subject Expert and Group Leader for syllabus setting of Electronics, Electronics and
Telecommunication and Industrial Electronics branches at the faculty of Maharashtra State, Board
of Technical Education.
· Subject In-charge for Laboratory Manual Development, Technical Teacher’s Training Institute,
Pune.
· Subject In-charge for Question Bank Development Project, Technical Teacher’s Training Institute,
Pune.
· Subject In-charge for the preparation of Teacher’s Guide, Board of Technical Examination,
Maharashtra state.
· Subject Expert for a State Level Technical Paper Presentation Competition organized by Bharati
Vidyapeeth’s Jawaharlal Nehru Institute of Technology, Pune.
· Local Inquiry Committee (LIC) member of Engineering faculty of Pune University.
· Awarded on 15th August 2006 by Pune Municipal Corporation for contributing in education field
and technical writing.
· Awarded on the occasion of International Women’s Day at Yashawantrao Chavan Pratishthan
Sabhagrih, Mumbai by Bharatiya Shikshan Sanstha.
(ii)
®
Microprocessors and
Introduction to Microcontroller
(8085, 8086, 8051 - Architecture, Interfacing and Programming)
Atul P. Godse
M. S. Software Systems (BITS Pilani)
B.E. Industrial Electronics
Formerly Lecturer in Department of Electronics Engg.
Vishwakarma Institute of Technology
Pune
® TM
TECHNICAL
PUBLICATIONS
An Up-Thrust for Knowledge
(iii)
Microprocessors and
Introduction to Microcontroller
(8085, 8086, 8051 - Architecture, Interfacing and Programming)
All publishing rights (printed and ebook version) reserved with Technical Publications. No part of this book
should be reproduced in any form, Electronic, Mechanical, Photocopy or any information storage and retrieval
system without prior permission in writing, from Technical Publications, Pune.
Published
®
by : TM
Amit Residency, Office No.1, 412, Shaniwar Peth, Pune - 411030, M.S. INDIA.
TECHNICAL P h . : + 9 1 - 0 2 0 - 2 4 4 9 5 4 9 6 / 9 7 , Te l e f a x : + 9 1 - 0 2 0 - 2 4 4 9 5 4 9 7
PUBLICATIONS Email : [email protected] Website : www.vtubooks.com
Printer :
Repro India Ltd.
50/2 TTC, MIDC,
Mahape, Navi Mumbai
Price ` 585/-
(iv)
9789350992609 [1]
The importance of Microprocessors and Introduction to
Microcontroller is well known in various engineering fields.
Overwhelming response to our books on various subjects inspired us to
write this book. The book is structured to cover the key aspects of the
subject Microprocessors and Introduction to Microcontroller.
®
topics. Each chapter is well supported with necessary illustrations,
practical examples and solved problems. All the chapters in the book
are arranged in a proper sequence that permits each topic to build
P
upon earlier studies. All care has been taken to make students
comfortable in understanding the basic concepts of the subject.
R
The book not only covers the entire scope of the subject but
explains the philosophy of the subject. This makes the understanding of
this subject more clear and makes it more interesting. The book will be
E very useful not only to the students but also to the subject teachers.
The students have to omit nothing and possibly have to cover nothing
F
more.
A
making this book a reality. Much needed moral support and
encouragement is provided on numerous occasions by our whole
family. We wish to thanks the Publisher and the entire team of
C Technical Publications who have taken immense pain to get this book
in time with quality printing.
Authors
A. P. Godse
D. A. Godse
Dedicated to God
(v)
TT able of Contents
Chapter - 1 8085 Processor (1 - 1) to (1 - 22)
1.1 Features ............................................................................................................ 1 - 2
1.2 Architecture of 8085......................................................................................... 1 - 3
1.2.1 Register Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 4
(vi)
1.4.3 Reset Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 13
2.2.7 Input/Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 29
2.5.2 Flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 38
(vii)
2.5.4 Assembly Language Program to Machine Language Program . . . . . . . . . . . . . 2 - 40
(viii)
4.2 8085 Interrupt Structure and Operation .......................................................... 4 - 4
4.2.1 Types of Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 - 4
(ix)
6.4.2.1 Control Circuitry, Instruction Decoder, ALU . . . . . . . . . . . . . . . 6 - 8
(x)
7.2 Addressing Memory ..........................................................................................7 - 6
7.3 Addressing I/O ...................................................................................................7 - 8
7.4 Minimum Mode 8086 System and Timings .......................................................7 - 8
7.4.1 Minimum Mode Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 - 8
(xiii)
Review Questions ................................................................................................12 - 20
Two Marks Questions with Answers ..............................................................12 - 21
(xv)
Chapter - 15 Programmable Interval Timer/Counter (15 - 1) to (15 - 18)
8253/8254
15.1 Features .........................................................................................................15 - 2
15.2 Block Diagram................................................................................................15 - 3
15.3 Operational Description ................................................................................15 - 5
15.4 Mode Definition.............................................................................................15 - 7
15.5 Programming Examples ...............................................................................15 - 14
15.6 Interfacing of 8253/54 in I/O Mapped I/O ..................................................15 - 15
Review Questions ...............................................................................................15 - 16
Two Marks Questions with Answers .............................................................15 - 17
(xvi)
Chapter - 17 8051 Microcontroller (17 - 1) to (17 - 36)
17.1 Introduction to 8051 Microcontroller ...........................................................17 - 2
17.2 Features of 8051 and 8051 Family Microcontrollers.....................................17 - 3
17.3 Architecture of 8051......................................................................................17 - 4
17.3.1 Central Processing Unit (CPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 - 4
18.1.6 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 4
18.5.2 Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 23
18.5.3 Subtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 25
18.8.2 Jump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 36
(xix)
Program 25 : To generate a square wave on the port pin P1.0.......................... 18 - 61
Program 26 : To find the sum of 10 numbers stored in the array. ..................... 18 - 62
Program 27 : Data transfer from memory block B1 to memory block B2. ......... 18 - 64
Program 28 : Data transfer from memory block B1 to memory block B2. ......... 18 - 65
Program 29 : To search a byte in a given numbers............................................. 18 - 66
Program 30 : Multiply two 8-bit numbers using repetitive addition.................. 18 - 68
Program 31 : To find the average of given N numbers....................................... 18 - 69
Program 32 : To find factorial of a number ........................................................ 18 - 71
Program 33 : To find Fibonacci series of N given terms. .................................... 18 - 72
Program 34 : Write an assembly language program to move 5 bytes of data stored
at location 8000H onwards to the location C000H onwards and
vice-versa. ..................................................................................... 18 - 73
Program 35 : An array of 10 numbers is stored at location 4000H onwards ..... 18 - 74
Program 36 : Write an assembly language program to realize following logic circuit
using Boolean instructions of 8051............................................... 18 - 75
Program 37 : Write a program to load accumulator with values 55H and complement
70 times........................................................................................ 18 - 75
Program 38 : Program to count the number of ONE's and ZERO's in two consecutive
data memory locations. ................................................................ 18 - 75
Program 39 : Write a program to save the status of bits P1.3 and P1.4 on RAM bit
location 5 and 6 respectively. ....................................................... 18 - 76
Program 40 : What is the content of R5 after execution of the following program ? ...
.............................................................................................................................18 - 76
Review Questions ................................................................................................18 - 77
Two Marks Questions with Answers ..............................................................18 - 78
(xx)
Chapter - 19 8051 I/O Ports, Timer, Serial Port
& Interrupts (19 - 1) to (19 - 48)
(xxii)
Lab Experiments
Lab Experiment 1 : Store 8-bit data in memory.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 42
Lab Experiment 7 : Check results after execution of INR B, INR C and INX B instructions. . . 2 - 47
Lab Experiment 8 : Check results after execution of DCR C, DCR B and DCX B instructions. 2 - 47
(xxiii)
Lab Experiment 22 : Calculate the sum of series of numbers. . . . . . . . . . . . . . . . . . . . . . . 3 - 4
Lab Experiment 23 : Data transfer from memory block B1 to memory block B2. . . . . . . . . . 3 - 6
Lab Experiment 35 : Add each element of array with the elements of another array.. . . . . . 3 - 21
Lab Experiment 41 : Find the number of negative, zero and positive numbers. . . . . . . . . . 3 - 27
Lab Experiment 42 : Multiply two eight bit numbers with shift and add method. . . . . . . . . . 3 - 29
Lab Experiment 43 : Divide 16-bit number with 8-bit number using shifting technique. . . . 3 - 30
(xxiv)
Lab Experiment 45 : Program to test RAM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 - 35
Lab Experiment 52 : Generate and display BCD up counter with frequency 1 Hz. . . . . . . . 3 - 44
Lab Experiment 53 : Generate and display BCD down counter with frequency 1 Hz . . . . . 3 - 45
Lab Experiment 55 : Identify the error and correct the given delay routine. . . . . . . . . . . . . 3 - 48
(xxv)
Lab Experiment 68 : Transmit message using 8251. . . . . . . . . . . . . . . . . . . . . . . . . . . 13 - 20
Lab Experiment 70 : Hardware and software for 64-key matrix keyboard interface . . . . . . 14 - 7
Lab Experiment 71 : Hardware and software for interfacing 8-digit 7-segment display. . . 14 - 13
Lab Experiment 72 : Hardware and software for 8 ´ 8 keyboard interface using 8279. . . 14 - 36
Lab Experiment 73 : Hardware and software to interface 8 ´ 4 matrix keyboard using 8279 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 - 41
Lab Experiment 74 : Hardware and software to interface eight 7-segment digits using 8279. . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 - 42
Lab Experiment 76 : Interface 4´ 4 matrix keyboard and 4 digit 7-segment display using 8279 .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 - 47
(xxvi)
1 8085 Processor
Contents
1.1 Features . . . . . . . . . . . . . . . . . . April/May-04
1.2 Architecture of 8085 . . . . . . . . . . . . . . . . . . April/May-04, Nov./Dec.-04,
. . . . . . . . . . . . . . . . . . Dec.-07, 08, 09, 10, June-06,
. . . . . . . . . . . . . . . . . . May-10, 11
1.3 Pin Definitions of 8085 . . . . . . . . . . . . . . . . . . May/June-09, Nov./Dec.-06, 08, 09,
. . . . . . . . . . . . . . . . . . April/May-10
1.4 Bus Organization . . . . . . . . . . . . . . . . . . Nov./Dec.-04
(1 - 1)
TM
TM
12. It provides five hardware interrupts : TRAP, RST 7.5, RST 6.5, RST 5.5 and INTR.
13. It has serial I/O control which allows serial communication.
14. It provides control signals (IO/M, RD, WR) to control the bus cycles and hence
external bus controller is not required.
15. The external hardware (another microprocessor or equivalent master) can detect
which machine cycle microprocessor is executing using status signals
(IO/M, S0, S1). This feature is very useful when more than one processors are
using common system resources (memory and I/O devices).
16. It has a mechanism by which it is possible to increase its interrupt handling
capacity.
17. The 8085 has an ability to share system bus with Direct Memory Access controller.
This feature allows to transfer large amount of data from I/O device to memory or
from memory to I/O device with high speeds.
18. It can be used to implement three chip microcomputer with supporting I/O
devices like IC 8155 and IC 8355.
Fig. 1.1 (See Fig. 1.1 on next page) shows the architecture of 8085.
It consists of various functional blocks as listed below :
· Registers
· Arithmetic and Logic Unit
· Instruction decoder and machine cycle encoder
· Address buffer
· Address/Data buffer
· Incrementer/Decrementer address latch
· Interrupt control
· Serial I/O control
· Timing and control circuitry.
TM
2. Temporary Registers
a) Temporary data register b) W and Z registers.
2. Temporary Registers :
a) Temporary Data Register : The ALU has two inputs. One input is supplied by the
accumulator and other from temporary data register. The programmer cannot access this
temporary data register. However, it is internally used for execution of most of the
arithmetic and logical instructions.
For example : ADD B is the instruction in the arithmetic group of instructions which
adds the contents of register A and register B and stores result in register A. The addition
operation is performed by ALU. The ALU takes inputs from register A and temporary
data register. The contents of register B are transferred to temporary data register for
applying second input to the ALU.
b) W and Z registers : W and Z registers are temporary registers. These registers are
used to hold 8-bit data during execution of some instructions. These registers are not
available for programmer, since 8085 uses them internally.
TM
S Z X AC X P X CY
c) Instruction Register : In a typical processor operation, the processor first fetches the
opcode of instruction from memory (i.e. it places an address on the address bus and
memory responds by placing the data stored at the specified address on the data bus). The
CPU stores this opcode in a register called the instruction register. This opcode is further
sent to the instruction decoder to select one of the 256 alternatives.
TM
The 8085 executes seven different types of machine cycles. It gives the information
about which machine cycle is currently executing in the encoded form on the S0, S1 and
IO/M lines. This task is done by machine cycle encoder.
TM
Fig. 1.4 (a) and (b) show 8085 pin configuration and functional pin diagram of 8085
respectively. The signals of 8085 can be classified into seven groups according to their
functions.
+5 V GND
1 2 40 20
X1 X2 VCC VSS
X1 1 40 VCC Serial SID 5
I/O
SOD 4
X2 2 39 HOLD ports
28
RESET OUT 3 38 HLDA A15
High-order
SOD 4 37 A8 address bus
CLK(OUT)
TRAP 6 21
SID 5 36 RESET IN RST 7.5 7
Externally initiated signals
19
RST 6.5 8
TRAP 6 35 READY AD0
RST 5.5 9 Multiplexed
RST 7.5 7 34 IO / M address / data
INTR 10
AD7 bus
RST 6.5 8 33 S1
12
READY 35
RST 5.5 9 32 RD
HOLD 39
INTR 10 31 WR RESET IN 36
8085A
INTA 11 30 ALE 8085A
AD0 12 29 S0
acknowledgment
INTA 11
External signal
AD7 19 22 A9
VSS 20 21 A8 3 37
TM
LC Tuned Circuit :
It is a LC resonant tank circuit. The
resonant frequency for this circuit is given by
X1
1
L C fr =
2p L ( C ext + C int )
X2
Where Cint is the internal capacitance and
it is normally 15 pF. The output frequency of
Fig. 1.6 LC circuit this circuit has 10 % variations. To minimize
the variations in the output frequency, it is
recommended to have C ext at least twice that of Cint i.e. 30 pF.
TM
+5 V
X1 Pull-up
resistance
Crystal
External
X1
X2 clock
8085
C
Non-connected X2
(NC)
Fig. 1.8 Crystal clock circuit Fig. 1.9 External frequency source
External Clock :
Fig. 1.9 shows how to drive clock input of 8085 with external frequency source. Here
external clock is applied at X 1 input and X 2 input is kept open.
IC 74LS373
AD0 D Q A0
AD1 A1
AD2 A2
CLK
AD3 A3
AD4 A4
AD5 A5
AD6 A6
AD7 A7
G OC
Enable Output control
ALE
D0
D1
D2
D3
D4
D5
D6
D7
+5 V
IN 4148 75 K
To 8085
Reset
100 W
1 mF
No
contact
TM
As we know that, after power up or reset 8085 fetches its first instruction from 0000H
address, and it has to be the first instruction from monitor program. Therefore EPROM
consisting of monitor program must be located from address 0000H in any
8085 microprocessor system.
8085
IO/M
MEMR
RD
WR
MEMW
IOR
IOW
TM
0 0 1 0 1 1 1
0 1 0 1 0 1 1
0 1 1 1 1 1 1
1 0 1 1 1 0 1
1 1 0 1 1 1 0
1 1 1 1 1 1 1
Table 1.1
Same truth table can be implemented using 3:8 decoder as shown in Fig. 1.13.
+5V
G VCC Y0
Y1
MEMR
Y2
3:8 MEMW
WR A Decoder Y3
RD B Y4
IO/M C (74LS138) Y5
IOR
Y6
IOW
Y7
G1 G2
TM
Unidirectional Buffers : 1
20
1A1 VCC 1G 1Y1
As we know, the address bus is 2 18
unidirectional, 8-bit unidirectional buffer,
1A2 1Y2
74LS244 is used to buffer higher address 4 16
Bi-directional Buffer : 17
2A8 2Y4
3
9 A8 B8 11
DIR G
1 19
Direction Enable
control
1 19 10
+5 V +5 V
40 35 20
AD7
1 VCC READY 18 VCC 19
X1 A7
17 16
A15 28 A6
2MHz 14 15
crystal 2 74LS373 A5 Low
X2 A8 13 12
20 pF A4 order
21 8 9 A3 Address
7 6
AD7 19 A2 bus
4 4 5
SOD AD0 A1
AD0 3 2
5 SID G OC GND A0
12
33 S 11 1 10
1 30
NC 29 8085 A ALE
S0 34 +5 V
37 IO/M
CLK OUT 32
38 RD 9 11
100K HLDA 31 D7
11 WR 8 12 D6
INTA 7 13
36 74LS245 D5
6 14
RESET IN Bidirectional D4 Data
1mF 6 5 15
TRAP Bus D3 bus
1K 7 4 16
Driver D2
RST7.5 3 17
8 D1
RST6.5 2 18 D0
RESET DIR G GND
9
RST5.5 OUT
10 1 19 10 20
INTR
From 39
Interrupt HOLD +5 V
source Vss
6 16
20 VCC
G3
O7
O6 9
IO/M IOW Control
3 74LS138 10 bus
A O5 IOR
RD 2 2 3-to-8 13
A decoder O2 MEMW
WR 1 1 14
A O1 MEMR
5 0
G2 O0
G1 GND
4 8
It also shows clock and reset circuits. Interrupt lines which are not in use are
grounded. This is necessary because floating interrupt line may cause false triggering of
interrupt. Similarly, since the DMA controller is not used, HOLD line is also grounded. As
we know READY signal is used to synchronize slow peripherals with the microprocessor.
When it is low, microprocessor enters in the wait state and when it is high, it indicates
that the memory or peripheral is ready to send or receive data. Here, the READY signal is
tied high to prevent the microprocessor from entering the wait state. ALE signal is
connected to the clock input of the latch, to latch the low order address in T1 of the
machine cycle. To control the direction of the bi-directional buffer 74LS245, RD signal from
8085 is connected to DIR input of the bi-directional buffer. Thus, when RD signal is low,
DIR is low and data flows from memory or I/O device to the microprocessor, performing
read operation. When RD signal is high, DIR is high and data flows from microprocessor
to memory or I/O device performing write operation.
Review Questions
Section 1.1
Q.1 Explain architectural features of 8085. May-04, Marks 4
Section 1.2
Q.1 With neat functional block diagram, explain the architecture of 8085 microprocessor.
June-06, Dec.-07, Dec.-04,08,09,10, May-04,10,11,12, Marks 16
Q.2 Explain the architecture, data flow and instruction execution of 8085 microprocessor.
May-11, Marks 8
Q.3 Give the format of flag register in 8085. Explain each flag.
Q.4 Define the function of parity flag and zero flag in 8085. June-12, Marks 2
Section 1.3
Q.1 Write about the pin configuration of 8085 processor and explain them in detail.
June-09, Marks 16
TM
Section 1.4
Q.1 Draw the schematic of latching low-order address bus in 8085 microprocessor.
Dec.-11, Marks 2
TM
Q.10 What is the need for ALE signal in 8085 microprocessor? Dec.-04,09, May-10
Ans. : The ALE signal is used to demultiplex (separate) AD0 - AD7 lines to A0 - A7
(address lines) and D0 - D7 (data lines). The separation of address lines and data lines is
achieved by connecting a external latch to AD0 - AD7 lines and enabling the latch when
ALE signal is active.
Q.13 If a 5 MHz crystal is connected with 8085; what is the value of system clock
frequency and one T-state ? Dec.-07
Crystal frequecny 5 MHz
Ans. : System clock frequency = = = 2.5 MHz,
2 2
1
one T-state = = 0.4 µsec.
2.5 ´ 10 6
Q.14 What are the important control signals in 8085 microprocessor ? Dec.-08
Ans. : The important control signals in 8085 microprocessor are : ALE, IO M, RD and
WR
TM
Contents
2.1 Instruction Classification
2.2 Instruction Set of 8085 . . . . . . . . . . . . . . . . . . May/June-07,09; April/May-04, 10
. . . . . . . . . . . . . . . . . . Nov./Dec.-07, 08, 09
2.3 Addressing Modes
2.4 Instruction Set Summary
2.5 Assembly Language Programming
2.6 Programming Examples
2.7 Instruction Comparisons
2.8 Instruction Formats
(2 - 1)
TM
In the previous chapter we have studied block diagram of microprocessor 8085. The
block diagram shows microprocessor's functions for data processing and data handling. It
also shows how each of these logic functions are connected together. Such microprocessor
performs a particular task by executing proper sequence of instructions. Thus to perform a
task in a particular microprocessor system, programmer has to know the instructions
supported by microprocessor used in the microprocessor system.
This chapter explains the set of instructions supported by the 8085 microprocessor and
explains how to write programs (set of instructions written in a proper sequence to
perform a particular task) using them. This chapter also gives a large number of programs
to perform different tasks.
TM
of B and C registers cannot be added directly. To add two 16-bit numbers the
8085 provides DAD instruction. It adds the data within the register pair to the
contents of the HL register pair and resulted sum is stored in the HL register
pair.
· Subtraction : Any 8-bit number, or the contents of a register, or the contents of a
memory location can be subtracted from the contents of the accumulator and the
result is stored in the accumulator. The resulted borrow bit is stored in the carry
flag. In 8085, no two other registers can be added directly.
· Increment/Decrement : The 8085 has the increment and decrement instructions
to increment and decrement the contents of any register, memory location or
register pair by 1.
TM
In this section, the instructions from all groups are explained with the help of
examples. Before to discuss these instructions, let us get familiar with the notations used in
the explanation of instructions. These are:
Notation Meaning
M Memory location pointed by HL register pair
r 8-bit register
rp 16-bit register pair
rs Source register
rd Destination register
addr 16-bit address / 8-bit address
1. MVI r, data (8) This instruction directly loads a specified register with an 8-bit data
given within the instruction. The register r is an 8-bit general
purpose register such as A, B, C, D, E, H and L.
Example :
MVI B, 60H ; This instruction will load 60H directly into the B register.
2. MVI M, data (8) This instruction directly loads an 8-bit data given within the
instruction into a memory location. The memory location is specified
by the contents of HL register pair.
204FH 204FH
2051H 2051H
TM
3. MOV rd, rs This instruction copies data from the source register into destination
register. The rs and rd are general purpose registers such as A, B, C,
D, E, H and L. The contents of the source register remain unchanged
after execution of the instruction.
Operation : rd ¬ rs
Example : A = 20H
MOV B, A ; This instruction will copy the contents of register A (20H) into
register B.
4. MOV M, rs This instruction copies data from the source register into memory
location pointed by the HL register pair. The rs is an 8-bit general
purpose register such as A, B, C, D, E, H and L.
Operation : (HL) ¬ rs
5. MOV rd, M This instruction copies data from memory location whose address is
specified by HL register pair into destination register. The contents of
the memory location remain unchanged. The rd is an 8-bit general
purpose register such as A, B, C, D, E, H and L.
Operation : rd ¬ (HL)
6. LXI rp, data (16) This instruction loads immediate 16 bit data specified within the
instruction into register pair or stack pointer. The rp is 16-bit register
pair such as BC, DE, HL or 16-bit stack pointer.
TM
Example :
LXI B,1020H ; This instruction will load 10H into B register and 20H into C
register.
7. STA addr This instruction stores the contents of A register into the memory
location whose address is directly specified within the instruction.
The contents of A register remain unchanged.
Operation : (addr) ¬ A
Example : A = 50H
STA 2000H ; This instruction will store the contents of A register (50H) to
memory location 2000H.
8. LDA addr This instruction copies the contents of the memory location whose
address is given within the instruction into the accumulator. The
contents of the memory location remain unchanged.
Operation : A ¬ (addr)
9. SHLD addr This instruction stores the contents of L register in the memory
location given within the instruction and contents of H register at
address next to it. This instruction is used to store the contents of H
and L registers directly into the memory. The contents of the H and
L registers remain unchanged.
10. LHLD addr This instruction copies the contents of the memory location given
within the instruction into the L register and the contents of the next
memory location into the H register.
11. STAX rp This instruction copies the contents of accumulator into the memory
location whose address is specified by the specified register pair. The
rp is BC or DE register pair. This register pair is used as a memory
pointer. The contents of the accumulator remain unchanged.
Operation : (rp) ¬ A
12. LDAX rp This instruction copies the contents of memory location whose
address is specified by the register pair into the accumulator. The rp
is BC or DE register pair. The register pair is used as a memory
pointer.
Operation : A ¬ (rp)
13. XCHG This instruction exchanges the contents of the register H with that of
D and of L with that of E.
Operation : H « D and L « E
TM
1. ADD r This instruction adds the contents of the specified register to the
contents of accumulator and stores result in the accumulator. The r
is 8-bit general purpose register such as A, B, C, D, E, H and L.
Operation : A ¬A + r
2. ADD M This instruction adds the contents of the memory location pointed by
HL register pair to the contents of accumulator and stores result in
the accumulator. The HL register pair is used as a memory pointer.
This instruction affects all flags.
Operation : A ¬A + M
3. ADI data (8) This instruction adds the 8 bit data given within the instruction to
the contents of accumulator and stores the result in the accumulator.
Example : A = 50H
ADI 70H ; This instruction will add 70H to the contents of the accumulator
(50H) and it will store the result in the accumulator (C0H).
4. ADC r This instruction adds the contents of specified register to the contents
of accumulator with carry. This means, if the carry flag is set by
some previous operation, it adds 1 and the contents of the specified
register to the contents of accumulator, else it adds the contents of
the specified register only. The r is 8-bit general purpose register
such as A, B, C, D, E, H and L.
TM
Operation : A ¬ A + r + CY
Operation : A ¬ A + M + CY
6. ACI data (8) This instruction adds 8 bit data given within the instruction to the
contents of accumulator with carry and stores result in the
accumulator.
7. DAD rp This instruction adds the contents of the specified register pair to the
contents of the HL register pair and stores the result in the HL
register pair. The rp is 16-bit register pair such as BC, DE, HL or
stack pointer. Only higher order register is to be specified for register
pair within the instruction.
Operation : HL ¬ HL + rp
TM
DAD D ; This instruction will add the contents of DE register pair, 1020H to
the contents of HL register pair, 2050H. It will store the result,
3070H in the HL register pair.
8. SUB r This instruction subtracts the contents of the specified register from
the contents of the accumulator and stores the result in the
accumulator. The register r is 8-bit general purpose register such as
A, B, C, D, E, H and L.
Operation : A ¬A – r
Operation : A ¬A – M
10. SUI data (8) This instruction subtracts an 8 bit data given within the instruction
from the contents of the accumulator and stores the result in the
accumulator.
Example : A = 40H,
SUI 20H ; This instruction will subtract 20H from the contents of accumulator
(40H). It will store the result (20H) in the accumulator.
TM
11. SBB r This instruction subtracts the specified register contents and borrow
flag from the accumulator contents. This means, if the carry flag
(borrow for subtraction) is set by some previous operation, it
subtracts 1 and the contents of the specified register from the
contents of accumulator, else it subtracts the contents of the specified
register only. The register r is 8-bit register such as A, B, C, D, E, H
and L.
Operation : A ¬ A – r – CY
SBB C ; This
; instruction will subtract the contents of C register (20H) and
carry flag (1) from the contents of accumulator (40H). It will store
the result (40H – 20H – 1 = 1FH) in the accumulator.
12. SBB M This instruction subtracts the contents of memory location pointed by
HL register pair from the contents of accumulator and borrow flag
and stores the result in the accumulator.
Operation : A ¬ A – M – CY
13. SBI data (8) This instruction subtracts 8 bit data given within the instruction and
borrow flag from the contents of accumulator and stores the result in
the accumulator.
Operation : A ¬ A - data(8) - CY
TM
14. DAA This instruction adjusts accumulator to packed BCD (Binary Coded
Decimal) after adding two BCD numbers.
Instruction works as follows :
1. If the value of the low - order four bits (D3-D0) in the accumulator
is greater than 9 or if auxiliary carry flag is set, the instruction adds
6 (06) to the low-order four bits.
2. If the value of the high-order four bits (D7 - D4) in the
accumulator is greater than 9 or if carry flag is set, the instruction
adds 6 (60) to the high-order four bits.
Example :
If, A = 0011 1001 = 39 BCD
and C = 0001 0010 = 12 BCD then
ADD C ; Gives A = 0100 1011 = 4BH
DAA ; adds 0110 because 1011>9,
; A=0101 0001 = 51 BCD
If A = 1001 0110 = 96 BCD
and D = 0000 0111 = 07 BCD then
ADD D ; Gives A = 1001 1101 = 9DH
DAA ; adds 0110 because 1101 > 9,
A = 1010 0011 = A3H,
1010 > 9 so adds 0110 0000,
A = 0000 0011 = 03 BCD, CF = 1.
Operation : r ¬r + 1
Example : B = 10H
INR B ; This instruction will increment the contents of B register (10H) by
one and stores the result (10+1 = 11H) in the same i.e. B register.
16. INR M This instruction increments the contents of memory location pointed
by HL register pair by 1. The result is stored at the same memory
location. The HL register pair is used as a memory pointer.
Operation : M¬ M+1
TM
17. INX rp This instruction increments the contents of register pair by one. The
result is stored in the same register pair. The rp is register pair such
as BC, DE, HL or stack pointer (SP).
Operation : rp ¬ rp + 1
Example : HL = 10FFH
INX H ; This instruction will increment the contents of HL register pair
(10FFH) by one. It will store the result (10FF + 1 = 1100H) in the
same i.e. HL register pair.
18. DCR r This instruction decrements the contents of the specified register by
one. It stores the result in the same register. The register r is 8-bit
general purpose register such as A, B, C, D, E, H and L.
Operation : r ¬r – 1
Example : E = 20H
DCR E ; This instruction will decrement the contents of E register (20H) by
one. It will store the result (20 – 1 = 1FH) in the same, i.e. E
register.
19. DCR M This instruction decrements the contents of memory location pointed
by HL register pair by 1. The HL register pair is used as a memory
pointer. The result is stored in the same memory location.
Operation : M ¬M – 1
20. DCX rp This instruction decrements the contents of register pair by one. The
result is stored in the same register pair. The rp is register pair such
TM
Operation : rp ¬ rp – 1
Example : DE = 1020H
DCX D ; This instruction will decrement the contents of DE register pair
(1020H) by one and store the result (1020 – 1 = 101FH) in the
same, DE register pair.
Operation : A¬ AÙr
1010 1010
0000 1111
————————
0 0 0 0 1 0 1 0 = 0AH
2. ANA M This instruction logically ANDs the contents of memory location
pointed by HL register pair with the contents of accumulator. The
result is stored in the accumulator. The HL register pair is used as a
memory pointer.
Operation : A¬ AÙM
Example : A = 01010101 = (55H), HL = 2050H(2050H) ® 10110011 = (B3H)
ANA M ; This instruction will logically AND the contents of memory location
pointed by HL register pair (B3H) with the contents of accumulator
0101 0101
(55H). It will store the result (11H) in the accumulator.
1011 0011
–––––––––––––––
0 0 0 1 0 0 0 1 = 11H
TM
3. ANI data This instruction logically ANDs the 8 bit data given in the instruction
with the contents of the accumulator and stores the result in the
accumulator.
Operation : A ¬ A Ù data (8)
1 1 1 1 0 0 0 0 Masking pattern
X X X X 0 0 0 0 Result
Masked bits
TM
Operation : A ¬ A Å data
Example : A = 10110011 = (B3H)
XRI 39H ; This instruction will logically XOR the contents of accumulator
1011 0011 (B3H) with 39H. It will store the result (8AH) in the accumulator.
00 1 1 1 0 0 1
————————
1 0 0 0 1 0 1 0 = 8AH
The XOR instruction is used if some bits of a register or memory location must be
inverted. This instruction allows part of a number to be inverted or complemented. This is
illustrated in Fig. 2.2.
X X X X X X X X Result
Inverted bits
Fig. 2.2 Inversion of part of a number using XOR operation
7. ORA r This instruction logically ORs the contents of specified register with
the contents of accumulator and stores the result in the accumulator.
Each bit in the accumulator is ORed with corresponding bit in
register r. i.e. D0 bit in accumulator is ORed with D 0 bit in register r,
D1 in A with D1 in r and so on upto D7 bit. The register r is 8-bit
general purpose register such as A, B, C, D, E, H and L.
Operation : A¬ AÚ r
+ 1 1 1 1 0 0 0 0 Setting pattern
1 1 1 1 X X X X Result
Set bits
Operation : A– r
TM
11. CMP M This instruction subtracts the contents of the memory location
specified by HL register pair from the contents of the accumulator
and sets the condition flags as a result of subtraction. It sets zero flag
if A = M and sets carry flag if A < M. The HL register pair is used
as a memory pointer.
Operation : A – M
Example : A = 1011 1000 (B8H), HL = 2050H and (2050H) = 1011 1000 (B8H)
CMP M ; This instruction will compare the contents of memory location
(B8H) and the contents of accumulator. Here A = M so zero flag
will set after the execution of the instruction.
12. CPI data This instruction subtracts the 8 bit data given in the instruction from
the contents of the accumulator and sets the condition flags as a
result of subtraction. It sets zero flag if A = data and sets carry flag
if A < data.
Operation : CY ¬ 1
TM
Operation : CY ¬ CY
Operation :
Before Execution
CY B7 B6 B5 B4 B3 B2 B1 B0
After Execution
B7 B6 B5 B4 B3 B2 B1 B0 B7
2. RRC This instruction rotates the contents of the accumulator right by one
position. Bit B0 is placed in B7 as well as in CY.
TM
Operation :
Before execution
B7 B6 B5 B4 B3 B2 B1 B0 CY
After execution
B0 B7 B6 B5 B4 B3 B2 B1 B0
Operation :
Before execution
CY B7 B6 B5 B4 B3 B2 B1 B0
After execution
B7 B6 B5 B4 B3 B2 B1 B0 CY
4. RAR This instruction rotates the contents of the accumulator right by one
position. Bit B0 is placed in CY and CY is placed in B7.
TM
Operation :
Before execution
B7 B6 B5 B4 B3 B2 B1 B0 CY
After execution
CY B7 B6 B5 B4 B3 B2 B1 B0
1. PUSH rp This instruction decrements stack pointer by one and copies the
higher byte of the register pair into the memory location pointed by
stack pointer. It then decrements the stack pointer again by one and
copies the lower byte of the register pair into the memory location
pointed by stack pointer. The rp is 16-bit register pair such as BC,
DE, HL. Only higher order register is to be specified within the
instruction.
TM
SP Lower byte
SP
B C B C 10
1FFFH PUSH D 1FFFH
D 10 E 50 D 10 E 50
2000H 2000H
H L H L
2. PUSH PSW This instruction decrements stack pointer by one and copies the
accumulator contents into the memory location pointed by stack
pointer. It then decrements the stack pointer again by one and copies
the flag register into the memory location pointed by the stack
pointer.
Operation : SP ¬ SP – 1
(SP) ¬ A
SP ¬ SP – 1
(SP) ¬ Flag register
TM
PUSH PSW ; This instruction decrements the stack pointer (SP = 2000H) by one
(SP = 1FFFH) and copies the contents of the accumulator (20H)
into the memory location 1FFFH. It then decrements the stack
pointer again by one (SP = 1FFEH) and copies the contents of the
flag register (80H) into the memory location 1FFEH.
20 80 PUSH PSW 20 80
1FFFH 1FFFH 20
2000H 2000H
SP
(i.e. data 50H) into B register, and increment the stack pointer
again by one.
2002H 2002H
4. POP PSW This instruction copies the contents of memory location pointed by
the stack pointer into the flag register and increments the stack
pointer by one. It then copies the contents of memory location
pointed by stack pointer into the accumulator and increments the
stack pointer again by one.
2002H 2002H
5. SPHL This instruction copies the contents of HL register pair into the stack
pointer. The contents of H register are copied to higher order byte of
stack pointer and contents of L register are copied to the lower byte
of stack pointer.
Operation : SP ¬ HL
TM
Example : HL = 2500H
SPHL ; This instruction will copy 2500H into stack pointer. So after
execution of instruction stack pointer contents will be 2500H.
Operation : L « (SP)
H « (SP + 1)
30 40 60 XTHL 60 50 30
2701H 2701H
2702H 2702H
1. JMP addr This instruction loads the PC with the address given within the
instruction and resumes the program execution from this location.
Operation : PC ¬ addr
Example :
JMP 2000H ; This instruction will load PC with 2000H and processor will fetch
next instruction from this address.
2. Jcond addr This instruction causes a jump to an address given in the instruction
if the desired condition occurs in the program before the execution of
the instruction. The Table 2.1 shows the possible conditions for
jumps.
TM
Note : The stack is a part of read/write memory set aside for storing
intermediate results and addresses.
3000H 3000H
4. C cond addr This instruction calls the subroutine at the given address if a
specified condition is satisfied. Before call it stores the address of
instruction next to the call on the stack and decrements stack pointer
by two. The Table 2.2 shows the possible conditions for calls.
Instruction code Description Condition for CALL
CC Call on carry CY = 1
CNC Call on not carry CY = 0
CP Call on positive S = 0
CM Call on minus S = 1
CPE Call on parity even P = 1
CPO Call on parity odd P = 0
CZ Call on zero Z = 1
CNZ Call on not zero Z = 0
5. RET This instruction pops the return addr (address of the instruction next
to CALL in the main program) from the stack and loads program
counter with this return address. Thus transfers program control to
the instruction next to CALL in the main program.
TM
SP ® 27FD 00
27FE 62
27FF
RET ; This instruction will load PC with 6200H and it will transfer
program control to the address 6200H. It will also increment the
stack pointer by two.
Before Execution After Execution
27FFH 27FFH
6. R condition This instruction returns the control to the main program if the
specified condition is satisfied. Table 2.3 shows the possible
conditions for return.
Instruction code Description Condition for RET
RC Return on carry CY = 1
RNC Return on not carry CY = 0
RP Return on positive S = 0
RM Return on minus S = 1
RPE Return on parity even P = 1
RPO Return on parity odd P = 0
RZ Return on zero Z = 1
RNZ Return on not zero Z = 0
Table 2.3 Conditions for return
7. PCHL This instruction loads the contents of HL register pair into the
program counter. Thus the program control is transferred to the
location whose address is in HL register pair.
Operation : PC ¬ HL
Example : HL = 6000H
PCHL ; This instruction will load 6000H into the program counter.
TM
8. RST n This instruction transfers the program control to the specific memory
address as shown in Table 2.4. This instruction is like a fixed address
CALL instruction. These fixed addresses are also referred to as vector
addresses. The processor multiplies the RST number by 8 to calculate
these vector addresses. Before transferring the program control to the
instruction following the vector address RST instruction saves the
current program counter contents on the stack like CALL instruction
Instruction code Vector Address
RST 0 0 ´ 8 = 0000H
RST 1 1 ´ 8 = 0008H
RST 2 2 ´ 8 = 0010H
RST 3 3 ´ 8 = 0018H
RST 4 4 ´ 8 = 0020H
RST 5 5 ´ 8 = 0028H
RST 6 6 ´ 8 = 0030H
RST 7 7 ´ 8 = 0038H
Table 2.4 Vector addresses for return instructions
Example : SP = 3000H
2000H RST 6 ; This instruction will save the current contents of the program
counter (i.e. address of next instruction 2001H) on the stack and it
will load the program counter with vector address
6 ´ 8= 4810= 30H) 0030H.
1. IN addr(8-bit) This instruction copies the data at the port whose address is specified
in the instruction into the accumulator.
Operation : A ¬ (addr)
Example : Port address = 80H, data stored at port address 80H, (80H) = 10H
IN 80H ; This instruction will copy the data stored at address 80H, i.e. data
10H in the accumulator.
2. OUT addr(8-bit) This instruction sends the contents of accumulator to the output port
whose address is specified within the instruction.
TM
Operation : (addr) ¬ A
Example : A = 40H
OUT 50H ; This instruction will send the contents of accumulator(40H) to the
output port whose address is 50H.
Operation : IE (F/F) ¬ 1
Operation : IE (F/F) ¬ 0
5. SIM This instruction masks the interrupts as desired. It also sends out
serial data through the SOD pin. For this instruction command byte
must be loaded in the accumulator.
D7 D6 D5 D4 D3 D2 D1 D0
TM
Example : i) A = 0EH
D7 D6 D5 D4 D3 D2 D1 D0
Register A
SOD SOE X RST7.5 MSE M7.5 M6.5 M5.5
0 0 0 0 1 1 1 0 = 0EH
SIM ; This instruction will mask RST 7.5 and RST 6.5 interrupts where as
RST 5.5 interrupt will be unmasked. It will also disable serial
output.
6. RIM : This instruction copies the status of the interrupts into the
accumulator. It also reads the serial data through the SID pin.
D7 D6 D5 D4 D3 D2 D1 D0
Example :
RIM ; After execution of RIM instruction if the contents of accumulator
are 4BH then we get following information.
D7 D6 D5 D4 D3 D2 D1 D0
Example :
MVI A, 20H ; Moves 8 bit immediate data (20H) into accumulator
MVI M, 30H ; Moves 8 bit immediate data (30H) into the
; memory location pointed by HL register pair.
LXI SP, 2700H ; Moves 16 bit immediate data (2700H) into SP.
LXI D, 10FFH ; Moves 16 bit immediate data (10FFH) into DE
; register pair ( D = 10H and E = FFH).
Example :
MOV A, B ; Moves the contents of register B into the accumulator.
SPHL ; Moves the contents of HL register pair into stack pointer.
ADD C ; Adds the contents of register C into the contents of accumulator
; and stores result in the accumulator.
Example :
LDA 2000H ; Loads the 8 bit contents of memory location
; 2000H into the accumulator.
SHLD 3000H ; Stores the HL register pair into two consecutive memory
; locations. Lower byte i.e. the contents of L register into memory
; location 3000H and higher byte i.e. the contents of H register
; into memory location 3001H.
TM
Example :
LDAX B ; Loads the accumulator with the contents of
; memory location pointed by BC register pair.
MOV M, A ; Stores the contents of accumulator into the
; memory location pointed by HL register pair.
Example :
CMA ; Complements contents of accumulator.
RAL ; Rotates the contents of accumulator left through carry.
Note : Many of the advanced processors support addressing mode called index
addressing mode. In this mode, the address of the operand within the memory is
generated by adding the offset/displacement to the register specified in the instruction.
The offset/displacement is also a part of the instruction. In 8085 such addressing mode is
not available. However, we can implement such kind of program structure, by using
memory pointer (HL register), any other register pair and a instruction sequence given
below :
LXI H, Base_addr ; Loads the base address
LXI B, Offset/Displacement ; Loads the offset or displacement
DAD B ; Gives the addition of HL and BC in HL register pair.
MOV A, M ; Load the data from memory in the accumulator
By incrementing or decrementing contents of BC register or loading another contents,
we can change the index/offset/displacement.
TM
13 XCHG H « D, L « E No 1 Register
Arithmetic Group
7. DAD rp HL ¬ HL + rp CY 1 Register
TM
Logic Group
Rotate Group
Branch Group
3. PCHL PC ¬ HL No 1 Register
TM
Stack Group
TM
5. SPHL SP ¬ HL No 1 Register
Input/Output Group
1. EI IE(F/F) ¬ 1 No 1 –
2. DI IE(F/F) ¬ 0 No 1 –
3. NOP No operation No 1 –
TM
2.5.2 Flowchart
To develop the programming logic programmer has to
write down various actions which are to be performed in
proper sequence. The flow chart is a graphical tool that
allows programmer to represent various actions which are to
be performed. The graphical representation is very useful for
clear understanding of the programming logic.
The Fig. 2.7 shows the graphic symbols used in the
flowchart.
Oval : It indicates start or stop operation.
Arrow : It indicates flow with direction.
Parallelogram : It indicates input/output operation.
Rectangle : It indicates process operation.
Diamond : It indicates decision making operation.
A Double sided Rectangle : It indicates execution of
Fig. 2.7 Graphic symbols pre-defined process (subroutine).
used in flowchart Circle with alphabet : It indicates continuation.
TM
A : Any alphabet
The Fig. 2.8 shows sample flowchart.
Start
Input process
parameters
Call subroutine
Process
Display results
Stop
Let us define a program statement as 'write an assembly language program to add two
numbers'. The three tasks are involved in this program :
· Load two hex numbers
· Add numbers and
· Store the result in the memory
These tasks can be symbolically presented as flow chart, as shown in the Fig. 2.9.
(See Fig. 2.9 on next page)
Next job is to find the suitable 8085 assembly language instruction/s for each task.
These instructions are as follows :
Task 1 instructions :
MVI A, 20H ; Load 20H as a first number in register A
MVI B, 40H ; Load 40H as a second number in register B
Task 2 instruction :
ADD B ; Add two numbers and save result in register A
TM
Start
Add
numbers
Store the
result
Stop
TM
TM
Flowchart
Sample problem
(2000H) = 14H Start
(2001H) = 89H
Result = 14H + 89H = 9DH
Get the first number
Source program
LXI H, 2000H ; HL points 2000H Get the second number
MOV A, M ; Get first operand
INX H ; HL points 2001H
ADD M ; Add second operand Add two numbers
INX H ; HL points 2002H
MOV M, A ; Store result at 2002H
HLT ; Terminate program execution Store the result
TM
Flowchart
Start
End
TM
Source Program 1
LHLD 2000H ; Get first 16-bit number in HL
XCHG ; Save first 16-bit number in DE
LHLD 2002H ; Get second 16-bit number in HL
MOV A, E ; Get lower byte of the first number
ADD L ; Add lower byte of the second number
MOV L, A ; Store result in L register
MOV A, D ; Get higher byte of the first number
ADC H ; Add higher byte of the second number with carry
MOV H, A ; Store result in H register
SHLD 2004H ; Store 16-bit result in memory locations 2004H and 2005H.
HLT ; Terminate program execution
Source program 2
LHLD 2000H ; Get first 16-bit number
XCHG ; Save first 16-bit number in DE
LHLD 2002H ; Get second 16-bit number in HL
DAD D ; Add DE and HL
SHLD 2004H ; Store 16-bit result in memory locations 2004H and 2005H.
HLT ; Terminate program execution
In program 1 eight bit addition instructions are used (ADD and ADC) and addition is
performed in two steps. First lower byte addition using ADD instruction and then higher
byte addition using ADC instruction. In program 2 16-bit addition instruction (DAD) is
used.
TM
Source program
LHLD 2000H ; Get first 16-bit number in HL
XCHG ; Save first 16-bit number in DE
LHLD 2002H ; Get second 16-bit number in HL
MOV A, E ; Get lower byte of the first number
SUB L ; Subtract lower byte of the second number
MOV L, A ; Store the result in L register
MOV A, D ; Get higher byte of the first number
SBB H ; Subtract higher byte of second number with borrow
MOV H, A ; Store 16-bit result in memory locations 2004H and 2005H.
SHLD 2004H ; Store 16-bit result in memory locations 2004H and 2005H.
HLT ; Terminate program execution.
Flowchart
START
END
TM
Lab Experiment 7 : Check results after execution of INR B, INR C and INX B instructions.
Statement : If the contents of B = FFH and C = FFH then after execution of following
instructions give the contents of register B and register C.
Instructions :
1. INR B
2. INR C
3. INX B
1. INR B
B ® FFH
+ 01H
®B
00H
\ B = 00H and C = FFH
2. INR C
C ® FFH
+ 01H
00H ® C
\ B = FFH and C = 00H
3. INX B
BC ® FF FF H
+ 00 01 H
00 00 H ® B C
\ B = 00H and C = 00H
Lab Experiment 8 : Check results after execution of DCR C, DCR B and DCX B instructions.
Statement : If the contents of B = 00H and C = 00H then after execution of following
instructions give the contents of register B and register C.
Instructions :
1. DCR C
2. DCR B
3. DCX B
1. DCR C
C ® 00H
– 01H
FFH ®C
\ B = 00H and C = FFH
TM
2. DCR B
B ® 00H
– 01H
FFH ® B
\ B = FFH and C = 00H
3. DCX B
BC ® 0 0 0 0 H
– 0 0 0 1 H
F F F F H ® BC
\ B = FFH and C = FFH
Source program
LDA 2200 H ; Get the number Complement the
number
CMA ; Complement the number
ADI, 01H ; Add one in the number
Add one
STA 2300H ; Store the result
HLT ; Terminate program execution
Store the result
End
TM
Flowchart
(See flowchart on next page)
Source program
LDA 2200H ; Get the packed BCD number
ANI F0H ; Mask lower nibble
RRC
RRC
RRC
RRC ; Adjust higher BCD digit as a lower digit
TM
Start
END
TM
Subroutine program :
6100H SUB : PUSH B
6101H PUSH H
6102H LXI B, 4080H
6105H LXI H, 4090H
6108H DAD B
6109H SHLD 2200H
610CH POP H
610DH POP B
610EH RET
Solution : The Table 2.5 shows the instruction sequence and the contents of all registers
and stack after execution of each instruction.
Table 2.5
Sample problem :
(2000H) = 7FH
(2001H) = 89H
Result = 7FH + 89H = 108H
(2002H) = 08H
(2003H) = 01H
Flowchart
Start
End
Source program :
LXI H, 2000H ; HL Points 2000H
MOV A, M ; Get first operand
INX H ; HL Points 2001H
ADD M ; Add second operand
INX H ; HL Points 2002H
MOV M, A ; Store the lower byte of result at 2002H
MVI A, 00 ; Initialize higher byte result with 00H
ADC A ; Add carry in the high byte result
INX H ; HL Points 2003H
MOV M, A ; Store the higher byte of result at 2003H
HLT ; Terminate program execution
TM
Flowchart
Start
Rotate 4 times
right
Store result in
C register
End
TM
Flowchart
Start
Stop
Source program :
MOV A, B
RAR
MOV B, A
MOV A, C
RAR
MOV C, A
HLT
Flowchart
Start
Add HL with
HL
Stop
TM
Sample problem :
HL = 1025 = 0001 0000 0010 0101
\ HL = 0001 0000 0010 0101
+ HL = 0001 0000 0010 0101
-----------------------------------------------
Result = 0010 0000 0100 1010
Source program :
DAD H
TM
6000H LXI SP, 27FFH 6000H LXI SP, 27FFH ; Initialize stack pointer
:
:
TM
Table 2.6
TM
Flowchart :
Start
Result = Result X no
Yes If
number < 2 No = No – 1
?
No Is
No
no = 0
Result = 1 Load counter ?
initialize result
Yes
Store result
End
Subroutine Program :
FACTO : LXI H,0000H
MOV B, C ; Load counter
BACK : DAD D ;
DCR B ;
JNZ BACK ; Multiply by successive addition
XCHG ; Store result in DE
DCR C ; Decrement counter
CNZ FACTO ; Call subroutine FACTO
RET ; Return to main program
TM
Solution :
Flags This instruction affects all flags. This instruction does not affect
flags
T-states required 4 7
Solution :
Flags This instruction affects all flags This instruction affects all flags
TM
T-states required 4 4
Solution :
Flags This instruction does not affect This instruction does not affect
flags flags
Required T-states 10 16
4. Memory read
5. Memory read
Solution :
TM
Flags This instruction does not affect This instruction does not affect
flags flags
Required T-states 10 6
2. Memory read
3. Memory read
Solution :
Flags This instruction does not affect This instruction does not affect
flags flags
Required T-states T-states required are undefined 4-T states. After this instruction
because this instruction halts processor fetches the next
the processor. The processor instruction after NOP
can be restarted by a valid
interrupt or by applying a
RESET signal
TM
ß Example 2.6 List out differences and similarities between CALL - RET and PUSH-POP
instructions.
Solution : Differences :
1 These instructions are used for the These instructions are used to store register
execution of subroutine data temporarily in memory.
2 CALL instruction store the address of next PUSH instruction stores register contents in
instruction after it in the stack and loads PC the stack.
with address given in the instruction.
3 RET instruction loads the address from POP instruction gets the register contents
stack into PC. from the stack.
Similarities :
1. They use stack memory.
2. CALL and PUSH instructions decrement stack pointer by 2.
3. RET and POP instructions increment stack pointer by 2
4. Instructions do not affect flags except POP PSW instruction.
Review Questions
Section 2.1
Q.1 Explain the classification of the instruction set of 8085 microprocessor with suitable
examples.
Section 2.2
Q.1 Explain the operations carried out when 8085 executes the instructions.
i) MOV A, M ii) XCHG
iii) DAD B iv) DAA Dec.-07, Marks 16
Q.2 With suitable examples, Explain the function of various data transfer and data
manipulation instructions of 8085. May-10,11, Marks 10
Q.3 How are the 8085 instructions classified according to the functional categories ?
Dec.-11, Marks 2
Q.4 Describe with suitable examples the data transfer, loading and storing instructions.
June-12, Marks 8
Q.6 Explain the operations carried out when 8085 executes the instructions.
POP PSW Dec.-07, Marks 16
Q.7 Discuss the organizations of the 8085 stack and the various instructions that will
operate on the stack. Dec-09, June-11, Marks 10
Q.8 Describe with a suitable example the operation of stack. June-12, Marks 8
Q.9 How is PUSH B instruction executed ? Find the status after the execution.
May-11, Marks 2
Q.10 Explain the sequence of events in the execution of CALL and RET instructions.
June-07, Marks 8
Q.11 What is the use of branching instructions ? Give Example. June-12, Marks 2
Q.12 State the function of given 8085 instructions : JP, JPE, JPO, JNZ
May-11, Marks 2
Section 2.3
Q.1 With example explain the different addressing modes of 8085 and the different types
of instruction. Dec.-04, Marks 16
TM
Q.2 Define and explain the addressing modes of 8085 with example.
June-06,07,11, May-08,10,11,12 Dec.-09,10, Marks 16
Section 2.4
Q.1 Explain the instruction set of 8085 with examples. Dec.-08, Marks 16
Section 2.5
Q.1 What is program ?
Q.2 Give the steps involved in programming.
Q.3 What is flowchart ? Explain its use.
Q.4 Explain the process of writing assembly language program with the help of example.
Q.5 What do you mean by hand assembly ? Explain with the help of example.
Q.6 Explain the process of executing the program on the microprocessor training kit.
Section 2.6
Q.1 Write a program with a flowchart to multiply two 8-bit numbers.
Dec.-11, Marks 8
Q.3 Write an assembly language program for arranging an array of 8-bit unsigned number
in ascending order. June-12, Marks 8
Section 2.7
Q.1 Compare the similarities and differences of CALL and RET instructions with PUSH
and POP instructions. Dec.-11, Marks 8
Q.2 Explain the operational difference between the following pairs of instructions.
i) SPHL and XTHL ii) CALL addr and JMP addr
iii) LHLD and SHLD addr iv) XRA A and MVI A, 00H
v) INR A and ADI 01 H vi) DAD RP and DAA.
Section 2.8
Q.1 Describe the instruction format of 8085 microprocessor. May-11, Marks 4
TM
Q.2 How many operations are there in the instruction set of 8085 microprocessor ?
Ans. : There are 74 operations in the 8085 microprocessor.
Q.4 List out the five categories of the 8085 instructions. Give examples of the
instructions for each group.
Ans. :
· Data transfer group - MOV, MVI, LXI.
· Arithmetic group - ADD, SUB, INR.
· Logical group -ANA, XRA, CMP.
· Branch group - JMP, JNZ, CALL.
· Stack I/O and Machine control group – PUSH, POP, HLT.
Q.6 What is the difference between the shift and rotate instructions?
Ans. : A rotate instruction is a closed loop instruction. That is, the data moved out at
one end is put back in at the other end. The shift instruction loses the data that is
moved out of the last bit locations.
Ans. : XCHG : This instruction exchanges the contents of the register H with that of
D and of L with that of E.
SPHL : This instruction copies the contents of HL register pair into the stack
pointer. The contents of H register are copied to higher order byte of stack pointer
and contents of L register are copied to the lower byte of stack pointer. This allows
indirect way of initializing stack pointer.
TM
Ans. : The sim instruction masks the interrupts as desired. It also sends out serial
data through the SOD pin.
Q.17 Write the operation carried out when 8085 executes RST0 instruction.
Dec.-07
Ans. : When 8085 executes RST0 instruction, the program control is transferred to
memory address 0000H. Before transfer of program control RST0 instruction saves the
current program counter contents on the stack and decrements stack pointer by 2.
Q.18 Write the difference between opcode and operand. May-08
Q.22 What are the use of CALL and RET instructions of 8085? Dec-09
Ans. : Refer section 2.2.6.
Q.23 Mention the instructions used for data transfer with I/O ports. May-10
Ans. : The instructions used for data transfer with I/O ports are : 1. IN addr 2. OUT
adder
TM
Ans. :
Q.25 What do you understand by the term 'program status word' and state how it
can be read ? Dec.-10
Q.26 What is the value of register A after each of the following instructions ?
MOV A, # 26H
RR A
RR A
RR A
RR A
SWAP A Dec.-10
Ans. : A = 26 H
qqq
TM
Contents
3.1 Looping, Counting and Indexing
3.2 Timers
3.3 Code Conversion
3.4 BCD Arithmetic
(3 - 1)
TM
In the last chapter we have seen the instruction set of 8085 and some simple assembly
language programs using it. We know that, the program is an implementation of certain
logic by executing group of instructions. To implement program logic we need to take help
of some common programming techniques such as looping, counting, indexing and code
conversion.
In this chapter, we are going to study how to implement these programming
techniques using 8085 assembly language and some programming examples using them.
This chapter also introduces the BCD arithmetic and programming techniques to
implement BCD arithmetic using 8085 assembly language.
TM
Start
Is Is
No looping looping Yes
over over
? ?
Yes No
End End
Flowchart 1 Flowchart 2
2. The actual data manipulation occurs in the processing section. This is the section
which does the work.
3. The loop control section updates counters, indices (pointers) for the next iteration.
4. The result section analyzes and stores the results.
Note : The processor executes initialization section and result section only once, while it
may execute processing section and loop control section many times. Thus, the execution
time of the loop will be mainly dependent on the execution time of the processing section
and loop control section. The flowchart 1 shows typical program loop. The processing
section in this flowchart is always executed at least once. If you interchange the position of
the processing and loop control section then it is possible that the processing section may
not be executed at all, if necessary. Refer flowchart 2.
TM
Program Examples
Lab Experiment 22 : Calculate the sum of series of numbers.
Statement : Calculate the sum of series of numbers. The length of the series is in memory
location 2200H and the series itself begins from memory location 2201H.
a. Assume the sum to be 8 bit number so you can ignore carries. Store the sum at
memory location 2300H.
b. Assume the sum to be 16 bit number. Store the sum at memory locations 2300H
and 2301H.
a. Sample problem :
2200H = 04H
2201H = 20H
2202H = 15H
2203H = 13H
2204H = 22H
Result = 20 + 15 + 13 + 22 = 6AH
\ 2300H = 6AH
Flowchart :
Start
Sum=0
Pointer = 2201H
Count = (2200H)
Pointer = Pointer +1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Sum
End
TM
Source program :
LDA 2200H
MOV C, A ; Initialize counter
SUB A ; sum = 0
LXI H, 2201H ; Initialize pointer
BACK : ADD M ; SUM = SUM + data
INX H ; Increment pointer
DCR C ; Decrement counter
JNZ BACK ; If counter ¹ 0 repeat
STA 2300H ; Store sum
HLT ; Terminate program execution
b. Sample problem :
2200H = 04H 2201H = 9AH
2202H = 52H 2203H = 89H 2204H = 3EH
Result = 9AH + 52H + 89H + 3EH = 1B3H
\ 2300H = B3H Lower byte 2301H = 01H Higher byte
Flowchart :
Start
Sum high = 0
Sum low = 0
Pointer = 2201H
Count = (2200H)
No Is
Carry 1
?
Yes
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Sum low
(2301H) = Sum high
End
TM
Source program :
LDA 2200H
MOV C, A ; Initialize counter
LXI H, 2201H ; Initialize pointer
SUB A ; Sumlow = 0
MOV B, A ; Sumhigh = 0
BACK : ADD M ; Sum = sum + data
JNC SKIP
INR B ; Add carry to MSB of SUM
SKIP : INX H ; Increment pointer
DCR C ; Decrement counter
JNZ BACK ; Check if counter ¹ 0 repeat
STA 2300H ; Store lower byte
MOV A, B
STA 2301H ; Store higher byte
HLT ; Terminate program execution
Lab Experiment 23 : Data transfer from memory block B1 to memory block B2.
Statement : Transfer ten bytes of data from one memory to another memory block. Source
memory block starts from memory location 2200H where as destination memory block
starts from memory location 2300H.
Flowchart :
Start
Initialize counter = 10
No Is
Count = 0
?
Yes
End
TM
Source program :
MVI C, 0AH ; Initialize counter
LXI H, 2200H ; Initialize source memory pointer
LXI D, 2300H ; Initialize destination memory pointer
BACK : MOV A, M ; Get byte from source memory block
STAX D ; Store byte in the destination memory block
INX H ; Increment source memory pointer
INX D ; Increment destination memory pointer
DCR C ; Decrement counter
JNZ BACK ; If counter ¹ 0 repeat
HLT ; Terminate program execution
Sample problem :
(2200H) = 03H
(2201H) = B2H
Result = B2H + B2H + B2H
= 216H
(2300H) = 16H
(2301H) = 02H
Note : In 8085 multiplication can be done by repetitive addition.
Flowchart :
Start
Initialize second
number as a counter
Result = 0
Decrement counter
No Is
count = 0
?
Yes
End
TM
Source program :
LDA 2200H
MOV E, A
MVI D, 00 ; Get the first number in DE register pair
LDA 2201H
MOV C, A ; Initialize counter
LXI H, 0000H ; Result = 0
BACK : DAD D ; Result = result + first number
DCR C ; Decrement count
JNZ BACK ; If count ¹ 0 repeat
SHLD 2300H ; Store result
HLT ; Terminate program execution
Flowchart :
Start
Quotient = 0
Quotient = quotient + 1
Is
No dividend <
divisor
Yes
Remainder = dividend
End
TM
Sample problem :
(2200H) = 60H
(2201H) = A0H
(2202H) = 12H
Result = A060H/12H = 8E8H Quotient
and 10H remainder
(2300H) = E8H
(2301H) = 08H
(2302H) = 10H
(2303H) = 00H
Source program :
LHLD 2200H ; Get the dividend
LDA 2202H
MOV C, A ; Get the divisor
LXI D, 0000H ; Quotient = 0
BACK : MOV A, L
SUB C ; Subtract divisor
MOV L, A ; Save partial result
JNC SKIP ; If CY ¹ 1 jump
DCR H ; Subtract borrow of previous subtraction
SKIP : INX D ; Increment quotient
MOV A, H
CPI, 00 ; Check if dividend < divisor
JNZ BACK ; If no repeat
MOV A, L
CMP C
JNC BACK
SHLD 2302H ; Store the remainder
XCHG
SHLD 2300H ; Store the quotient
HLT ; Terminate program execution
TM
Flowchart :
Start
Neg number = 0
Pointer = 2201H
Count = (2200H)
No Is
MSB =1
?
Yes
Neg number =Neg number+ 1
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Neg number
End
Sample problem :
(2200H) = 04H
(2201H) = 56H
(2202H) = A9H
(2203H) = 73H
(2204H) = 82H
Result = 02 since 2202H and 2204H contain numbers with a MSB of 1.
Source program :
LDA 2200H
MOV C, A ; Initialize count
MVI B, 00 ; Negative number = 0
LXI H, 2201H ; Initialize pointer
BACK : MOV A, M ; Get the number
ANI 80H ; Check for MSB
JZ SKIP ; If MSB = 1
TM
Flowchart :
Start
Count = 2200H
Pointer = (2201H)
Max = 0
No Is
Max < (Pointer)
?
Yes
Max = (Pointer)
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Max
End
TM
Sample problem :
(2200H) = 04
(2201H) = 34H
(2202H) = A9H
(2203H) = 78H
(2204H) = 56H
Result = (2202H) = A9H.
Source program :
LDA 2200H
MOV C, A ; Initialize counter
XRA A ; Maximum = Minimum possible value = 0
LXI H, 2201H ; Initialize pointer
BACK : CMP M ; Is number > maximum
JNC SKIP
MOV A, M ; Yes, replace maximum
SKIP : INX H
DCR C
JNZ BACK
STA 2300H ; Store maximum number
HLT ; Terminate program execution
Source program :
MVI B, 00H
MVI C, 08H
MOV A, D
BACK : RAR
JNC SKIP
INR B
SKIP : DCR C
JNZ BACK
HLT
TM
Flowchart :
Start
Initialize count = 0
Initialize counter = 8
Rotate contents of
accumulator so that
LSB will go in carry
No Is
carry = 1
?
Yes
Increment count
Decrement counter
No Is
counter = 0
?
Yes
Stop
Source program :
MVI B, 09 ; Initialize counter 1
START : LXI H, 2200H ; Initialize memory pointer
MVI C, 09H ; Initialize counter 2
BACK : MOV A, M ; Get the number
INX H ; Increment memory pointer
CMP M ; Compare number with next number
JC SKIP ; If less, don’t interchange
JZ SKIP ; If equal, don’t interchange
MOV D, M
MOV M, A
DCX H
TM
MOV M, D
INX H ; Interchange two numbers
SKIP : DCR C ; Decrement counter 2
JNZ BACK ; If not zero, repeat
DCR B ; Decrement counter 1
JNZ START ; If not zero, repeat
HLT ; Terminate program execution
Flowchart :
Start
Initialize counter 1 = 09
Is
No (Pointer – 1) >(Pointer)
?
Interchange contents
Decrement counter 2
Increment memory pointer
No Is
counter 2 = 0
?
Yes
Decrement counter 1
Is
counter 1 = 0
?
Stop
TM
Sample problem :
2200H = 4H
2201H = 20H
2202H = 15H
2203H = 13H
2204H = 22H
Result = 20 + 22 = 42H
\ 2210H = 42H
Flowchart :
Start
Sum = 0
Pointer = 2201H
Count = (2200H)
Is
(Pointer) = even No
number
?
Yes
Pointer = Pointer +1
Count = Count – 1
No Is
carry = 0
?
Yes
(2300H) = Sum
End
TM
Source program :
LDA 2200H
MOV C, A ; Initialize counter
MVI B, 00H ; sum = 0
LXI H, 2201H ; Initialize pointer
BACK : MOV A, M ; Get the number
ANI 01H ; Mask Bit1 to Bit7
JNZ SKIP ; Don’t add if number is ODD
MOV A, B ; Get the sum
ADD M ; SUM = SUM + data
MOV B, A ; Store result in B register
SKIP : INX H ; Increment pointer
DCR C ; Decrement counter
JNZ BACK ; If counter 0 repeat
STA 2210H ; Store sum
HLT ; Terminate program execution
Sample program :
2200H = 4H
2201H = 9AH
2202H = 52H
2203H = 89H
2204H = 3FH
Result = 89H + 3FH = C8H
\ 2300H = 61H Lower byte
2301H = 01H Higher byte
Source program :
LDA 2200H
MOV C, A ; Initialize counter
LXI H, 2201H ; Initialize pointer
MVI E, 00 ; Sumlow = 0
MOV D, E ; Sumhigh = 0
BACK : MOV A, M ; Get the number
TM
Flowchart :
Start
Sum = 0
Pointer = 2201H
Count = (2200H)
Is
(Pointer) = odd No
number
?
Yes
Pointer = Pointer +1
Count = Count – 1
No Is
carry = 0
?
Yes
(2300H) = Sum
End
TM
Flowchart :
Start
Lookup Table
Initialize source memory pointer
Initialize destination memory pointer Address Digit Square
6100H 0 0H
Get the number
6101H 1 1H
6103H 3 9H
Store square in the
destination memory location 6104H 4 10H
6105H 5 19H
Increment source memory pointer
Increment destination memory pointer 6106H 6 24H
6107H 7 31H
Is 6108H 8 40H
No
last number
?
6109H 9 51H
Yes
Stop
TM
Source program :
LXI H, 2000H ; Initialize memory pointer
MVI B, 52H ; Initialize counter
BACK : MOV A, M ; Get the number
CMP C ; Compare with the given byte
JZ LAST ; Go last if match occurs
INX H ; Increment memory pointer
DCR B ; Decrement counter
JNZ B ; If not zero, repeat
LXI H, 0000H
SHLD 2200H
JMP END ; Store 00 at 2200H and 2201H
LAST : SHLD 2200H ; Store memory address
END : HLT ; Stop
Flowchart :
Start
Is
(Pointer) = Yes
Search byte
?
No
Yes
Store 00 as a result
Stop
TM
Flowchart :
Start
Set carry = 0
Check
No for last
digit
Yes
Stop
TM
Source program :
LXI H,6000H ; Initialize pointer1 to first number
LXI D,6100H ; Initialize pointer2 to second number
LXI B,6200H ; Initialize pointer3 to result
STC
CMC ; Carry = 0
BACK : LDAX D ; Get the digit
ADD M ; Add two digits
DAA ; Adjust for decimal
STAX B ; Store the result
INX H ; Increment pointer1
INX D ; Increment pointer2
INX B ; Increment result pointer
MOV A, L
CPI 06H ; Check for last digit
JNZ BACK ; If not last digit repeat
HLT ; Terminate program execution
Lab Experiment 35 : Add each element of array with the elements of another array.
Statement : Add 2 arrays having ten 8-bit numbers each and generate a third array of
result. It is necessary to add the first element of array1 with the first element of array-2
and so on. The starting addresses of array1, array2 and array3 are 2200H, 2300H and
2400H, respectively.
Source program :
LXI H, 2200H ; Initialize memory pointer 1
LXI B, 2300H ; Initialize memory pointer 2
LXI D, 2400H ; Initialize result pointer
BACK : LDAX B ; Get the number from array 2
ADD M ; Add it with number in array 1
STAX D ; Store the addition in array 3
INX H ; Increment pointer1
INX B ; Increment pointer2
INX D ; Increment result pointer
MOV A, L ;
CPI 0AH ; Check pointer1 for last number
JNZ BACK ; If not, repeat
HLT ; Stop
TM
Flowchart :
Start
Pointer 1 = Pointer 1 + 1
Pointer 2 = Pointer 2 + 1
Pointer 3 = Pointer 3 + 1
Yes Is
Pointer 1 <10
?
No
Stop
Source program :
LXI H, 2200H ; Initialize memory pointer1
LXI D, 2300H ; Initialize memory pointer2
MVI C, 32H ; Initialize counter
BACK : MOV A, M ; Get the number
ANI 01H ; Check for even number
JNZ SKIP ; If ODD, don’t store
MOV A, M ; Get the number
STAX D ; Store the number in result list
TM
Is No
number = even
?
Yes
(Pointer 2) number
Pointer 2 = pointer 2 + 1
Pointer 1 = pointer 1 + 1
Counter = counter – 1
No Is
counter = 0
?
Yes
End
TM
Source program :
Two blocks (3000 – 30FF and 3050 – 314F) are overlapping. Therefore it is necessary to
transfer last byte first and first byte last.
MVI C, FFH ; Initialize counter
LXI H, 30FFH ; Initialize source memory pointer
LXI D, 314FH ; Initialize destination memory pointer
BACK : MOV A, M ; Get byte from source memory block
STAX D ; Store byte in the destination memory
; block
DCX H ; Decrement source memory pointer
DCX ; Decrement destination memory pointer
DCR C ; Decrement counter
JNZ BACK ; If counter ¹ 0 repeat
HLT ; Stop execution
Solution :
Step 1 : Move bytes from location 10 till the end of array by four bytes downwards.
TM
Solution : Shift bytes from location 14 till the end of array upwards by 4 characters i.e.
from location 10 on words.
LXI H, 210DH ; Initialize source memory pointer at the 14th
; location of the array.
LXI D, 2109H ; Initialize destination memory pointer at the
; 10th location of the array.
MOV A, M ; Get the character
STAX D ; Store character at new location
INX D ; Increment destination pointer
INX H ; Increment source pointer
MOV A, L ; [ check whether desired
CPI 32H ; bytes are shifted or not]
JNZ REPE ; If not repeat the process.
HLT ; Stop
TM
Flowchart :
Start
No Is
Parity odd
?
Yes
Increment memory
Pointer
Decrement character
counter
Is
character No
counter 0?
Yes
End
TM
Source program :
LXI H, 2040H
MOV C, M ; Counter for character
REPEAT : INX H ; Memory pointer to character
MOV A, M ; Character in accumulator
ORA A ; ORing with itself to check parity.
JPO PEREVEN
ORI 80H ; If odd parity place even parity in
; D7(80).
PEREVEN : MOV M, A ; Store converted even parity character.
DCR C ; Decrement counter.
JNZ REPEAT ; If not zero go for next character.
HLT ; Terminate program execution
Lab Experiment 41 : Find the number of negative, zero and positive numbers.
Statement : A list of 50 numbers is stored in memory, starting at 6000H. Find number of
negative, zero and positive numbers from this list and store these results in memory
locations 7000H, 7001H, and 7002H respectively.
Source program :
LXI H, 6000H ; Initialize memory pointer
MVI C, 00H ; Initialize number counter
MVI B, 00H ; Initialize negative number counter
MVI E, 00H ; Initialize zero number counter
BEGIN : MOV A, M ; Get the number
CPI 00H ; If number = 0
JZ ZERONUM ; Goto zeronum
ANI 80H ; If MSB of number = 1 i.e. if
JNZ NEGNUM ; Number is negative goto NEGNUM
INR D ; Otherwise increment positive number Counter
JMP LAST ;
ZERONUM : INR E ; Increment zero number counter
JMP LAST
NEGNUM : INR B ; Increment negative number counter
LAST : INX H ; Increment memory pointer
INR C ; Increment number counter
MOV A, C
CPI 32H ; If number counter = 5010 then
JNZ BEGIN ; Store otherwise check next number
TM
Flowchart :
Start
Yes Is
number = 0
?
No
Is
number <0? Yes
Increment zero
number counter MSB=1
No Increment negative
number counter
Increment positive number
counter
Numbers = Numbers + 1
No Is
number =50
?
Yes
Stop
TM
Lab Experiment 42 : Multiply two eight bit numbers with shift and add method.
Statement : Multiply the 8-bit unsigned number in memory location 2200H by the 8-bit
unsigned number in memory location 2201H. Store the 8 least significant bits of the result
in memory location 2300H and the 8 most significant bits in memory location 2301H.
Sample problems :
(2200) = 1100 (0CH)
(2201) = 0101 (05H)
Multiplicand Multiplier Result
1100 (1210) 0101 (510) 12 ´ 5 = 6010
For simplicity, Multiplicand and Multiplier are taken 4-bit each.
B 7 B 6 B5 B4 B3 B2 B1 B 0 CY B 3 B 2 B1 B 0
0 0 0 0 0 0 0 0 0 0 1 0 1 Initial stage
Step 2 0 0 0 0 0 0 0 0 1 0 1 0 0 Shift
Source program :
LXI H, 2200H ; Initialize the memory pointer
MOV E, M ; Get multiplicand
MVI D, 00H ; Extend to 16-bits
INX H ; Increment memory pointer
MOV A, M ; Get multiplier
LXI H, 0000H ; Product = 0
MVI B, 08H ; Initialize counter with count 8
MULT : DAD H ; Product = product ´ 2
RAL
JNC SKIP ; Is carry from multiplier 1 ?
TM
Flowchart :
Start
Product = 0
Count = 8
Multiplicand = (2200H)
Multiplier = (2201H)
Product = 2 X product
( Shift left 1 bit )
Multiplier = 2 X multiplier
(Shift left 1 bit )
Is
No carry from
Multiplier 1
?
Yes
Count = count – 1
Yes
No Is
count = 0
?
Yes
End
Lab Experiment 43 : Divide 16-bit number with 8-bit number using shifting technique.
Statement : Divide the 16-bit unsigned number in memory locations 2200H and 2201H
(most significant bits in 2201H) by the 8-bit unsigned number in memory location 2300H
store the quotient in memory location 2400H and remainder in 2401H.
Assumption : The most significant bits of both the divisor and dividend are zero.
TM
Sample problem :
For simplicity, Dividend and divisor are taken 8-bit and 4-bit respectively
Dividend = 0110 0001 (61H) Divisor = 0111 (07H)
B7 B 6 B5 B4 B3 B2 B 1 B0 B3 B 2 B1 B 0
0 1 1 0 0 0 0 1 0 0 0 0 Initial stage
Source program :
MVI E, 00 ; Quotient = 0
LHLD 2200H ; Get dividend
LDA 2300 ; Get divisor
MOV B, A ; Store divisor
MVI C, 08 ; Count = 8
NEXT : DAD H ; Dividend = Dividend ´ 2
MOV A, E
RLC
MOV E, A ; Quotient = ´ 2
MOV A, H ;
SUB B ; Is most significant byte of Dividend
; > divisor
JC SKIP ; No, go to Next step
MOV H, A ; Yes, subtract divisor
INR E ; and Quotient = Quotient + 1
TM
Flowchart :
Start
Dividend = Dividend X 2
Quotient = Quotient X 2
Is
Divisor < = Yes
8 MSBS of
Dividend
?
8 MSBS of dividend = 8 MSBS of
No dividend – divisor
Quotient = Quotient + 1
Count = count –1
No Is
count = 0
?
Yes
(2400H) = Quotient
(2401 H) = Remainder
End
TM
Flowchart :
Start
Is
number > 9 No
or AC = 1
?
Yes
Is
number > 9 No
or CY = 1
?
Yes
End
Sample Problem :
Let us see the execution of DAA instruction.
1. If the value of the low order four bits (D 3 -D 0 ) in the accumulator is greater than 9
or if auxiliary carry flag is set, the instruction adds 6 (06) to the low-order four
bits.
TM
2. If the value of the high-order four bits (D7-D4) in the accumulator is greater than 9
or if carry flag is set, the instruction adds 6(06) to the high-order four bits.
Note : To check auxiliary carry flag it is necessary to get the flag register contents in
one of the registers and then we can check the auxiliary carry flag by checking bit 4 of
that register. To get the flag register contents in any general purpose register we require
stack operation and therefore stack pointer is initialized at the beginning of the source
program.
Source program :
LXI SP, 27FFH ; Initialize stack pointer
MOV E, A ; Store the contents of accumulator
ANI 0FH ; Mask upper nibble
CPI 0AH ; Check if number is greater than 9
JC SKIP ; If no go to skip
MOV A, E ; Get the number
ADI 06H ; Add 6 in the number
JMP SECOND ; Go for second check
SKIP : PUSH PSW ; Store accumulator and flag contents
; in stack
POP B ; Get the contents of accumulator in B
; register and
; flag register contents in C register
MOV A, C ; Get flag register contents in
; accumulator
ANI 10H ; Check for bit 4
JZ SECOND ; If zero, go for second check
MOV A, E ; Get the number
ADI 06 ; Add 6 in the number
SECOND : MOV E, A ; Store the contents of accumulator
ANI F0H ; Mask lower nibble
RRC
RRC
RRC
RRC ; Rotate number 4 bit right
CPI 0AH ; Check if number is greater than 9
JC SKIP1 ; If no go to skip 1
MOV A, E ; Get the number
ADI 60H ; Add 60 H in the number
TM
Source Program :
LXI H, 4000H ; Initialize memory pointer
BACK : MVI M, FFH ; Writing ‘1’ into RAM
MOV A, M ; Reading data from RAM
CPI FFH ; Check for ERROR
JNZ ERROR ; If yes go to ERROR
INX H ; Increment memory pointer
MOV A, H
CPI 50H ; Check for last check
JNZ BACK ; If not last repeat
LXI H, 4000H ; Initialize memory pointer
BACK1 : MVI M, 00H ; Writing ‘0’ into RAM
MOV A, M ; Reading data from RAM
CPI 00H ; Check for ERROR
INX H ; Increment memory pointer
MOV A, H
CPI 50H ; Check for last check
JNZ BACK1 ; If not last, repeat
HLT ; Stop execution
TM
Algorithm
1. Read number a.
2. Find a2 by performing a ´ a and store result 1.
3. Read number b.
4. Find b2 by performing b ´ b and store result 2.
5. Result = Result 1 + Result 2
Flowchart
See flowchart on next page.
Program
MVI E, Number a ; Get the number a
CALL MULTIPLY ; Call multiply subroutine
SHLD 2200H ; Store result 1
MVI E, Number b ; Get the number b
CALL MULTIPLY ; Call multiply subroutine
XCHG ; Store result 2 in DE
LHLD 2200H ; Get the result 1 in HL
DAD D ; HL ¬ HL + DE
HLT ; Stop
MULTIPLY : MVI D, 00 ; Extend to 16-bit
MOV A, E ; Multiplier = multiplicand
LXI H, 0000H ; Product = 0
MVI B, 08H ; Initialize counter with count 8
TM
Start
Read a
Multiplicand = a
Multiplier = a
Call Multiply
Store result 1
Read b
Multiplicand = b
Multiplier = b
Call multiply
Store result 2
Stop
TM
Lab Experiment 48 : Program to count given data in a set of numbers Dec.-08, Marks 6
Statement : Write a program to count the number of times the data 02 is present in a set of
20 numbers.
It is assumed that set of 20 numbers are stored from memory location 2000H.
Statement : Write an assembly language program in 8085 to multiply two 16-bit numbers.
LXI SP, 27FFH ; Initialize stack pointer
LXI H, 0000H ; Result = 0 (Lower word)
SHLD 2000H ; Result = 0 (Higher word)
LXI D, number 1 ; Multiplicand
LXI B, number 2 ; Multiplier as a counter
BACK : DAD D ; Result (HL) = HL + DE
JNC NEXT ; If no carry goto NEXT
PUSH H ; Save HL register
LHLD 2000H ; Get the higher word
INX H ; Increment word by 1
SHLD 2000H ; Save higher word
TM
3.2 Timers
In the real time applications, such as traffic light control, digital clock, process control,
serial communication, it is important to keep a track with time. For example in traffic light
control application, it is necessary to give time delays between two transitions. These time
delays are in few seconds and can be generated with the help of executing group of
instructions number of times. This software timers are also called time delays or software
delays. Let us see how to implement these time delays or software delays.
As you know microprocessor system consists of two basic components, Hardware and
software. The software component controls and operates the hardware to get the desired
output with the help of instructions. To execute these instructions, microprocessor takes fix
time as per the instruction, since it is driven by constant frequency clock. This makes it
possible to introduce delay for specific time between two events. In the following section
we will see different delay implementation techniques.
In this program, the instructions DCR C and JNZ BACK execute number of times
equal to count stored in the C register. The time taken by this program for execution can
be calculated with the help of T-states. The column to the right of the comments indicates
the number of T-states in the instruction cycle of each instruction. Two values are specified
for the number of T-states for the JNZ instruction. The smaller value is applied when the
condition is not met, and the larger value applied when it is met. The first instruction
MVI C, count executed only once and it requires 7 T-states. There are count – 1 passes
through the loop where the condition is met and control is transferred back to the first
instruction in the loop (DCR C). The number of T-states that elapse while C is not zero are
(count - 1) ´ (4+10). On the last pass through the loop, the condition is not met and the
loop is terminated. The number of T states that elapse in this pass are 4 + 7.
\ Total T-states required to execute the given program
= 7 + (count–1) ´ (4 + 10 ) + (4 + 7)
MVI C Loops Last loop
For count = 5
Number of T-state = 7 + (5 –1) ´ (14) + (11)
= 7 + 56 + 11
= 74
Assuming operating frequency of 8085A is 2 MHz,
1
Time required for 1 T-state =
2 MHz
= 0.5 msec
Total time required to execute the given program = 74 ´ 0.5 msec.
= 37 msec.
Maximum delay possible with 8-bit count.
The maximum count that can be loaded in the 8 bit register is FFH (255) so the
maximum delay possible with 8 bit count, assuming operating frequency 2 MHz
= (7 + (255 – 1) ´ (14) + (11)) ´ 0.5 msec.
= 1787 msec.
With these calculations, it can be noticed that delay with 8 bit count suitable for small
delays and not for large delays.
TM
In this program, the instructions DCX B, MOV A, C, ORA B and JNZ BACK execute
number of times equal to count stored in BC register pair. The instruction LXI B, count is
executed only once. It requires 10 T-states. The number of T-states required for
one loop = 6 + 4 + 4 + 10 = 24 T-states. The number of T-states required for
last loop = 6 + 4 + 4 + 7 = 21 T-states. So total T-states required for execution of given
program are
= 10 + (count–1) ´ 24 + 21
LXI B Loops Last loop
for count = 03FFH (102310 )
Number of T-states = 10 + (1022) ´ 24 + 21
= 24559
Assuming operating frequency of 8085A as 2 MHz, the time required for,
T state = 0.5 msec.
\ Total time required to execute the given program
= 24559 ´ 0.5 msec
= 12279.5 msec
= 12.2795 msec
= 1 ´ 10 6
Delay Program :
LXI B, count ; 16-bit count
BACK : DCX B ; Decrement count
MOV A, C
ORA B ; Logically OR B and C
JNZ BACK ; If result is not zero repeat
1 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
æ 1 ´ 10 6 – 31 ö
count = ç ÷ + 1 » 41666
ç 24 ÷ 10
è ø
count = 41666 10
= A2C2H
Delay Subroutine :
Delay : LXI D, count ; Initialize count
BACK : DCX D ; Decrement count
MOV A, E ;
ORA D ; Logically OR D and E
JNZ BACK ; If result is not 0 repeat
RET ; Return to main program
Flowchart :
Start
Initialize counter = 00
Delay
Call display
Initialize counter
Call delay
Decrement counter
Increment counter
No Is
counter = 0
?
No Is
count > FFH Yes
?
RET
Yes
Stop
1 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
TM
æ 1 ´ 10 6 – 31 ö
count = ç ÷+1 » 4166610
ç 24 ÷
è ø
count = 4166610 = A2C2H
Lab Experiment 52 : Generate and display BCD up counter with frequency 1 Hz.
Statement : Write a program for displaying BCD up counter. Counter should count
numbers from 00 to 99H and it should increment after every 1 sec. Assume operating
frequency of 8085 equal to 3 MHz. Display routine is available.
Solution :
LXI SP, 27FFH ; Initialize stack pointer
MVI C, 00H ; Initialize counter
BACK : CALL Display ; Call display subroutine
CALL Delay ; Call delay subroutine
MOV A, C ;
ADI , 01 ; Increment counter
DAA ; Adjust it for decimal
MOV C, A ; Store count
CPI ,00 ; Check count is > 99
JNZ BACK ; If not, repeat
HLT ; Stop
Delay Subroutine :
Delay : MVI B, Multiplier-count ; Initialize multiplier count
BACK1: LXI D, Initialize Count
BACK : DCX D ; Decrement count
MOV A, E ;
ORA D ; Locally OR D and E
JNZ BACK ; If result is not 0, repeat
DCR B ; Decrement multiplier count
JNZ BACK1 ; If not zero, repeat
RET ; Return to main program.
Operating frequency : 3 MHz
1
\ Time for one T-state = = 0.333 msec
3 MHz
TM
Flowchart :
Start
Initialize counter = 0
Call display
Delay
Call delay
Initialize counter
Increment counter
Decrement counter
No Is
counter = 0
?
No Is
count > 99 Yes
?
RET
Yes
Stop
Required Time
\ Number of T-states required = = 3 ´ 10 6
Time for 1- T state
1 sec
=
0.333 msec
Let us take multiplier count = 3.
3 ´ 10 6
\ Number of T-states required by inner loop = = 1 ´ 10 6
3
\ 1 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
» 4166610
count = 4166610 = A2C2H
Lab Experiment 53 : Generate and display BCD down counter with frequency 1 Hz
Statement : Write a program for displaying BCD down counter. Counter should count
numbers from 99 to 00 and it should decrement after every 1 sec. Assume display and
delay routines are available.
TM
Solution : Flowchart :
Start
Initialize counter = 99
Call display
Call delay
No Is
count < 0
?
Yes
End
Source program :
MVI C, 99H ; Initialize counter
BACK : MOV A, C ;
ANI 0F ; Mask higher nibble
CPI 0F
JNZ SKIP
MOV A, C
SUI 06 ; Subtract 6 to adjust decimal count
MOV D, A
SKIP : MOV A, C
OUT 05 ; send count on output port
CALL Delay ; Wait for 0.5 seconds
DCR C ; decrement count
MOV A, C
CPI FF
JNZ BACK ; If not zero, repeat
HLT ; Stop execution
Delay subroutine :
Delay : LXI D, Count
Back : DCX D ; 6 T-states
MOV A, D ; 4 T-states
TM
ORA E ; 4 T-states
JNZ Back ; 10 T-states
RET
1 1
1 T-state = =
Operating frequency 3.072 ´ 10 6
= 3.2552 ´ 10 –7
0.5 sec
Number of T-states required = = 1.536 ´ 10 6
3.2552 ´ 10 –7
1.536 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
1.536 ´ 10 6 – 31
Count = + 1 = 63999.708
24
= 64000
= FA00H
Lab Experiment 55 : Identify the error and correct the given delay routine.
Statement : The delay routine given below is in infinite loop, identify the error. Correct
the program, give the machine cycles and T states of each instruction and also find the
maximum delay generated. Assume 1 "T" state = 320 ns.
DELAY : LXI H, N
L1 : DCX H
JNZ L1
Solution : 1) The fault in the above program is at instruction JNZ L1. This condition
always evaluates to be true hence loops keeps on executing and hence infinite loop.
2) Reason for infinite looping : - The instruction DCX H decrease the HL pair count
one by one but it does not affect the zero flag. So when count reaches to 0000H in HL pair
zero flag is not affected and JNZ L1 evaluates to be true and loop continues. Now HL
again decrements below 0000H and HL becomes FFFFH and thus execution continues.
3) The modification in the program is as follows :
No. of T states
DELAY : LXI H, N ; Load 16 bit count ® 10 T-states
L1 : DCX H ; Decrement count ® 6 T-states
MOV A, L ; ® 4 T-states
ORA H ; logically OR H and L ® 4 T-states
JNZ L1 ; If result is not 0 repeat ® 10 T-states
\ Total number of T states required for program execution are
= 10 + (count – 1) ´ 24 + 21
TM
= 0.50407904 seconds
= 3CH + 7 = 43H
TM
Sample Problem :
(2200H) = 67H
(2300H) = 6 ´ 0AH + 7 = 3CH + 7 = 43H
Source Program :
LDA 2200H ; Get the BCD number
MOV B, A ; Save it
ANI 0FH ; Mask most significant four bits
MOV C, A ; Save unpacked BCD1 in C register
MOV A, B ; Get BCD again
ANI F0H ; Mask least significant four bits
RRC ; Convert most significant four
RRC ; bits into unpacked BCD2
RRC ;
RRC ;
Flowchart :
Start
Multiply BCD2
number by 10
Add BCD1
Store result
Stop
TM
To represent the number in BCD requires twelve bits or three BCD digits as shown
below
12310 = 0001 0010 0011
Digit2 digit1 digit0
Step 1 : If the number is equal to or greater than 100, divide number by 100 (i.e.
subtract 100 repeatedly until the remainder is less than 100). The quotient
gives the most significant digit, digit 2 of the BCD number. If number is less
than 100 go to step 2.
Step 2 : If the number i.e. remainder of first division is equal to or greater than 10
divide number by 10 repeatedly until the remainder is less than 10. The
quotient gives the digit 1. If number is less than 10, go to step 3.
Source Program :
LXI SP, 27FFH ; Initialize stack pointer
LDA 6000H ; Get the binary number in accumulator
CALL BIN TO BCD ; Call subroutine BIN TO BCD
HLT ; Terminate program execution
Flowchart :
Start
Check if Yes
number
is > 100
Divide number by 100
No
Check if Yes
reminder
is > 10
Divide number by 10
No
Digit 0 = reminder
Stop
TM
Source Program :
LXI H, 6200H ; Initialize lookup table pointer
LXI D, 6000H ; Initialize source memory pointer
LXI B, 7000H ; Initialize destination memory pointer
TM
Flowchart :
Start
Lookup Table
1 06
Get the number
2 5B
3 4F
Find the 7-segment code
4 66
7 07
Increment source memory pointer
Increment destination memory pointer 8 7F
9 6F
Is
No last
number
?
Yes
Stop
TM
Therefore, by adding 30H we can convert number into its ASCII equivalent and by adding
37H we can convert letter to its ASCII equivalent. Let us see the program for binary to
ASCII code conversion.
Flowchart :
Start
Initialize count = 5
Is Yes
number > A
Get the number ?
No
CALL ASCII Number = number + 30 Number = number + 37
Decrement counter
No Is
count = 0
?
Yes
End
TM
Sample Problem :
(2000H) = 1
(2001H) = 2
(2002H) = 9
(2003H) = A
(2004H) = B Result (2200H) = 31
(2201H) = 32
(2202H) = 39
(2203H) = 41
(2204H) = 42
Subroutine Documentation :
Subroutine ‘ASCII’ converts a hexadecimal digit to ASCII.
Passing parameter : The digit is passed using accumulator.
Return value : In the accumulator.
Register used : Accumulator.
Stack used : From 27FEH to 27FDH
Source Program :
LXI SP, 27FFH ; Initialize stack pointer
LXI H, 2000H ; Source memory pointer
LXI D, 2200H ; Destination memory pointer
MVI C, 05H ; Initialize the counter
BACK : MOV A, M ; Get the number
CALL ASCII ; Call subroutine ASCII
STAX D ; Store result
INX H ; Increment source memory pointer
INX D ; Increment destination memory pointer
DCR C ; Decrement count by 1
JNZ BACK ; if not zero, repeat
HLT ; Stop program execution subroutine ASCII
ASCII : CPI, 0AH ; Check if number is 0AH
JNC NEXT ; If yes goto next otherwise continue
ADI 30H ;
JMP LAST
NEXT : ADI 37H
LAST : RET ; Return to main program
TM
The addition is carried out as in normal binary addition and the sum is 1 0 0 1, which
is BCD code for 9.
The sum 1 1 1 0 is an invalid BCD number. This has occurred because the sum of the
two digits exceeds 9. Whenever this occurs the sum has to be corrected by the addition of
six (0110) in the invalid BCD number, as shown below
6 0110 ¬ BCD for 6
+ 8 1000 ¬ BCD for 8
-------- ----------
14 1110 ¬ Invalid BCD number
+ 0110 ¬ Add 6 for correction
-----------------------------
0001 0100 ¬ BCD for 14
1424 3 12
4 4 3
1 4
After addition of 6 carry is produced into the second decimal position.
TM
In this, case, result (0001 0001) is valid BCD number, but it is incorrect. To get the
correct BCD result correction factor of 6 has to be added to the least significant digit sum,
as shown.
8 1000 ¬ BCD for 8
+ 9 1001 ¬ BCD for 9
------- -------------------
17 00010001 ¬ Incorrect BCD result
+ 00000110 ¬ Add 6 for correction
--------------------------------------
00010111 ¬ BCD for 17
Going through these three cases of BCD addition we can summarize the BCD addition
procedure as follows :
1. Add two BCD numbers using ordinary binary addition.
2. If four-bit sum is equal to or less than 9, no correction is needed. The sum is in
proper BCD form.
3. If the four-bit sum is greater than 9 or if a carry is generated from the four-bit
sum, the sum is invalid.
4. To correct the invalid sum, add 01102 to the four-bit sum. If a carry results from
this addition, add it to the next higher-order BCD digit.
The 8085 supports DAA (Decimal Adjust Accumulator) instruction for adjusting the
result of addition to the BCD number. (See chapter 2 for DAA instruction).
Sample problem :
Get the first BCD number
(2200H) = 39
(2201H) = 45
Get the second BCD number
Result = (2300H) = 39 + 45
= 7E + 6 = 84
(lower nibble is greater than 9 so add 6) Add two BCD numbers
Source program :
LXI H, 2200H ; Initialize pointer Adjust result to valid BCD number
TM
Sample problem :
(HL) = 3629
(DE) = 4738
Flowchart :
Start
End
TM
Source program :
MOV A, L ; Get lower 2 digits of no. 1
ADD E ; Add two lower digits
DAA ; Adjust result to valid BCD
STA 2300H ; Store partial result
MOV A, H ; Get most significant 2 digits of no. 2
ADC D ; Add two most significant digits
DAA ; Adjust result to valid BCD
STA 2301H ; Store partial result
HLT ; Terminate program execution.
Source Program :
MVI A,99H
SUB E ; Find the 99's complement of subtrahend
INR A ; Find 100's complement of subtrahend
TM
Source Program :
MVI C, Multiplier ; Load BCD multiplier
MVI B, 00 ; Initialize counter
LXI H, 0000H ; Result = 0000
MVI E, multiplicand ; Load multiplicand
MVI D, 00H ; Extend to 16-bits
BACK : DAD D ; Result ¬ Result + Multiplicand
M