Microprocessors Chapter 2: Programming With 8085 Microprocessor
Microprocessors Chapter 2: Programming With 8085 Microprocessor
Instruction cycle:
Instruction contains in the program and is pointed by the program counter. It is first moved
to the instruction register and is decoded in binary form and stored as an instruction in the
memory. The computer takes a certain period to complete this task i.e., instruction
fetching, decoding and executing on the basis of clock speed. Such a time period is called
‘Instruction cycle’ and consists two cycles namely fetch and decode and Execute cycle.
In the fetch cycle the central processing unit obtains the instruction code the memory for its
execution. Once the instruction code is fetched from memory, it is then executed. The
execution cycle consists the calculating the address of the operands, fetching them,
performing operations on them and finally outputting the result to a specified location.
ADD R1, R0
Op-code address
Here R0 is the source register and R1 is the destination register. The instruction adds the
contents of R0 with the content of R1 and stores result in R1.
8085 A can handle at the maximum of 256 instructions (2 8)(246 instructions are used) . The
sheet which contains all these instructions with their hex code, mnemonics, descriptions
and function is called an instruction sheet. Depending on the number of address specified in
instruction sheet, the instruction format can be classified into the categories.
One address format (1 byte instruction): Here 1 byte will be Op-code and operand will
be default. E.g. ADD B, MOV A,B
Two address format (2 byte instruction) :Here first byte will be Op-code and second
byte will be the operand/data.
E.g. IN 40H, MVI A, 8-bit Data
Three address format (3 byte instruction): Here first byte will be Op-code, second and
third byte will be operands/data. That is
2nd byte- lower order data.
Classification of an instruction
Data transfer group: The instructions which are used to transfer data from one register
to another register or register to memory.
Arithmetic group: The instructions which perform arithmetic operations such as
addition, subtraction, increment, decrement etc.
Logical group: The instructions which perform logical operations such as AND, OR, XOR,
COMPARE etc.
Branching group: The instructions which are used for looping and branching are called
branching instructions like jump, call etc.
Miscellaneous group: The instructions relating to stack operation, controlling purposes
such as interrupt operations are fall under miscellaneous group including machine
control like HLT, NOP.
It is the longest group of instructions in 8085. This group of instruction copy data from a source
location to destination location without modifying the contents of the source. The transfer of
data may be between the registers or between register and memory or between an I/O device
and accumulator. None of these instructions changes the flag. The instructions of this group
are:
# Write a program to load memory locations 7090 H and 7080 H with data 40H and
50H and then swap these data.
Soln :
MVI H, 70H
MVI L, 90H
MVI A, 40H
MOV M, A
MOV C, M
MVI L, 80H
MVI B, 50H
MOV M, B
MOV D, M
Reference: Ramesh S. Gaonkar | 8
Microprocessors Chapter 2 : Programming with 8085 Microprocessor
MOV M, C
MVI L, 90H
MOV M, D
HLT
LDAX B A= [9000]
– E. g. STAX B
LXI B, 9500H output
LXI D, 9501H [9500] = 32
MVI A, 32H [9501] = 7A
STAX B
MVI A, 7AH
STAX D [DE] A
MVI m, 7AH
Instructions are command to perform a certain task in microprocessor. The instruction consists
of op-code and data called operand. The operand may be the source only, destination only or
both of them. In these instructions, the source can be a register, a memory or an input port.
Similarly, destination can be a register, a memory location, or an output port. The various
format (way) of specifying the operands are called addressing mode. So addressing mode
specifies where the operands are located rather than their nature. The 8085 has 5 addressing
mode:
LXI B, 4567H
5) Implied or Inherent addressing mode:
The instructions of this mode donot have operands. E.g.
NOP: No operation
HLT: Halt
EI: Enable interrupt
DI: Disable interrupt
Q) What do you understand by addressing modes in microprocessor? Explain all the addressing
modes of 8085 up with suitable example for each.
The 8085 microprocessor performs various arithmetic operations such as addition, subtraction,
increment and decrement. These arithmetic operations have the following mnemonics.
1) ADD R/M
– 1 byte add instruction.
– Adds the contents of register/memory to the contents of the accumulator and
stores the result in accumulator.
– E. g. Add B; A [A] + [B]
2) ADI 8 bit data
– 2 byte add immediate instruction.
– Adds the 8 bit data with the contents of accumulator and stores result in
accumulator.
– E g. ADI 9BH ; A A+9BH
3) SUB R/M
– 1 byte subtract instruction.
– Subtracts the contents of specified register / m with the contents of accumulator
and stores the result in accumulator.
– E. g. SUB D ; A A-D
4) SUI 8 bit data
– 2 byte subtract immediate instruction.
– Subtracts the 8 bit data from the contents of accumulator stores result in
accumulator.
– E. g. SUI D3H; A A-D3H
5) INR R/M, DCR R/M
– 1 byte increment and decrement instructions.
– No flags affected
7) ADC R/M and ACI 8-bit data ( addition with carry (1 byte))
– ACI 8-bit data= immediate (2 byte).
– Adds the contents of register or 8 bit data whatever used suitably with the
Previous carry.
8) SBB B/M
– 1 byte instruction.
– Subtracts the contents of register or memory from the contents of accumulator
and stores the result in accumulator.
– e. g. SBB D ; A A-D-Borrow
SBI 8 bit data
– 2 byte instruction.
– Subtracts the 8-bit immediate data from the content of the accumulator and
stores the result in accumulator.
– E.g. SBI 70H ; A A-70-Borrow
9) DAD Rp(double addition)
– 1 byte instruction.
– Adds register pair with HL pair and store the 16 bit result in HL pair.
– E. g. LXI H, 7320H
LXI B, 4220H
DAD B; HL=HL+BC
Reference: Ramesh S. Gaonkar | 13
Microprocessors Chapter 2 : Programming with 8085 Microprocessor
7320+4220=B540
CY CY
8085 performs subtraction operation by using 2’s complement and the steps used are:
1) Converts the subtrahend (the number to be subtracted) into its 1’s complement.
2) Adds 1 to 1’s complement to obtain 2’s complement of the subtrahend.
3) Adds 2’s complement to the minuend (the contents of the accumulator).
4) Complements the carry flag.
CY
B=97H, A=65H
97H: 1 0 0 1 0 1 1 1
MVI B, 65H 1’s complement of 97H : 0 1 1 0 1 0 0 0
SUB B +1
2’s Complement of 97H: 0 1 1 0 1 0 0 1
65H: + 0 1 1 0 0 1 0 1
0 1 1 0 0 1 1 1 0
(Result in 2’s complement form)
CY
CY=1, A= CE: 1 1 0 0 1 1 1 1 0
1’s complement: 0 0 1 1 0 0 0 0 1
2’s complement: 0 0 1 1 0 0 0 1 0
32
1. The memory location 2050H holds the data byte F7H. Write instructions to transfer
the data byte to accumulator using different op-codes: MOV, LDAX and LDA.
LXI H, 2050H LXI B, 2050H LDA 2050H
MOV A, M LDAX B
2. Register B contains 32H, Use MOV and STAX to copy the contents of register B in
memory location 8000H.
LXI H, 8000H LXI D, 8000H
MOV M, B MOV A, B
3. The accumulator contains F2H, Copy A into memory 8000H. Also copy F2H directly into
8000H.
Reference: Ramesh S. Gaonkar | 15
Microprocessors Chapter 2 : Programming with 8085 Microprocessor
4. The data 20H and 30H are stored in 2050H and 2051H. WAP to transfer the data to
3000H and 3001H using LHLD and SHLD instructions.
MVI A, 20H
STA 2050H LHLD 2050H
MVI A, 30H SHLD 3000H
STA 2051H HLT
5. Pair B contains 1122H and pair D contains 3344H. WAP to exchange the contents of B
and D pair using XCHG instruction.
LXI B, 1122H B=11, C=22
LXI D, 3344H D=33, E=44
MOV H, B
MOV L, C
XCHG (Exchange DE pair with HL pair)
MOV B, H
MOV C, L
HLT
6. WAP to add two 4 digit BCD numbers equals 7342 and 1989 and store result in BC
register.
LXI H, 7342H
LXI B, 1989H
MOV A, L
ADD C
DAA
MOV C, A
MOV A, H
ADC B
DAA
MOV B, A
7. Register BC contain 2793H and register DE contain 3182H. Write instruction to add
these two 16 bit numbers and place the sum in memory locations 2050H and 2051H.
What is DAA instruction? Explain its purpose with an example.(Back Paper 2062)
MOV A, C 93H: 1 0 0 1 0 0 1 1
ADD E +82H: 1 0 0 0 0 0 1 0
MOV L, A L=15 15H 1 0
0 0 1 0 1 0 1 15H
MOV A, B 27H: 0 0 1 0 0 1 1 1
ADC D +31H: 0 0 1 1 0 0 0 1
MOV H, A H=59H 0 1 0 1 1 0 0 1 59H
SHLD 2050H ; [2050] 15H , [2051] 59H
Note: SHLD stores the contents of L in specified location and contents of H in next
higher location.
In many applications data are presented in decimal number. In such applications, it may be
convenient to perform arithmetic operations directly in BCD numbers.
The microprocessor cannot recognize BCD numbers; it adds any two numbers in binary. In
BCD addition, any number larger than 9 (from A to F) is invalid and needs to be adjusted by
adding 6 in binary.
A microprocessor is basically a programmable logic chip. It can perform all the logic functions of
the hardwired logic through its instruction set. The 8085 instruction set includes such logic
functions as AND, OR, XOR and NOT (Complement):
1) The instructions implicitly assume that the accumulator is one of the operands.
2) All instructions reset (clear) carry flag except for complement where flag remain
unchanged.
3) They modify Z, P & S flags according to the data conditions of the result.
4) Place the result in the accumulator.
5) They do not affect the contents of the operand register.
The logical operations have the following instructions.
This group has four instructions, two are for rotating left and two are for rotating right. The
instructions are:
However these procedures are invalid when logic 1 is rotated from D7 to D0 or vice
versa.
Q) Explain the instructions that fall in data transfer, arithmetic and logical groups with
example: Show how the flags are affected by each instruction : - (10) [2061 Ashwin (2)]
The microprocessor is a sequential machine; it executes machine codes from one memory
location to the next. The branching instructions instruct the microprocessor to go to a
different memory location and the microprocessor continues executing machine codes from
that new location.
The branching instructions are the most powerful instructions because they allow the
microprocessor to change the sequence of a program, either unconditionally or under
certain test conditions. The branching instruction code categorized in following three
groups:
Jump instructions
Call and return instruction
Restart instruction
Jump Instructions:
The jump instructions specify the memory location explicitly. They are 3 byte instructions, one
byte for the operation code followed by a 16 bit (2 byte) memory address. Jump instructions
can be categorized into unconditional and conditional jump.
Unconditional Jump
8085 includes unconditional jump instruction to enable the programmer to set up continuous
loops without depending only type of conditions. E.g. JMP 16 bit address: loads the program
counter by 16 bit address and jumps to specified memory location.
Here, 40H is higher order address and 00H is lower order address. The lower order byte enters
first and then higher order.
E.g.
MVI A, 80H START: IN 00H
OUT 43H OUT 01H
MVI A, 00H JMP START
L1: OUT 40H
INR A
JMP L1
HLT
Conditional Jump
The conditional jump instructions allow the microprocessor to make decisions based on certain
conditions indicated by the flags. After logic and arithmetic operations, flags are ser or reset to
reflect the condition of data. These instructions check the flag conditions and make decisions to
change or not to change the sequence of program. The four flags namely carry, zero, sign and
parity used by the jump instruction.
Mnemonics Description
JC 16 bit Jump on carry (if CY=1)
JNC 16 bit Jump on if no carry (if CY=0)
JZ 16bit Jump on zero (if Z=1)
JNZ 16bit jump on if no zero (if Z=0)
JP 16bit jump on positive (if S=0)
JM 16bit jump on negative (if S=1)
JPE 16bit Jump on parity even (if P=1)
JPO 16bit Jump on parity odd (if P=0)
E.g. WAP to move 10 bytes of data from starting address 9500 H to 9600H
2008 MOV A, M
2009 STAX D ; Store the contents of accumulator to register pair.
200A INX H ; Increment the register pair by 1.
200B INX D
200C DCR B
200D JNZ 2008
2010 HLT
Q .Write to transfer 30 data starting from 8500 to 9500H if data is odd else store 00H.
MVI B, 1EH
LXI H, 8500H
LXI D, 9500H
L2: MOV A, M
ANI 01H
JNZ L1 ; If data is odd then go to L1.
MVI A, 00H
JMP L3
L1: MOV A, M
L3: STAX D
INX D
INX H
DCR B
JNZ L2
HLT
Call and return instructions: (Subroutine)
Call and return instructions are associated with subroutine technique. A subroutine is a group
of instructions that perform a subtask. A subroutine is written as a separate unit apart from the
main program and the microprocessor transfers the program execution sequence from main
program to subroutine whenever it is called to perform a task. After the completion of
subroutine task microprocessor returns to main program. The subroutine technique eliminates
the need to write a subtask repeatedly, thus it uses memory efficiently. Before implementing
the subroutine, the stack must be defined; the stack is used to store the memory address of the
instruction in the main program that follows the subroutines call.
2007 MOV B, A
Q. What is the purpose of branching instruction? List out all the branching in 8085 and
explain each with example. (2+8) (2046 shrawan)
Restart Instruction:
8085 instruction set includes 8 restart instructions (RST). These are 1 byte instructions and
transfer the program execution to a specific location.
RST 0 C7 0000H
RST 1 CF 0008H
RST 2 D7 0010H
RST 3 DF 0018H
RST 4 E7 0020H
RST 5 EF 0028H
RST 6 F7 0030H
RST 7 FF 0038H
When RST instruction is executed, the 8085 stores the contents of PC on SP and transfers the
program to the restart location. Actually these restart instructions are inserted through
additional hardware. These instructions are part of interrupt process.
STACK
The stack is defined as a set of memory location in R/W memory, specified by a programmer in
a main memory. These memory locations are used to store binary information temporarily
during the execution of a program.
The beginning of the stack is defined in the program by using the instruction LXI SP, 16 bit
address. Once the stack location is defined, it loads 16 bit address in the stack pointer register.
Storing of data bytes for this operation takes place at the memory location that is one less than
the address e.g. LXI SP, 2099H
Here the storing of data bytes begins at 2098H and continuous in reverse order i.e 2097H.
Therefore, the stack is initialized at the highest available memory location to prevent the
program from being destroyed by the stack information. The stack instructions are:
BEORE EXECUTION
H= 93 L= 20
B= 47 C=32
D= AB E=CD
A= 34 F= 10
AFTER EXECUTION
H= 34 L=10
B=AB C=CD
D= 47 E=32
A= 93 F=20
Note: STACK Works in LIFO (Last In First Out) manner.
Question: What do you mean by stack and subroutine? What is the purpose of stack in
subroutines call? Explain the concept of subroutines call and usage along with the
changes in program execution sequence with a suitable example for 8085 microprocessor.
(3+2+5) [2063 kartik]
Counter:
It is designed simply by loading an appropriate number into one of the registers and
using the INR or DCR instructions. A loop is established to update a count, and each
count is checked to determine whether it has reached the final number, if not the loop
is repeated.
Time Delay
When we use loop by counter, the loop causes the delay. Depending upon the clock
period of the system, the time delay occurred during looping. The instructions within
the loop use their own T-states so they need certain time to execute resulting delay.
Suppose we have an 8085 micro processor with 2MHZ clock frequency. Let us
use the instruction MVI which takes 7 T-states. Clock frequency of system (f) = 2 mhz
Clock period (T) = 1/f= ½ *10-6 = 0.5Microproceesor
Time to execute MVI = 7 T-states * 0.5
= 305Micro processor
Eg.MVI C, FFH 7
LOOP: DCR C 4
JNZ LOOP 10/7
Here register C is loaded with count FFH (25510) by using MVI which takes 7 T-states.
Nest 2 instructions DCR and JNZ form a loop with a total of 14 (4+10) T-states. The loop
is repeated 255 times until C=0. The time delay in loop (TC) with 2 mhz frequency is
Tl= ( T * loop T- states * count)
Where, Tl = time delay in loop
T= system clock period
Count = decimal value for counter
Tl= 0.5 * 10-6 * 14 * 255
= 1785 ms
But JNZ takes only 7 T-states when exited from loop i. e. last count = 0.50 adjusted loop
delay
Tla= Tl-(3 T-states – clock period)
= 1785 ms – 1.5 ms = 1783.5 ms
Total delay loop of program
TD= Time to execute outside code + TLA inside Loop
= 7 * 0.5MS + 1783.5 ms
= 1787 ms 1.8 ms
To increase the time delay beyond 1-8 ms for 2MHZ microprocessor, we need to use
counter for register pair or loop within a loop.
T-States Clocks
MVI B, 40H; 64 7 7*1
L2: MVI C, 80H; 128 7 7*64
L1: DCR C 4 4*128*64
JNZ L1 10/7 (10*127+7*1) *64
DCR B 4 4*64
JNZ L2 10/7 10*63+7*1
RET 10 10*1
115854
For 2 MHZ Microprocessor
Total time taken to execute above
Subroutine = 1158 * 0.5*10-6 S
= 57.927 ms
Conversion of BCD number into binary number employs the principle of positional
weighting in a given number.
– Separate an 8- bit packed BCD number into two 4 bit unpacked BCD digits i.e.
BCD1 and BCD2.
– Convert each digit into its binary value according to its position.
– Add both binary numbers to obtain the binary equivalent of the BCD number.
E.g.
A BCD number between 0 and 99 is stored in an R/W memory location called the Input
Buffer (INBUF). Write a main program and a conversion subroutine (BCDBIN) to convert
the BCD number into its equivalent binary number. Store the result in a memory
location defined as the Output Buffer (OUTBUF).
LXI H, 2020H
MVI E, 0A H
MOV A, M ; 0111 0010
ANI F0H ; 0111 0000
RRC
RRC
RRC
RRC
MOV B,A
XRA A
L1: ADD B; 7 10+2
DCR E
JNZ L1
MOV C, A
MOV A, M
ANI 0FH
ADD C
STA 2030H
HLT
Alphanumeric codes:
CPI 0AH
RC
SUI 07H
RET
CODE: PUSH H
LXI H, 1170H
ADD L
MOV L, A
MOV A, M
STAX B
POP H
RET