0% found this document useful (0 votes)
25 views25 pages

RSGaonkar8085 1

Contains very important information regarding the working of microprocessors .It includes several instructions on how to operate 8085 microprocessors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views25 pages

RSGaonkar8085 1

Contains very important information regarding the working of microprocessors .It includes several instructions on how to operate 8085 microprocessors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

176

pROGRAMMING THE 80R:

Explain the use of logic instructions in


U Write a program in the proper format showing
mne
setting, and resetting individual bits masking,
OExplain the unconditional and conditional Jump
memory addresses, Hex machine codes,
monics, and comments. instructions and how flags are used by the condi-
to
OExplain the arithmetic instructions, and recog tional Jump instructions change the sequence
nize the flags that are set or reset for given data of a program.
conditions. to illustrate an
O Write a program application of
OWrite aset of instructions to perform an addition Jump instructions.
and a subtraction (in 2's complement). OList the important steps in writing and tro.
U Explain the logic instructions, and recognize the bleshooting a simple program.
fiags that are set or reset for given data conditions.
OWritea set of instructions to illustrate logic oper
ations.

6.1 DATA TRANSFER (COPY) OPERATIONS

One of the primary functions of the microprocessor is copying data, from a register (or
VOor mnemory) called the source, to another register (or I/O or memory) called the des
tination. In technical literature, the copying function is frequently labeled as the data
transfer function, which is somewhat misleading. In fact, the contents of the source re
not transferred, but are copied into the destination register without modifying the contents
of the Source.
Several instructions are used to copy data (as listed in Chapter 2). This section is
concerned with the following operations.
MOV : Move Copy a data byte.
MVI : Move Immediate Load adata byte directly.
OUT :0utput to Port Send a data byte to an output device.
IN :Input from Port Read a data byte from an input device.
The term copy is equally valid for input/output functions because the contents of the
source are not altered, However, the term data transfer is used so commonly to indicate
the data copy function that, in this book, these terms are used interchangeably when the
meaning is not ambiguous.
In addition to data copy instructions, it is necessary to introduce two machine
control operations to execute programs.
HLT: Halt Stop processing and wait.
NOP: No Operation Donot perform any operation.
These operations (opcodes) are explained and illustrated below with examples.
Instructions The data transfer instructions copy data from a source into a destination
without modifying the contents of the source. The previous contents of the destination are
replaced by the contents of the source.
INTRODUCTION TO 8085
INSTRUCTIONS 177

Important Note: ln the 8085 processor, data


transfer instructions do not affect the nags.
Opcode Operand
MOV Rd,Rs*
Description
Move
O This is a 1-byte instruction
UCopies data from source register Rs to destina
MVI R,8-bit*
tion register Rd
Move Immediate
OThis is a 2-byte instruction
DLoads the 8 bits of the second byte into the
OUT
register specified
8-bit port address Output to Port
OThis is a 2-byte instruction
D Sends (copies) the contents of the accumulator
(A) to the output port specified in the second
byte
IN 8-bit port address Input from Port
DThis is a 2-byte instruction
OAccepts (reads) data from the input port speci
fied in the second byte, and loads into the ac
cumulator
HLT Halt
O This is a 1-byte instruction
OThe processor stops executing and enters wait
state
OThe address bus and data bus are placed in
high impedance state. No register contents are
affected
NOP No Operation
OThis is a 1-byte instruction
ONo operation is performed
nGenerally used to increase processing time or
substitute in place of an instruction, When an
error occurs in a program and an instruction
needs to be eliminated, it is more convenient
to substitute NOP than to
reassemble the
whole program

*The Symbols Rd, Rs. and Rare generic terms; they represent any of the 8085 g.hit registers: A, B, C, D, E, H,
and L.
178
PROGRAMMING THE 8085
Example Load the accumulator A with the data byte 82H (the letter H
indicates hexadecimal num.
6.1 ber), and save the data in register B.
Instructions MVIA, 82H,
MOV B,A
The first instruction is a 2-byte instruction that loads the
82H, and the second instruction MOVB,A copies the accumulator with the data byte
ister B without changing the contents of the
contents of the accumulator in reg
accumulator.
Example Write instructions to read eight ON/OFF switches
6.2 connected to the input port with the ad
dress 0OH, and turn on the devices connected to the
output port with the address 01lH, as
shown in Figure 6.1. (I/O port addresses are given in
hexadecimal.)
Solution The input has eight switches that are connected to the data bus
Any one of the switches can be connected to +5 V through the tri-state buffer.
each switch controls the corresponding device at the (logic 1) or to ground (logic 0), and
needs to read the bit pattern on the switches and send theoutput port. The microprocessor
same bit pattern to the output
port to turn on the corresponding devices.
+5 V

S, OFF 0

S ON 1 D,
Ss OFF 0 Data Bus
S4 OFF 0 Tri-State 4F
Buffer A 4F
B
Flags
S3 ON 1
D
Input
S ON Port H
00H
S ON 1 Do
So ON 1
EN Turm Solid State
OFF Relay Air Conditioner
Input Enable ON
Space Heater
OFF
4F Output Coffeepot
Port OFF Radio
01H ON
1 Light I
ON
1 Light 2
ON
Light 3
EN ON
Light 4
Output Enable
FIGURE 6.1
Reading Data at Input Port and Sending Data to Output Port
INTRODUCTION TO
8085
INSTRUCTIONS
Instructions IN 00H
OUT 01H
HLT

When the microprocessor executes the


instruction IN 00H, it enables the tri-State
buffer. 1 he bit pattern 4FH formed by the switch positions is placed on the data bus and
transferred to the accumulator. This is called reading an input port.
When the microprocessor executes the next instruction, OUT
contents of the accumulator on the data bus and enables the output port01H, it places the
01H. (This 1s also
called writing data to an output port.) The output port latches the bit pattern
ON/OFF the devices connected to the port according to the bit pattern. In Figureanao.l,turisu
bit pattern 4FH will turn on
the devices connected to the output port data lines D6, D3, D2
D, and Do: the space heater and four light bulbs. To turn off some of the devices and turn
on other devices, the bit pattern can be modified by changing the switch positions. For ex
ample, to turn on the radio and the coffeepot and turn off all other devices, the switches
S, and S, should be on and the others should be off. The microprocessor willread the bit
pattern 0011 0000, and this bit pattern will turn on the radio and the coffeepot and turn
off other devices.

The preceding explanation raises two questions:


1. What are the second bytes in the instructions IN and OUT?
2. How are they determined?
In answer to the first question, the second bytes are I/O port addresses. Each
I/O port is identified with a number or an address similar to the postal address of a
house. The second byte has eight bits, meaning 256 (2°) combinations; thus 256 in
connected to
put ports and 256 output ports with addresses from 00H to FFH can be
the system.
interfacing)
The answer to the second question depends on the logic circuit (called
Chapter 5).
used to connect and identify a port by the system designer (see

6.1.1 Addressing Modes


copy 8-bit data from a
The above instructions are commands to the microprocessor to
register, an input port,
source into a destination. In these instructions, the source can be a
register or an output
or an 8-bit number (00H to FFH). Similarly, a destination can be a of specify
The various formats
port. The sources and destination are, in fact, operands.
modes. The 8085 instruction set has the fol.
ing the operands are called the addressing by the corespond
lowing addressing modes. (Each mode is followed by an example and Chapter 2 )
the analogy discussed in
ing piece of restaurant conversation from
R,Data (Pass the butter)
1. Immediate Addressing-MVI
Addressing-MOV Rd,Rs (Pass the bowl)
bo Register (Combination number 17 on the menn)
3. Direct Addressing--IN/OUT Port#
next chapter (I will have what Susie has)
4. Indirect Addressing-Illustrated in the
80 PROGRAMMING THE 8088

The classification of the addressing modes is unimportant, except that it provides


some clues in understanding mnemonics. For example, in the case of the MVIopcode, the
letter I suggests that the second byte is data and not a register. What is important is to be
come familiar with the instructions. After youstudy the examples given in this chapter.
you will see a pattern begin to emerge.

6.1.2 lustrative Program: Data Transfer-From Register


to Output Port
PROBLEM STATEMENT
Load the hexadecimal number 37H in register B, and display the number at the output
port labeled PORT1.
PROBLEM ANALYSIS
This problem is similar to the illustrative program discussed in Section 2.4.1. Even
though this is a very simple problem it is necessary to break the problem into small steps
and to outline the thinking process in terms of the tasks described in Section 6.1.
STEPS
Step 1: Load register B with a number.
Step 2: Send the number to the output port.
QUESTIONS TO BE ASKED
O Is there an instruction to load the register B? YES-MVIB.
DIs there an instruction to send the data from register B to the output port? NO. Review
the instruction OUT. This instruction sends data from the accumulator to an outputport.
D The solution appears to be as follows: Copy the number from register Binto accumu
lator A.
DIs there an instruction to copy data from one register to another register? YES--MOV
Rd,Rs.

FLOWCHART
The thinking process described here and the steps necessary to write the program can be
represented in apictorial format, called a flowchart. Figure 6.2 describes the preceding
steps in a flowchart.
Flowcharting is an art. The flowchart in Figure 6.2 does not include all the steps de
scribed earlier. Although the number of steps that should be represented in a flowchart is
ambiguous, not all of them should be included. That would defeat the purpose of the
flowchart. It should represent a logical approach and sequence of steps in solving the
problem. Afowchart is similar to the block diagram of a hardware system or to the out
line of a chapter. Information in each block of the flowchart should be similar to the head
ing of a paragraph. Generally, a flowchart is used for two purposes: to assist and clarify
the thinking process and to communicate the programmer's thoughts or logic to others.
Symbols commonly used in flowcharting are shown in Figure 6.3. Two types or
symbols-rectangles and ovals- -are already illustrated in Figure 6.2. The diamond is
FIGURE 6.2
Flowchart Start

Enter
Number
in a Register

Output
Number

Stop

Meaning

Arrow: Indicates the direction of the program execution

Rectangle: Represents a process or an operation

Diamond: Represents a decision-making block

Oval: Indicates the beginning or end of a program

Double-sided rectangle: Represents a predefined process such as a


subroutine.

Circle with an arrow: Represents continuation (an entry or exit) to


a different page

FIGURE 6.3
Flowcharting Symbols 181
182 PROGRAMMING THE 8085

used with Jump instructions for decision making (see Figure 6.10), and the double-sided
rectangle is used for subroutines (see Chapter 9).
The flowchart in Figure 6.2 includes what steps to do and in what sequence. As a
rule, ageneral flowchart does not include how to perform these steps or what registers are
being used. The steps described in the flowchart are translated into an assembly language
progranm in the next section.

ASSEMBLY LANGUAGE PROGRAM

Tasks 8085 Mnemonics

1. Load register B with 37H. MVIB,37H*


2. Copy the number from B to A. MOV A,B
3. Send the number to the output-port 01H. OUT PORTI
4. End of the program. HLT

TRANSLATION FROM ASSEMBLY LANGUAGE TO MACHINE


LANGUAGE
Now, to translate the assembly language program into machine language, look up the
hexadecimal machine codes for each instruction in the 8085 instruction set and write each
machine code in the sequence, as follows:

8085 Mnemonics Hex Machine Code


1. MVIB,37H 06
37
2. MOV A,B 78
3. OUT PORTI D3
01
4. HLT 76

This program has six machine codes and will require six bytes of memory to enter
the program into your system. If your single-board nmicrocomputer has R/W memory
starting at the address 2000H, this program can be entered in the memory locations
2000H to 2005H. The format generally used to write an assembly language program is
shown below.

PROGRAM FORMAT
Memory Machine
Address Code Instruetion
(Hex) (Hex) Opcode Operand Comments
XX00 06 MVI B,37H ;Load register B with data 37H
XX01 37

*A number followed by the letter H represents a hexadecimul number.


+Enter high-order address (page number) of your R/W memory in place of XX.
NTRODUCTION TO 8085 INSTRUCTIONS 183

XX02 78
XX03 MOV A,B
D3 :Copy (B) into (A)
XX04 OUT PORTI :Display accumulator contents
PORT1*
XX05 76 ; (37H) at Portl
HLT
:End of the program
This
programn has five columns:
Operand, and Comments. Each is describedMemory Address, Machine Code,
in the context of a single-board Opcode,
puter. microcom
Memory Address These are 16-bit addresses of the user (R/W)
where the machine code of the program is stored. The memory in the system,
XX00; the symbol XX represents the page number of thebeginning address is shown as
available R/W memory in the
microcomputer, and 00 represents the line number. For example, if the microcomputer has
theuser memory at 2000H, the symbol XX represents page number 20H; if
the user
memory begins at 0300H, the symbol XX represents page 03H. Substitute the appropri
ate page when entering the machine code of a program.
Machine Code These are the hexadecimal numbers (instruction codes) that are entered
(or stored) in the respective memory addresses through the hexadecimal keyboard of the
microcomputer. The monitor program, which is stored in Read-Only memory (ROM) of
the microcomputer, translates the Hex numbers into binary digits and stores the binary
digits in the R/W memory.
If the system has R/W memory with the starting address at 2000H and the output
port address 01H, the program will be stored as follows:
Memory Hex
Address Memory Contents Code

2000 o0000 110 06


2001 O01 1 0111 37
2002 0111100 0 78
2003 11010011 D3
2004 O000 0 00 1 01
2005 0111 0110 76

Opcode (Operation Code) An instruction is divided into two parts: Opcode and
Operand, Opcodes are the abbreviated symbols specified by the manufacturer (Intel) to
indicate the type of operation or function that will be performed by the machine code
Operand The operand part of an instruClion speCimes the item to be processed: it can
memory address.
be 8- or 16-bit data, a register, or a

system. If an output port is not available on your system, see "How to


*Enter the output port address of yourPort" later in this section.
ACCUte a Program without an Output
186
PROGRAMMING THE 802e

D, D, Do
D, D, D, D, D, 0 0 0 =70H
0 11 1 0
The output port requires 70H, and it can be sent to the port by loading the accumulator
with 70H.

PROGRAM

Memory Machine Mnemonic


Comments
Address Code Instruction
HÊ-LO*
2000 3E MVIA,70H :Load the accumulator with the bit pattern
2001 70 necessary to turn on the devices
2002 D3 OUT 01H ;Send the bit pattern to the port 01H, and
turn on the devices
2003 01
2004 76 HLT ;End of the program
PROGRAM OUTPUT
This program simulates controlling of the devices connected to the output port by dis
playing 70H on a seven-segment LED display. If your system has individual LEDs, the
binary pattern111 0000will be displayed.

6.1.4 Review of Important Concepts


1. Registers are used to load data directly or to save data bytes.
2. In data transfer (copying), the destination register is modified but the source register
retains its data.
3. The 8085 transfers data from an input port to the accumulator (IN) and from the ac
cumulator to an output port (0UT). The instruction OUT cannot send data from ny
other register.
4. The data copy instructions do not affect the flags.
See Questions and Assignments 1-7 at the end of this chapter.

6.2 ARITHMETIC OPERATIONS


sub-
The 8085 microprocessor performs various arithmetic as addition.
traction, increment, and decrement. These arithmeticoperations,
operations such
have the following
mnemonics.

Obange the high-order memory adaress 20 to the appropriate


tSubstitute the appropriate port address.
address for your system.
INTRODUCTION TO 8085
INSTRUCTIONS 187

ADD: Add
Add the contents of a register.*
ADI: Add Immediate Add 8-bit data.
SUB: Subtract
SUI : Subtract Immediate Subtract the contents of a register.
INR : Increment Subtract 8-bit data.
Increase the contents of a register by 1.
DCR: Decrement Decrease the contents of a register by 1.
The arithmetic operations Add and Subtract are performed in relation to the
the accumulator. However, the Increment or the Decrement contents of
operations can be performed
in any register. The instructions for these operations are explained
below.
INSTRUCTIONS
These arithmetic instructions (except INR and DCR)

1. assume implicitly that the accumulator is one of the operands.


2. modify allthe flags according to the data conditions of the result.
3. place the result in the accumulator.
4. do not affect the contents of the operand register.
The instructions INR and DCR

1. affect the contents of the specified register.


2. affect all flags except the CY flag.
The descriptions of the instructions (including INR and DCR) are as follows:

Opcode Operand Description


ADD R Add
O This is a 1-byte instruction
OAdds the contents of register R to the contents of the ac
cumulator
Add Immediate
ADI 8-bit
OThis is a 2-byte instruction
n Adds the second byte to the contents of the accumulator
Rd Subtract
SUB
OThis is a 1-byte instruction
nSubtracts the contents of register R from the contents of
the accumulator
Subtract Immediate
SUI 8-bit
OThis is a 2-byte instruction

excluded here; they are discussed in Chapter 7.


operations are
*Memory-related arithmetic D, E, H, andL.
A, B, C,
TR represents any of registers
188 PROGRAMMING THE 8085

OSubtracts the second byte from the contents of the accu


mulator
INR R* Increment
OThis is a 1-byte instruction
OIncreases the contents of register R by 1
Caution: All flags except the CY are affected
DCR R* Decrement
DThis is a 1-byte instruction
Decreases the contents of register R by 1
Caution: All flags except the CY are affected
6.2.1 Addition
The 8085 performs addition with 8-bit binary numbers and stores the sum in the accu
mulator. If the sum is larger than eight bits (FFH), it sets the Carry flag. Addition can be
performed either by adding the contents of a source register (B,C, D, E, H, L,or mem
ory)to the contents of the accumulator (ADD) or by adding the second byte directly to
the contents of the accunmulator (ADI).

Example The contents of the accumulator are 93H and the contents of register Care B7H. Add
6.3 both contents.

Instruction ADD C

CY D, D, D, D4 D, D, D, Do
(A) : 93H = 1 00 1 0 0 1 1

(C) B7H = 1 0 1 1 0 1 1 1
Carry
SUM (A): 14AH = 2 0 1 0 0 1 0 1
CY
Flag Status: S =0,Z= 0, CY =1
When the 8085 adds 93H and B7H, the sum is 14AH; it is larger than eight bits.
Therefore, the accumulator will have 4AH in binary, and the CY flag will be set. The re
sult in the accumulator (4AH) is not 0, and bit D, is not 1; therefore, the Zero and the
Sign flags willbe reset.

*R represents any of registers A, B, C, D, E, H, andL.


TThe P and AC fags are not shown here. In this chapter, the focus will be on the Sign, Zero, and Carry flags.
INTRODUCTION TO 8085 INSTRUCTIONS 189

Add the number 35H directly to the sum in the previous example when the CY filag is set. Exarmple
6.4
Instruction ADI 35H

CY
(A) : 4AH = 1 0 1 00 10 10
+

(Data) 35H = 00 1 1 0 1 0 1
(A) : 7FH = 0 0 1 1 1 11 1 1

Flag Status: S = 0, Z =0, CY =0


The addition of 4AH and 35H does not generate a carry and will reset the previous Carry
flag. Therefore, in adding numbers, it is necessary to count how many times the CY flag
is set by using some other programming techniques (see Section 7.3.2).

Assume the accumulator holds the data byte FFH. Illustrate the differences in the flags set Example
by adding 01H and by incrementing the accumulator contents. 6.5

Instruction ADI 01H

CY
(A) : FFH = 1 111 11 11

(Data) 01H = 0000 000 1


1111 11| Çarry
(A) :1 0OH=I 10 00 0000
CY
Flag Status: S = 0, Z = 1, CY =1

After adding 01H to FFH, the sum in the accumulator is 0 with a carry. Therefore, the CY
and Z flags are set. The Sign flag is reset because D, is 0.
Instruction INR A
The accumulator contents will be 00H, the same as before. However, the instruction INR
will not affect the Carry flag: it will remain in its previous status.
Flag Status: S = 0, Z = 1, CY = NA

FLAG CONCEPTS AND CAUTIONS


As described in the previous chapter, the flags are flip-flops that are set or reset after the
execution of arithmetic and logic operations, with some exceptions. In many ways, the
flags are like signs on an interstate highway that help drivers find their destinations.
190
PROGRAMMING THE 8088
Drivers may see one or more signs at a time. They may take the exit
sign they are looking for, or they may continue along the when they find the
interstate and ignore the signs
Similarly, flags are signs of data conditions. After an operation, one or
may be set, and they can be used to change more flags
the direction of the program sequence by us
ing Jump instructions, which will be described later.
alert for them to make a decision. If the flags are notHowever, the programmer should be
grammer can ignore them. appropriate for the tasks, the pro
Caution #1 In Example 6.3, the CY flag is set, and in Example 6.4, the CY
set. The critical concept here is that if the flag is re
programmer
ter the subsequent instructions. However, the flag can be ignores the flag, it can be lost af
not interested in using it. ignored when the programmer is

Caution #2 In Example 6.5, two flags are set. The


flags to make decisions or may ignore them if they areprogrammer may use one or more
irelevant.
Caution #3 The CY flag has a dual function; it is used as a carry in
borrow in subtraction. addition and as a

The importance of flags cannot be emphasized enough, and a


them is critical in writing assembly language thorough understanding of
programs.
1. Flags are flip-flops in the ALU
(arithmetic/logic unit). They are affected (set or reset)
by the operations in the ALU; therefore, operations, such as
side the ALUdo not affect the flags. copy, that take place out
2. The status of the flags is determined by the result of
an operation. In most instances.
the result is in the accumulator. However, in some
operations, such as Increment
(INR), results can be in registers other than the accumulator.
3. There is no relationship between a result and the bit
positions of the flag register. In
Example 6.3, the answer of the addition is 4AH with a carry. The binary
follows: answer is as

Result
Flag Register
CY D, D, Ds D, D, D, D, Do D, D, D, D, D, D, D, Do
1 |0 1 0 0 1 0 1 0
4A
CY
Carry Flag Set to Ibecause the answer is larger than eight bits; there is acarry gener
ated out of the last bit D,. During the addition, bits D, through D, may
but these carries do not affect the CY flag. generate carries,

Misconception #1 Bit D, in the result (4AH) corresponds to the bit position of the CarrY
flag D, in the flag register; therefore, the Carry flag is reset.
Misconception #2 In the addition process, bits Do of 93H and B7H
other bit additions generate carries); therefore the Carry flag is set. generate carry (0
a
INTRODUCTION TO 8085 INSTRUCTIONS 191

Zero Flag Reset to 0 because the answer is not zero. The Zero flag is set only when all
eight bits in the result are 0.

Misconception #3 Bit D, in the result (4AH) is 1, and it corresponds to bit D, (Zero flag
position) in the flag register. Therefore, the Z flag is set.

