Microprocessor and Microcontroller Based Systems (For Cse & It)
Microprocessor and Microcontroller Based Systems (For Cse & It)
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. These instructions can be classified into the following five functional categories: data transfer
(copy) operations, arithmetic operations, logical operations, branching operations, and machine-
control operations.
This group of instructions copy data from a location called a source to another location called a destination,
without modifying the contents of the source. In technical manuals, the term data transfer is used for this
copying function. However, the term transfer is misleading; it creates the impression that the contents of the
source are destroyed when, in fact, the contents are retained without any modification. The various types of
data transfer (copy) are listed below together with examples of each type:
Types Examples
2. Specific data byte to a register or a 2. Load register B with the data byte 32H.
memory location.
Load accumulator
LDA 16-bit The c ntents f a mem ry location, specified by
address a16-bit address in the perand, are copied to the
accumulator. The contents of the source are not
altered.
Example: LDA 2034 or LDA XYZ
2. Arithmetic Operations
These instructions perform arithmetic operations such as addition, subtraction, increment, and
decrement.
Addition - Any 8-bit number, or the contents of a register or the contents of a memory location can be
added to the contents of the accumulator and the sum is stored in the accumulator. No two other 8-bit
registers can be added directly (e.g., the contents of register B cannot be added directly to the contents
of the register C). The instruction DAD is an exception; it adds 16-bit data directly in register pairs.
Subtraction - Any 8-bit number, or the contents of a register, or the contents of a memory location can
be subtracted from the contents of the accumulator and the results stored in the accumulator. The
subtraction is performed in 2's compliment, and the results if negative, are expressed in 2's
complement. No two other registers can be subtracted directly.
decrement by 1. Similarly, the 16-bit contents of a register pair (such as BC) can be incremented or
decrement by 1. These increment and decrement operations differ from addition and subtraction in an
important way; i.e., they can be performed in any one of the registers or in a memory location.
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
3. Logical Operations
These instructions perform various logical operations with the contents of the accumulator.
AND, OR Exclusive-OR - Any 8-bit number, or the contents of a register, or of a memory location can be
logically ANDed, Ored, or Exclusive-ORed with the contents of the accumulator. The results are stored in
the accumulator.
Rotate- Each bit in the accumulator can be shifted either left or right to the next position.
Compare- Any 8-bit number, or the contents of a register, or a memory location can be compared for
equality, greater than, or less than, with the contents of the accumulator.
Complement - The contents of the accumulator can be complemented. All 0s are replaced by 1s and all 1s
are replaced by 0s.
Complement accumulator
The contents of the accumulator are complemented. No
CMA none flags
are affected.Example: CMA
Complement carry
CMC none The Carry flag is complemented. No other flags are affected.
Example: CMC
Set Carry
STC none The Carry flag is set to 1. No other flags are affected.
Example: STC
Branching Operations
This group of instructions alters the sequence of program execution either conditionally or
unconditionally.
Jump - Conditional jumps are an important aspect of the decision-making process in the
programming. These instructions test for a certain conditions (e.g., Zero or Carry flag) and alter the
program sequence when the condition is met. In addition, the instruction set includes an instruction
called unconditional jump.
Call, Return, and Restart - These instructions change the sequence of a program either by calling
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
a subroutine or returning from a subroutine. The conditional Call and Return instructions also can
test condition flags.
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
10
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
11
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
12
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
SAMPLE PROGRAMS
1. In data transfer, the contents of the source are not destroyed; only the contents of the destination are
changed. The data copy instructions do not affect the flags.
2. Arithmetic and Logical operations are performed with the contents of the accumulator, and the results are
stored in the accumulator (with some expectations). The flags are affected according to the results.
3. Any register including the memory can be used for increment and decrement.
4. A program sequence can be changed either conditionally or by testing for a given data condition.
8. Instruction Format
An instruction is a command to the microprocessor to perform a given task on a specified data. Each
13
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
instruction has two parts: one is task to be performed, called the operation code (opcode), 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 16-bit ) data, an internal register, a memory location, or 8-bit (or 16-bit) address. In some
instructions, the operand is implicit.
The 8085 instruction set is classified into the following three groups according to word size:
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 opcode and operand in the same byte. Operand(s) are internal register and
are coded into the instruction.
For example:
Copy the contents of the accumulator in MOV C,A 0100 1111 4FH
the register C.
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.
Coded as 01 ddd sss where ddd is a code for one of the 7 general registers which is the destination of the
data, sss is the code of the source register.
14
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
ADD r
A <-- A + r
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:
DATA
Assume that the data byte is 32H. The assembly language instruction is written as
ADI data
A <-- A + data
OUT port
where port is an 8-bit device address. (Port) <-- A. Since the byte is not the data but points directly to where it is
located this is called direct addressing.
Three-Byte Instructions
In a three-byte instruction, the first byte specifies the opcode, and the following two bytes specify the 16-bit
address. Note that the second byte is the low-order address and the third byte is the high-order address.
opcode + data byte + data byte
For example:
15
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
2085H.
rp is one of the pairs of registers BC, DE, HL used as 16-bit registers. The two data bytes are 16-bit data in
L H order of significance.
rp <-- data16
Example:
LXI H,0520H coded as 21H 20H 50H in three bytes. This is also immediate addressing.
LDA addr
A <-- (addr) Addr is a 16-bit address in L H order. Example: LDA 2134H coded as 3AH 34H 21H. This is
also an example of direct addressing.
Looping:
The program is written in such a way that it executes certain set of instructions repeatedly to execute a certain
task for a number of times. Example: to perform multiplication of two numbers. This is nothing but repeated
addition.
Counting: The program has a track of how many times the instruction or a set of instructions are executed. Eg:
When division operation is performed a register is used to count the number of times the subtraction is done. The
content of that register will give the quotient.
Indexing: it allows the user to point or refer the data stored in a sequential memeory location one by one.
Sample Programs
Program
16
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
MVI A, 30H
RRC
RRC
RRC
OUT PORT1
HLT
PROGRAM:
OBSERVATION:
Input: 80 (4150)
80 (4251)
Output: 00 (4152)
01 (4153)
17
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
MVI C, 00 Initialize C to 00
LDA 4150 Load the value to Acc.
MOV B, A Move the content of Acc to B register.
LDA 4151 Load the value to Acc.
SUB B
JNC LOOP Jump on no carry.
CMA Complement Accumulator contents.
INR A Increment value in Accumulator.
INR C Increment value in register C
LOOP: STA 4152 Store the value of A-reg to memory address.
MOV A, C Move contents of register C to Accumulator.
STA 4153 Store the value of Accumulator memory
HLT Terminate the program.
OBSERVATION:
Input: 06 (4150)
02 (4251)
Output: 04 (4152)
01 (4153)
PROGRAM:
18
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
MOV A, D
STA 4153 Store the MSB of result in Memory
HLT Terminate the program.
OBSERVATION:
Input: FF (4150)
FF (4151)
Output: 01 (4152)
FE (4153)
PROGRAM:
LXI H, 4150
MOV B, M Get the dividend in B – reg.
MVI C, 00 Clear C – reg for qoutient
INX H
MOV A, M Get the divisor in A – reg.
NEXT: CMP B Compare A - reg with register
JC LOOP Jump on carry to LOOP
SUB B Subtract A – reg from B- reg.
INR C Increment content of register
JMP NEXT C.
Jump to NEXT
LOOP: STA 4152 Store the remainder in
MOV A, C Memory
STA 4153 Store the quotient in memory
HLT Terminate the program.
OBSERVATION:
19
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
Input: FF (4150)
FF (4251)
PROGRAM:
OBSERVATION:
Output: FE (4300)
20
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
PROGRAM:
21
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
OBSERVATION:
Output: 0A (4300)
ALGORITHM:
PROGRAM:
LXI H,4200
MOV C,M
DCR C
REPEAT: MOV D,C
LXI H,4201
LOOP: MOV A,M
INX H
CMP M
JC SKIP
MOV B,M
22
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
MOV M,A
DCX H
MOV M,B
INX H
SKIP: DCR D
JNZ LOOP
DCR C
JNZ REPEAT
HLT
OBSERVATION:
PROGRAM:
LXI H,4200
MOV C,M
DCR C
REPEAT: MOV D,C
LXI H,4201
LOOP: MOV A,M
INX H
23
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
CMP M
JNC SKIP
MOV B,M
MOV M,A
DCX H
MOV M,B
INX H
SKIP: DCR D
JNZ LOOP
DCR C
JNZ REPEAT
HLT
OBSERVATION:
PROGRAM:
LXI H,4150
MOV A,M Initialize memory
ADD A pointer
MSD X 2
MOV B,A Store MSD X 2
24
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
ADD A MSD X 4
ADD A MSD X 8
ADD B MSD X 10
INX H Point to LSD
ADD M Add to form HEX
INX H
MOV M,A Store the result
HLT
OBSERVATION:
Output: 4152 : 1D H
PROGRAM:
25
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
OBSERVATION:
Input: 4150 : FF
26
SEC1312 MICROPROCESSOR AND MICROCONTROLLERBASED SYSTEMS
UNIT 2 PREPARED BY: U. ANITHA & P.GRACE KANMANI PRINCE
PROGRAM:
OBSERVATION:
LDA 4500
SUI 30
CPI 0A
JC SKIP
SUI 07
SKIP: STA 4501 HLT
OBSERVATION:
Input: 4500 31
Output: 4501 0B
PROGRAM:
LOOKUP TABLE:
4125 01
4126 04
4127 09
4128 16
4129 25
4130 36
4131 49
4132 64
4133 81
OBSERVATION:
Input: 4150: 05
Input : 4150: 11
RESULT:
Thus the program to find the square of the number from 0 to 9 using a Look up table was executed