Microprocessor 8085 programs



















PROGRAMS FOR TRAINEES


Ø  Add parity bit to 7-bit ASCII
characters


Ø  Find the number of negative, zero and
positive numbers


Ø  Inserting string in a given array of
characters


Ø  Deleting string in a given array of
characters




Ø  Divide 16-bit number with 8-bit number
using shifting technique


Ø  Sub routine to  perform the task
of DAA


Ø  Program to test RAM


Ø  Program to generate Fibonacci number


Ø  Generate a delay of 0.4 seconds


Ø  Arrange in Descending Order


Ø  Data transfer from one memory block  to other memory block.


Ø  Find the factorial of a number


Ø  Find the Square Root of a given number


Ø  Split a HEX data into two nibbles and
store it


Ø  Add two 4-digit BCD numbers


Ø  Subtraction of two BCD numbers


Ø  Multiply two 2-digit BCD
numbers









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.



Source program :

v  LXI H, 2040H

v  MOV C ,M : Counter for character

v  REPEAT:INX H : Memory pointer to character

v  MOV A,M : Character in accumulator

v  ORA A : ORing with itself to check parity.

v  JPO PAREVEN : If odd parity place 

v  ORI 80H even parity in D7 (80).

v  PAREVEN:MOV M , A :
Store converted even parity character.

v  DCR C : Decrement
counter.

v  JNZ REPEAT : If not
zero go for next character.

v  HLT : Terminate program execution








Find the number of negative, zero and positive numbers


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.



Source program :

v  LXI H, 6000H : Initialize memory pointer

v  MVI C, 00H : Initialize number counter

v  MVI B, 00H : Initialize negative number counter

v  MVI E, 00H  : Initialize zero number
counter

v  BEGIN:MOV A, M : Get the number

v  CPI 00H : If number = 0

v  JZ ZERONUM : Goto zeronum

v  ANI 80H : If MSB of number = 1i.e. if

v  JNZ NEGNUM number is negative goto NEGNUM

v  INR D : otherwise increment positive number
counter

v  JMP LAST

v  ZERONUM:INR E : Increment zero number counter

v  JMP LAST

v  NEGNUM:INR B : Increment negative number
counter

v  LAST:INX H : Increment memory pointer

v  INR C : Increment number counter

v  MOV A, C

v  CPI 32H : If number counter = 5010 then

v  JNZ BEGIN : Store otherwise check next number

v  LXI H, 7000 :
Initialize memory pointer.

v  MOV M, B : Store
negative number.

v  INX H

v  MOV M, E : Store zero
number.

v  INX H

v  MOV M, D : Store
positive number.

v  HLT : Terminate execution








Inserting string in a given array of characters


Statement: Write an 8085
assembly language program to insert a string of four characters from the tenth
location in the given array of 50 characters.


Solution:
Step 1: Move bytes from
location 10 till the end of array by four bytes downwards.
Step 2: Insert four bytes at
locations 10, 11, 12 and 13.
Source program :

v  LXI H, 2l31H :
Initialize pointer at the last location of array.

v  LXI D, 2l35H : Initialize
another pointer to point the last location of array after insertion.

v  AGAIN: MOV A, M : Get the character

v  STAX D : Store at the new location

v  DCX D : Decrement destination pointer

v  DCX H : Decrement source pointer

v  MOV A, L : [check whether desired

v  CPI 05H  bytes are shifted or not]

v  JNZ AGAIN : if not repeat the process

v  INX H : adjust the memory pointer

v  LXI D, 2200H : Initialize the memory pointer to
point the string to be inserted

v  REPE: LDAX D : Get the character

v  MOV M, A : Store it in the array

v  INX D : Increment source pointer

v  INX H  : Increment destination pointer

v  MOV A, E : [Check whether the 4 bytes

v  CPI 04     are inserted]

v  JNZ REPE  : if not repeat the process

v  HLT : stop








Deleting string in a given array of characters


Statement: Write an 8085
assembly language program to delete a string of 4 characters from the tenth
location in the given array of 50 characters.


Solution:

Shift bytes from location 14
till the end of array upwards by 4 characters i.e. from location 10 onwards.

Source program :

v  LXI H, 2l0DH :Initialize
source memory pointer at the 14thlocation of the array.

v  LXI D, 2l09H :
Initialize destination memory pointer at the 10th location of the array.

v  MOV A, M : Get the character

v  STAX D : Store character at new location