Sign Flag Reset to 0 because D, in the result is 0. The position of the sign flag in the
flag register is also D,. But it is just a coincidence. The microprocessor designer could
have chosen bit D, for the Sign flag and bit D, for the Zero flag in the flag register. The
Sign flag is relevant only when we are using signed numbers.
Misconception #4 If the Sign flag is set, the result must be negative.
See Questions and Programming Assignments 9 through 19 at the end of this chapter.

6.2.2 Ilustrative Program: Arithmetic Operations -Addition


and Increment

PROBLEM STATEMENT
Write a program to perform the following functions, and verify the output.
1. Load the number 8BH in register D.
2. Load the number 6FH in register C.
3. Increment the contents of registerCby one.
4. Add the contents of registers C and D and display the sum at the output PORT1.

PROGRAM
The illustrative program for arithmetic operations using addition and increment is pre
sented as Figure 6.5 to show the register contents during some of the steps.

PROGRAM DESCRIPTION
1. The first four machine codes load 8BH in register D and 6FH in register C (see Figure
6.5). These are Data Copy instructions. Therefore, no flags will be affected; the flags
will remain in their previous status. The status of the flags is shown as X to indicate
no change in their status.
2. Instruction INR Cadds Ito 6FH and changes the contents of C to 70H. The result is
nonzero and bit D, is zero; therefore, the S and Z flags are reset. However, the CY flag
is not affected by the INR instruction.
3. To add (C) to (D), the contents of one of the registers must be transferred to the accu
mulator because the 8085 cannot add two registers directly. Review the ADD instruc
tion. The instruction MØV A,Ccopies 70H from register C into the accumulator with
out affecting (C). See the register contents.
192 PROGRAMMING THE 8085

