Open In App

8085 program to determine if the number is prime or not

Last Updated : 13 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Problem – Write an assembly language program for determining if a given number is prime or not using 8085 microprocessor.

If the number is prime, store 01H at the memory location which stores the result, else 00H.

Examples:

Input : 03H
Output : 01H
The number 3 only has two divisors, 1 and 3. 
Hence, it is prime.
Input : 09H
Output : 00H
The number 9 has three divisors, 1, 3 and 9. 
Hence, it is composite.

A prime number is the one which has only two divisors, 1 and the number itself.
A composite number, on the other hand, has 3 or more divisors.

Algorithm:

  1. Take n as input
  2. Run a loop from i = n to 1. For each iteration, check if i divides n completely or not. If it does, then i is n’s divisor
  3. Keep a count of the total number of divisors of n
  4. If the count of divisors is 2, then the number is prime, else composite

How to find out if i is a divisor or not?
Keep subtracting i from the dividend till the dividend either becomes 0 or less than 0. Now, check the value of dividend. If it’s 0, then i is a divisor, otherwise it’s not.

Steps:

  1. Load the data from the memory location (2029H, arbitrary choice) into the accumulator
  2. Initialize register C with 00H. This stores the number of divisors of n
  3. Move the value in the accumulator in E. This will act as an iterator for the loop from n to 1.
  4. Move the value in the accumulator in B. B permanently stores n because the value in the accumulator will change
  5. Move the value in E to D and perform division with the accumulator as the dividend and D as the divisor.
  6. Division: Keep subtracting D from A till the value in A either becomes 0 or less than 0. After this, check the value in the accumulator. If it’s equal to 0, then increment the count of divisors by incrementing the value in C by one
  7. Restore the value of the accumulator by moving the value in B to A and continue with the loop till E becomes 0
  8. Now, move the number of divisors from C to A and check if it’s equal to 2 or not. If it is, then store 01H to 202AH (arbitrary), else store 00H.

202AH contains the result.

ADDRESS LABEL MNEMONIC
2000H LDA 2029H
2001H
2002H
2003H MVI C, 00H
2004H
2005H MOV E, A
2006H MOV B, A
2007H LOOP1 MOV D, E
2008H LOOP2 CMP D
2009H JC DIVIDENDLESSTHAN0
200AH
200BH
200CH SUB D
200DH JNZ LOOP2
200EH
200FH
2010H DIVIDENDLESSTHAN0 CPI 00H
2011H
2012H JNZ NOTADIVISOR
2013H
2014H
2015H INR C
2016H NOTADIVISOR MOV A, B
2017H DCR E
2018H JNZ LOOP1
2019H
201AH
201BH MOV A, C
201CH MVI C, 00H
201DH
201EH CPI 02H
201FH
2020H JNZ COMPOSITE
2021H
2022H
2023H INR C
2024H COMPOSITE MOV A, C
2025H STA 202AH
2026H
2027H
2028H HLT


Store the value of n in 2029H. If 202AH contains 01H, then n is prime, else composite.


Similar Reads

8085 program to check whether the given 16 bit number is palindrome or not
Problem - Write an assembly language program to check whether the given 16 bit number is palindrome or not. If number is palindrome then store 01 at memory location 3050 otherwise store FF at memory location 3050. Note - A palindrome number is a number that remains the same when its digits are reversed. Assume that 16 bit number, to check for palin
5 min read
8085 program to convert an 8 bit number into Grey number
Prerequisite - Binary to/from Gray Code Problem - Write an assembly language program in 8085 which convert an 8 bit number into grey number Example - Assumption - 8 bit number (input) is stored at memory location 2050 and output to be stored at memory location 3050. Algorithm - Load the content of memory location 2050 in AccumulatorReset carry flag
2 min read
8085 program to count number of ones in the given 8-bit number
Problem – Write a program to count number of ones in the given 8-bit number use register B to display the count of ones where starting address is 2000 and the number is stored at 3000 memory address and store result into 3001 memory address. Example – Algorithm – Move 00 to register B immediately for countMove 08 to register C immediately for shift
3 min read
8085 program to convert an 8 bit BCD number into hexadecimal number
Problem - Write an assembly language program in 8085 microprocessor to convert an 8 bit BCD number into hexadecimal number. Assumptions - Assume that starting address of the program is 2000, input memory locations, 2050, 2051, and output memory location is 2052. Example - INPUT: 2050:02H 2051: 09H OUTPUT:2052: 1DH Algorithm - Initialize memory poin
2 min read
8085 program to search a number in an array of n numbers
Problem - Write an assembly language program in 8085 to search a given number in an array of n numbers. If number is found, then store F0 in memory location 3051 otherwise store 0F in 3051. Assumptions - Count of elements in an array is stored at memory location 2050. Array is stored from starting memory address 2051 and number which user want to s
4 min read
8085 program to find the factorial of a number
Problem - Write an assembly language program for calculating the factorial of a number using 8085 microprocessor. Example - Input : 04H Output : 18H as 04*03*02*01 = 24 in decimal => 18H In 8085 microprocessor, no direct instruction exists to multiply two numbers, so multiplication is done by repeated addition as 4x3 is equivalent to 4+4+4 (i.e.
2 min read
8085 program to count the number of ones in contents of register B
Problem: Write an assembly language program to count the number of ones in the contents of register B and store the result at memory location 3050. Example: Algorithm: Convert the decimal number in Accumulator to its binary equivalent.Rotate the digits of the binary number right without carry.Apply a loop till the count is not zero to change the va
4 min read
8085 program to find square root of a number
Problem – Write an assembly language program in 8085 microprocessor to find square root of a number. Example - Assumptions - Number, whose square root we need to find is stored at memory location 2050 and store the final result in memory location 3050. Algorithm - Assign 01 to register D and ELoad the value, stored at memory location 2050 in accumu
3 min read
8085 program to find 1's and 2's complement of 8-bit number
Problem - Write a program to find 1's and 2's complement of 8-bit number where starting address is 2000 and the number is stored at 3000 memory address and store result into 3001 and 3002 memory address. Example - Algorithm - Load the data from memory 3000 into A (accumulator)Complement content of accumulatorStore content of accumulator in memory 3
2 min read
8085 program to find 1’s and 2’s complement of 16-bit number
Prerequisite - 8085 program to find 1’s and 2’s complement of 8-bit number Problem - – Write a program to find 1’s and 2’s complement of 16-bit number where starting address is 2000 and the number is stored at 3000 memory address and store result into 3002 and 3004 memory address. Example - Algorithm - Load a 16-bit number from memory 3000 into a r
2 min read