v  INX D : Increment destination pointer

v  INX H : Increment source pointer

v  MOV A, L : [check whether desired

v  CPI 32H  bytes are shifted or not]

v  JNZ REPE : if not repeat the process

v  HLT : stop








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:
(2200) = 1100 (0CH)
(2201) = 0101 (05H)
Multiplicand = 1100 (1210)
Multiplier =  0101 (510)
Result = 12 x 5 = (6010)

Source program :

v  LXI H, 2200 : Initialize the memory pointer

v  MOV E, M : Get multiplicand

v  MVI D, 00H : Extend to 16-bits

v  INX H : Increment memory pointer

v  MOV A, M : Get multiplier

v  LXI H, 0000 : Product = 0

v  MVI B, 08H : Initialize counter with count 8

v  MULT: DAD H : Product = product x 2

v  RAL

v  JNC SKIP : Is carry
from multiplier 1 ?

v  DAD D : Yes, Product =Product + Multiplicand

v  SKIP:  DCR B : Is counter = zero

v  JNZ MULT : no, repeat

v  SHLD 2300H : Store the result

v  HLT : End of program








Divide 16-bit number with 8-bit number using shifting
technique


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.

Source program :

v  MVI E, 00 : Quotient = 0

v  LHLD 2200H : Get dividend

v  LDA 2300 : Get divisor

v  MOV B, A : Store divisor

v  MVI C, 08 : Count = 8

v  NEXT: DAD H  : Dividend = Dividend x 2

v  MOV A, E

v  RLC

v  MOV E, A : Quotient = Quotient x 2

v  MOV A, H

v  SUB B : Is most significant byte of Dividend
> divisor

v  JC SKIP : No, go to Next step

v  MOV H, A : Yes, subtract divisor

v  INR E : and Quotient = Quotient + 1

v  SKIP:DCR C : Count = Count - 1

v  JNZ NEXT : Is count =0 repeat

v  MOV A, E

v  STA 2401H : Store Quotient

v  Mov A, H

v  STA 2410H : Store remainder

v  HLT : End of program.








Sub routine to perform the task of DAA


Statement: Assume the DAA
instruction is not present. Write a sub routine which will perform the same
task as DAA.


Sample Problem:
Execution of DAA instruction:
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.

Source program :

v  LXI SP, 27FFH : Initialize stack pointer

v  MOV E, A : Store the contents of accumulator

v  ANI 0FH : Mask upper nibble

v  CPI 0A H : Check if number is greater than 9

v  JC SKIP : if no go to skip

v  MOV A, E : Get the number

v  ADI 06H : Add 6 in the number

v  JMP SECOND : Go for second check

v  SKIP: PUSH PSW : Store accumulator and flag
contents in stack

v  POP B : Get the contents of accumulator in B
register and flag register contents in C register

v  MOV A, C : Get flag register contents in
accumulator

v  ANI 10H  : Check for bit 4

v  JZ SECOND  : if zero, go for second check

v  MOV A, E  : Get the number

v  ADI 06 : Add 6 in the number

v  SECOND: MOV E, A : Store the contents of
accumulator

v  ANI FOH : Mask lower nibble

v  RRC 

v  RRC

v  RRC

v  RRC : Rotate number 4 bit right

v  CPI 0AH : Check if number is greater than 9

v  JC SKIPl  : if
no go to skip 1

v  MOV A, E : Get the number

v  ADI 60 H  : Add 60 H in the number

v  JMP LAST : Go to last

v  SKIP1: JNC LAST   : if carry flag = 0
go to last

v  MOV A, E : Get the number

v  ADI 60 H  : Add 60 H in the number

v  LAST: HLT








Program to test RAM


Statement: To test RAM by
writing '1' and reading it back and later writing '0' (zero) and reading it
back. RAM addresses to be checked are 40FFH to 40FFH. In case of any error, it
is indicated by writing 01H at port 10H.


Source program :

v  LXI H, 4000H : Initialize memory pointer

v  BACK: MVI M, FFH : Writing '1' into RAM

v  MOV A, M : Reading data from RAM

v  CPI FFH : Check for ERROR

v  JNZ ERROR : If yes go to ERROR

v  INX H : Increment memory pointer

v  MOV A, H

v  CPI SOH : Check for last check

v  JNZ BACK : If not last, repeat

v  LXI H, 4000H : Initialize memory pointer

v  BACKl: MVI M, OOH : Writing '0' into RAM

