Skip to content
Related Articles

Related Articles

8085 program to determine if the number is prime or not

Improve Article
Save Article
Like Article
  • Last Updated : 13 Jun, 2018

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.

ADDRESSLABELMNEMONIC
2000HLDA 2029H
2001H
2002H
2003HMVI C, 00H
2004H
2005HMOV E, A
2006HMOV B, A
2007HLOOP1MOV D, E
2008HLOOP2CMP D
2009HJC DIVIDENDLESSTHAN0
200AH
200BH
200CHSUB D
200DHJNZ LOOP2
200EH
200FH
2010HDIVIDENDLESSTHAN0CPI 00H
2011H
2012HJNZ NOTADIVISOR
2013H
2014H
2015HINR C
2016HNOTADIVISORMOV A, B
2017HDCR E
2018HJNZ LOOP1
2019H
201AH
201BHMOV A, C
201CHMVI C, 00H
201DH
201EHCPI 02H
201FH
2020HJNZ COMPOSITE
2021H
2022H
2023HINR C
2024HCOMPOSITEMOV A, C
2025HSTA 202AH
2026H
2027H
2028HHLT


Store the value of n in 2029H. If 202AH contains 01H, then n is prime, else composite.
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!