Chapter 2 Programming and Instruction Set PDF
Chapter 2 Programming and Instruction Set PDF
Introduction
Instruction formats
Addressing modes
Instruction set
Assembler directives
2
INTRODUCTION
For e.g.
0100 1111
4
INTRODUCTION CONT..
5
INTRODUCTION CONT..
For e.g.
MOV to indicate data transfer
6
INTRODUCTION CONT..
Assembly language program to add two numbers
MVI A, 2H ;Copy value 2H in register A
MVI B, 4H ;Copy value 4H in register B
ADD B ;A = A + B
Note:
Assembly language is specific to a given processor
For e.g. assembly language of 8085 is different
than that of Motorola 6800 microprocessor
7
INTRODUCTION CONT..
Microprocessor understands Machine Language
only!
Microprocessor cannot understand a program
written in Assembly language
A program known as Assembler is used to
convert a Assembly language program to
machine language
Assembly Machine
Assembler
Language Language
Program
Program Code
8
INSTRUCTION FORMATS
1-byte Instructions
2-byte Instructions
3-byte Instructions
9
INSTRUCTION FORMAT CONT..
One-byte Instructions
Includes Opcode and Operand in the same byte
• Examples
10
INSTRUCTION FORMATS CONT..
Two-byte Instructions
First byte specifies Operation Code
Examples
Three-byte Instructions
• First byte specifies Operation Code
• Second & Third byte specifies Operand
• Examples
Opcode Operand Binary Hex Code
Code
LXI H, 2050H 0010 0001 21H
0101 0000 50H
0010 0000 20H
LDA 3070H 0011 1010 3AH
0111 0000 70H 12
0011 0000 30H
ADDRESSING MODES
Format of a typical Assembly language
instruction is given below
[Label:] Mnemonic [Operands] [;comments]
HLT
MVI A, 20H
MOV M, A ;Copy A to memory location
whose address is stored in
register pair HL
LOAD: LDA 2050H ;Load A with contents of memory
location with address 2050H
READ: IN 07H ;Read data from Input port
with address 07H 13
ADDRESSING MODES
1. Register Addressing
Examples
MOV A, B
ADD C
15
ADDRESSING MODES
2. Immediate Addressing
Value of the operand is given in the instruction
itself
Example
MVI A, 20H
LXI H, 2050H
ADI 30H
SUI 10H
16
ADDRESSING MODES
3. Memory Addressing
Indirect addressing
17
ADDRESSING MODES
Examples
LDA 2050H ;load A with contents of memory
location with address 2050H
18
ADDRESSING MODES
3.2 Indirect Addressing
A memory pointer register is used to store
the address of the memory location
Example
MOV M, A ;copy register A to memory location whose
address is stored in register pair HL
H L
20H 50H 2050H 30H
A 30H
19
ADDRESSING MODES
Examples
IN 07H
OUT 21H
20
ADDRESSING MODES
For example:
CMP ; complement content of accumulator
21
INSTRUCTION SET
Simple program to compute 2’s complement of a
number
LDA 2345H ; A [2345] =12H
CMA ; A ED
INR A ; AEE
STA 2345H ; 2345HEE
HLT ; terminate program
22
INSTRUCTION SET CONT..
8085 is an 8-bit device
it can have up to 28 =256 instructions.
However, the 8085 only uses
23
INSTRUCTION SET CONT..
These instructions can be grouped into
Instruction group No of opcodes No of Instruction type
Data Transfer 83 13
Arithmetic Operation 62 14
Logical operation 43 15
Branch operation 36 8
Stack operation 15 9
I/O operation 2 2
Interrupt operation 5 5
Total 246 66
24
INSTRUCTION SET CONT..
Data Transfer Instructions
1. INSTRACTION TYPE MVI r,d8
Move immediate value to register
rd8
d8 any 8 bit value
r register (A,B,C,D,E,H or L) 7 opcode
Example:
MVI B,45H ; B =45H; 25
INSTRUCTION SET CONT..
Data Transfer Instructions con..
2. INSTRACTION TYPE MOV r1,r2
Move value from register r2 to r1
r1,r2 = Register(A,B,C,D,E,H or L)
7*7 49 opcode
One byte Instruction
Example:
MOV B,C ; copy value from register C to B
26
INSTRUCTION SET CONT..
Data Transfer Instructions con..
3. INSTRACTION TYPE MOV r, M
Move value from memory, M to register r
r= Register(A,B,C,D,E,H or L)
7 opcode
Memory address from HL register
27
INSTRUCTION SET CONT..
Data Transfer Instructions con..
4. INSTRACTION TYPE MOV M, r
Move value from register, r to memory, M
r= Register(A,B,C,D,E,H or L)
7 opcode
Memory address from HL register
28
INSTRUCTION SET CONT..
Data Transfer Instructions con..
5. INSTRACTION TYPE LXI rp, d16
Load extended register immediate value of 16
bit
rp= Register pair(BC, DE or HL)
3 opcode
d16 any 16 bit value
31
INSTRUCTION SET CONT..
Data Transfer Instructions con..
8. INSTRACTION TYPE STA a16
Store accumulator value to memory address
a16
Three byte Instruction
One byte for opcode
Two byte for operand
33
INSTRUCTION SET CONT..
Data Transfer Instructions con..
10. INSTRACTION TYPE LAX rp
Load accumulator from memory pointed by
extended register pair, rp
rp can be BC or DE
Indirect addressing
One byte Instruction
Two opcodes
Example:
LAX B; A values from memory pointed by BC
LAX C; A values from memory pointed by DE 34
INSTRUCTION SET CONT..
Data Transfer Instructions con..
11. INSTRACTION TYPE STAX rp
Store content of accumulator to memory pointed
by extended register rp
rp can be BC or DE
Indirect addressing
One byte Instruction
Two opcodes
Example:
STAX B; content of A copied to memory pointed by BC
STAX C; content of A copied to memory pointed by DE 35
INSTRUCTION SET CONT..
Data Transfer Instructions con..
12. INSTRACTION TYPE LHLD a16
Load HL register pair from direct memory
address a16
a16 address value
Absolute addressing
Three byte Instruction
One opcodes
Example:
LHLD 1234H; HL content of memory pointed by 1234H
36
INSTRUCTION SET CONT..
Data Transfer Instructions con..
13. INSTRACTION TYPE SHLD a16
Store HL pair directly to memory address
starting at a16
Absolute addressing
Three byte Instruction
One opcodes
Example:
SHLD 1234H; HL to memory pointed by 1234H
37
INSTRUCTION SET CONT..
These instructions can be grouped into
Instruction group No of opcodes No of Instruction type
Data Transfer 83 13
Arithmetic Operation 62 14
Logical operation 43 15
Branch operation 36 8
Stack operation 15 9
I/O operation 2 2
Interrupt operation 5 5
Total 246 66
38
INSTRUCTION SET CONT..
Arithmetic group of instructions
Addition and Subtraction
No multiplication or division
14 instruction type
62 instruction
One of the operand must be Accumulator
Others can be
Content of 8 bit register
Content of memory location pointed by HL pair
8 bit immediate data
39
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
1. INSTRACTION TYPE ADD R
Add content of R to Accumulator
R A,B,C,D,E,H,L or M
8 opcode
Result stored on Accumulator
One byte instruction
Affect flags:
Cy, P, AC, Z, S
Example
ADD E ; AA+E 40
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
2. INSTRACTION TYPE ADI d8
Add immediate value d8 to Accumulator
one opcode
Result stored on Accumulator
two byte instruction
Affect flags:
Cy,P,AC,Z,S
Example
ADI B1H; AA+B1H
41
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
3. INSTRACTION TYPE INR R
Increment R, register
R: A, B, C, D, E, H, L or M
Eight opcode
One byte instruction
Affect flags:
P, AC, Z, S
Example
INR A; AA+01h
42
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
4. INSTRACTION TYPE ADC R
Add content of R with carry to accumulator
R: A, B, C, D, E, H, L or M
Eight opcode
One byte instruction
Affect flags:
Cy, P, AC, Z, S
Example
ADC B; AA+B
43
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
5. INSTRACTION TYPE ACI d8
Add immediate with carry to accumulator
One opcode
Two byte instruction
Affect flags:
Cy, P, AC, Z, S
Example
ADC B; AA+B
44
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
6. INSTRACTION TYPE SUB R
Subtract content of R from Accumulator
R: A,B,C,D,E,H,L or M
Eight opcode
One byte instruction
Affect flags:
Cy, P, AC, Z, S
Example
SUB B; AA-B
45
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
7. INSTRACTION TYPE SUI d8
Subtract d8 from accumulator
One opcode
Two byte instruction
Affect flags:
Cy, P, AC, Z, S
Example
SUI 12H; AA-12H
46
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
8. INSTRACTION TYPE DCR R
Decrement register R
R: A,B,C,D,E,H,L or M
8 opcode
One byte instruction
Affect flags:
P, AC, Z, S not Cy
Example
DCR B; BB-01H
47
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
9. INSTRACTION TYPE SBB R
Subtract R with borrow from accumulator
R: A,B,C,D,E,H,L or M
8 opcode
One byte instruction
Affect flags:
P, AC, Z, S, Cy
Example
SBB B; AA-B
48
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
10. INSTRACTION TYPE SBI d8
Subtract immediate with borrow from
accumulator
One opcode
Two byte instruction
Affect flags:
P, AC, Z, S, Cy
Example
SBI F2h ; AA-d8
49
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
11. INSTRACTION TYPE INX rp
Increment extended register pair rp
rp : BC, DE, HL
Three opcode
One byte instruction
No flag affected
Example
INX B
INX B = INX C???
50
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
12. INSTRACTION TYPE DCX rp
Decrement extended register pair rp
rp : BC, DE, HL
Three opcode
One byte instruction
No flag affected
Example
DCX B
51
INSTRUCTION SET CONT..
Arithmetic group of instructions con..
13. INSTRACTION TYPE DAD rp
Double add
Add content of rp to HL
rp : BC, DE, HL
Three opcode
One byte instruction
No flag affected except Cy
Example
DAD B
52
INSTRUCTION SET CONT..
These instructions can be grouped into
Instruction group No of opcodes No of Instruction type
Data Transfer 83 13
Arithmetic Operation 62 14
Logical operation 43 15
Branch operation 36 8
Stack operation 15 9
I/O operation 2 2
Interrupt operation 5 5
Total 246 66
53
INSTRUCTION SET CONT..
Logical group of instructions
15 instruction type
45 instructions
Includes
AND
OR
Exclusive OR
Complement accumulator
Complement carry flag
Compare and rotate 54
INSTRUCTION SET CONT..
Logical group of instructions cont..
AND operation
Operand
Accumulator and
8 bit immediate data, register or memory pointed by
HL
Bitwise AND operation
Flag
S, P and Z based on result
Cy reset
Ac set
55
INSTRUCTION SET CONT..
Logical group of instructions cont..
1. INSTRACTION TYPE ANA R
AND accumulator with register R
R: A, B, C, D, E, H, L or MEMORY
OR operation
Operand
Accumulator and
Bitwise OR operation
Flag
S, P and Z based on result
58
Cy and Ac reset
INSTRUCTION SET CONT..
Logical group of instructions cont..
3. INSTRACTION TYPE ORA R
OR accumulator with Register R
R: A, B, C, D, E, H, L or MEMORY
Exclusive OR operation
Operand
Accumulator and
Flag
S, P and Z based on result
61
Cy and Ac reset
INSTRUCTION SET CONT..
Logical group of instructions cont..
5. INSTRACTION TYPE XRA R
Ex-OR accumulator with Register R
R: A, B, C, D, E, H, L or MEMORY
1 opcode
Result on Accumulator
Example :
XRI 45h 63
INSTRUCTION SET CONT..
Logical group of instructions cont..
7. INSTRACTION TYPE CMA
Complement Accumulator
One byte instruction
One opcode
Result on Accumulator
Example :
CMA
64
Affects no flag
INSTRUCTION SET CONT..
Logical group of instructions cont..
8. INSTRACTION TYPE CMC
One opcode
Example :
CMC 65
INSTRUCTION SET CONT..
Logical group of instructions cont..
9. INSTRACTION TYPE STC
Example :
70
RAL
INSTRUCTION SET CONT..
Logical group of instructions cont..
Affects Cy : LSB
Application:
To check LSB
To divide by 2
Example :
71
RRC
INSTRUCTION SET CONT..
Logical group of instructions cont..
Affects Cy : MSB
Application:
To check MSB
To multiply by 2
Example :
72
RAL
INSTRUCTION SET CONT..
These instructions can be grouped into
Instruction group No of opcodes No of Instruction type
Data Transfer 83 13
Arithmetic Operation 62 14
Logical operation 43 15
Branch operation 36 8
Stack operation 15 9
I/O operation 2 2
Interrupt operation 5 5
Total 246 66
73
INSTRUCTION SET CONT..
Stack group of instructions
Stack
Data structure implemented in a RAM
LIFO (last in first out)
Store data and address
SP (stack pointer)
16-bit special register
Pointes to top of stack
Four opcode
Example:
POP B ; load BC from top of stack
Four opcode
Example:
PUSH D ; store content of DE to top of stack
One opcode
Example:
SPHL ; SP HL
78
INSTRUCTION SET CONT..
Stack group of instructions cont..
5. INSTRACTION TYPE XTHL
One byte
One opcode
Example:
XTHL
79
INSTRUCTION SET CONT..
Stack group of instructions cont..
6. INSTRACTION TYPE INX SP
Increment SP by one
Example:
INX SP
80
INSTRUCTION SET CONT..
Stack group of instructions cont..
7. INSTRACTION TYPE DCX SP
Decrement SP by one
Example:
DCX SP
81
INSTRUCTION SET CONT..
Stack group of instructions cont..
8. INSTRACTION TYPE DAD SP
Add content of SP to HL
One opcode
Example:
DAD SP
No operation
Do nothing
Application :
To generate small delay
Data Transfer 83 13
Arithmetic Operation 62 14
Logical operation 43 15
Branch operation 36 8
Stack operation 15 9
I/O operation 2 2
Interrupt operation 5 5
Total 246 66
84
INSTRUCTION SET CONT..
Branch group of instructions
Eight Instruction Type
36 instruction
85
INSTRUCTION SET CONT..
Unconditional Branch Instruction
1. INSTRACTION TYPE JMP a16
Example:
JMP 12EFh; PC 12EFh
86
INSTRUCTION SET CONT..
Unconditional Branch Instruction
2. INSTRACTION TYPE PCHL
Example:
PCHL ; PC [HL]
87
INSTRUCTION SET CONT..
Conditional Branch instructions
3. INSTRACTION TYPE JNC a16
Example: JZ 1234h 89
INSTRUCTION SET CONT..
Conditional Branch instructions cont..
7. INSTRACTION TYPE JPO a16
Restart
n: 0-7
Example:
RST 0 ; CALL 0*8= CALL 0000h
101
INSTRUCTION SET CONT..
These instructions can be grouped into
Instruction group No of opcodes No of Instruction type
Data Transfer 83 13
Arithmetic Operation 62 14
Logical operation 43 15
Branch operation 36 8
Stack operation 15 9
I/O operation 2 2
Interrupt operation 5 5
Total 246 66
102
INSTRUCTION SET CONT..
I/O instructions
1. INSTRACTION TYPE IN a8
Two byte
One opcode
Example:
IN 0Fh ; A InPort[OFh]
103
INSTRUCTION SET CONT..
I/O instructions
2. INSTRACTION TYPE OUT a8
Two byte
One opcode
Example:
OUT FFh ; A Output Port[FFh]
104
CONTENTS
Introduction
Instruction formats
Addressing modes
Instruction set
Assembler directives
105
ASSEMBLER DIRECTIVES
Definition:
the microprocessor
106
ASSEMBLER DIRECTIVES CONT..
What they do:
107
ASSEMBLER DIRECTIVES CONT..
ORG:
Origin
To assign starting/beginning address for module of
segment
ORG 1000H
Following instruction must be stored in memory location
starting at 1000H
END:
End program
For program termination
108
ASSEMBLER DIRECTIVES CONT..
DB:
DATA 21H
Define Byte
20H
To allocate and initialize data byte
DATA DB 20H,21H
Memory named Data has 2 consecutive information 20H
and 21H on it
DATAWORD 40H
DW:
22H
Define Word
ACH
To define and initialize 16-bit word
20H
DATAWORD DW 20ACH, 2240H
Memory named DATAWORD has 4 consecutive
byte with information40H, 22H, ACH and 20H 109
ASSEMBLER DIRECTIVES CONT..
EQU:
Equate
To assign value to variables
X EQU A1H
X A1H
Exam MACRO
….
….
….
110
Exam ENDM
CONTENTS
Introduction
Instruction formats
Addressing modes
Instruction set
Assembler directives
111
SIMPLE PROGRAM EXAMPLES
Write an assembly language program to transfer data from
register B to C.
MOV C, B ;CB
112
SIMPLE PROGRAM EXAMPLES
Write an assembly language program to store data from B
MOV A, B ; A B
HLT
113
SIMPLE PROGRAM EXAMPLES
MOV B, A ;B A
MOV A , B ;AB
HLT 114
SIMPLE PROGRAM EXAMPLES
Write an assembly language program to add two 8-bit numbers
2052h.
LXI H 2050H ; HL2050
MOV A, M ; AM[2050]
INX H ; HL2051
ADD H ; A A+M[HL=2051]
INX H ; HL2052
MOV M , A ; M[2052]A
116
SIMPLE PROGRAM EXAMPLES
117
SIMPLE PROGRAM EXAMPLES
LXI D, 2601h ; DE first address 2601h
STAX D ; M[DE]A.
119
Main Program:
LXI SP, XXXX H ; Initialize stack pointer.
LXI D, 2201 H ; Get D-E pair with 2201 H.
LXI H, 2100 H ; Get H-L pair with 2100 H.
MOV B, M ; Store the count N in B-register.
START LXI H, 2100 H ; Get H-L pair with 2100 H.
MOV C, M ; Store the number N in C-register
INX H ; Increment the H-L pair.
MOV A, M ; AM[HL]
DCR C ; Decrement count.
LOOP INX H ; Increment the H-L pair.
CMP M ; Compare the second number with first number.
JNC NXT ; If the accumulator content is larger
; then jump to NXT.
MOV A, M ; Else move [M −LH ] to accumulator.
120
NXT DCR C ; Decrement count.
JNZ LOOP ; Jump to LOOP if not zero.
STAX D ; Store the largest number in memory location
; addressed by D-E register pair.
CALL SUBR ; Call the subroutine SUBR to put 00 H to the
; memory location where the largest number
; was found.
INX D ; Increment D-E register pair.
DCR B ; Decrement B.
JNZ START ; Jump to START if not zero.
121
Subroutine Program:
122