v  MOV A, M : Reading data from RAM

v  CPI OOH : Check for ERROR

v  INX H : Increment memory pointer

v  MOV A, H

v  CPI SOH : Check for last check

v  JNZ BACKl : If not
last, repeat

v  HLT : Stop Execution








Program to generate Fibonacci number


Statement: Write an
assembly language program to generate fibonacci
number.


Source program :

v  MVI D, COUNT : Initialize counter

v  MVI B, 00 : Initialize variable to store previous
number

v  MVI C, 01 : Initialize variable to store current
number

v  MOV A, B :[Add two numbers]

v  BACK: ADD C :[Add two numbers]

v  MOV B, C : Current number is now previous number

v  MOV C, A : Save result as a new current number

v  DCR D : Decrement count

v  JNZ BACK : if count  0 go to BACK

v HLT : Stop.








Generate a delay of 0.4 seconds


Statement: Write a program
to generate a delay of 0.4 sec if the crystal frequency is 5 MHz.


Calculation: In 8085, the operating frequency is
half of the crystal frequency,

ie.Operating frequency    = 5/2
= 2.5 MHz

Time for one T -state     =

Number of T-states required 
=   1 x 106

Source program :

v  LXI B, count : 16 - bit count

v  BACK: DCX B : Decrement count

v  MOV A, C

v  ORA B : Logically OR Band C

v  JNZ BACK : If result is not zero repeat








Arrange in Descending order


Statement: Arrange an
array of 8 bit unsigned no in descending order


v  START:MVI B, 00 ; Flag = 0

v  LXI H, 4150 ; Count = length of array

v  MOV C, M

v  DCR C ; No. of pair = count -1

v  INX H ; Point to start of array

v  LOOP:MOV A, M ; Get kth
element

v  INX H

v  CMP M ; Compare to (K+1) th
element

v  JNC LOOP 1 ; No interchange if kth >= (k+1) th

v  MOV D, M ; Interchange if out of order

v  MOV M, A ;

v  DCR H

v  MOV M, D

v  INX H

v  MVI B, 01H ; Flag=1

v  LOOP 1:DCR C ; count down

v  JNZ LOOP ;

v  DCR B ; is flag = 1?

v  JZ START ; do another sort, if yes

v  HLT ; If flag = 0, step execution










Data transfer from one memory block to other memory block.

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.


v  LXI H, 4150 : Initialize memory pointer

v  MVI B, 08 : count for 8-bit

v  MVI A, 54

v  LOOP : RRC

v  JC LOOP1

v  MVI M, 00 : store zero it no carry

v  JMP COMMON

v  LOOP2: MVI M, 01 : store one if there is a carry

v  COMMON:  INX H

v  DCR B : check for carry

v  JNZ LOOP

v  HLT : Terminate the program








Find the factorial of a number


Statement: Program to
calculate the factorial of a number between 0 to 8



Source
program :

v  LXI SP, 27FFH ; Initialize stack pointer

v  LDA 2200H ; Get the number

v  CPI 02H ; Check if number is greater than 1

v  JC LAST

v  MVI D, 00H ; Load number as a result

v  MOV E, A

v  DCR A

v  MOV C,A ; Load counter one less than number

v  CALL FACTO ; Call subroutine FACTO

v  XCHG ; Get the result in HL

v  SHLD 2201H ; Store result in the memory

v  JMP END

v  LAST: LXI H, 000lH ; Store result = 01

v  END: SHLD 2201H

v  HLT

Subroutine
Program:

v  FACTO:LXI H, 0000H

v  MOV B, C ; Load counter

v  BACK: DAD D

v  DCR B

v  JNZ BACK ; Multiply by successive addition

v  XCHG ; Store result in DE

v  DCR C ; Decrement counter

v  CNZ FACTO ; Call subroutine FACTO

v RET ;
Return to main program








Find the Square Root of a given number


Statement: Write a program
to find the Square Root of an 8 bit binary number. The binary number is stored
in memory location 4200H and store the square root in 4201H.



Source program :

v  LDA 4200H : Get the given data(Y) in A register

v  MOV B,A : Save the data in B register

v  MVI C,02H : Call the divisor(02H) in C register

v  CALL DIV : Call division subroutine to get
initial value(X) in D-reg


v  REP: MOV E,D : Save the initial value in E-reg

v  MOV A,B : Get the dividend(Y) in A-reg

