Chapter_Four
Chapter_Four
Instruction Set
and
Programming Techniques
1
Outline
Instruction Formats: Single Byte, Two Bytes & Three Bytes Instructions
Opcode Format
Looping, Counting and Indexing Counter and Timing delays Stack and
Subroutines
However, instructions are commonly referred to in terms of bytes rather than words
1. One-Byte Instructions:
A 1-byte instruction includes the opcode and operand in the same byte.
Operand(s) are internal register and are coded into the instruction
4
Cntd…
These instructions are 1-byte instructions performing three different tasks.
In the first instruction, both operand registers are specified.
In the second instruction, the operand B is specified and the accumulator is assumed.
Similarly, in the third instruction, the accumulator is assumed to be the implicit operand.
These instructions are stored in 8- bit binary format in memory; each requires one memory
location.
MOV rd, rs
rd rs copies contents of rs into rd.
1. Example: MOV A,B
Coded as 01111000 = 78H = 170 octal (octal was used extensively in instruction design of such
processors).
ADD r
AA+r
5
Cntd…
2.Two-Byte Instructions: In a two-byte instruction, the first byte specifies the operation code
and the second byte specifies the operand. Source operand is a data byte immediately following the
opcode. For example:
1. rp data16
2. LDA addr
10
Cntd…
11
Cntd…
12
Arithmetic instructions in 8085 microprocessor
Arithmetic Instructions are the instructions which perform basic arithmetic operations such as
addition, subtraction and a few more.
In 8085 microprocessor, the destination operand is generally the accumulator.
Following is the table showing the list of arithmetic instructions:
13
Cntd…
14
Cntd…
15
Logical instructions in 8085 microprocessor
Logical instructions are the instructions which perform basic logical operations such as AND,
OR, etc.
In 8085 microprocessor, the destination operand is always the accumulator.
Here logical operation works on a bitwise level.
Following is the table showing the list of logical instructions:
16
Cntd…
17
Cntd…
In the table,
18
Branching instructions in 8085 microprocessor
Branching instructions refer to the act of switching execution to a different instruction sequence
as a result of executing a branch instruction.
b) Conditional Jump Instructions: Transfers the program sequence to the described memory address
only if the condition in satisfied.
20
Cntd…
2. Call Instructions – The call instruction transfers the program sequence to the memory address
given in the operand.
Before transferring, the address of the next instruction after CALL is pushed onto the stack. Call
instructions are 2 types:
(a) Unconditional Call Instructions: It transfers the program sequence to the memory address given
in the operand.
21
Cntd…
(b) Conditional Call Instructions: Only if the condition is satisfied, the instructions executes.
22
Cntd…
3. Return Instructions – The return instruction transfers the program sequence from the subroutine
to the calling program.
Return instructions are 2 types: Unconditional Jump Instructions and Conditional Jump
Instructions.
a) Unconditional Return Instruction: The program sequence is transferred unconditionally from
(b) Conditional Return Instruction: The program sequence is transferred unconditionally from
24
Stack I/O, and Machine Control Instructions:
The following instructions affect the Stack and/or Stack Pointer:
PUSH - Push Two bytes of Data onto the Stack
POP - Pop Two Bytes of Data off the Stack
XTHL - Exchange Top of Stack with H & L
SPHL - Move content of H & L to Stack Pointer
The I/0 instructions are as follows:
IN - Initiate Input Operation
OUT - Initiate Output Operation
The Machine Control instructions are as follows:
EI - Enable Interrupt System
DI - Disable Interrupt System
HLT - Halt
NOP - No Operation
25
Cntd…
1. Program 1. Store the data byte 32H into memory location 4000H.
MVI A, 32H
STA 4000H
HLT
LDA 2000H
MOV B, A
LDA 4000H
STA 2000H
MOV A, B
STA 4000H
26
Cntd…
3A. Write an assembly program to add two numbers
3B. Write an assembly program to add two numbers
Program with carry
MVI D, 99H
MVI D, 8BH
MVI E, 99H
MVI C, 6FH
MOV A, D
MOV A, C ADD E
ADD D MOV C, A
HLT JNC END
MVI B,01H
INR B
END: HLT
4. Write an assembly program to multiply a number by 8 Program
MVI D, 8BH
MVI C, 6FH
MOV A, C
MUL D
HLT 27
Cntd…
5. Subtract the contents of memory location 4001H from the memory location 4000H and place the
result in memory location 4002H.
(4000H) = 51H
PROGRM
(4001H) = 19H
LXI H,4000H
Result = 51H - 19H = 38H
MOV A, M
INX H
SUB M
INX H
MOV M, A
HLT
28
Cntd…
6. Add the contents of memory location 4000H from the memory location 4001H and place the result
in memory location 4002H. ( with out carry)
INX H INX H
MOV B, M ADD M
ADD B INX H
INX H MOV M, A
MOV M, A HLT
HLT
29
Cntd…
6. Add the contents of memory location 4000H from the memory location 4001H and place the result
in memory location 4002H. ( with carry)
MOV B, M HLT
MVI C,00H
ADD B
JNC SKIP
INR C
30
Cntd…
7. Add the 16-bit number in memory locations 4000H and 4001H to the 16-bit number in memory
locations 4002H and 4003H.
Store the result in memory locations 4004H and 4005H with the most significant byte in memory
location 4005H.
PROGRM
(4000H) = 15H
LHLD 4000H
(4001H) = 1CH
XCHG
(4002H) = B7H
LHLD 4002H
(4003H) = 5AH
DAD D
Result = 1C15 + 5AB7 = 76CCH
SHLD 4004H
(4004H) = CCH
HLT
(4005H) = 76H
31
Cntd…
8. Subtract the 16-bit number in memory locations 4002H and 4003H from the 16-bit number in
memory locations 4000H and 4001H.
Store the result in memory locations 4004H and 4005H with the most significant byte in memory
location 4005H.
PROGRM
(4000H) = 19H LHLD 4000H
XCHG
(400IH) = 6AH
LHLD 4002H
(4002H) = I5H MOV A, E
SUB L
(4003H) = 5CH
MOV L, A
Result = 6A19H – 5C15H = OE04H MOV A, D
SBB H
(4004H) = 04H MOV H, A
(4005H) = OEH SHLD 4004H
HLT 32
Cntd…
9. Find the l's complement of the number stored at memory location 4400H and store the
complemented number at memory location 4300H.(8bit)
(4400H) = 55H
Result = (4300H) = AAH
Solution
LDA 4400H
CMA
STA 4300H
HLT
10. 1st complement of 16bit
LHLD 8100
MOV A, L
CMA
MOV L, A
MOV A, H
CMA
MOV H, A 33
Cntd…
11. Write a program to shift an eight bit data four bits right. Assume that data is in register C.
MVI C, 0FH
MOV A, C
RRC
RRC
RRC
RRC
MOV C, A
HLT
MVI C, 0FH
MOV A, C
RAR
RAR
RAR
RAR
MOV C, A
HLT
RAR
Cntd…
12. Write an assembly program to find greatest between two numbers
Program
MVI B, 30H
MVI C, 40H
MOV A, B
CMP C
JZ EQU
JC GRT
HLT
36
Cntd…
13. Subtraction of two 8bit numbers, Manually store 1st no in the memory location C050,
Manually store 2nd no in the memory location C051, // Result is stored in C053
LXI H,C050
MOV A,M
INX H
SUB M
INX H
MOV M,A
HLT
# ORG C050
# DB 95H,65H
37
Looping, Counting and Indexing Counter and Timing delays Stack and Subroutines
1. LOOPING – in-order to run certain set of instructions repeatedly to execute a particular task for
certain number of time
2. COUNTING – used to count as how many times the instruction/set of instructions are executed
3. INDEXING – allows programmer to point or refer the data stored in sequential memory location
one by one
Loops Have Four Sections
1. Initialization Section ,
2. Processing Section,
3. Loop Control,
4. Result Section
38
Cntd…
1. Initialization section initializes
Loop counters for counting how many times loop is executed
Address registers for indexing which give pointers to memory locations and other variables
2. Processing section:
The actual data manipulation occurs in the processing sections. This section does the work
3. Loop control section updates counters, indices(pointers) for the next iteration
4. Result section analyses and stores the results
1. Calculate The Sum Of Series Of Numbers
39
Cntd…
1. Assembly program to find the product of two 8 bit (4x5)
Program:
MVI C,04
SUB A
MVI B,05
BACK: ADD B
DCR C
JNZ BACK
STA 2300
HLT
40
Cntd…
2. The sum of 1 up 10 addition 8 bit
Program:
MVI A, 00
MVI B, 01
MVI C, A
LOOP: ADD B
INR B
DCR C
JNZ LOOP
HLT
41
Cntd…
3.The sum of odd number from 1 up 10(8 bit)
Program 1. MVI A,00
MVI D,00 program 2:
MVI B,00 MVI A,00
MVI C,0A
MVI B,01
BACK: MOV A,C MVI C,05
ANI 01
JNZ ODD LOOP: ADD B
JMP NEXT
INR B
ODD: MOV A,C INR B
ADD D DCR C
MOV D,A JNZ LOOP
HLT
NEXT: DCR C
JNZ BACK
MOV A,D
STA 2211
42
HLT
Cntd…
4.The sum of even number from 1 up 10(8 bit)
MVI A,00
MVI D,00
MVI B,00
MVI C,0A
NEXT: DCR C
JNZ BACK
MOV A,B
STA 2210
43
HLT
Cntd…
5.The sum of Odd and even a number from 1 up 10(8 bit)
MVI A,00
MVI D,00
MVI B,00
MVI C,0A
NEXT: DCR C
JNZ BACK
MOV A,B
STA 2210
MOV A,D
STA 2211
HLT 44
Cntd…
6.Take N numbers from memory 2200 and sum even and odd number and store the result (8 bit)
LDA 2200H
MOV C,A
MVI D,0
MVI B,0
LXI H,2201
BACK:MOV A,M
ANI 01
JNZ ODD
MOV A,B
ADD M
MOV B,A
JMP NEXT
ODD: MOV A,D
ADD M
MOV D,A
NEXT: INX H
DCR C
JNZ BACK
MOV A,B
STA 2210
MOV A,D
STA 2211
HLT
45
Cntd…
7.8085 program to find maximum and minimum of 10 numbers
LXI H,2050
MOV B,M ;MAXIMUM
MOV C,M ;MIMIMUM
MVI D,05 ;COUNTER
LOOP: MOV A,M
CMP B
JC MIN
MOV B,A
MIN: CMP C
JNC SKIP
MOV C,A
SKIP: INX H
DCR D
JNZ LOOP
LXI H,2060
MOV M,B
INX H
MOV M,C
HLT 46
Cntd…
8. Arrange an numbers in Ascending Order
LXI H,5000 // Set pointer for array
MOV C,M // Load the Count
DCR C // Decrement Count
REPEAT: MOV D,C
LXI H,5001 // Load starting address of data
LOOP: MOV A,M // copy content of memory location to Accumulator
INX H
CMP M
JC SKIP // Jump to skip if carry not generated
MOV B,M // copy content of memory location to B - Register
MOV M,A // copy content of A - Register to memory location
DCX H // Decrement content of HL pair of registers
MOV M,B
INX H // Increment content of HL pair of registers
SKIP: DCR D
JNZ LOOP // Jump to LOOP if not Zero
DCR C
JNZ REPEAT // Jump to REPEAT if not Zero
47
HLT
Cntd…
9. Arrange an numbers in Descending Order
LXI H,5000 ;Set pointer for array
MOV C,M ;Load the Count
DCR C ;Decrement Count
REPEAT: MOV D,C
LXI H,5001 ;Load starting address of data array
LOOP: MOV A,M ;copy content of memory location to Accumulator
INX H
CMP M
JNC SKIP ;Jump to skip if carry not generated
MOV B,M ;copy content of memory location to B - Register
MOV M,A ;copy content of A - Register to memory location
DCX H ;Decrement content of HL pair of registers
MOV M,B
INX H ;Increment content of HL pair of registers
SKIP: DCR D
JNZ LOOP ;Jump to LOOP if not Zero
DCR C ;Decrement Count
JNZ REPEAT ;Jump to REPEAT if not Zero
48
HLT ;Terminate Program
Timer Delay
Timer delay using nop instruction
Each NOP instruction takes 4T states of the processor time to execute.
So by executing NOP instruction in between two instructions we can get delay of 4T states
49
Cntd…
Timer delay using 8 bit counters
Counting can create time delays. Since the execution times of the instructions used in a counting
routine are known, the initial value of the counter, required to get specific time delays can be
determined.
In this program the instruction DCR C and JNZ BACK execute number of times equal to count
stored in the C registered. The time taken by this program for execution can be calculated with
the helps of T state.
The column to the right of the comments indicates the number of T state in the instruction cycle
of each instructions.
Two values are specified for the number of T state for JNZ instructions.
The smaller value is applied when the condition is not met and the larger value applied when the
condition is met.
The first instruction MVI C count executed only once and it requires 7 T state. There are count -
1 passes through the loop (DCR C).
50
Cntd…
The number of T states elapse while C is not zero are (count-1) X (4x10).
On the last pass through the loop the condition is not met and the loop is terminated.
51
Using 16bit counter
in this program, the instruction DEX B, MOVA,C ,ORA B, and JNZ BACK execute number of
times equal to count stored in BC register pair. The instruction LXI B, count is executed only once.
It requires 10T-state.
the number of T state required for last loop. =6+4+1+7=24 T-state. Total state required for
execution of a given program.
52
Cntd…
53
Cntd…
Maximum delay possible with 16 bit count
1. The maximum count that can be loaded in the 16 bit register pair is FFFFH(65535)
2. So the maximum delay possible with 8 bit count, assume operating frequency 2MHZ
=10(10+(65535-1)X(24+(21))X0.5Nsec
=0.786425 second
If the application requires the delay more than this then the nested loop technique is used to
implement the delays.
54
Timer delay using Nested Loop
Code conversion allows users to translate a number that is represented using one code to coding
system.
1. Binary to BCD
2. BCD to binary
3. BCD to Hex
4. Hex to BCD
6. Binary to ASCII
7. ASCII to binary
Reading Assignment
56
End of Chapter Four
???
57