FMPM Module 2
FMPM Module 2
SYLLABUS:
Introduction to 8085 Assembly Language Programming: The 8085 Programming Model,
Instruction Classification, Instruction, Data and Storage, Writing assembling and Execution of
a simple program, Overview of 8085 Instruction Set, Writing and Assembling Program.
Introduction to 8085 Instructions: Data Transfer Operations, Arithmetic Operations, Logic
Operation, Branch Operation, Writing Assembly Languages Programs, Debugging a Program.
H (8) L (8)
Register Diagram
Registers
The 8085 has six general-purpose registers to store 8-bit data; these are identified as B(8
bit),C(8 bit),D(8 bit),E(8 bit),H(8 bit), and L(8 bit) as shown in the figure. They can be combined
as register pairs – BC, DE, and HL – to perform some 16-bit operations. The programmer can
use these registers to store or copy data into the registers by using data copy instructions.
Accumulator
The accumulator is an 8-bit register that is a part of arithmetic/logic unit (ALU). This
register is used to store 8-bit data and to perform arithmetic and logical operations. The result
of an operation is stored in the accumulator. The accumulator is also identified as register A.
Flag Register
Last three bits i.e. D2, D1, D0 are the binary code of source register A
i.e. 111.
Instruction 2 : SUB B
1 0 0 1 0 0 0 0
Operation code for SUB Code of Source Operand B
Hence, the op-code of above instruction in binary is 10010000 i.e. 90 H i.e. one byte
only.
Where first five bit i.e. D7 to D3 indicates operation to be performed is subtraction i.e.
10010.
Last three bits i.e. D2, D1, D0 are the binary code of destination register B i.e. 000.
b) 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 op-code.
Two byte instructions require two consecutive memory locations to store in memory.
Examples
Instruction 1 : MVI A, 40 H
First byte of the instruction 3E H
0 0 1 1 1 1 1 0
Operation code for MVI A
Source: https://youtu.be/mHG5z6KGpGg
DATA TRANSFER GROUP
1. MOV Rd, Rs
Rd = destination register
Rs = Source register
Description
This instruction copies data from source register Rs to destination register Rd without
modifying source register Rs.
The Rs and Rd can be any general purpose register like A,B,C,D,E,H,L.
Operation performed : Rd Rs
Flags affected : None
Length in byte : One
Addressing mode : Register
Machine cycle : 1M/C [op-code fetch]
T-state : 4T
Examples
MOV H,A HA
MOV A,L A L
MOV B,C BC
2. MOV Rd, M and MOV M, Rs
Rd = destination register
Rs = Source register
M = Memory pointer [HL]
Description
MOV Rd, M instruction copies data from memory location pointed by HL register pairs
to destination Rd without modifying source memory location.
MOV M, Rs instruction copies the data from the source register Rs to memory location
pointed by HL register pair without modifying source register Rs.
The Rs and Rd can be any general purpose registers like A,B,C,D,E,H,L.
BEFORE USING THIS INSTRUCTION the register pair HL must be loaded with valid
address of the data byte because M always refers HL register HL register pair for the
memory address.
Operations performed : Rd M or Rd [HL] for MOV Rd, M
: M Rs or [HL] Rs or [HL] Rs for MOV M, Rs
Flags affected : None
Length in byte : One
Addressing Mode : Indirect
Machine cycle : 2M/C [op-code fetch + memory read] for MOV Rd, M
: 2M/C [op-code fetch + memory wire] for MOV M, Rs
T – state : 4 + 3 = 7 t for both instructions
Examples
Assume HL contains D000H
MOV B,M : copies the contents of the memory location D000H pointed
by HL to Register pair to destination register B.
MOV M,C : copies the contents of the source register C to memory
location D000H Pointer BY HL register pair seven
instructions for MOV Rd, M.
3. MVI Rd, 8 bit data and MVI M, 8 bit data
Description
MVI Rd, 8 bit data instruction copies the 8 bit immediate data specified in the
instructions to destination register Rd.
MVI M, 8 bit data instruction copies the 8 bit immediate data to memory location
pointed by HL register, hence this instruction HL must be located with valid address
of the memory locations.
The Rd can be any general purpose register like A,B,C,D,E,H,L.
Operation performed : Rd 8 bit immediate data for MVI Rd, 8 bit data
: M 8 bit immediate data or [HL] 8 bit data for MOV M,
8 bit data
Flags affected : None
Length in byte : Two
Addressing Mode : Immediate for MVI Rd, 8 bit data
: Immediate indirect for MVI M, 8 bit data
Machine cycle : 2M/C [op-code fetch + memory read] for MVI Rd,
8 bit data
: 3 M/C [op-code fetch + memory read + memory write] for
MVI M, 8 bit
T–State : 4+3 =7 for MVI Rs, 8 bit data
: 4+3 +3=10 T for MVI M, 8 bit data
4. LXI Rp , 16 bit immediate data Rp= register pair
Description
This instruction copies the 16 bits immediate data specified in the instruction to
destination register Rp.
The Rp can be a pair of general purpose register like BC,DE,HL and SP.
Operation performed : Rp 16 bit immediate data
Flags affected : None
Length in byte : Three
Addressing mode : Immediate
Machine cycle : 3 M/C [op-code fetch + memory read + memory read]
T–state : 4 + 3 +3 =10 T
Examples
LXI H, 1234H : copy 16 bit immediate data 1234H to register pair HL where
12 copied to H and 34 to L after execution.
LXI H, 3000H : copy 16 bit immediate data 3000 to stack pointer SP.
5. LDA 16 bit address
Description
This instruction copies a byte from memory location specified in the instructions to
accumulator i.e. A register.
The 16 bit address of the memory location is specified along with the instructions.
Operations performed : A[16 bit address ]
Flags affected : None
Length in byte : Three
Addressing mode : Direct
Machine cycle : 4M/C [op-code fetch + memory read + memory read +
memory write]
T-state : 4+3+3+3=13T
Examples
LDA D100H : copies the contents of memory location D100H to accumulator i.e. A register.
6. STA 16 bit address
Description
This instruction copies a byte from accumulator i.e. A register to memory location
specified in the instructions.
The 16 bit address of the memory location is specified along with the instructions.
Operations performed : [16 bit address ] A
Flags affected : None
Length in byte : Three
Addressing mode : Direct
Machine cycle : 4M/C [op-code fetch + memory read + memory read +
memory write]
T–state : 4+3+3+3=13T
Examples
STA D100H : copies the contents of accumulator i.e. A register to memory location D100H
7. LHLD 16 bit address
Description
This instruction copies contents of two consecutive memory location to H and L
registers without modifying the contents of memory location.
The 16 bit address of the first memory location is specified along with the instructions.
The content of the first memory location is copied to L register and contents of next
memory location [address + 1] copied to H register.
Operation performed : L 16 bit address
: H 16 bit address + 1
Flags affected : None
Length in byte : Three
Addressing mode : Direct
Machine cycle : 5M/C [op-code fetch + memory read + memory read +
memory read + memory read]
T-state : 4+3+3+3+3 = 16T
Examples
LHLD D100 H : copies the contents of memory location D100H to L register and D101 H to H
register.
8. SHLD 16 bit address
Description
This instruction copies contents of H and L registers to two consecutive memory
location without modifying the contents of H and L register.
The 16 bit address of the first memory location is specified along with the instructions
The of L register is copied to first memory location specified and content of H register
is copied to next memory location [address + 1 ].
Operation performed : 16 bit address L
: [16 bit address + 1] H
Flags affected : None
Length in byte : Three
Addressing mode : Direct
Machine cycle : 5M/C [op-code fetch+ memory read + memory read
+ memory write + memory write]
T-state : 4+3+3+3+3 = 16T
Examples
SHLD D100 H : copies the contents of reg. L to memory location D100H and reg. H to memory
location D101 H.
9. LDAX Rp
Description
The instruction copies the content of the memory location specified in the register pair
Rp to accumulator i.e. A register without modifying the content of memory location.
The 16 bit address of the location must be specified in register pair before the
execution of the instruction.
Register pairs are BC and DE only.
Operation performed : A Rp
Flags affected : None
Length in byte : One
Addressing mode : Indirect
Machine cycle : 2 M/C [op-code fetch + memory read]
T–state : 4 + 3 = 7T
Examples
Assume BC contains memory address C000H.
LDAX B : Copies the contents of memory location whose address is specified in BC register
pair to A accumulator.
10. STAX Rp
Rp = register pair
Description
The instructions copies the content of accumulator i.e. A register to the memory
location specified register pair Rp without modifying the content of accumulator.
The 16 bit address of the memory location must be specified in register pair before
the execution of this instruction.
Register pairs are BC and DE only.
Operations performed : Rp A
Flags affected : None
Length in byte : One
Addressing mode : Indirect
Machine cycle : 2M/C [op-code fetch + memory write]
T-state : 4+3=7T
Examples
Assume BC contents memory address C000H.
STAX B : Copies the contents of accumulator A to the memory location whose address is
specified in BC register pair.
11. XCHG
Description
This instructions exchanges the content of HL register pair with DE register pair.
Hence this instruction exchanges the content of H registers with D register and the
contents of L register with E register.
This instruction uses w and z register internally to perform exchange between HL and
DE registers pairs.
Operations performed : HD and LE
Flags affected : None
Length in byte : One
Addressing mode : Implicit or Implied
Machine cycle : 1 M/C [op-code]
T-state : 6T
Examples
Suppose
HL = 1234H and DE = 4321H then
After the execution of XCHG
HL = 4321 H and DE = 1234 H
12. IN 8 bit port address
Description
This instruction copies the 8 bit data from I/O port whose 8 bit address is specified
along with the instruction to accumulator.
The 8 bit port address is specified along with the instruction.
In this instruction the address of port is 8 but microprocessor transfer
16 bit address on the address list.
The 8 bit port address is transferred on lower order address bus A0 – A7 as well as
on higher order addition bus A8 – A15 and port is accessed.
Operation performed : A 8 bit port address
Flags affected : None
Length in byte : Direct
Addressing mode : Direct
Machine cycle : 3M/C [op-code fetch + memory read + I/O read]
T-state : 4 + 3 + 3 = 10 T
Examples
IN 80H : copies 8 bit data from I/O port whose 80H to accumulator.
Start
Stop
Program
MVI A, 30H ; Load A with 30H
MVI B, 44H ; Load B with 44H
MVI H, 93H ; Load H with 93.H
HLT or RST 1 ; Stop or restart
After the execution of the above
program, the registers A, B and H will get
loaded with 30H, 44H and 93H. At the Start
end of the above program you can use
either HLT or RST 1 instruction. If you
have used HLT instruction, then after the Load A register with data stored in D000H
execution of the program, the
microprocessor will be halted. In this
case we cannot check the result of the Copy data byte from A reg. to B reg.
program in the registers. Hence, your
data and result loaded in the register will
be lost due to the execution of monitor Load A register with data stored in E000H
program.
Hence, instead of using HLT
Copy data byte from A reg. to H reg.
instruction, it is the best way to use RST
n instruction. After the execution of the
RST instruction, the contents of the all
register will be restored depending on the Stop
kit manufacturer so that you can check
the contents of the desired register.
Program 2 : Load B, H with data byte from the memory locations D000H and E000H.
Here, the data bytes are stored in the memory locations D000H and E000H respectively.
Now you can use direct addressing mode instruction like LDA or MOV Rs, M, but in this case,
you have to use LDA not MOV Rd, M because data bytes are not stored at consecutive
memory locations.
Stop
Program :
MVI B, 65H ; Load B reg.
with 65H
MVI C, 87H ; Load C reg. with 87H
MOV A, B ; Load A reg. with B reg.
ADD C ; A reg. <- A reg. + B reg.
MOV B, A ; Store result in B reg.
HLT ; Stop
After the execution, the result of addition ECH will be stored in B register.
Program 2 : Add the contents of two memory locations D000H and D001H. Store result
in memory location D002H.
Here, load the numbers from both memory locations in internal registers and perform
addition operation. Finally, store result back to new memory location.
Assume memory locations contents data given below :
Start
Stop
D000H = 64H
D001H = 85H
Algorithm
Step 1 : Load first number from memory location D000H
Step 2 : Load second number from memory location D001H
Step 3 : Add first number with second number
Step 4 : Store result to memory location D002H
Step 5 : Stop
Program
LDA D000H ; Load first number from D000H in A reg.
MOV B, A ; Copy it to B reg.
LDA D001H ; Load second number from D001H in A reg.
ADD B ; A reg. <- A reg. + B reg.
STA D002H ; Store result at memory location D002H
HLT ; Stop
After the execution, you can check the result of addition in memory location D002H.
2.2 Subtraction Instructions
1. SUB Rs/SBB Rs
Rs = Source register
Description
The SUB Rs instruction subtracts the contents of source register Rs from accumulator
and store result of subtraction in accumulator, hence accumulator gest modified.
The SBB Rs instruction subtracts the content of source register Rs and Cy flag from
accumulator and store result in the accumulator, hence accumulator gets modified.
The source register Rs can be any general purpose registers like A,B,C,D,E,H,L.
The Cy flags will acts as barrow flag for the subtraction.
Operation performed : AA–Rs for SUB Rs
: AA–Rs-Cy for SBB Rs
Flags affected : All flags
Length in byte : One
Addressing mode : Register
Machine cycle : 1 M/C [op-code fetch]
T-State : 4T
Examples
Assume A = 40H and C = 20H
SUB C : subtracts the contents of C register from accumulator and store result
20H in accumulator A.
Assume A = 20H and C = 40H
SUB C ; subtracts the contents of C register from accumulator and store result
E0H set 1.
Assume A = 12H,H = 04H and Cy = 1
SBB H ; Subtract the contents of H register and Cy flag from accumulator and
store result 0DH in ; accumulator.
2. SUB M/SBB M
M = Memory pointer
Description
The SUB M instruction subtracts the contents of memory location pointer by HL
register pair from accumulated and store result of addition in accumulator,hence
accumulator gets modified.
The SBB M instruction subtracts the content of memory location pointed by HL
register pair and Cy flag from accumulator and store result of addition in
accumulator,hence accumulator gets modified.
The 16 bit address of the memory location must be loaded in register pair HL before
the execution of this instruction.
Operation performed : AA-[HL] or AA-M for SUB M
: AA-[HL]-Cy or AA-M-Cy for SBB M
Flags affected : All flags
Length in byte : One
Addressing mode : Indirect
Machine cycle : 2 M/C [op-code fetch + memory read]
T-State : 4+3=7T
Examples
Assume A = 40H, HL = D000H and D000H contains 35H
SUB M ; Subtracts the contents of memory location pointed by HL register pair
and Cy flag fromaccumulator and store result EFH in accumulator which
is 2ndcomplement of 11H i.e. 11H[-17 in BCD].
3. SUI 8 bit immediate data/SBI 8 bit immediate data
Description
The SUI 8 bit instruction subtracts the 8 bit immediate data specified in the instruction
from the contents of accumulator and store result of subtraction in accumulator,hence
accumulator gets modified.
The SBI 8 bit data instruction subtracts the 8 bit immediate data specified in instruction
and Cy flag from the contents of accumulator and store in the accumulator, hence
accumulator gets modified.
Operation performed : A A–8 bit immediate data for SUI 8 bit data
: A A-8 bit immediate data – Cy for ACI 8 bit data
Flags affected : All flags
Length in byte : Two
Addressing mode : Immediate
Machine cycle : 2 M/C [op-code fetch + memory read]
T-State : 4+3=7T
Examples
Assume A = 10H
SUI 05H ; Subtracts 8 bit data 05H from the content of accumulator and store
result 0BH in accumulator A
Assume A = 90H and Cy = 1
SBI 80H ; Subtracts the 8 bit data 80H and Cy flag from the content of
accumulator and store result 0FH in accumulator.
Program 1 : Load B and C with data bytes 65H and 87H.Subtract B from C, and store
result in B.
In this case, two operands are available in B and C register. Hence, you have to move
the contents of C in accumulator and then subtract the contents of B register from it.
Algorithm
Step 1 : Load B reg. with 65H
Step 2 : Load C reg. with 87H
Step 3 : Load accumulator with C reg.
Step 4 : Subtract B reg. with A reg.
Step 5 : Store result in B register.
Step 6 : Stop
Start
Stop
Program
MVI B, 65H ; Load B reg. with 65H
MVI C, 87H ; Load C reg. with 87H
MOV A, C ; Load A reg. with C. reg.
SUB B ; A reg. A reg. - B reg.
MOV B, A ; Store result in B reg.
HLT ; Stop
After the execution, the result of addition 22H will be stored in B register.
Program 2 : Subtract the content of memory locations D001H from D00H. Store result
in memory location D002H.
Here, load the numbers from both the memory locations in internal registers and perform
subtraction operation. Finally, store result back to new memory location.
Assume memory locations contents data given below.
D000H = 85H and D001H = 20H
Algorithm
Step 1 : Load first number from memory location D000H
Step 2 : Load second number from memory location D001H
Step 3 : Subtract second number from first numbers.
Step 4 : Store result to memory location D002H.
Step 5 : Stop
Program
LDA D001H ; Load second number from D000H in A reg.
MOV B, A ; Copy it to B reg.
LDA D000H ; Load first number from D001H in A reg.
ADD B ; A reg. ←A reg. - B reg.
STA D002H ; Store result at memory location D002H
HLT ; Stop
Start
Stop
Before the execution of the above program, store both numbers in memory locations as
given above.
After the execution, you can check the result of subtraction in memory location D002H.
OTHER ARITHMETAIC INSTRUCTION
1. INR R/INR M
R = Register
M–Memory pointer [HL]
Description
The INR instruction increments the contents of specified register R by 1 means it adds
1 to register R, hence R gets modified.
The INR M instruction increments the contents of memory location pointed by HL
register pair by 1 means it adds the contents of memory location, hence memory
location gets modified.
The register R can be any general purpose registers like A, B, C, D, E, H, L.
Before the execution of the INR M instruction, the HL register pair must be loaded
with valid addition of the memory location where data is stored.
Operation performed : RR+1 forINR R
: MM+1 or [HL][HL]+1 for INR M
Flags affected : All flags except Cy flag
Length in byte : One
Addressing mode : Register for INR R
: Indirect for INR M
Machine cycle : 1 M/C [op-code fetch] for INR R
: 3 M/C [op-code fetch + memory read + memory write] for
INR M
T–State : 4T for INR R
: 4 + 3 + 3 = 10 T for INR M
Examples
Assume C = 10H
INR C ; Increment the contents of C register by 1;after the execution the
contents of C will be 11H
Assume HL = D200H and at memory location D200H contains 06H
INR M ; Increment the content of the memory location pointed by HL register
pair by 1 and memory location is updated with the result 07H after the
execution.
2. DCR R/DCR M
R = Register
M = Memory pointer[HL]
Description
The DCR R instruction decrements the content of specified register R by 1 means it
subtracts 1 from register R, hence R gets modified.
The INR M instruction decrements the content of memory location pointed by HL
register pair by 1 means it subtracts 1 to the contents of memory location, hence
memory location gets modified.
The register R can be any general purpose register like A, B, C, D, E, H, L.
Before the execution of the DCR R instruction, the HL register pair must be loaded
with valid address of the memory location where data is stored.
Examples
Assume C = 10H
DCR C ; Decrement the contents of C register by 1,hence after the execution
the contents of C will be 0FH
Assume HL = D200H and at memory location D200H contains 06H
DCR M ; Decrement the contents of the location pointed by HL register pair by
1 and memory ; location is updated with the result 05H after the
execution.
10. INX Rp
Rp = Register pair
Description
This instruction increments the contents of specified register Rp by 1 means it adds 1
to register pair Rp, hence Rp gets modified.
The register pair Rp can be any register pairs like BC, DE, HL, Sp.
Operation performed : RpRp+1
Flags affected : None
Length in byte : One
Addressing mode : Register
Machine cycle : 1 M/C [op-code fetch]
T-State : 6T
Examples
Assume BC = 0010H
INX B ; Increment the contents of BC pair by 1,hence after the execution the
contents of BC will be 0011H
11. DCX Rp
Description
This instruction decrements the content of specified register pair Rp by 1 means it
subtracts 1 from Register pair Rp, hence Rp gets modified.
The register pair Rp can be any register pairs like BC, DE, HL, Sp.
Operation performed : RpRp-1
Flags affected : None
Length in byte : One
Addressing mode : Register
Machine cycle : 1 M/C [op-code fetch]
T-State : 6T
Examples
Assume BC = 0010H
DCX B ; Decrement the content of the BC register pair by 1, hence after the
execution the contents of BC will be 00FH
12. DAA (Decimal adjust Accumulator)
Description
This instruction is used to convert the result of two addition of two packed BCD
numbers to a valid BCD number.
DAA only works on accumulator A register.
So, DAA instruction must be used immediately after the ADD/ADC/ADI/ACI
Instructions.
This is only the instruction that uses auxiliary carry AC flags to perform operation of
conversion of binary To BCD.
The ADD/ADC/ADI/ACI instruction adds the two BCD number in hexadecimal format
and DAA instruction adds 6 to the higher nibble of A accumulator.
Examples
If A = 99 BCD and B = 99 BCD
Then Add B 1001 1001 A = 99 BCD
+ 1001 1001 B = 99 BCD
0011 0010 A = 32H and Cy = 1, AC = 1
As per the working of microprocessor the above result is correct ,however for us its is not.
This is because the microprocessor has treated both the number during addition as
hexadecimal number.
Hence in above example after addition Carry and Auxiliary carry flags are set, therefore
DAA instruction adds 6 to higher as well as lower nibble of reg.A to get correct BCD result i.e
198 BCD of which 98 is in reg.A and Cy = 1 as given below :
0011 0010 A = 32H and Cy = 1, AC = 1
+ 0110 0110
1001 1000 = 98 in BCD form
Examples
Assume A = 89H and C = 98 H
XRA C : Ex-OR’s the contents of C register with accumulator and store result 59H in
accumulator A. Cy = 0 and Ac = 0
Assume A = 30H, HL = D200H and at memory location D200H = 40 H
XRA M : Ex-OR’s the contents of memory location pointed by HL register pair with accumulator
and store result 54H in accumulator. Cy = 0 and Ac = 0
6. XRI 8bit immediate data
Description
This instruction logically Ex-OR’s the 8 bit immediate data specified in the instruction
with accumulator bit by bit i.e. D0 of A register and so on till D7 bit of both operands.
The result Ex-ORing is stored in accumulator, hence accumulator gets modified.
Operation Performed : A A Ex-OR 8 bit immediate data
Flags Affected : S, Z, P are affected and Cy = 0 and AC = 0
Length on Byte : Two
Addressing Mode : Immediate
Machine cycle : 2 M/C [op-code fetch + memory read]
T–State : 4+3=7T
Examples
Assume A = 89H
XRI 98H : Ex-OR’s the 8 bit immediate data with accumulator and store the result 99H in the
accumulator A. Cy = 0 and AC = 1
7. CMA [Complement Accumulator]
Description
This instruction complements the contents of accumulator A.
CMA only works on accumulator A register.
The complement is performing a NOT operation bit by bit where all 0’s are replace
with 1’s and all 1’s with 0’s.
Operation Performed : AA
Flags Affected : None
Length on Byte : One
Addressing Mode : Implied or Implicit
Machine cycle : 1 M/C [op-code fetch]
T-State : 4T
Examples
Assume A = 55H
CMA 0101 0101 = 55H before the execution of CMA
1010 1010 = AA after the execution of CMA
Program 1 : Find one's complement of 8 bit number stored in B register and store result
Back to B register.
To find complement of the content of B register, the number must be transferred to A
register because CMA instruction works only with accumulator.
Algorithm
Step 1 : Load B register with number Start
Step 2 : Move the content to B reg. to A reg.
Step 3 : Complement A reg. Load number in B register
Step 4 : Store result back to B reg.
Step 5 : Stop Copy it to A register
Program
MVI B FFH ; Load number FFH in B reg. Complement number
MOV A, B ; Copy it to A reg.
CMA ; Complement number FFH. Store result in B register
MOV B, A ; Copy Result to B reg.
HLT ; Stop
Stop
After the execution, the result of complement
i.e. 00H will be stored in B register.
Program 2 : Find One's complement of 8 bit number stored in memory location D000H
And store result in memory location D001H.
The number must be loaded in accumulator from the memory location D000H. Then find
complement.
Algorithm
Step 1 : Load number in A reg. from memory location
Step 2 : Find complement of number
Step 3 : Store result in memory location.
Step 4 : Stop
Program
LDA D000H ; Load number to A reg. from memory location
CMA ; Complement the number
STA D001H ; Store result to memory location.
HLT ; Stop
Start
Complement number
Stop
Before the execution, the number must be stored in memory location D000H.
After the execution, the complement of the number will stored in memory location D001H.
Program 3 : Find two's complement of 8 bit number stored in B register and store resul
t back to B register.
To find complement of the content of B register, the
Start number must be transferred to A register because CMA
instruction works only with accumulator.
Load number in B register Now CMA gives you One's complement of number and
then by adding 01 to the result one's complement, you will
get two'2 complement of the number.
Copy it to A register Algorithm
Step 1 : Load B register with number
Complement the number Step 2 : Move the contents of B reg. to A reg.
Step 3 : 2's Complement A reg.
Add 1 to get 2nd complement Step 4 : Store result back to B reg.
Step 5 : Stop
Store results in B register
Program
Stop
MVI B, FFH : Load number FFH in B reg.
MOV A,B : copy it to A reg.
CMA : Complement number FFH
ADI 01H : 2's complement of number
MOV B, A : Store result to B reg.
HLT : Stop
After the execution, the result of two's complement i.e. 01H will be stored in
B register.
Program 4 : Find two's complement of 8 bit number stored in memory location D000H
and store result in memory location D001H.
The number must be loaded in accumulator from the memory location D000H. Then find
two's complement.
Algorithm
Step 1 : Load number in A reg. from memory location
Step 2 : Find two's complement of number
Step 3 : Store result in memory location.
Step 4 : Stop
Program
LDA D000H ; Load number to A reg. from memory location
CMA ; Complement the number
ADI 01H ; Two's complement of number
STA D001H ; Store result to memory location
HLT ; Stop
Start
Stop
Before the execution, the number must be stored in memory location D000H.
After the execution, the two's complement of number will stored in memory location
D001H.
OTHER LOGICAL INSTRUCTION
1. CMC [Complement Carry]
Description
This instruction complements the carry flag Cy.
The complement is performing a NOT operation on Cy flag i.e. if Cy is the set then
instruction will reset it and vice versa.
Operation Performed : Cy Cy Complement
Flags Affected : Only Carry Cy
Length on Byte : One
Addressing Mode : Implied or Implicit
Machine cycle : 1 M/C [op-code fetch]
T-State : 4T
Examples
Assume Cy = 1
After execution of CMC instruction Cy will be reset i.e. 0
2. STC [Set Carry]
Description
This instruction sets carry flag to 1 if Cy is reset else it does not effect on Cy flag.
Operation Performed : Set 1 Cy
Flags Affected : Only Carry Cy
Length on Byte : One
Addressing Mode : Implied or Implicit
Machine cycle : 1 M/C [op-code fetch]
T-State : 4T
Examples
If Cy = 0, after the execution of CMC instruction Cy will be reset i.e. 0.
If Cy = 1, then execution of the instruction do not affect Cy flag.
3. CMP Rs / CMP M
Rs = Source Register
M = Memory pointer
Description
The CMP Rs instruction compares the content of accumulator with the content source
register Rs.
The CMP M instruction compares the content of accumulator with the content of
memory location pointed by HL register pair.
The result of comparison is reflected in flag register by setting CY or Z flags.
The comparison is actually done by non- destructive subtraction of the content of
source register or memory location from the accumulator i.e. the source and the
destination will not change, but the flags set to indicate the results of the comparison.
Operation Performed : flags A compare Rs/M
Flags Affected : Only CY , P and Z and Cy flags are used to indicate the
result of comparison
Length on Byte : One
Addressing Mode : Register for CMP Rs
: Indirect for CMP M
Machine cycle : 1 M/C [op-code fetch] for CMP Rs
: 2 M/C [op-code fetch + memory read] for CMP M
T-State : 4T for CMP Rs
: 4+3=7T for CMP M
Examples
Case 1 : Assume A = 20H and B = 10H
CMP B Compares content of register B with accumulator
A = 0010 0000
– B = 0001 0000
0001 0000 Cy = 0 and Z = 0 indicate A > B
Case 2 : Assume A = 10H and B = 20H
CMP B Compares content of register B with accumulator
A = 0001 0000
– B = 0010 0000
1111 0000 Cy = 1 and Z = 0 indicate A < B
Case 3 : Assume A = 20H and B = 10H
CMP B Compares content of register B with accumulator
A = 0010 0000
– B = 0010 0000
0000 0000 Cy = 0 and Z = 1 indicate A = B
Cy
D7 D6 D5 D4 D3 D2 D1 D0
Cy
D7 D6 D5 D4 D3 D2 D1 D0
D7 D6 D5 D4 D3 D2 D1 D0
8. RAR
Description
This instruction rotate the content of accumulator toward right by one bit position i.e
D7 is shifted D6 to D5 so on till D0 to Cy to D7 bit position of accumulator.
This instruction operates only on accumulator i.e. A register.
Operation performed
Cy
D7 D6 D5 D4 D3 D2 D1 D0
Start
Copy it to A.reg
Stop
After the execution, the result of byte exchange i.e. FAH will be available in
B register.
Examples
JMP D100H : transfer program control to D100H memory location directly.
CALL E000H : transfer program control to subroutine stored at memory
unconditionally.
RET : transfer program control back to the calling program
1. JMP 16 bit address
Description
This is an unconditional jump instruction which transfer program control to the target
memory location whose 16 bit address is specified in the instruction.
Now the instructions from the target memory location and onward will be executed in
the sequence by the microprocessor.
Operation performed : PC 16 bit address
Flags affected : None
Length in byte : Three
Addressing mode : Immediate
Machine cycle : 3 M/C [op-code fetch + memory read + memory read]
T-State : 4 + 3 + 3 = 10T
Examples
JMP D000H : This instruction transfer program control to D000H and start execution from that
location and onwards.
2. Conditional jump Instructions
Description
Conditional jump instructions checks the condition, if condition is satisfied then and
only then program control is transferred to target memory location whose address is
specified in the instruction.
If the condition is not satisfied, then execute next instruction after Jump instruction.
Different conditional jump instructions are given below :
JNC 16 bit address Jump if Cy = 0 [Reset]
JC 16 bit address Jump if Cy = 1 [Set]
JNC 16 bit address Jump if Z = 0 [Reset]
JZ 16 bit address Jump if Z = 1 [Set]
JP 16 bit address Jump if S = 0 [Reset]
JM 16 bit address Jump if S = 1 [Set]
JPE 16 bit address Jump if P = 1 [Set]
JPO 16 bit address Jump if P = 0 [Reset]
Operation performed : PC 16 bit address if condition Is satisfied else
PC PC+3
Flags affected : None but flags are checked
Length in byte : Three
Addressing Mode : Immediate
Machine Cycle : 3 M/C [op-code fetch + memory read + memory read] if
Condition is true
: 2 M/C if condition is false
T–State : 4+3+3=10T if condition is true
: 4+3=7T if condition is false
Examples
JNC D100H : Jump to the memory location if Cy = 0
JNC D2000H : Jump to memory location if Z =1
3. CALL 16 bit address
Description
The CALL instruction is used to transfer the program control to the sub-program
control or subroutine.
When the 8085executes the CALL instruction, the stack pointer is decremented by
one and copies the content of PC on the stack i.e. return address of the next
instruction after the CALL instruction.
Now the instructions from the target memory location specified in the CALL instruction
and onward will be executed in the sequence by the microprocessor.
Then, the 8085 loads the 16 bit address specified in the CALL instruction i.e. address
of the first instruction at the end of the subroutine, in to the PC and RET instruction at
the end of the subroutine will transfer program control back to the instruction after the
CALL instruction in the calling program by copying the return address from the stack,
back to PC.
Operation performed : SP SP – 1
: [SP] PCH,
: SP SP – 1
: [SP] PCL
: PC 16 bit address
OR
: [SP – 1] PCH
: [SP – 2] PCL
: PC 16 bit address
Flags affected : None
Length in byte : Three
Addressing Mode : Immediate register indirect
Machine Cycle : 5 M/C [op-code fetch + memory read + memory write]
: 2 M/C if condition if false
T-State : 6+3+3+3+3=18 T if condition is True.
: 6+3=9T if condition is false
Examples
CALL D100H : Transfer program control to subroutine which is stored from the address D100H
and start execution.
Operation Performed : PC HL
Flags affected : None
Length in bytes : One
Addressing mode : Register
Machine cycle : 1 M/C [op code fetch]
T – State : 6T
Example
LXI H, D200 H
PCHL ;Program control is transferred to memory location D200H and
microprocessor starts execution from this address.
Algorithm
Step 1 : Load MSB result register with 00H
Step 2 : Load first number in B register
Step 3 : Load second number in C register
Step 4 : Copy first number to A register for addition
Step 5 : Add first number with second number
Step 6 : Check result < 8 bit, if yes then jump to Step 8
Step 7 : Increment MSB result register by 1
Step 8 : Store result
Step 9 : Stop
Start
N Is
Cy = 1?
Stop
Program
MVI D, 00H ; Initialize MSB result register
MVI B, FFH ; Load first number in B reg.
MVI C, FFH ; Load second number in C reg.
MOV A, B ; Copy first number to A reg.
ADD C ; Add second number with first number
JNC DN ; Check result > 8 bit, if no go to label DN
INR D ; Increment MSB result register by 1
DN : MOV L, A ; Store LSB of result
MOV H, D ; Store LSB of result
HLT ; Stop
After the execution, the result will be 01FEH which is available in HL register pair.
2. Addition of two 8 bit numbers stored in memory locations D000H and D001H
respectively. Assume result is greater than 8 bit. Store the LSB of result in memory
location D002H and MSB in D003H.
In this case, the numbers must be loaded in general purpose registers and then perform
operation of addition on them. After the addition, store result to memory locations. Before the
execution of the program, store numbers in memory locations D000H and D001H.
Algorithm
Step 1 : Load first number in reg. from memory location
Step 2 : Load second number in another reg. from memory location
Step 3 : Initialize MSB result register with 00H
Step 4 : Add first number to second number
Start
N Is
Cy = 1?
Stop
3. Addition of two 2 digits BCD numbers stored in memory locations D000H and
D001H respectively. Assume result is greater than 2 digits. Store the LSB of result
in memory location D002H and MSB in D003H.
In this case, the numbers must be loaded in general purpose registers and then perform
operation of addition on them. After the addition, store result to memory location.
Assume memory location D001H contains 55 and D001 contains 55.
Algorithm
Step 1 : Load first BCD number in another register from memory location
Step 2 : Load second BCD number in another register from memory location.
Step 3 : Initialize MSB counter with 00H
Step 4 : Add first result to second number.
Step 5 : Adjust result to BCD
Step 6 : Check result <8 bit, if yes then go to step 8
Step 7 : Increment MSB counter by 1
Step 8 : Store result to memory locations.
Step 9 : Stop
Start
N Is
Cy=1?
Y
Increment MSB result counter by 1
Stop
Before the execution of the above program, store numbers in memory locations D000H
and D001H.
Program
LDA D00H ; Load first number in A register
MOV B, A ; Copy it to B reg.
LDA D100H ; Load second number in A register
MVI C, 00H ; Initialize MSB results register with 0
ADD B ; Add first number to second number
DAA ; Adjust result to BCD
JNC DN ; Check result < 8 bit, if yes then go to DN
INC C ; Increment MSB result integer
DN: STA D002H ; Store LSB of result
MOV A, C
STA D003H ; Store MSB of result
HLT ; Stop
After the execution of the above program, you can check the result in memory locations
D002H and D003H i.e. LSB of result 10 in D002H and MSB of result 01 in D003H.
4. Program for multiplication Of Two 8 Bit Numbers Using Successive Addition
Method And Shift And Add Method
Method 1 : Multiplication using successive addition method
In this method, the multiplication can be added multiplier times. for example, if you want
to multiply 4 by 5, then you can add 4 five times or 5 four times to get result 20. Hence, in this
program we will take 5 as addition counter and adds 4 five times.
Algorithm
Step 1 : Load multiplier in B reg.
Step 2 : Load multiplicand C reg.
Step 3 : Initialize result register with OOH
Step 4 : Add multiplicand with result register
Step 5 : Decrement multiplier
Step 6 : Check multiplier > 0, if no, then go to step 4
Step 7 : Store result
Step 8 : Stop
Program
MVI B, 05H ; Load B reg. with multiplier
MVI C, 04H ; Load C reg. with multiplicand
MVI A, 00H ; Initialize A reg. as a result register
UP: ADD C ; Add multiplicand with result register
DCR B ; Decrement multiplier
JNZ UP ; Check multiplier > 0, if no, then go to UP
MOV D, A ; Store result in D reg.
HLT ; Stop
Start
Multiplier = multiplier - 1
N Is Multiplier
= 0?
Y
Store result in D register
Stop
After the execution, the result of multiplication will be available in D register. But when we
take larger 8 bit number, then result will be greater than 8 bit. In such case, above program
can not be used. Hence following program will solve this problem :
1 0 1 0 1 0 1 0 AA H Multiplier
1 0 1 0 1 0 1 0 AA H Multiplicand
00000000
10101010
00000000
10101010
00000000
10101010
00000000
10101010
0111000011100100 70E4 H Result
7 0 E 4
Algorithm
Step 1 : Load multiplicand in BC reg. pair
Step 2 : Load multiplier in A reg.
Step 3 : Initialize result register HL with 0
Step 4 : Initialize shift counter in D reg. with 8
Step 5 : Result register = result register + result register [Shift result left by 1 bit]
Step 6 : Rotate multiplier to left by 1 bit
Step 7 : Check carry = 0, if yes, the go to step 9
Step 8 : Result register = result register + multiplicand
Step 9 : Decrement shift counter by
Step 10 : Check shift counter = 0, if no, go to step 5
Step 11 : Stop
Start
Y
Is Cy=0?
N
Result Register = Result register + multiplicand
N Is shift
counter = 0?
Stop
Program
LXI B, 00AAH ; Load multiplicand in BC reg. pair
MVI A, AAH ; Load multiplier in A register
LXI H, 0000H ; Initialize result register HL with 0000H
MVI D, 08H ; Initialize shift counter in D reg. with 8
UP: DAD H ; Shift result Left by 1 bit
RLC ; Rotate multiplier to left by 1 bit
JNC DN ; Check carry = 0, if yes, the go to DN
DAD B ; Result register = result register + multiplicand
DN: DCR D ; Decrement shift counter
JNZ UP ; Check shift counter
HLT ; Stop
After execution, the result of multiplication i.e. 70E4H will be available in HL register pair.
5. Program for division of two 8 bit Number using Successive Subtraction Method
In this method, we subtracts divisor from dividend until divisor is equal or greater than
dividend. So, one counter is required to count successive subtraction which gives you quotient
of the division operation and the end of the successive subtraction, we will get remainder. For
example, suppose we want to divide 0B by 2 then it can be perform as given below.
Step 1 : Initialize Quotient Counter in C register with 0, load dividend in A register and divisor
in B register
Step 2 : A=A-13=0B— 02 = 09
Increment C = C + 1 = 1
Compare Divisor with A, A > Divisor perform next subtraction
Step 3 : A = A - B = 09 — 02 = 07
Increment C = C + 1 = 1
Compare Divisor with A, A > Divisor perform next subtraction
Step 4 : A = A — B = 07 — 02 =05
Increment C = C +1 =3
Compare Divisor with A, A > Divisor perform next subtraction
Step 5 : A = A — B = 05 — 02 = 03
Increment C = C +1 = 4, Compare Divisor with A, A > Divisor perform next subtraction
Step 6 : A = A — B = 03 — 02 = 01
Increment C = C +1 = 5, Compare Divisor with A, A < Divisor stop subtraction
Start
Y Is divided >=
Divisor?
Stop
Algorithm
Step 1 : Load dividend in A register
Step 2 : Load divisor in B register
Step 3 : Initialize Quotient Counter in C register with 0
Step 4 : Dividend = dividend — divisor
Step 5 : Increment quotient counter by 1
Step 6 : Compare result of subtraction with divisor
Step 7 : Check result > divisor, if yes go to Step 4
Step 8 : Stop
Program
MVI A, 0BH; Load dividend in A register
MVI B, 02H ; Load divisor in B register
MVI C, 00H ; Initialize Quotient Counter in C register with 0
UP: SUB B ; Dividend = dividend — divisor
INR C ; Increment quotient counter by 1
CMP B ; Compare result of subtraction with divisor
JNC UP ; Check result > divisor, if yes go to UP
HLT ; Stop
After execution, the remainder will be available in A register and the quotient in C register.
6. Program To Transfer Block Transfer
In block transfer, we transfer the data bytes from one part of memory to another part of
memory. Assume 10 bytes are stored in the memory from address D000H and the starting
address of the destination block where we want to transfer these 10 bytes is D1000H.
D009H D109H
Now, to read bytes from the source block and then copy it to destination block, we need
two memory pointers. Hence, we can use HL and BC register pair as a memory pointers for
source and destination block. Again, we need a byte counter to tell microprocessor, how many
bytes stored in the block or to indicate length of block.
Algorithm :
Step 1 : Initialize bytes counter with length of block
Step 2 : Initialize memory pointer for source block
Step 3 : Initialize memory pointer for destination block.
Step 4 : Read byte or number from source block
Step 5 : Copy it to destination block.
Step 6 : Increment source and destination memory pointers by 1
Step 7 : Decrement byte counter by 1
Step 8 : Check byte counter > 0, if yes, then go to step 4
Step 9 : Stop
Program :
MVI D, 0AH ; Initialize bytes counter with length of block
LXI H, D000H ; Initialize memory pointer for source block
LXI B, D100H ; Initialize memory pointer for destination block
NEXT: MOV A, M ; Read number from source block AE ->[HL]
STAX B ; Copy it to destination block. [BC] <- A
INX H ; Increment source memory pointer HL<-HL+1
INX B ; Increment destination memory pointer BC<-BC+1
DCR D ; Decrement byte counter by 1 D<-D-1
JNZ NEXT ; Check byte counter > 0, if yes, then go to NEXT
HLT ; Stop
Before the execution, the ten data bytes must stored from memory location D000H.
After the execution, the contents of source block will be transferred to destination block
starting from D100H.
Start
N Is byte
counter =
0?
Stop
Program :
MVI C, OAH ; Initialize comparison or pass counter
UP1: LXI H, D100H ; Initialize memory pointer to read number from array
MVI B, 09H ; Initialize byte counter with length – 1 = 9
UP: MOV A, M ; Load number from the memory
INX H ; Increment memory pointer by 1 to read next number
CMP M ; Compare number with next number
JNC DN ; If number is > than next number then to DN
MOV D, M ; Exchange number and next number D to [HL]
MOV M,A ; [HL] <- A
DCX H ; HL <- HL – 1
MOV M, D ; HL <- D
INX H ; HL <- HL + 1
DN: DCR B ; Decrement byte counter by 1
JNZ UP ; If not zero then go to UP
DCR C ; Decrement comparison counter by 1
JNZ UP1 ; If not zero , then go to UP1
HLT ; Stop
Before the execution of the program the ten data bytes must be sorted from the memory
location D100H.
After the execution the all 10 bytes of the array will be arranged in the descending order
at same memory location.
8. Program to arrange numbers in array or block of data in ascending order
To arrange numbers in array in ascending order the above both i.e. for byte and word
program for descending order can be executed by replacing JNC instructions with JC
instruction.
Assume the array contains 10 data bytes and the starting address of the array is D100H.
Algorithm :
Step 1 : Initialize comparison or pass counter
Step 2 : Initialize memory pointer to read number from array
Step 3 : Initialize byte counter
Step 4 : Read numbers from an array
Step 5 : Compare 2 numbers
Step 6 : If numbers less or equal to next number then go to step8
Step 7 : Interchange or swap numbers
Step 8 : Increment memory pointer to read next number from array
Step 9 : Decrement byte counter by one
Step 10 : If word counter not equal to 0 then go to step 4
Step 11 : Decrement comparison counter by one
Step 12 : If comparison counter not equal to zero then go to step 2
Step 13 : Stop
Program :
MVI C, OAH ; Initialize comparison or pass counter
UP1: LXI H, D100H ; Initialize memory pointer to read number from array
MVI B, 09H ; Initialize byte counter with length -1 = 9
UP: MOV A, M ; Load number from the memory
INX H ; Increment memory pointer by 1 to read next number
CMP M ; Compare number with next number
JNC DN ; If number < next number then to DN
MOV D, M ; Exchange number and next number D to [HL]
MOV M,A ; [HL] <- A
DCX H ; HL <- HL – 1
MOV M, D ; HL <- D
INX H ; HL <- HL + 1
DN: DCR B ; Decrement byte counter by 1
JNZ UP ; If not zero then go to UP
DCR C ; Decrement comparison counter by 1
JNZ UP1 ; If not zero , then go to UP1
HLT ; Stop
Before the execution of the program the ten data bytes must be stored from memory
location D100H.
After the execution the all 10 bytes of the array will be arranged in ascending order at
same memory locations.
9. Program to find smallest number from the array
To find smallest number from the array the numbers in the array must be compared.
12
12
02
31
02
02
Smallest number from
45 02 the array of 5 number
65
Hence we need a memory pointer to read number from the array and also need byte
counter to read desired numbers only.
Assume the array contains 10 data bytes starting address of the array is D100H.
Algorithm :
Step 1 : Initialize comparison or pass counter
Step 2 : Initialize memory pointer to read number from array
Step 3 : Initialize byte counter
Step 4 : Read numbers from an array
Step 5 : Compare 2 numbers
Step 6 : If numbers < next number then go to step8
Step 7 : Replace number with next number which is smallest
Step 8 : Increment memory pointer to read next number in the array
Step 9 : Decrement byte counter by 1
Step 10 : If byte counter is not equal to zero then go to step 5
Step 11 : Store results
Step 12 : Stop
Program :
MVI C, OAH ; Initialize byte counter with OAH
LXI H, D100H ; Initialize memory pointer to read numbers
MOV A, M ; Read number from array
INX H ; Increment memory pointer by 1 to read next number
DCR C ; Decrement byte counter by 1
UP: CMP M ; Compare number with next number
JNC DN ; If number < next number then to DN
MOV A, M ; Replace number with next number which is smallest
DN: INX H ; Increment memory pointer by 1
DCR C ; Decrement byte counter by 1
JNZ UP ; If byte counter is not equal to zero then go to up
STA D10AH ; Store smallest number in memory
HLT ; Stop
Before the execution of the program the ten data bytes must be stored from memory
location D100H.
After the execution smallest number will be available in memory at location D10AH.