v  MOV C,D : Get the divisor(X) in C-reg

v  CALL DIV : Call division subroutine to get
initial value(Y/X) in D-reg


v  MOV A, D : Move Y/X in A-reg

v  ADD E : Get the((Y/X) + X) in A-reg

v  MVI C, 02H : Get the divisor(02H) in C-reg

v  CALL DIV : Call division subroutine to get
((Y/X) + X)/2 in D-reg.This is XNEW


v  MOV A, E : Get Xin A-reg

v  CMP D : Compare X and XNEW

v  JNZ REP : If XNEW is not equal to X, then
repeat

v  STA 4201H : Save the square root in memory

v  HLT : Terminate program execution

Subroutine Program:

v  DIV: MVI D, 00H : Clear D-reg
for Quotient

v  NEXT:SUB C : Subtract the divisor from dividend

v  INR D : Increment the quotient

v  CMP C : Repeat subtraction until the

v  JNC NEXT : divisor is less than dividend

v  RET : Return to main program

Note: The square root can be taken by
an iterative technique. First, an initial value is assumed. Here, the initial
value of square root is taken as half the value of given number. The new
value of square root is computed by using an expression XNEW = (X + Y/X)/2
where, X is the initial value of square root and Y is the given number. Then,
XNEW is compared with initial value. If they are not equal then the above
process is repeated until X is equal to XNEW after taking XNEW as initial
value. (i.e., X ←XNEW)

                                                       

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 :

v  LXI H, 4200H : Set pointer data for array

v  MOV B,M : Get the data in B-reg

v  MOV A,B : Copy the data to A-reg

v  ANI OFH : Mask the upper nibble

v  INX H : Increment address as 4201

v  MOV M,A : Store the lower nibble in memory

v  MOV A,B : Get the data in A-reg

v  ANI FOH : Bring the upper nibble to lower nibble
position

v  RRC

v  RRC

v  RRC

v  RRC

v  INX H

v  MOV M,A : Store the upper nibble in memory

v  HLT : Terminate program execution








Add two 4-digit BCD numbers


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:
(HL) =3629
(DE) =4738
Step 1 : 29 + 38 =
61 and auxiliary carry flag = 1
:.add 06
61 + 06 = 67
Step 2 :  36 +
47 + 0 (carry of LSB) = 7D
Lower nibble of addition is
greater than 9, so add 6.
7D + 06 = 83
Result = 8367
Source program :

v  MOV A, L : Get lower 2 digits of no. 1

v  ADD E : Add two lower digits

v  DAA : Adjust result to valid BCD

v  STA 2300H : Store partial result

v  MOV A, H : Get most significant 2 digits of
number

v  ADC D : Add two most significant digits

v  DAA : Adjust result to valid BCD

v  STA 2301H : Store partial result

v  HLT : Terminate program execution.








Subtraction of two BCD numbers


Statement: Subtract the
BCD number stored in E register from the number stored in the D register.


Source Program: 

v  MVI A,99H

v  SUB E : Find the 99's
complement of subtrahend

v  INR A : Find 100's
complement of subtrahend

v  ADD
D            : Add
minuend to 100's complement of subtrahend

v  DAA : Adjust for BCD

v  HLT : Terminate
program execution

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 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.
The steps for finding 100's complement
BCD subtraction are :
  • Find the 100's complement of
    subtrahend

  • Add two numbers using BCD adition








Multiply two 2-digit BCD numbers


Statement: Write an
assembly language program to multiply 2 BCD numbers


Source Program: 

v  MVI C, Multiplier : Load BCD multiplier

v  MVI B, 00 : Initialize counter

v  LXI H, 0000H : Result = 0000

v  MVI E, multiplicand : Load multiplicand

v  MVI D, 00H : Extend to 16-bits

v  BACK: DAD D : Result Result
+ Multiplicand

v  MOV A, L : Get the lower byte of the result

v  ADI, 00H

v  DAA : Adjust the lower byte of result to BCD.

v  MOV L, A : Store the lower byte of result

v  MOV A, H : Get the higher byte of the result

v  ACI, 00H

v  DAA : Adjust the higher byte of the result to BCD

v  MOV H, A : Store the
higher byte of result.

v  MOV A, B : [Increment

v  ADI 01H : counter

v  DAA : adjust it to BCD and

v  MOV B,A : store it]

v  CMP C : Compare if count = multiplier

v  JNZ BACK  : if not equal repeat

v  HLT : Stop