MPMC EEE Unit - II Programming 8085 Processor
MPMC EEE Unit - II Programming 8085 Processor
Contents:
Instruction format-addressing mode-assembly language format-data transfer, data
manipulation and control instructions-programming: loop structure with counting
& indexing-lookup table-subroutine instructions-stack.
INSTRUCTION FORMAT:
An instruction is a command to the microprocessor to perform a given task on a
specified data. Each instruction has two parts: one is task to be performed, called
the operation code (op-code), and the second is the data to be operated on, called
the operand.
The operand (or data) can be specified in various ways. It may include 8-bit (or 16bit ) data, an internal register, a memory location, or 8-bit (or 16-bit) address. In
some instructions, the operand is implicit.
Instruction word size
The 8085 instruction set is classified into the following three groups according to
word size:
1. One-word or 1-byte instructions
2. Two-word or 2-byte instructions
3. Three-word or 3-byte instructions
In the 8085, "byte" and "word" are synonymous because it is an 8-bit
microprocessor. However, instructions are commonly referred to in terms of bytes
rather than words.
One-Byte Instructions
A 1-byte instruction includes the op-code and operand in the same byte.
Operand(s) are internal register and are coded into the instruction.
For example:
Assume that the data byte is 32H. The assembly language instruction is written as
MVI M, 30H
LXI D, 10FFH
: moves the content of the register pair into the stack pointer.
ADD C
SHLD 3000H
Data transfer instructions are used to move data between registers, register pairs
and between memory and registers. These instructions are described below
MOV r1, r2 (Move register); (r1) (r2); The contents of register r2 are moved to
register r1
EX.; MOV A,B ; (A) (B)
MOV r, M (Move from memory); (r) ((H) (L)); The contents of the memory
location where address is in the registers H and L
are moved to register r.
EX.; MOV A,M ; (A) (M)
MOV M, r (Move to memory); ((H) (L)) (r) ; The contents of register r are
moved to memory location whose address is in
registers H and L.
EX.; MOV M,A ; (M) (A)
MVI r, data (Move to register immediate); (r) (byte 2); The contents of byte 2
of the instruction are moved to register r.
EX.; MVI B,45 ; (B) 45H
MVI M, data (Move to memory immediate); ((H) (L)) (byte 2) ;The contents of
bytes 2 of the instruction are moved to the memory
location where address is in the registers H and L.
EX.; MVI M,45; (M) 45H
LXI rp, data 16 (Load register pair immediate); (rh) (byte 3) (rl) (byte 2)
Byte 3 of instruction is moved to the higher order register
and byte 2 of instruction is moved to the lower order
register.
EX.; LXI D, 8152 ; (DH) 52 (DL) 81
LDA addr (Load accumulator direct) ; (A) ((byte 3) (byte 2)) ; The contents of
memory location where address is specified by byte 2 and byte
3 of the instruction are moved to register A.
EX.; LDA 8152 ; (A) ((8152))
STA addr (store accumulator direct) ; ((byte 3) (byte 2)) (A) ; The contents of
the accumulator are moved to the memory location where
address is specified in byte 2 and byte 3 of the instruction
EX.; STA 4285H ; (4285) (A)
(L) ((byte 3) (byte 2))
(H) ((byte 3) (byte 2) +1)
EX.; LHLD 4155; (L) (4155)
`
(H) (4156)
Arithmetic instructions:
(A) (A)-(byte 2)
(r) (r)-1
(rh)(rl)-1
(H)(L)+(rh)(rl)
(PC)(byte 3) (byte 2)
Else (PC)
PCHL (jump H and L indirect more H and L to PC)
(PCH)(H)
(PCL)(L)
The contents of the register H are moved to the higher order 8 bits of the
register PC. The contents of the register L are moved to the lower order 8
bits of the register PC
Subroutine call and return instruction:
There are other types of branch instructions like CALL, C.condition, RET,
R.condition and RST. These instructions allow the program to jump to a certain
place to execute a number of instructions and then return to the same original
place. This is made possible by storing the address of memory location from where
the jump was made in stack.
CALL addr (call)
((SP)-1) (PCH)
((SP)-2) (PCL)
(SP)(SP)-2
(PC) (byte 3)(byte 2)
The higher order 8 bits of the next instruction address are moved to the memory
location where address is 1 less than the contents of register SP. The lower order 8
bits of the next instruction address are moved to the memory location whose
address is 2 less than the contents of the register SP. The contents of the register SP
are decremented by 2. The control is transferred to the instruction whose address is
specified in byte 3 and byte 2 of the current location
((SP)-1) (PCH)
((SP)-2) (PCL)
(SP)(SP)-2
(PC) (byte 3)(byte 2)
Else (PC)
RET (return)
(PCL)((SP));
(PCH)((SP)+1);
(SP)(Sp)+2
The contents of the memory location whose address is specified in register SP are
moved to the lower order 8 bits of register PC. The contents of the memory
location whose address is specified in 1 more than the contents of register SP are
moved to the higher order 8 bits of the register PC. The contents of the register SP
are incremented by 2.
R condition (conditional return)
If condition = true, then
(PCL)((SP));
(PCH)((SP)+1);
(SP)(Sp)+2
Else (PC)
RST n (restore)
((SP)-1) (PCH)
((SP)-2) (PCL)
(SP)(SP)-2
(PC) 8xn
The higher order 8 bit of the next instruction address are moved to the memory
location whose address is 1 less than the contents of the register SP. The lower
order 8 bit of the next instruction address are moved to the memory location whose
address is 2 less than the contents of the register SP. The contents of the register SP
are decremented by 2. The control is transformed to the instruction where address
is 8 times the contents of the NNN (n is represented on a 3 bit number NNN in opcode)
(H)((SP)+1)
SPHL (move HL to SP)
(SP)(H)(L)
IN port (input)
(A)data
The data placed on the 8 bit bidirectional data bus by the specified port is
moved to register A
OUT port (output)
(data)(A)
The contents of register A are placed on the 8 bit bidirectional data bus for
transmission to the specified port
EI (enable interrupt)
The interrupt system is enabled following the execution of the next
instruction
DI (disable interrupt)
The interrupt system is disabled immediately following the execution of the
DI instruction
HLT (halt)
The processor is stopped. The registers and flags are unsffected
NOP (No operation)
No operation is performed. The registers and flags are unaffected
RIM (Read interrupt mask)
After the execution of the RIM instruction the accumulator is loaded with the
restart interrupt masks pending interrupts if any and the contents of the serial input
data line (SID)
SIM (set interrupt mask)
During the execution of SIM instruction the contents of the accumulator will be
used in programming the restart interrupt masks
ASSEMBLY LANGUAGE PROGRAM:
Let us define a program statement as write an assembly language program to
subtract two numbers. The three tasks are involved in this program
Load two hex numbers
Subtract numbers and
Store the result in the memory
These tasks can be symbolically presented as flow chart as shown in the figure
below
Next job is to find the suitable 8085 assembly language instructions for each tasks.
These instructions are as follows
Task 1 instruction:
MVI A, 20H: load 20H as a first number in register A
MVI B, 40H : load 40H as a second number in register B
Task 2 instruction:
ADD B
: add two numbers and save it in register A
Task 3 instruction:
STA 2200H : store the result in memory location 2200H
HLT
: stop the program execution
We need to execute three tasks in a sequence this corresponding instructions in the
same sequence constitutes an assembly language program (ALP).
Assembly Language Program to Machine Language Program:
Once the assembly language program is ready, it is necessary to convert it in the
machine language program. It is possible to do this by referring the proper hex
code for each assembly instruction from the 8085 instructions set manual. This
process is known as hand assembly and the resulted machine language program is
also known as hex code. Let us see the hex code (opcode) for this program.
Mnemonics
MVI A,20H
MVI B, 40H
ADD B
STA 2200H
HLT
Programming Examples:
Hex code
3EHopcode
20Hoperand
06Hopcode
40Hoperand
80Hopcode
32Hopcode
00Hoperand (low byte addr)
22Hoperand (high byte addr)
76Hopcode
(OR)
Program (ii)
LXI H, 1000H
LXI D, 2000H
MOV B, M
LDAX D
MOV M, A
MOV A, B
STAX D
HLT
LXI H, 2000H
MVI M, 52H
HLT
6003H
6006H
6009H
600CH
LXI H, 2000H
LXI B, 1020H
CALL SUB
HLT
SUBROUTINE PROGRAM:
6100H
SUB: PUSH B
6101H
PUSH H
6102H
LXI B, 4080H
6105H
LXI H, 4090H
6108H
DAD B
6109H
SHLD 2200H
610CH
POP H
610DH
POP B
610EH
RET
LOOPING, COUNTING & INDEXING:
Looping:
In this technique the program is instructed to execute certain set of instructions
repeatedly to execute a particular task number of times. For example to add ten
numbers stored in the consecutive memory locations we have to addition ten times
Counting:
This technique allows programmer to count how many times the instruction/set of
instructions are executed
INDEXING:
This technique allows programmer to point or refer the data stored in sequential
memory locations one by one. Let us see the program loop to understanding
looping, counting & indexing.
The program loop is the basic structure which forces the processor to repeat a
sequence of instructions. Loop has four sections
Initialization section
Processing section
Loop control section
Result section
The initialization section establishes the starting values of Loop counters for
counting how many times loop is executed.
Address registers for indexing which give pointers to memory locations, and
other variables.
The actual data manipulation occurs in the processing section. This is the section
which does the work.
The loop control section updates counters, indices (pointers for the next iteration).
The result section analyses and stores the results.
Program Examples:
P1: calculate the sum of series of numbers
LDA 2200H
:
Initialize the memory
MOV C, A
:
Initialize counter
SUB A
:
A = 0; (SUM=0)
LXI H, 2201H
:
Initialize pointer
BACK ADD M
:
SUM = SUM + data
INX H
:
increment pointer
DCR C
:
decrement counter
JNZ BACK
:
if counter o repeat
STA 2300H
:
store SUM
HLT
:
terminate program execution
Program:
BACK:
SKIP:
LDA 2200H
MOV C, A
XRA A
LXI H, 2202H
CMP M
JNC SKIP
MOV A, M
INX H
DCR C
JNZ BACK
STA 2300H
HLT
:
:
:
:
initialize counter
maximum = minimum, possible value=0
initialize pointer
is number>maximum
:
:
P5: multiply two 8 bit numbers with shift and add method
LXI H, 2200H
MOV E, M
MVI D, 00H
INX H
MOV A, M
LXI H, 0000H
MVI B, 08H
MULT:
DAD H
RAL
JNC SKIP
DAD D
SKIP:
DCR B
JNZ MULT
SHLD 2300H
HLT
P6: program to count given data in a set of numbers
LXI 2000H
MVI B, 14H
MVI C, 00H
BACK:
MOV A, M
CPI 02H
JNZ NEXT
INR C
NEXT:
INX H
DCR B
JNZ BACK
HLT
Exercise programs:
Store 8 bit data in memory
Exchange the contents of memory locations
Add two 8 bit numbers
Code conversion:
This technique allows programmer to translate a number represented using one
coding system to another. For example when we accept any number from the
keyboard it is in ASCII code. But for processing we have to convert this number in
its hex equivalent. The code conversion involve some basic conversions such as
BCD to binary conversion
Binary to BCD conversion
BCD to seven segment code conversion
Binary to ASCII conversion
ASCII to binary conversion
Binary to BCD conversion:
LXI SP, 27FFH
LDA 6000H
CALL BIN TO BCD
HLT
:
:
:
:
STEP 1:
STEP 2:
STEP 3:
MVI E, 00H
CMP B
JC STEP2
SUB B
INR E
JMP STEP 1
CMP C
JC STEP3
SUB C
INR D
JMP STEP 2
STA 6100H
MOV A, D
STA 6101H
MOV A, E
STA 6102H
POP D
POP D
RET
Code
3F
06
5B
4F
66
6D
6
7
8
9
7D
07
7F
6F
Program:
BACK:
LXI H, 6200H
LXI D, 6000H
LXI B, 7000H
LDAX D
MOV L, A
MOV A, M
STAX B
INX D
INX B
MOV A, C
CPI 05H
JNZ BACK
HLT
Exercise programs:
2 digit BCD to binary conversions
Find the square of given numbers
Binary to BCD conversion
Find the Z-segment codes for given number
Find the ASCII character for given numbers
Multiply two 2 digit BCD numbers
Add two 4 digit BCD numbers
Subtraction of two BCD numbers
===========================================================