Programs of 8085
Programs of 8085
Statement: Store the data byte 32H into memory location 4000H.
Program 1:
Program 2:
Note: The result of both programs will be the same. In program 1 direct addressing instruction is used,
whereas in program 2 indirect addressing instructions is used.
Program 1:
LDA 2000H : Get the contents of memory location 2000H into accumulator
Source: www.csitnepal.com
Program 2:
Note: In Program 1, direct addressing instructions are used, whereas in Program 2, indirect
addressing instructions are used.
Statement: Add the contents of memory locations 4000H and 4001H and place the result in memory
location 4002H.
Sample problem
(4000H) = 14H
(4001H) = 89H
Source: www.csitnepal.com
Source program
Flowchart
Note: In Program 1, direct addressing instructions are used, whereas in Program 2, indirect
addressing instructions are used.
Statement: Add the 16-bit number in memory locations 4000H and 4001H to the 16-bit number in
memory locations 4002H and 4003H. The most significant eight bits of the two numbers to be added
al
are in memory locations 4001H and 4003H. Store the result in memory locations 4004H and 4005H
with the most significant byte in memory location 4005H.
ep
itn
cs
Source: www.csitnepal.com
Program - 5.a: Add two 16-bit numbers - Source Program 1
Sample problem:
(4000H) = 15H
(4001H) = 1CH
(4002H) = B7H
(4003H) = 5AH
(4004H) = CCH
(4005H) = 76H
Source Program 1:
SHLD 4004H : Store I6-bit result in memory locations 4004H and 4005H.
Source: www.csitnepal.com
Flowchart
Source program 2:
SHLD 4004H : Store I6-bit result in memory locations 4004H and 4005H.
NOTE: In program 1, eight bit addition instructions are used (ADD and ADC) and addition is
performed in two steps. First lower byte addition using ADD instruction and then higher byte addition
ep
Source: www.csitnepal.com
QN 1. Subtract two 8-bit numbers
Statement: Subtract the contents of memory location 4001H from the memory location 2000H and
place the result in memory location 4002H.
Sample problem:
(4000H) = 51H
(4001H) = 19H
Source program:
Flowchart
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Add contents of two memory locations
Statement: Add the contents of memory locations 40001H and 4001H and place the result in the
memory locations 4002Hand 4003H.
Sample problem:
(4000H) = 7FH
(400lH) = 89H
(4002H) = 08H
(4003H) = 0lH
Source program:
Source: www.csitnepal.com
INX H :HL Points 4002H
Flowchart
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Subtract two 16-bit numbers
Statement: Subtract the 16-bit number in memory locations 4002H and 4003H from the 16-bit
number in memory locations 4000H and 4001H. The most significant eight bits of the two numbers
are in memory locations 4001H and 4003H. Store the result in memory locations 4004H and 4005H
with the most significant byte in memory location 4005H.
Sample problem:
(4000H) = 19H
(400IH) = 6AH
(4004H) = 04H
(4005H) = OEH
Source program:
SHLD 4004H : Store l6-bit result in memory locations 4004H and 4005H.
Source: www.csitnepal.com
Flowchart
Statement: Find the l's complement of the number stored at memory location 4400H and store the
complemented number at memory location 4300H.
Sample problem:
(4400H) = 55H
Source: www.csitnepal.com
Source program:
Flowchart
Statement: Find the 2's complement of the number stored at memory location 4200H and store the
complemented number at memory location 4300H.
Sample problem:
(4200H) = 55H
Source: www.csitnepal.com
Source program:
Flowchart
Statement: Pack the two unpacked BCD numbers stored in memory locations 4200H and 4201H and
store result in memory location 4300H. Assume the least significant digit is stored at 4200H.
Sample problem:
(4200H) = 04
(4201H) = 09
Result = (4300H) = 94
al
ep
itn
cs
Source: www.csitnepal.com
Source program:
Flowchart
Statement: Two digit BCD number is stored in memory location 4200H. Unpack the BCD number
and store the two digits in memory locations 4300H and 4301H such that memory location 4300H
al
will have lower BCD digit.
ep
Sample problem:
itn
cs
Source: www.csitnepal.com
(4200H) = 58
(4301H) = 05
Source program:
RRC
RRC
RRC
Flowchart
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Execution format of instructions
Statement: Read the program given below and state the contents of all registers after the execution of
each instruction in sequence.
Main program:
400CH HLT
Subroutine program:
4101H PUSH H
ep
itn
cs
Source: www.csitnepal.com
4102H LXI B, 4080H
4109H DAD B
410CH POP H
410DH POP B
410EH RET
Note:
The table given gives the instruction sequence and the contents of all register and stack
after execution of each instruction.
TABLE
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Right shift, bit of data( 8 bit and 16 bit)
Sample problem:
(4200H) = 58
(4301H) = 05
Statement: Write a program to shift a 16 bit data, 1 bit right. Assume that
data is in BC register pair.
Source program 2 Flowchart for Source
program1
MOV A, B
RAR
MOV B, A
MOV A, C
RAR
MOV C, A
HLT
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Left Shifting of a 16-bit data
Statement: Program to shift a 16-bit data 1 bit left. Assume data is in the HL register
Source Program
Statement: Write a set of instructions to alter the contents of flag register in 8085.
Statement: Calculate the sum of series of numbers. The length of the series is in memory location
ep
Source: www.csitnepal.com
a. Consider the sum to be 8 bit number. So, ignore carries. Store the sum at memory location 4300H.
b. Consider the sum to be 16 bit number. Store the sum at memory locations 4300H and 4301H.
Sample problem 1:
4200H = 04H
4201H = 10H
4202H = 45H
4203H = 33H
4204H = 22H
Result = 10 +41 + 30 + 12 = H
4300H = H
LDA 4200H
MOV C, A : Initialize
counter
SUB A : sum = 0
Source: www.csitnepal.com
Sample problem 2:
4200H = 04H
420lH = 9AH
4202H = 52H
4203H = 89H
4204H = 3EH
Source program 2
LDA 4200H
JNC SKIP
MOV A, B
al
Source: www.csitnepal.com
QN 1. Multiply two 8-bit numbers
Statement: Multiply two 8-bit numbers stored in memory locations 2200H and 2201H by repetitive
addition and store the result in memory locations 2300H and 2301H.
Sample problem 1:
(2200H) = 03H
(2201H) = B2H
= 216H
(2300H) = 16H
(2301H) = 02H
LDA 2200H
MOV E, A
MVI D, 00 : Get the first
number in DE register pair
LDA 2201H
MOV C, A : Initialize counter
LX I H, 0000 H : Result = 0
BACK: DAD D : Result =
result + first number
DCR C : Decrement count
JNZ BACK : If count 0
repeat
SHLD 2300H : Store result
HLT : Terminate program
execution
Source: www.csitnepal.com
Sample problem 1:
(2200H) = 60H
(2201H) = A0H
(2202H) = l2H
(2300H) = E8H
(2301H) = 08H
(2302H= 10H
(2303H) 00H
Source: www.csitnepal.com
Statement: Find the number of negative elements (most significant bit 1) in a block of data. The
length of the block is in memory location 2200H and the block itself begins in memory location
2201H. Store the number of negative elements in memory location 2300H
Sample problem 1:
(2200H) = 04H
(2201H) = 56H
(2202H) = A9H
(2203H) = 73H
(2204H) = 82H
LDA 2200H
MOV C, A : Initialize count
MVI B, 00 : Negative number
=0
LXI H, 2201H : Initialize
pointer
BACK: MOV A, M : Get the
number
ANI 80H : Check for MSB
JZ SKIP : If MSB = 1
INR B : Increment negative
number count
SKIP: INX H : Increment
pointer
DCR C : Decrement count
JNZ BACK : If count 0 repeat
MOV A, B
STA 2300H : Store the result
HLT : Terminate program
execution
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Find the largest of given numbers
Statement: Find the largest number in a block of data. The length of the block is in memory location
2200H and the block itself starts from memory location 2201H.
Store the maximum number in memory location 2300H. Assume that the numbers in the block are
all 8 bit unsigned binary numbers.
Sample problem 1:
(2200H) = 04
(2201H) = 34H
(2202H) = A9H
(2203H) = 78H
(2204H) =56H
LDA 2200H
MOV C, A : Initialize counter
XRA A : Maximum =
Minimum possible value = 0
LXI H, 2201H : Initialize
pointer
BACK: CMP M : Is number>
maximum
JNC SKIP : Yes, replace
maximum
MOV A, M
SKIP: INX H
DCR C
JNZ BACK
STA 2300H : Store maximum
number
HLT : Terminate program
execution
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Count number of one's in a number
Statement: Write a program to count number of l's in the contents of D register and store the count in
the B register.
Sample problem 1:
(2200H) = 04
(2201H) = 34H
(2202H) = A9H
(2203H) = 78H
(2204H) =56H
al
Source: www.csitnepal.com
Source program : Flowchart for program
MVI B, 00H
MVI C, 08H
MOV A, D
BACK: RAR
JNC SKIP
INR B
SKIP: DCR C
JNZ BACK
HLT
Statement: Write a program to sort given 10 numbers from memory location 2200H in the ascending
order.
Source: www.csitnepal.com
Statement: Calculate the sum of series of even numbers from the list of numbers. The length of the
list is in memory location 2200H and the series itself begins from memory location 2201H. Assume
the sum to be 8 bit number so you can ignore carries and store the sum at memory location 2210H.
Sample problem 1:
2200H= 4H
2201H= 20H
2202H= l5H
2203H= l3H
2204H= 22H
= 42H
LDA 2200H
MOV C, A : Initialize
counter
MVI B, 00H : sum = 0
LXI H, 2201H : Initialize
pointer
BACK: MOV A, M : Get the
number
ANI 0lH : Mask Bit l to Bit7
JNZ SKIP : Don't add if
number is ODD
MOV A, B : Get the sum
ADD M : SUM = SUM +
data
MOV B, A : Store result in B
register
SKIP: INX H : increment
pointer
DCR C : Decrement counter
JNZ BACK : if counter 0
repeat
STA 2210H : store sum
HLT : Terminate program
execution
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Calculate the sum of series of odd numbers
Statement: Calculate the sum of series of odd numbers from the list of numbers. The length of the list
is in memory location 2200H and the series itself begins from memory location 2201H. Assume the
sum to be 16-bit. Store the sum at memory locations 2300H and 2301H.
Sample problem 1:
2200H = 4H
2201H= 9AH
2202H= 52H
2203H= 89H
2204H= 3FH
LDA 2200H
MOV C, A : Initialize
counter
LXI H, 2201H : Initialize
pointer
MVI E, 00 : Sum low = 0
MOV D, E : Sum high = 0
BACK: MOV A, M : Get the
number
ANI 0lH : Mask Bit 1 to Bit7
JZ SKIP : Don't add if
number is even
MOV A, E : Get the lower
byte of sum
ADD M : Sum = sum + data
MOV E, A : Store result in E
register
JNC SKIP
INR D : Add carry to MSB
of SUM
SKIP: INX H : Increment
al
pointer
ep
itn
cs
Source: www.csitnepal.com
QN 1. Find the square of given number
Statement: Find the square of the given numbers from memory location 6100H and store the result
from memory location 7000H.
Sample problem 1:
2200H = 4H
2201H= 9AH
2202H= 52H
2203H= 89H
2204H= 3FH
al
Source: www.csitnepal.com
2300H= H Lower byte
Statement: Two decimal numbers six digits each, are stored in BCD package form. Each number
occupies a sequence of byte in the memory. The starting address of first number is 6000H Write an
assembly language program that adds these two numbers and stores the sum in the same format
starting from memory location 6200H.
Source: www.csitnepal.com
INX D : Increment pointer2
INX B : Increment result pointer
MOV A, L
CPI 06H : Check for last digit
JNZ BACK : If not last digit repeat
HLT : Terminate program execution
Statement: Two decimal numbers six digits each, are stored in BCD package form. Each number
occupies a sequence of byte in the memory. The starting address of first number is 6000H Write an
assembly language program that adds these two numbers and stores the sum in the same format
starting from memory location 6200H.
Source: www.csitnepal.com
QN 1. Separate even numbers from given numbers
Statement: Write an assembly language program to separate even numbers from the given list of 50
numbers and store them in the another list starting from 2300H. Assume starting address of 50
number list is 2200H.
Statement: Write assembly language program with proper comments for the following:
A block of data consisting of 256 bytes is stored in memory starting at 3000H. This block is to be
shifted (relocated) in memory from 3050H onwards. Do not shift the block or part of the block
anywhere else in the memory.
Two blocks (3000 - 30FF and 3050 - 314F) are overlapping. Therefore
it is necessary to transfer last byte first and first byte last.
Source Program:
Source: www.csitnepal.com
QN 1. Add parity bit to 7-bit ASCII characters
Statement: Add even parity to a string of 7-bit ASCII characters. The length of the string is in
memory location 2040H and the string itself begins in memory location 2041H. Place even parity in
the most significant bit of each character.
LXI H, 2040H
MOV C ,M : Counter for character
REPEAT:INX H : Memory pointer to
character
MOV A,M : Character in accumulator
ORA A : ORing with itself to check parity.
JPO PAREVEN : If odd parity place
ORI 80H even parity in D7 (80).
PAREVEN:MOV M , A : Store converted
even parity character.
DCR C : Decrement counter.
JNZ REPEAT : If not zero go for next
character.
HLT : Terminate program execution
Statement: A list of 50 numbers is stored in memory, starting at 6000H. Find number of negative,
zero and positive numbers from this list and store these results in memory locations 7000H, 7001H,
and 7002H respectively.
JMP LAST
ZERONUM:INR E : Increment zero
ep
number counter
JMP LAST
itn
cs
Source: www.csitnepal.com
NEGNUM:INR B : Increment negative
number counter
LAST:INX H : Increment memory
pointer
INR C : Increment number counter
MOV A, C
CPI 32H : If number counter = 5010
then
JNZ BEGIN : Store otherwise check next
number
LXI H, 7000 : Initialize memory pointer.
MOV M, B : Store negative number.
INX H
MOV M, E : Store zero number.
INX H
MOV M, D : Store positive number.
HLT : Terminate execution
QN 1. Multiply two eight bit numbers with shift and add method
Statement: Multiply the 8-bit unsigned number in memory location 2200H by the 8-bit unsigned
number in memory location 2201H. Store the 8 least significant bits of the result in memory location
2300H and the 8 most significant bits in memory location 2301H.
Sample problem:
Result = 12 x 5 = (6010)
Source: www.csitnepal.com
MVI B, 08H : Initialize counter with
count 8
MULT: DAD H : Product = product x
2
RAL
JNC SKIP : Is carry from multiplier 1
?
DAD D : Yes, Product =Product +
Multiplicand
SKIP: DCR B : Is counter = zero
JNZ MULT : no, repeat
SHLD 2300H : Store the result
HLT : End of program
Statement: Divide the 16-bit unsigned number in memory locations 2200H and 2201H (most
significant bits in 2201H) by the B-bit unsigned number in memory location 2300H store the quotient
in memory location 2400H and remainder in 2401H.
Assumption: The most significant bits of both the divisor and dividend
are zero.
MVI E, 00 : Quotient = 0
LHLD 2200H : Get dividend
LDA 2300 : Get divisor
MOV B, A : Store divisor
MVI C, 08 : Count = 8
NEXT: DAD H : Dividend =
Dividend x 2
MOV A, E
RLC
MOV E, A : Quotient = Quotient x 2
MOV A, H
SUB B : Is most significant byte of
Dividend > divisor
JC SKIP : No, go to Next step
MOV H, A : Yes, subtract divisor
INR E : and Quotient = Quotient + 1
SKIP:DCR C : Count = Count - 1
JNZ NEXT : Is count =0 repeat
al
MOV A, E
STA 2401H : Store Quotient
ep
Mov A, H
STA 2410H : Store remainder
itn
cs
Source: www.csitnepal.com
HLT : End of program.
Statement: Assume the DAA instruction is not present. Write a sub routine which will perform the
same task as DAA.
Sample Problem:
1. If the value of the low order four bits (03-00) in the accumulator is greater than
9 or if auxiliary carry flag is set, the instruction adds 6 '(06) to the low-order four
bits.
2. If the value of the high-order four bits (07-04) in the accumulator is greater
than 9 or if carry flag is set, the instruction adds 6(06) to the high-order four bits.
RRC
RRC
ep
Source: www.csitnepal.com
than 9
JC SKIPl : if no go to skip 1
MOV A, E : Get the number
ADI 60 H : Add 60 H in the number
JMP LAST : Go to last
SKIP1: JNC LAST : if carry flag = 0 go
to last
MOV A, E : Get the number
ADI 60 H : Add 60 H in the number
LAST: HLT
Source program :
Statement: Write a program to generate a delay of 0.4 sec if the crystal frequency is 5 MHz.
Source program :
Source: www.csitnepal.com
JNZ BACK : If result is not zero repeat
START:MVI B, 00 ; Flag = 0
LXI H, 4150 ; Count = length of array
MOV C, M
DCR C ; No. of pair = count -1
INX H ; Point to start of array
LOOP:MOV A, M ; Get kth element
INX H
CMP M ; Compare to (K+1) th element
JNC LOOP 1 ; No interchange if kth >= (k+1) th
MOV D, M ; Interchange if out of order
MOV M, A ;
DCR H
MOV M, D
INX H
MVI B, 01H ; Flag=1
LOOP 1:DCR C ; count down
JNZ LOOP ;
DCR B ; is flag = 1?
JZ START ; do another sort, if yes
HLT ; If flag = 0, step execution
Statement: Transfer ten bytes of data from one memory to another memory block. Source memory
block starts from memory location 2200H where as destination memory block starts from memory
location 2300H.
Source: www.csitnepal.com
QN 1. Find the factorial of a number
Subroutine Program:
FACTO:LXI H, 0000H
MOV B, C ; Load counter
BACK: DAD D
DCR B
JNZ BACK ; Multiply by
successive addition
XCHG ; Store result in DE
DCR C ; Decrement counter
CNZ FACTO ; Call
subroutine FACTO
RET ; Return to main
program
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. Split a HEX data into two nibbles and store it
Statement: Write a simple program to Split a HEX data into two nibbles and store it in memory
Source program :
Statement: Add two 4 digit BCD numbers in HL and DE register pairs and store result in memory
locations, 2300H and 2301H. Ignore carry after 16 bit.
Sample Problem:
(DE) =4738
:.add 06
al
61 + 06 = 67
ep
itn
cs
Source: www.csitnepal.com
Step 2 : 36 + 47 + 0 (carry of LSB) = 7D
7D + 06 = 83
Result = 8367
Source program :
Statement: Subtract the BCD number stored in E register from the number stored in the D register.
Source Program:
MVI A,99H
Note: When two BCD numbers are subtracted, we can use DAA instruction for
ajusting the result to BCD. Therefore, the subtraction of BCD number is carried
itn
cs
Source: www.csitnepal.com
out 10's complement or 100's complement.
The 10's complement of a decimal number is equal to the 99's complement plus 1.
The 99's complement of a number can be found by subtracting the number from
99.
Source Program:
Source: www.csitnepal.com
QN 1. 2-Digit BCD to binary conversion
Statement: Convert a 2-digit BCD number stored at memory address 2200H into its binary
equivalent number and store the result in a memory location 2300H.
Sample problem 1:
(2200H) = 67H
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. 2-Digit BCD to binary conversion
Statement: Convert a 2-digit BCD number stored at memory address 2200H into its binary
equivalent number and store the result in a memory location 2300H.
Sample problem 1:
(2200H) = 67H
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. HEX to Decimal conversion
Statement: Convert the HEX number in memory to its equivalent decimal number
Source program :
Converting A9 we get:
A9 /64=45 Hundreds = 01
Since 64(100 decimal) cannot be subtracted from 45 no. of hundreds = 01. Now
count tens 45/0A=3B Tens = 01 Now from 09, 0A cannot be subtracted. Hence
tens = 06 the decimal equivalent of A9 is 169.
al
ep
itn
cs
Source: www.csitnepal.com
QN 1. HEX to binary conversion
Source Program:
al
ep
itn
cs
Source: www.csitnepal.com