Memory Machine Instruction Comments and


Address (H) Code Opcode Operand Register Contents
The first four machine codes
load the registers as
HI-LO
XX00 16 MVI SZ
D,8BH A
XX F
01 8B B 6F C
02 OE MVI C,6FH D 8B

03 6F

04 0C INR C Add 01 to (C): 6F + 01 = 70H

A 70 SZ CY LL
00
05 79 MOV A,C B 70 C

D 8B E

06 82 ADD D A FB
SZ CY P
I0 0
07 D3 OUT PORTI B 70 C

08 PORT # PORTI D 8B E

09 76 HLT End of the program

FIGURE 6.5
Ilustrative Prograrm for Arithmetic Operations-Using Addition and Increment

4. Instruction ADD Dadds (D) to (A), stores the sum in A, and sets the Sign fiag as
shown below:

(A): 70H = 0111 000 0


+

(D) 8BH = 100 0 10 1 1


(A) FBH = 0 1 1 1 1 10 1 1 (see Figure 6.5)
CY
Flag Status: S = 1, Z= 0, CY =0

5. The sum FBH is displayed by the OUT instruction.


PROGRAM OUTPUT
This program will display FBH at the output port. If an output port is not available, the
program can be executed by entering NOP instructions in place of the OUT instruction
and the answer FBH can be verified by examining the accumulator A. (Most systems have
the Examine-Register operation.) Similarly, the contents of registers Cand D and the
flags can be verified.
NTRODUCTION TO 8085 INSTRUCTIONS 193

