UNIT II Programming The 8085 MICROPROCESSORS NOTES
UNIT II Programming The 8085 MICROPROCESSORS NOTES
Algorithm :
An algorithm is a step by step procedure to solve a given problem. The steps in an
algorithm are very precise and well-defined. If your problem is a headache, your algorithm might
look like this:
1) Have you been hit on the head? If yes, seek medical attention; if no, go to next step.
2) Have you taken a pain reliever? If no, take one now; if yes go to next step.
3) Have you eaten today? … and so on until it would end with wither a solution or advice to seek
medical attention.
Flowchart :
A flowchart is a graphical representation of an algorithm. These flowcharts play a vital
role in the programming of a problem and are quite helpful in understanding the logic of
complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the
program in any high level language.
Some of the most commonly used flowcharting symbols are given below :
1
Example :
Write an algorithm and draw the flowchart for finding the average of two numbers?
Steps: Input x
1. Input x
2. Input y Input y
3. Sum = x + y
Sum = x + y
4. Average = sum /2
5. Output average
Average = sum/2
Output
Average
END
Programming languages :
Each microcomputer has its own set of instructions based on the design of its CPU or
microprocessor. A language is essential to communicate with the internal parts of the computer
called ‘Computer Language’. These can be divided into three types :
Machine Language
Assembly Language
High level Language
The machine and assembly level languages are considered as low level languages. Low
level language is a medium of communication which is machine dependent. It is also specific to
a given computer. Programs written in these languages are not transferable to different types of
machines.
2
Machine language :
Machine language programs are programs that the computer can understand and execute directly.
These programs are in the form of binary, i.e., 0s and 1s. In this language, there is specific
binary code with different combinations for each instruction.
Ex :
S.No. Function Binary code/Machine code
1 To add the contents of the registers A and B 1000 0000
2 To transfer the contents of register B to the register A 0111 1000
3 To increment the contents of the register A 0011 1100
It is very difficult for the programmers to write a program in machine codes. The
program is very difficult to understand unless the programmer remembers all the machine codes,
which is impractical. It is almost impossible to write an error free program with 1s and 0s. Also
it is very tiring for the programmer to enter a machine language program written in binary into
the microcomputer’s memory.
However the machine language is preferred for simple programs and simple control
applications where less computation is required.
Assembly language :
The next programming level is ‘assembly language’. An assembly language program is
written in the form of ‘mnemonics’ and symbolic addresses. Mnemonic is a symbolic code for
each instruction. The mnemonic for a particular instruction consists of letters which suggests the
operation to be performed by that instruction. Hence assembly language program is much easier
and faster to the programmer than machine language program.
Ex :
S.No. Instruction Description
1 MOV A, B Copy the contents of the register B to the register A
2 INR A Increment the contents of the register A by one
3 ADD B The contents of B register are added to the contents of
register A
3
An assembly language program is specific to a given computer (i.e., machine dependent).
Programs written in these languages are not transferable to different types of machines.
Only with assembly language programming, the programs can not be executed by the
computer. Because, computer knows, only one language i.e., machine language. Hence
assembly language program must be converted into machine language program. So, a program,
called ‘assembler’ translates the assembly language program into machine language program.
High level language :
The programming language, which is machine independent and composed of English
language type statements is called ‘ High level language’. It terminates the difficulties
associated with machine and assembly languages. The high level languages, such as
FORTRAN, BASIC, PASCAL, COBOL, C, and C++ are widely used in these days. Programs
written in these languages for one computer can easily be used for any other type of computer.
Hence these languages are machine independent and portable.
Like assembly language program, a high level language program also requires a special
program known as ‘compiler’ for translating high level language program into machine language
program.
Instruction and data foramats :
Instruction formats :
An instruction is a command given to the microprocessor to perform a given task on specified
data. Each instruction has two parts, one is the task to be performed called the operation code
(op-code) and the second is the data to be operated on, known as operand. The operand or data
can be specified in various ways.
According to the byte size, the 8085 instructions are classified into three types. They are
One byte instructions
Two byte instructions
Three byte instructions
4
One byte instructions : A one byte instruction is one which contains only op-code and it do not
require any data or address.
Ex : MOV C, A Hex code : 4F (one byte)
ADD B Hex code : 80 (one byte)
CMA Hex code : 2F (one byte)
Two byte instructions : A two byte instruction is one which contains an 8-bit op-code and 8-bit
operand (data).
Ex : MVI A, 09 Hex code : 3E, 09 (two byte)
ADD B,07 Hex code : 80, 07 (two byte)
SUB A, 05 Hex code : 97, 05 (two byte)
Three byte instructions : A three byte instruction is one which contains an 8-bit op-code and
16-bit address.
Ex : LXI H, 8509 Hex code : 21, 09, 85 (three byte)
LDA 8509 Hex code : 3A, 09, 85 (three byte)
JMP 9567 Hex code : C3, 67, 95 (three byte)
STA 3572 Hex code : 32, 72, 35 (three byte)
Data formats :
The 8085 is an 8-bit microprocessor which process only binary numbers. But it is very
difficult to understand these binary numbers by a common user. So, these binary numbers have
been encoded in different data formats. The commonly known data formats are ASCII, BCD,
signed integers and unsigned integers. The ASCII code is a 7-bit alpha-numeric code that
represents decimal numbers, English alphabets and certain special characters. The ASCII stands
for “ American Standard Code for Information Interchange”.
The term BCD stand for Binary Coded Decimal, used for decimal numbers from 0-9. An
8-bit register can store two BCD numbers. A signed integer is either a positive or a negative
number. In 8085 microprocessor the most significant bit D 7 is used for the sign. Here 0 denotes
the positive sign and 1 denotes the negative sign. An integer without a sign can be represented
by all the 8-bits.
5
Instruction set of 8085 or Classification of 8085 instructions :
An instruction is a binary pattern designed inside a microprocessor to perform a specific
function. The entire group of instructions, called the instruction set, determines what functions
the microprocessor can perform. The 8085 microprocessor instruction set has 74 op-codes that
result in 246 instructions.
The instruction set of 8085 microprocessor is classified into five groups. They are
1. Data transfer (copy) group
2. Arithmetic group
3. Logical group
4. Branch group
5. Stack, I/O and Machine control group
Data transfer (copy) group instructions :
The data transfer instructions are used to transfer data from one register to another
register, from memory to register or register to memory but not from one memory location to
another memory location. Actually this data transfer instruction copies the data from source to
destination and the contents of the source are not altered. So, the data transfer performs basically
the ‘copy’ operation.
6
S.No. Opcode Operand Description Example Action
5 LXI Reg. Pair, Load reg. pair immediate with 16-bit LXI H, HL2500
16-bit data 2500H L00
data H25
6 LHLD 16-bit Load HL pair direct LHLD 2040H L contents
address of 2040
Hcontents
of 2041
7 STA 16-bit Store accumulator direct STA 2400H Acontents
address of 2400
8 STAX B/D Reg. Store accumulator indirect STAX B contents of
pair BC reg. pair
(address)
A
9 SHLD 16-bit Store HL pair direct SHLD 2040H 2040conte
address nts of L
2041conte
nts of H
10 XCHG None Exchange HL with DE XCHG HD
LE
7
S.No. Opcode Operand Description Example Action
1 ADD R Add the contents of register or memory ADD B AA+B
M location to accumulator ADD M AA+M
2 ADC R Add the contents of register or memory ADC B AA+B+CY
M location to accumulator with carry ADC M AA+M+CY
3 ADI 8-bit data Add immediate 8-bit data to ADI 45H AA+45
Accumulator
4 ACI 8-bit data Add immediate 8-bit data to ACI 58 AA+58+CY
Accumulator with carry
5 DAD Reg. pair Add the contents of register pair to HL DAD B HLHL+BC
pair
6 DAA None The contents of A are divided into two DAA A=00111000
4-bit BCD numbers A=0011 1000
7 SUB R Subtract the contents of register or SUB B AA-B
M memory location from accumulator SUB M AA-M
8 SBB R Subtract the contents of register or SBB B AA-B-CY
M memory location from accumulator with SBB M AA-M-CY
borrow
9 SUI 8-bit data Subtract immediate 8-bit data from SUI 45H AA-45
Accumulator
10 SBI 8-bit data Subtract immediate 8-bit data from SBI 63H AA-63-CY
Accumulator with borrow
11 INR R Increment the contents of register or INR B BB+1
M memory location by 1 INR M M=M+1
12 INX Reg. pair Increment the contents of register pair INX H HL=HL+1
by 1
13 DCR R Decrement the contents of register or DCR B BB-1
M memory location by 1 DCR M M=M-1
14 DCX Reg. pair Decrement the contents of register pair DCX H HL=HL-1
by 1
8
Logical instructions :
These instructions perform logical operations such as AND, OR, XOR, Rotate, Compare and
Complement on data in registers, memory and on conditional flags.
Any 8-bit data or the contents of register or memory location can logically have
AND, OR and XOR operation with the contents of Accumulator. And the result
is again stored in Accumulator.
Each bit in the Accumulator can be shifted either left or right to the next position.
Any 8-bit data or the contents of register or memory location can be compared
for: equality, greater than and less than with the contents of Accumulator. The
result is reflected in status flags.
The contents of Accumulator cane be complemented.
S.No. Opcode Operand Description Example Action
1 CMP R Compare the contents of register or CMP B If A<B, CY=1
M memory location with Accumulator CMP M If A=B, Z=1
If A>B, CY=0,
Z=0
2 CPI 8-bit data Compare 8-bit data immediate with CPI 45H If A<45 CY=1
Accumulator If A=45, Z=1
If A>45,
CY=0, Z=0
3 ANA R Logical AND the contents of register or ANA B AA & B
M memory location with Accumulator ANA M AA & M
4 ANI 8-bit data Logical AND the 8-bit data immediate ANI 56H AA & 56
with Accumulator
5 ORA R Logical OR the contents of register or ORA B AA | B
M memory location with Accumulator ORA M AA | M
6 ORI 8-bit data Logical OR the 8-bit data immediate ORI 56H AA | 56
with Accumulator
7 XRA R Logical XOR the contents of register or XRA B AA ^ B
M memory location with Accumulator XRA M AA ^ M
8 XRI 8-bit data Logical XOR the 8-bit data immediate XRI 56H AA ^ 56
9
with Accumulator
9 RLC None Rotate the contents of Accumulator left RLC An+1An
by 1
10 RRC None Rotate the contents of Accumulator right RRC An An+1
by 1
11 RAL None Rotate the contents of Accumulator left RAL An+1An
by 1 through carry CYA7
A0CY
12 RAR None Rotate the contents of Accumulator right RAR An An+1
by 1 through carry CYA0
A7CY
13 CMA None Complement the contents of CMA A
Accumulator
14 CMC None Complement carry CMC CY
15 STC None Set carry STC CY1
10
2 No Carry (NC) CY = 0
3 Zero (Z) Z=1
4 No Zero (NZ) Z=0
5 Plus (P) S=0
6 Minus (M) S=1
7 Parity Even (PE) P=1
8 Parity Odd (PO) P=0
11
depending upon the number. SPSP-2
PCaddress
Stack, I/O and Machine control group instructions :
This group of instructions performs the manipulation of the stack, I/O operation and machine
functions.
To store in and to take out the contents from the stack and for stack manipulation, the
stack +related instructions are used.
The I/O instructions allow a microprocessor to perform I/O operations with the outside
world.
The machine control instructions control the machine functions such as halt, interrupt or
do nothing.
S.No. Opcode Operand Description Example Action
1 PUSH Reg. pair The contents of register pair are PUSH B SP-1H
copied onto stack SP-2L
SPSP-2
2 POP Reg. pair The contents of top of the stack POP H LSP
are copied into register pair HSP+1
SPSP+2
3 IN 8-bit port The contents of I/O port are IN 80H A80
address copied into accumulator.
4 OUT 8-bit port The contents of accumulator are OUT 45H 45A
address copied into the I/O port.
5 EI None Enable interrupts except TRAP EI ---
6 DI None Disable interrupts except TRAP DI ---
7 HLT None Halt HLT ---
8 NOP None No operation NOP ---
9 RIM None Read interrupt mask RIM ---
10 SIM None Set interrupt mask SIM ---
12
The way of identifying the position of an operand in an instruction is known as addressing mode.
In 8085 microprocessor, there are five types of addressing modes.
i) Direct addressing mode
ii) Register addressing mode
iii) Register indirect addressing mode
iv) Immediate addressing mode
v) Implicit addressing mode
Direct addressing mode :
The mode, in which the address of the operand is given in the instruction itself, is known as the
direct addressing mode.
Ex:
1) LDA 9000H ; Load the contents of memory location into Accumulator
2) STA 8000H ; Store the contents of the Accumulator in the location 8000H
3) IN 01H ; Read the data from port whose address is 01H
Register addressing mode :
The mode, in which the operand is in one of the general purpose registers, is known as the
register addressing mode.
Ex:
1) MOV A, B ; Move the contents of B register to A register
2) SUB D ; Subtract the contents of D register from Accumulator
3) ADD B, C ; Add the contents of C register to the contents of B register
Register indirect addressing mode :
The mode, in which the address of the operand is given in an indirect way with the help of a
register pair is called register indirect addressing mode.
Ex:
1) MOV A, M ; Move the contents of the memory location pointed by the H-L pair to
Accumulator
2) CMP M ; Compare the contents of memory location with accumulator
3) ADD M ; Add the contents of memory location with accumulator
Immediate addressing mode :
13
The mode, in which the operand is a part of the instruction itself is known as immediate
addressing mode.
1) ADI 08H ; Add immediate the data 08H to the contents of the accumulator
2) LXI H, 8500H ; Load immediate the HL pair with the operand 8500H
3) MVI 06H ; Move the data 06H immediately to the accumulator
Implicit addressing mode :
The mode, which do not specify the operand in the instruction, is known as implicit addressing
mode.
Ex:
1) CMA ; Complement the contents of Accumulator
2) CMC ; Complement carry
3) RLC ; Rotate accumulator left by one bit
4) EI ; Enable interrupts
5) SIM ; Set interrupt mask
Assembly language programming examples :
Addition of two 8-bit numbers :
Algorithm :
1) Initialize HL pair with the memory address of 1st number i.e. 9000H
2) Clear the Accumulator
3) Add the contents of memory location M to accumulator
4) Increment memory pointer to 2nd number i.e., 9001H
5) Add the contents of memory location M to accumulator
6) Store the contents of accumulator in 9002H
7) Stop the execution
Flow chart :
14
Program:
Label Mnemonics Comments
Op-code Operand
LXI H,9000H Initialize the HL pair with 9000H and get the 1st number
MVI A,00H Clear accumulator
ADD M The first number is added to accumulator
INX H Increment the memory pointer to 2nd number i.e. 9001H
ADD M The second number is added to the contents of
accumulator
STA 9002H The contents of accumulator are stored in memory
location 9002H
HLT Stop the execution
Ex(i): Input:
9000 H – 29 H
Ex(ii):
Input: 9000 H
– 49 H
9001 H – 16 H 9002 H – 32 H
Result: 9002 H – 3F H Result: 9003 H – 7B H
Subtraction of two 8-bit numbers :
Algorithm :
1) Initialize HL pair with the memory address of 1st number
2) Move the 1st number into Accumulator
3) Increment HL pair to point to 2nd number
4) Subtract the 2nd number from the 1st number
5) Increment the HL pair to next memory location
6) Store the result
7) Stop the execution
15
Flow chart :
Program:
16
9001 H – 30 H 9002 H – 32 H
Result: 9002 H – 29 H Result: 9003 H – 17 H
Flow Chart :
17
Program :
18
Label Mnemonics Comments
Op-code Operand
LHLD 8000H Initialize HL pair with the memory address of 1st
number i.e. 8000H & 8001H
XCHG Exchange the 1st number with DE pair
LHLD 8002H Initialize HL pair with the memory address of 2nd
number i.e. 8002H & 8003H
MVI C,00 Move immediate the data 00H to carry
DAD D Add the 1st and 2nd numbers
JNC FWD Is carry? No, go to label FWD
INR C Increment carry by 1
FWD: SHLD 8004H Store the result in 8004H and 8005H
MOV A,C Move the carry to Accumulator
STA 8006H Store the carry in 8006H
HLT Stop the execution
19
7) Subtract the higher byte of the second number with borrow & Store the result in H
register
8) Finally, store the result in memory locations 8004H and 8005H
9) Stop the execution
Flow Chart :
Program :
20
Label Mnemonics Comments
Op-code Operand
LHLD 8000H Initialize HL pair with the memory address of 1st
number i.e. 8000H & 8001H
XCHG Exchange the 1st number with DE pair
LHLD 8002H Initialize HL pair with the memory address of 2nd
number i.e. 8002H & 8003H
MOV A,E Get the lower byte of the 1st number into accumulator
SUB L Subtract the lower byte of the second number
MOV L,A Store the result in L register
MOV A,D Get higher byte of the 1st number into accumulator
SBB H Subtract the higher byte of the second number with
borrow
MOV H,A Store the result in H register
SHLD 8004H Store the result in memory locations 8004H and 8005H
HLT Stop the execution
21
5) Shift the multiplier left by one bit
6) If these is a carry add multiplicand to product
7) Decrement the count
8) If count ≠ 0 then go to step 4
9) Store the product i.e. result in 8503H
10) Stop the execution
Flow chart :
Program :
Label Mnemonics Comments
22
Op-code Operand
LHLD 8500H Load the multiplicand in to HL pair
XCHG Exchange the multiplicand with DE pair
LDA 8502H Load the multiplier in to accumulator
LXI H,0000 Initialize HL pair with 0000H
MVI C,08 Initialize the count with 08H
LOOP: DAD H Shift the partial product by one bit
RAL Rotate multiplier left by one bit
JNC FWD Is multiplier bit=1? No, goto label FWD
DAD D Product = Product + Multiplicand
FWD: DCR C Count = count - 1
JNZ LOOP If count ≠ 0, goto label LOOP
SHLD 8503H Store the result in memory locations 85003H & 8504H
HLT Stop the execution
23
9. If count not equal to zero, go to step 4
10. Store the quotient in 8503H
11. Store the remainder in 8504H
12. Stop the execution
13. Initialize HL pair with the address of dividend
Flow Chart :
Program :
24
Label Mnemonics Comments
Op-code Operand
LHLD 8500H Initialize HL pair with the address of dividend
LDA 8502H Load the divisor in to accumulator
MOV B,A Move divisor into Register B from A
MVI C,08 Initialize Count = 08H
BACK: DAD H Shift dividend and quotient left by one bit
MOV A,H Move MSB of the dividend into accumulator
SUB B Subtract the divisor from MSB bits of the dividend
JC FWD Is MSB part of dividend > divisor? No, go to label FWD
MOV H,A Move MSB of the dividend in to Register H
INR L Increment quotient
FWD: DCR C Decrement count
JNZ BACK If count is not zero jump to label BACK
SHLD 8503H Store quotient in 8503H and remainder in 8504H
HLT Stop the execution
25
8. The larger number is placed in the accumulator
9. The process is repeated until the count is zero
10. Final result is stored in 8600H memory location
11. Stop the execution
Flow Chart :
Program :
26
Label Mnemonics Comments
Op-code Operand
LXI H, 8500H Initialize HL pair with the address of count
MOV C,M Move the count to register C
INX H Increment the memory pointer to 1st number
MOV A,M Move the 1st number into Accumulator
DCR C Decrement count by 1
LOOP1: INX H Get the next number
CMP M Compare the next number with previous number
JNC LOOP2 Is next number > previous maximum? No, go to label
LOOP2
MOV A,M If, yes, move the large number into accumulator
LOOP2: DCR C Decrement count by 1
JNZ LOOP1 If, count is not equal to zero, go to label LOOP1
STA 8600H Store the largest number in 8600H location
HLT Stop the execution
Ex : Input: 8500H – 06H (Count)
8501H – 0AH
8502H – 08H
8503H – 14H
8504H – 7EH
8505H – 2CH
8506H – 56H
Result : 8600H – 7EH (Largest number)
Smallest Number in an array :
Algorithm :
1. Initialize HL pair with the address of count
2. Move the count to register C
3. Move the first number into accumulator by incrementing the memory pointer
4. Get the next number by incrementing the memory pointer
27
5. Compare the first number with the second number in the memory
6. The smaller in the two is placed in the accumulator
7. The number in the accumulator is compared with the next number in memory
8. The smaller number is placed in the accumulator
9. The process is repeated until the count is zero
10. Final result is stored in 8600H memory location
11. Stop the execution
Flow Chart :
Program :
28
Label Mnemonics Comments
Op-code Operand
LXI H, 8500H Initialize HL pair with the address of count
MOV C,M Move the count to register C
INX H Increment the memory pointer to 1st number
MOV A,M Move the 1st number into Accumulator
DCR C Decrement count by 1
LOOP1: INX H Get the next number
CMP M Compare the next number with previous number
JC LOOP2 Is next number < previous smallest? No, go to label
LOOP2
MOV A,M If, Yes, move the smaller number into accumulator
LOOP2: DCR C Decrement count by 1
JNZ LOOP1 If, count is not equal to zero, go to label LOOP1
STA 8600H Store the smallest number in 8600H location
HLT Stop the execution
Stack :
29
Stack is a set of memory locations in the read/write memory which is used for temporary
storage of binary information during the execution of a program. It is implemented in the Last-
In-First-Out (LIFO) manner i.e, the data written first can be accessed last, one can write the data
on the top of the stack by a special operation known as PUSH. Data can be read or taken out
from the top of the stack by another special instruction known as POP.
A block of RAM area is allocated to the stack. A special purpose register known as stack
pointer (SP) points to the top of the stack. Whenever the stack is empty, it points to the bottom
address. If a PUSH operation is performed, the data are stored at the location pointed to by SP
and it is decremented by one. Similarly, if the POP operation is performed, the data are taken
out of the location pointed at by SP and SP is incremented by one.
Stack operation :
Operations on stack are performed using the two instructions namely PUSH and POP.
The contents of the registers are moved to certain memory locations after PUSH instruction.
Similarly, the contents of the memory are transferred back to registers by POP instruction.
For example, let us consider a stack whose stack top is 8506H. This is stored in the 16
bit stack pointer register as shown below.
Let us consider two registers (register pair) B and C whose contents are 25 and 62.
30
Let us now consider the POP operation: Before and after the POP operation in detail.
31
Subroutine:
It is a set of instructions written separately from the main program to execute a function
that occurs repeatedly in the main program.
For example, let us assume that a delay is needed three times in a program. Writing
delay programs for three times in a main program is nothing but repetition. So, we can write a
subroutine program called ‘delay’ and can be called any number of times we need.
The 8085 microprocessor has two instructions to implement the subroutines. They are
CALL and RET. The CALL instruction is used in the main program to call a subroutine and
RET instruction is used at the end of the subroutine to return to the main program. When a
subroutine is called, the contents of the program counter, which is the address of the instruction
following the CALL instruction is stored on the stack and the program execution is transferred to
the subroutine address. When the RET instruction is executed at the end of the subroutine, the
memory address stored on the stack is retrieved and the sequence of execution is resumed in the
main program.
32
Diagrammatic representation :
Program example:
Memory Mnemonics Comments
address Op-code Operand
8000 LXI SP, 8500H Initialize the stack pointer at 8500H
|
|
|
8020 CALL 8070H Call a subroutine program stored at 8070H
8023 Next The address of the next instruction following the CALL
instruction instruction
|
|
|
802F HLT End of the main program
33
Subroutine program :
Memory Mnemonics Comments
address Op-code Operand
8070 Instructions Beginning of the subroutine
| |
| |
| |
807B RET End of the subroutine
34
Delay subroutine with two registers :
Label Mnemonics Comments
Op-code Operand
MVI B,10H Get the number FFH in to register B
LOOP1: MVI C,FFH Get the number FFH in to register C
LOOP2: DCR C Decrement register C
JNZ LOOP2 Is C=00? No, jump to label LOOP2
DCR B Decrement register C
JNZ LOOP1 Is B=00? No, jump to label LOOP1
RET Return to the main program
35