Programming 8085 Microprocessor
Programming 8085 Microprocessor
Group – B
Module - 3
Arithmetic Group
Logical Group
Arithmetic Group:
Logical Group:
The instruction under this group perform logical operation such as AND, OR,
compare, rotate etc.
Examples are: ANA, XRA, ORA, CMP, RAL etc.
This group includes the instructions for conditional and unconditional jump,
subroutine call and return, and restart.
This group includes the instructions for input/output of Data to and from ports,
stack purpose and machine control purpose.
If 16-bit data are to be stored, they are stored in two consecutive memory
locations. The address of a memory location is of 16 bits i.e. 2 bytes.
The address of the memory location, I/O port or I/O device, where data
resides, may be given in the instruction itself.
Due to different ways of specifying data for instructions, the machine codes of
all instructions are not of the same length.
There are three types of the Intel 8085 instructions depending upon their size
(in ‘Byte’) as described below:
Three-Byte Instruction
Address modes
Each instruction requires certain data on which it has to operate. It has already
been explained that there are various techniques to specify data for
instructions. These techniques are called addressing modes. Intel 8085 uses
the following addressing modes.
Direct addressing
Register addressing
Immediate addressing
Direct Addressing:
In this mode of addressing the address of the operand (data) is given in the
instruction itself.
Examples are:
STA 2400(H): Store the content of the accumulator in the memory location
2400(H).
In this instruction 02 is the address of the port C of an I/O port from where the
data is to be read. Here, it is implied that the destination is the accumulator.
The 2nd byte of the instruction specifies the address of the port.
Register Addressing:
In register addressing mode the operands are in the general purpose registers.
The Opcode specifies the address of the registers in addition to the operation
to be performed.
Examples are:
In example 2 the Opcode for ADD B is 80H. IN this instruction one of the
operands is register B (its content is one of the data), which is indicated in the
instruction itself. In this type of instruction (arithmetic group) it is understood
that the other operand is in the accumulator. The Opcode 80H in the binary
form is 1000000. The first five bits, i.e. 10000 specify the operation to be
performed, i.e. ADD. The last three bits 000 are the binary code for register B
of 8085 microprocessor.
MOV A,M Move the content of the memory location, whose address is
HLT Halt
HLT Halt
Implicit Addressing:
There are certain instructions, which operate on the content of the
accumulator. Such instructions do not require the address of the operand.
Examples are: CMA, RAL, and RAR etc.
Some of Intel 8085 instructions are frequently, some occasionally
and some seldom used by the programmer. It is not necessary that
one should learn all the instructions to understand simple
programs. The beginner can learn about 15 to 20 important
instructions such as MOV, MVI, LXI, LDA, LHLD, STA, SHLD, ADD,
ADC, SUB, JMPJC, JNC, JZ, INZ, INX, DCR, CMP etc., and start to
understand simple programs given in later pages. While learning
programs one can understand new instructions, which he has not
learnt earlier. The explanations of some of the instructions are
given below.
MOV r,M
Example:
register B.
HLT Halt
In this example the instruction LXI H, 2000H loads H – L pair with 2000 H which
is the address of a memory location. Then the instruction MOV B, M will move
the content on the memory location 2000H to register B.
MOV M, r
The instruction is 1 byte. The data stored in register ‘r’ will be copied in the
memory address stored in H – L register pair.
MVI M, data
Example:
HLT Halt.
In the above example the instruction LXI H, 2400 H loads H – L pair with 2400 H
which is the address of a memory location. Then the instruction MVI M, 08 will
move 08 (H) data to memory location 2400 H. In the code form it is written as
36, 08. The Opcode for MVI is 36 and 08 (H) is the data which is to be moved to
the memory location 2400 H.
LXI rp, data 16Bit
LHLD addr16Bit
The content of the memory location, whose address is specified by the 2nd and
3rd bytes of the instruction, is loaded into register L. The content of the next
memory location is loaded into register H. For example, LHLD 2500H will load
the content of the memory location 2500 (H) into register L. The content of the
memory location 2501 (H) is loaded into register H.
LDAX rp
[A] [[rp]]. States 7, Flags; none. Addressing: register indirect. Machine cycles
2. The content of the memory location, whose address is in the register pair rp,
is loaded into the accumulator. For example LDAX B will load the content of
the memory location, whose address is in the B-C pair, into the accumulator.
This instruction is used only for B-C and D-E register pairs.
XCHG
Arithmetic Group:
[A] [A] +[r] States: 4 Flags: all. Addressing register. Machine cycles 2.
The content of register r is added to the content of the accumulator. The sum
is placed in the accumulator.
[A] [A] – [r], States: 4 Flags: all. Addressing: register. Machine cycles:1.
[A] [A] – [[H-L]]. States: 7. Flags all. Addressing : reg. indirect Machine cycles
: 2 The content of the memory location addressed by H-L pair is subtracted
from the content of the accumulator. The result is placed in the accumulator.
[A] [A] – [r] – [CS]. States: 4. Flags all. Addressing : register. Machine cycles:1.
The content of register r and carry status are subtracted from the content of
the accumulator. The result is placed in the accumulator. Carry Flag is reset.
[A] [A] – [[H-L]] – [CS]. States 7. Flags : all. Addressing: reg. indirect. Machine
cycles : 2 The content of the memory location addressed by H-L pair and carry
status are subtracted from the content of the accumulator. The result is placed
in the accumulator.
[A] [A] – data – [CS]. States : 7 Flags: all. Addressing: immediate. Machine
cycles 2.The data and carry status are subtracted from the content of the
accumulator. The result is placed in the accumulator. Carry Flag is reset.
[r][r] + 1. States 4. Flags all except carry flag. Addressing : register, Machine
cycle : 1.The content of register r is incremented by one. All flags except CS are
affected.
[[H-L]] [[H-L]] +1. States : 10 Flags: all except carry flag. Addressing: reg,
indirect.Machine cycles 3.The content of the memory location addressed by H
– L pair is incremented by one. All flags except CS are affected.
[r] [r] – 1. States: 4. Flags: all except carry flag, Addressing: register. Machine
cycles: 1. The content of register r is decremented by one. All flags except CS
are affected.
[[H-L]] [[H-L]] – 1. States: 10. Flags: all except carry flag. Addressing reg.
indirect. Machine cycles: 3. The content of the memory location addressed by
H-L pair is decremented by one. All flags except CS are affected.
The instruction DAA is used in the program after ADD, ADI, ACI, ADC, etc
instructions. After the execution of ADD, ADC, etc instructions the result is in
hexadecimal and it is placed in the accumulator. The DAA instruction operates
on this result and gives the final result in Binary Coded Decimal (BCD) system.
It uses carry and auxiliary carry for decimal adjustment. 6 is added to 4 LSBs of
the content of the accumulator if their value lies in between A and F or the AC
flag is set to 1. Similarly, 6 is also added to 4 MSBs of the content of the
accumulator if their value lies in between A and F or the CS flag is set to 1. All
status flags are affected. When DAA is sued data should be in decimal
numbers.
Logical Group:
The instructions of this group perform AND, OR, EXCLUSIVE-OR, EXCLUSIVE-
OR operations; compare, rotate or take complement of data in register or
memory.
ANA r. (AND memory with content of any register)
[A][A] [r]. States: 4. Flags: all. Addressing. reg. indirect. Machine cycles
1.The content of the register ‘r’ is AND ed with the accumulator. The result is
placed in the accumulator. All status flags are affected. The CS flag is set to 0
and AC to 1.
[A][A] [[H-L]]. States: 7. Flags all, Addressing: reg. indirect. Machine cycles:
2. The content of the memory location addressed by H-L pair is AND ed with
the accumulator. The result is placed in the accumulator. All status flags are
affected. The CS flag is set to 0 and AC to 1.
[A][A] data States : 7. Flags all, Addressing: reg. indirect. Machine cycles: 2
The 2nd byte of the instruction is data, and it is ANDed with the content of the
accumulator. The result is placed in the accumulator. The CS flag is set to 0 and
AC to 1.
[A] [A] [r] States 4. Flags. All. Addressing: register. Machine cycles 1.
The content of register r is OR with the content of the accumulator. The result
is placed in the accumulator. All status flags are affected. Carry and auxiliary
carry are cleared i.e. the CS and AC flags are set to 0.
The content of the memory location addressed by H-L pair is OR with the
content of the accumulator. All status flags are affected. Carry and auxiliary
carry are cleared i.e. the CS and AC flags are set to 0.
The 2nd byte of the instruction is data, and it is OR with the content of the
accumulator. The result is placed in the accumulator. All status flags are
affected. The CS and AC flags are set 0.
The content the memory location addressed by H-L pair is EXCLUSIVE – Ored
with the content of the accumulator. The result is placed in the accumulator.
All status flags are affected. The CS and AC flags are set to 0.
[A] [A] data States : 7 Flags : all Addressing immediate Machine cycles 2.
The 2nd byte of the instruction is data, and it is EXCLUSIVE – OR with the
content of the accumulator. The result is placed in the accumulator. All flags
are affected. The CS and AC flags are set to 0.
[A] 1’s Complement of [A]. States 4 Flags none. Machine cycles 1. Addressing :
implicit. 1’s complement of the content of the accumulator is obtained, and
the result is placed in the accumulator. To obtain the 1’s complement of a
binary number 0 is replaced by 1, and 1 by 0. For example, one’s complement
of 1100 is 0011.
[A] – [[H-L]] States 7. Flags all. Addressing register indirect. Machine cycles 2
The 2nd byte of the instruction is data, and it is subtracted from the content of
the accumulator. The status flags are set according to the result of
subtraction. But the result is discarded. The content of the accumulator
remains unchanged.
[An+1][An], [A0][A7],[CS][A7].
The content of the accumulator is rotated left by one bit. The seventh bit of
the accumulator is moved to carry bit as well as to the zero bit of the
accumulator. Only CS flag is affected as shown in Fig.3.1.
RRC. (Rotate accumulator right)
The content of the accumulator is rotated right by one bit. The zero bit of the
accumulator is moved to the seventh bit as well as to carry bit. Only CS flag is
affected as depicted in Fig.3.2
The content of the accumulator is rotated left one bit through carry. The
seventh bit of the accumulator is moved to carry, and the carry bit is moved to
the zero bit of the accumulator. Only carry flag is affected as shown in Fig.3.3
Branch Group:
The instructions of this group change the normal sequence of the Program.
There are two types of branch instructions: conditional and unconditional.
The conditional branch instructions transfer the program to the specified
label when certain condition is satisfied. The unconditional branch
instructions transfer the program to the specified label unconditionally.
JMP addr (label). (Unconditional jump: jump to the instruction specified by the
address).[PC]Label. States 10, Flags none. Addressing. immediate. Machine
cycles 3. Byte 2nd and byte 3rd of the instruction give the address of the label
where the program jumps. The address of the label is the address of the
memory location for next instruction to be executed. The program jumps to
the instruction specified by the address (label) unconditionally.
After the execution of the conditional jump instruction the program jumps
to the instruction specified by the address (label) if the specified condition is
fulfilled. The program proceeds further in the normal sequence if the
specified condition is not fulfilled. The execution of a conditional jump takes
3 machine cycles: 10 states. If condition is not true, only 2 machine cycles; 7
states are required for the execution of the instruction.
[PC] address (label), jump if Z=1. States : 7/10. Flags: none. Addressing :
immediate.Machine cycles: 2/3 The program jumps to the instruction specified
by the address (label) if the result is zero (i.e. the zero status Z=1). Here the
result after the execution of the preceding instruction is under consideration.
[PC] address (label), jump if Z=0. States : 7/10. Flags: none. Addressing :
immediate.Machine cycles: 2/3 The program jumps to the instruction specified
by the address (label) if the result is non zero (i.e. the zero status Z=0). Here
the result after the execution of the preceding instruction is under
consideration.
Addressing: immediate. Machine cycles: 2/3.If the result contains even number
of 1s, the program jumps to the instruction specified by the address (label).
[PC] address (label), jump if odd pairty; the parity status P = O, States: 7/10,
Flags: none, Addressing: immediate, Machine cycles 2/3. If the result contains
odd number of 1s, the program jumps to the instruction specified by the
address (label).
CALL addr (label). (Unconditional CALL: call the subroutine identified by the
operand)
Conditional Return
States 6/12. Flags none. Addressing: reg. indirect. Machine cycles. 1/3. If the
condition is true and the program returns from the subroutine, the execution
of a conditional return instruction takes 3 machine cycles, 12 states. If
condition is not true only 1 machine cycle, 6 states are required.
States: 6 Flags: none. Addressing: register Machine cycle: 1.The contents of H-L
pair are transferred to program counter. The contents of register H are moved
to high order 8 bits of register PC. The contents of register L are transferred to
low order 8 bits of register PC.
The data available on the port is moved to the accumulator . After instruction
IN, the address of the port is specified. The 2nd byte of the instruction contains
the address of the port. The address of a port is an 8-bit address. For
example, IN 01. The address of the port B of a microprocessor kit is 01 where
the processor is interfaced with a peripheral interfacing device 8255.
States: 12. Flags none. Addressing reg. indirect. Machine cycles 3.The content
of the register pair rp is pushed into the stack. The content of the register SP is
decremented by 2 to indicate new stack top.
The content of the accumulator is pushed into the stack. The contents of
status flags are also pushed into the stack. The content of the register SP is
decremented by 2 to indicate new stack top.
POP rp. (Pop the content of register pair, which was saved, from the stack)
The content of the register pair, which was saved earlier is moved from the
stack to the register pair.
States 10. Flags: none. Addressing: reg. indirect. Machine cycles: 3. The process
status word which was saved earlier during the execution of the program is
moved from the stack to PSW. The content of the accumulator, which was also
saved, is moved from the stack to the accumulator.
HLT (Halt)
The content of the register L are exchanged with the byte of the stack-top. The
contents of the H register exchanged with the byte below the stack-top.
[H-L]→[SP].
States: 4, Flags: none, Machine cycle 1. When this instruction is executed the
Interrupt Enable Flag is set and all interrupts are enabled.
DI (Disable Interrupt)
States:4. Flags: none, Machine cycle:1 When this instruction is executed the
Interrupt Enable Flag is reset and all interrupts are disabled.
Decision Contin
Process/ Start or End of a
making uation
Operation Program
to a
Counting: This technique allows programmer to count how many times the
instruction/set of instructions are executed.
The Program loop is the basic structure which forces the processor to repeat a
sequence of instructions. Loops have four sections.
• Initialization section.
• Processing section.
• Loop control section
• Result section.
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.
Programming Examples
ADDITION OF TWO 8 BIT NUMBERS
Algorithm:
Program:
MVI C, 00 : Initialize C register to 00
LDA 4150 : Load the 1st data to Accumulator
MOV B, A : Move the content of Accumulator to B
LDA 4151 : Load the 2nd data to Accumulator
ADD B : Add the value of register B with A
JNC LOOP : Jump on no carry to Label LOOP
INR C : Increment value of register C
LOOP: STA 4152 : Store the value of Accumulator (SUM)
MOV A, C : Move content of register C to Acc.
STA 4153 : Store the value of Acc. (CARRY)
HLT : Halt the program
Data 1
Illustration: Before execution: 4150
4151 Data 2
SUM
After execution: 4152
4153 CARRY
Flowchart:
SUBTRACTION OF TWO 8 BIT NUMBERS
Algorithm:
Data 1
Illustration: Before execution: 4150
4151 Data 2
DIFFERENCE
After execution: 4152
4153 BORROW
Algorithm:
1) Start the program by loading the first data into accumulator.
2) Move the data to a register, say B.
3) Get the second data and load into Accumulator.
4) Move the data to another register, C
5) Initialize the Accumulator with zero value
6) Initialize another register D with zero value which will store the Carry.
7) Add the content of B register with Accumulator content.
8) Check for carry.
9) Increment D (Carry) by 1 if carry present.
10) Decrement the 2nd data stored in Counter register C and repeat from
step 7, till Counter value decreases to zero.
11) Store the multiplied value and carry in memory location.
12) Terminate the program.
Flowchart: Draw it yourself
Program:
LDA 4150 : Load the 1st data to Accumulator
MOV B, A : Move the content of Accumulator to B
LDA 4151 : Load the 2nd data to Accumulator
MOV C, A : Move the content of Accumulator to B
MVI A, 00 : Initialize Accumulator with 00
MVI D, 00 : Initialize register D (Carry) with 00
REPEAT ADD B : Add the value of register B with A
JNC LOOP : Jump on no carry to Label LOOP
INR D : Increment value of register D if Carry present
LOOP DCR C : Decrement value of Counter register C
JNZ REPEAT : Repeat addition until Counter becomes zero
STA 4152 : Store the value of Accumulator (SUM)
MOV A, D : Move content of register D to Acc.
STA 4153 : Store the value of Acc. (CARRY)
HLT : Halt the program
Algorithm:
1) Start the program by initializing H-L reg. pair with the address of
memory location.
2) Move the data to a register, say B.
3) Initialize reg. C with 00 (H) which will be used to store the ‘Quotient’.
4) Get the second data and load into Accumulator.
5) Compare the two numbers to check for carry.
6) Subtract the two numbers.
7) Increment the value of C reg.
8) Check whether repeated subtraction is over and store the value of
Quotient and Remainder in memory location.
9) Terminate the program.
Flowchart: Draw it yourself
Program:
LXI H, 4150 : Load H-L reg. pair with memory address of 1st data
MOV B, M : Get the Dividend in reg. B
MVI C, 00 : Initialize reg. C with 00(H) for ‘Quotient’
INX H : Load H-L reg. pair with memory address of 2nd data
MOV A, M : Get the Divisor in accumulator A
NEXT: CMP B : Compare the two data
JC LOOP : Jump on carry to Label LOOP
SUB B : Subtract the value of reg. A from reg. B
INR C : Increment value of register C
JMP NEXT : Jump to NEXT
LOOP STA 4152 : Store the remainder in Memory
MOV A, C : Get the ‘Quotient’ in accumulator A
STA 4153 : Store the ‘Quotient’ in Memory Location
HLT : Terminate the program
Algorithm:
Let us assume, [HL] = 3629 & [DE] = 4738
Step1: Addition of two lower digits of the two numbers.
29 + 38 = 61 and auxiliary carry flag = 1, i.e. Auxiliary carry flag is set.
So add 06 (Decimal Adjust Accumulator) with the result to get actual
BCD result for the lower two digits, which will be: 61+06 = 67
Step2: Addition of two higher digits of the two numbers.
36+47+0 (carry of LSB) = 7D
Lower nibble is greater than 9. So add 06 with it to get actual BCD value.
7D+06 = 83 (Decimal Adjust Accumulator)
Result = 8367
Program:
LHLD 9000(H) : Load the 1st 4 digit of BCD number in HL reg. pair
XCHG : Move it to DE
LHLD 9002(H) : Load the 2nd 4 digit of BCD number in HL reg. pair
MOV A, L : Move the two lower order digits of the 2nd no. in A
ADD E : Add the two lower order digits of the two no.
DAA : Adjust the result to make it a valid BCD no.
STA 9004(H) : Store the partial result in memory location
MOV A, H : Load the two higher order digits of 2nd no. in A
ADC D : Add the 2 higher order digits of the 2 no. with previous carry
DAA : Adjust the result to make it a valid BCD no.
STA 9005(H) : Store the partial result in memory location
HLT : Terminate the program.
Flowchart: Addition of two 4 digit BCD numbers.
What is Subroutine in Assembly Language?
Subroutines are written separately and are stored in a memory location that is
different from the main program. You can call a subroutine multiple times
from the main program using a simple CALL instruction.
Aim: To convert given Hexadecimal number into its equivalent ASCII number
using 8085 instructions.
Algorithm:
Program:
In this section we will see how to generate the time delay using 8085
programs. The delay will be used in different places to simulate clocks, or
counters or some other area.
When the delay subroutine is executed, the microprocessor does not execute
other tasks. For the delay we use the instruction execution times. Executing
some instructions in a loop, the delay is generated. There are some methods of
generating delays. These methods are as follows.
One of the main usage of NOP instruction is in delay generation. The NOP
instruction takes four clock pulses for fetching, decoding and executing. If the
8085 MPU is working on 6MHz clock frequency, then the internal clock
frequency is 3MHz. So from that we can easily determine that each clock
period is 1/3 of a microsecond. So the NOP will be executed in 1/3 * 4 =
1.333µs. If we use the entire memory with NOP instruction, then 64K NOP
instructions will be executed. Then the overall delay will be 216 * 1.333µs =
87359.488µs, though the time is not so large and the program size is also large.
So this type of NOP instruction can be used to generate a short time delay for
few milliseconds.
Counter is another approach to generate a time delay. In this case the program
size is smaller. So in this approach we can generate more time delay in less
space. The following program will demonstrate the time delay using 8-bit
counter.
Program:
This technique can also be done using some Nested Loops to get larger delays.
The following code is showing how we can get some delay with one loop inside
another loop.
Program:
MVI B, FFH
Loop1: MVI C, FFH
Loop2: DCR C
JNZ Loop2
DCR B
JNZ Loop1
RET
From this block, if we calculate the delay, it will be nearly 305µs delay. It
extends the time of delay.
Instead of using 8-bit Counter, we can do that kind of task using 16-bit register
pair as 16 bit Counter. Using this method more time delay can be generated as
we use larger data as ‘Count Value’. As an example say, FFFF (H) instead of FF
(H). This method can be used to get more than 0.5 seconds delay.