By examining the contents of the registers, the following points can be confirmed:
1. The sum is stored in the accumulator.
2. The contents of the source registers are not changed.
3. The Sign (S) flag is set.

Even though the Sign (S)flag is set, this is not a negative sum. The microprocessor
sets the Sign flag whenever an operation results in D, = 1. The microprocessor cannot rec
ognize whether FBH is a sum, a negative number, or a bit pattern. It is your responsibil
ity to interpret and use the flags. (See "Flag Concepts and Cautions" in Section 6.2.1.) In
this example, the addition is not concerned with the signed numbers. With the signed
numbers, bit D, is reserved for a sign by the programmer (not by the microprocessor), and
no number larger than +12710 can be entered.

6.2.3 Subtraction
The 8085 performs subtraction by using the method of 2's comnplement. (If you are not
familiar with the method of 2's complement, review Appendix A2.)
Subtraction can be performed by using either the instruction SUB to subtract the
from the
contents of a source register or the instruction SUI to subtract an 8-bit number
the
contents of the accumulator. In either case, the accumulator contents are regarded as
minuend (the number from which to subtract).
(or
The 8085 performs the following steps internally to execute the instruction SUB
SUI).
complement.
Step 1: Converts subtrahend (the number to be subtracted) into its l's
subtrahend.
Step 2: Adds 1 to 1's complement to obtain 2's complement of the
accumulator).
Step 3: Add 2's complement to the minuend (the contents of the
Step 4: Complements the Carry flag.
These steps are illustrated in the following example.

B from
Register B has 65H and the accumulator has 97H. Subtract the contents of register Example
accumulator. 6.6
the contents of the
Instruction SUB B
Subtrahend(B): 6SH = 0 I 10 I 01
Step 1: I'scomplement of 6SH = I 0 0 I I0 10
(Substitute 0 for Iand 1for 0)

Add 01 to obtain 00 0 0 0 0 0 1
Step 2:
2's complement of 65H = I0 0 1 10 1 1
+
PROGRAMMING THE 8083
194

To subtract:97H- 65H,
01 I J
Add 97H to 2's complement of 65H = 100 1
hSary
Step 3: 0 0 11001 0
Step 4: Complement Carry 0 011 0010
Result (A): 32H
Flag Status: S = 0, Z= 0, CY = 0

If the answer is negative, it willbe shown in the 2's complement of the actual magnitude.
For example, if the above subtraction is performed as 65H 97H, the answer will be the
2's complement of 32H with the Carry (Borrow) flag set.

6.2.4 IIlustrative Program: Subtraction of Two Unsigned Nurnbers


PROBLEM STATEMENT
Write a program to do the following:
1. Load the number 30H in register B and 39H in register C.
2. Subtract 39H from 30H.
3. Display the answer at PORT1.
PROGRAM
The illustrative program for subtraction of two unsigned numbers is presented as Figure
6.6 to show the register contents during some of the steps.

PROGRAM DESCRIPTION
1. Registers B and Care loaded with 30H and 39H, respectively. The
instruction MOV
A,B copies 30H into the accumulator (shown as register contents). This is an
essenttal
step because the contents of a register can be subtracted only from the contents of the
accumulator and not from any other register.
2. Toexecute the instruction SUB Cthe
ternally:
microprocessor performs the following steps in
Step 1: 39H = 0 0 11 I00 I
T's complement of 39H = 1 I00 0 110
Step 2: Add 01 = 0 00 0 0 00 1
2's complement of 39H = 1|00 011 1
+
Step 3: Add 30H to 2's complement of 39H = 0 0
| 0 0 00
CY 0 1I|1 0 1 1 1
INTRODUCTION TO 8085 INSTRUCTIONS 195

Step 4: Complement carry II1 1 0 1 1|= F7H


Flag Status: S = 1, Z=0, CY = |
3. The number F7H is a 2's complement of the magnitude (39H- 30H)= 09H.
4. The instruction OUT displays F7H at PORT1.
PROGRAM OUTPUT
This program will display F7H as the output. In this program, the unsigned numbers were
used to perform the subtraction. Now, the question is: How do you recognize that the an
swer F7H is really a 2's complement of 09H and not a straight binary F7H?
The answer lies with the Carry flag. If the Carry flag (also known as the Borrow
flag in subtraction) is set, the answer is in 2's complement. The Carry flag raises a second
question: Why isn't it a positive sum with a carry? The answer is implied by the instruc
tion SUB (it is a subtraction).
There is no way to differentiate between a straight binary number and 2's comple
ment by examining the answer at the output port. The flags are internal and not easily dis
played. However, a programmer can test the Carry flag by using the instruction Jump On
Carry (JC) and can find a way to indicate that the answer is in 2's complement. (This is
discussed in Branch instructions.)

Memory Machine Instruction Comments and


Address (H) Code Opcode Operand Register Contents

HI-LO
XX00 06 MVI B,30H Load the minuend in register B
01 30 Load the subtrahend in register C
02 OE MVI C,39H
The register contents:
03 39 A 30
04 78 MOV A,B
R 30 39

A F7
SZ N
05 91 SUB
B 30 39

06 D3 OUT PORTI
07 PORT#
08 76 HLT

FIGURE 6.6
Ilustrative Program for Subtraction of Two Unsigned Numbers

6.2.5 Review of Important Concepts


1. The arithmetic operations implicitly assume that the contents of the accumulator are
one of the operands.
196 PROGRAMMING THE 8085

2. The results of the arithmetic operations are stored in the accumulator; thus, the previ.
ous contents of the accumulator are altered.
3. The flags are modified to reflect the data conditions of an operation.
4. The contents of the source register are not changed as a result of an arithmetic opera
tion.
5. In the Add operation, if the sum is larger than 8-bit, CY is set.
6. The Subtract operation is performed by using the 2's complement method.
7. If a subtraction results in a negative number, the answer is in 2's complement and CY
(the BorrOw flag) is set.
8. In unsigned arithmetic operations, the Sign flag (S) should be ignored.
9. The instructions INR (Increment) and DCR (Decrement) are special cases of the arith
metic operations. These instructions can be used for any one of the registers, and they
do not affect CY, even if the result is larger than 8-bit. All other flags are affected by
the result in the register used (not by the contents of the accumulator).

6.3 LOGIC OPERATIONS

A microprocessor is basically a programmable logic chip. It can perform all the logic
functions of the hard-wired logic through its instruction set. The 8085 instruction set in
cludes such logic functions as AND, OR, Ex OR, and NOT (complement). The opcodes
of these operations are as follows:*
ANA: AND Logically AND the contents of a register.
ANI : AND Immediate Logically AND 8-bit data.
ORA: OR
Logically OR the contents of a register.
ORI : OR Immediate Logically OR 8-bit data.
XRA: X-OR Exclusive-OR the contents of a register.
XRI : X-OR Immediate Exclusive-OR 8-bit data.
All logic operations are performed in relation to the contents of the accumulator. The in
structions of these logic operations are described below.
INSTRUCTIONS
The logic instructions
1. implicitly assume that the accumulator is one of the operands.
2. reset (clear) the CY flag. The instruction CMA is an exception; it does not atfect any
flags.
3. modify the Z, P, and S flags according to the data conditions of the result.
4. place the result in the accumulator.
5. do not affect the contents of the operand register.

*Memory-related logic operations are excluded here; they will be discussed in the next chapter.
INTRODUCTION TO 8085 INSTRUCTIONS 197

Opcode Operand Description


ANA R Logical AND with Accumulator
OThis is a 1-byte instruction
O Logically ANDs the contents of the register R with the
contents of the accumulator
D8085: CY is reset and AC is set
ANI 8-bit AND Immediate with Accumulator
OThis is a 2-byte instruction
O Logically ANDs the second byte with the contents of the
accumulator
O8085: CY is reset and ACis set
ORA R Logically OR with Accumulator
OThis is a 1-byte instruction
OLogically ORs the contents of the register R with the
contents of the accumulator
ORI 8-bit OR Immediate with Accumulator
OThis is a 2-byte instruction
OLogically ORs the second byte with the contents of the
accumulator
XRA R Logically Exclusive-OR with Accumulator
OThis is a 1-byte instruction
DExclusive-ORs the contents of register R with the con
tents of the accumulator
XRI 8-bit Exclusive-OR Immediate with Accumulator
OThisis a 2-byte instruction
OExclusive-ORs the second byte with the contents of the
accumulator
CMA Complement Accumulator
O This is a 1-byte instruction that complements the con
tents of the accumulator
ONo flags are affected

6.3.1 Logic AND


The process of performing logic operations through the software instructions is slightly
different from the hardwired logic. The AND gate shown in Figure 6.7(a) has two inputs
and one output. On the other hand, the instructionANA simulates eight AND gates, as
shown in Figure 6.7(b). For example, assume that register B holds 77H and the accumu
lator A holds 81H. The result of the instruction ANA B is 01H and is placed in the accu
mulator replacing the previous contents,as shown in Figure 6.7(b).
Figure 6.7(b) shows that each bit of register B is independently ANDed with each
bit of the accumulator, thus simulating eight 2-input AND gates.
198 PROGRAMMING THE 80A8

1 1
(B) =
L0
FHeDl 0 0

D
(A) = 1

Input - Output

(a) ANA B

(A) = 0 0 0 D4 (b)
1

FIGURE 6.7
AND Gate (a) and a Simulated ANA Instruction (b)

6.3.2 Illustrative Program:Data Masking with Logic AND


PROBLEM STATEMENT
Toconserve energy and to avoid an electrical overload on a hot afternoon, implement the
following procedures to control the appliances throughout the house (Figure 6.8). Assume
that the control switches are located in the kitchen, and they are available to anyone in the
house. Write a set of instructions to

1. turn on the air conditioner if switch S, of the input port 00H is on.
2. ignore all other switches of the input port even if someone attempts to turn on other
appliances.

(To perform this experiment on your single-board microcomputer, simulate the reading of
the input port 00H with the instruction MVIA, 8-bit data.)
PROBLEM ANALYSIS
In this problem you are interested in only one switch position, S, which is connected to
data line D,.Assume that various persons in the family have turned onthe switches of the
air conditioner (S;), the radio (S4), and the lights (S3, Sz. S1, So).
If the microprocessor reads the input port (IN 00H), the accumulator will have data
byte 9FH. This can be simulatedby using the instruction MVIA,9FH. However, if you
are interested in knowing only whether switch S, is on, you can mask bits D, through Do
by ANDing the input data with a byte that has 0 in bit positions D, through D, and I in
bit position D,.
D, D, Ds D4 D, D, D, Do
1 0 0 0 0 0 0 0 = 80H

After bits D, through D,have been masked, the remaining byte can be sent to the output
port to simulate turning on the air conditioner.
INTRODUCTION TO 8085 INSTRUCTIONS 199

PROGRAM

Memory Machine Instruetion


Address Code Opcode Operand Comments
HI-LO
XX00 3E MVI A,Data (This instruction simulates the
01 9F instruction IN 00H
02 E6 ANI 80H ;Mask all the bits except D,
03 80
04 D3 OUT 01H ;Turn on the air conditioner if
05 01 ; S, is on
06 76 HLT ;End of the program

PROGRAM OUTPUT
The instruction ANI 80H ANDs the accumulator data as follows:

(A) = 1 001 11| (9FH)


AND
(Masking Byte = 1 000 000 0 (80H)
(A) = 1000 0 000 (80H)
Flag Status: S = 1, Z =0, CY =0

The ANDing operation always resets the CY flag. The result (80H) will be placed in the
accumulator and then sent to the ouptput port, and logic 1 of data bit D, turns on the air
conditioner. Inthis example, the output (80H) is the same as the masking data byte (80H)
because switch S, (or data bit D) is on. If S, is off, the output will be zero.
The masking is a commonly used technique to eliminate unwanted bits in a byte.
The masking byte to be logically ANDed is determined by placing Os in bit positions that
are to be masked and by placing ls in the remaining bit positions.

6.3.3 OR, Exclusive-OR, and NOT


The instruction ORA (and ORI) simulates logic ORing with eight 2-input OR gates; this
process is similar to that of ANDing, explained in the previous section. The instruction
XRA (and XRI) performs Exclusive-ORing of eight bits, and the instruction CMA inverts
the bits of the accumulator.

Assume register B holds 93H and the accumulator holds 15H. Illustrate the results of the Example
instructions ORA B, XRA B, and CMA. 6.

1. The instruction ORA B will perform the following operation:


200
PROGRAMMING THE 8(8s

(B) = | 0 0 | 0 0 | | (93H)
OR
(A) = 0 00 | 0 I0 | 1SH)
(A) =| 0 0 1 0 L 9H)
Flag Status: S = 1,Z=0, CY = 0
The esult 97H will be placed in the accumulator, the CY lag will be reset, and the
other flags will be modificd to reflect the data conditions in the accumulator.
2. The instruction XRA B will perform the following
operation.
(B) = | 0 0 1 00 1 1 (93H)
X-OR
(A) =0 00 1 0 I 0 1 (15H)
(A) = | 000 0 1 |0 (86H)
Flag Status: S = 1,Z=0, CY = 0
The result 86H will be placed in the
shown.
accumulator, and the flags will be modified as
3. The instruction CMA will result in

(A) =0 001 0 I0 1 (15H)


CMA
(A) = 1 1 10 10 10 (EAH)
The result EAH will be placed in the
accumulator and no flags will be modiied.

6.3.4 Setting and Resetting Specific Bits


At various times, we may want to set or
reset a specific bit without atfecting the oçer
bits. OR logic can be used to set the bit, and
AND logic can be used to reset the bit.

Example In Figure 6.8, keep the radio on(D4)


6.8 continuously without affecting the functions of cher
appliances, even if someone turns off the switch S.
Solution To keep the radio on without affecting the other
appliances, the bi D, should e set by
ORing the reading of the input port with the data byte l0H as follows:
IN 00H: (A) = D, D, D, D, D, D, D, D,
ORI 1OH: =0 0 0 I ) 0 0 0
(A) = D, D, D, I D, D, D, D
Flag Status: CY 0; others willdepend on
data.
INTRODUCTION TO 8085 INSTRUCTIONS 201

ON: S, D

OFF: S
OFF: S.
Data Bus B085
ON: S Input Port
00H System
ON: S,
ON: S,
ON: S
Do
ON: So EN

Input Air Conditioner


Enable Heater
Coffeepo
Radio
Output
Port 01 Light 1
Light 2
Light 3
EN Light 4

Output Enable

FIGURE 6.8
Input Port to Control Appliances

The instruction IN reads the switch positions shown as D,-Do and the instruction ORI
sets the bit D, without affecting any other bits.

In Figure 6.8, assume it is winter, and turn off the air conditioner without affecting the Example
other appliances. 6.9

Solution
To turn off the air conditioner, reset bit D, by ANDing the reading of the input port with
the data byte 7FH as follows:

IN 00H: (A)= D, D, Ds D, D, D, D, Do
ANI 7FH: = 0

0 D, Ds D, D, D, D, D
Flag Status: CY = 0; others will depend onthe data bits.
The ANI instruction resets bit D, without affecting theother bits.
204
PROGRAMMING THE 8085
5. The logic operations cannot be performed dircctly with the contents of
6. The individual bits in the two registers.
accumulator can be set or reset using logic instructions
See Qucstions and Progranming Assignments 20-29 at the end of this chanter

6.4 BRANCH OPERATIONS


The branch instructions are the most powerful
instructions because they allow the mi.
croprocessor to change the sequence of a program, either unconditionally or under certain
test conditions. These instructions are the key to
puter.
the flexibility and versatility of a com
The microprocessor is a sequential machine; it
memory location to the next. Branch instructions executes machine codes from one
different memory location, and the instruct the microprocessor to go to a
microprocessor
from that new location. The address of the continues executing machine codes
new memory location is either
plicitly or supplied by the microprocessor or by extra specified ex
are classified in three hardware. The branch instructions
categories:
1. Jump instructions
2. Call and Return
instructions
3. Restart instructions

This section is concerned with


Return instructions are associated with applications of Jump instructions. The Call anc
the subroutine technique
Chapter 9; Restart instructions are associated with the and will be discussed in
cussed in Chapter 12. interrupt technique and will be is
The Jump instructions specify the
structions: one byte for the operation code,memory location explicitly. They are 3-byte in
instructions are classified into two categories:followed by a l6-bit memory address. Jump
Unconditional Jump and Conditional Jump.
6.4.1
The 8085
Unconditional Jump
instruction
set
Jump instruction enables includes one unconditional Jump
the programmer to set up instruction, The unconditional
continuous loops.
INSTRUCTION
Opcode Operand
JMP Description
16-bit
Jump
O This is a 3-byte
instruction
OThe second and third
bytes specify the 16-bit memoy
address. However, the second
specifies the low-order and thebyte
third byte spec
ifies the high-order
memory address

You might also like