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.
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.
- Take n as input
- 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
- Keep a count of the total number of divisors of n
- 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.
- Load the data from the memory location (2029H, arbitrary choice) into the accumulator
- Initialize register C with 00H. This stores the number of divisors of n
- Move the value in the accumulator in E. This will act as an iterator for the loop from n to 1.
- Move the value in the accumulator in B. B permanently stores n because the value in the accumulator will change
- Move the value in E to D and perform division with the accumulator as the dividend and D as the divisor.
- 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
- Restore the value of the accumulator by moving the value in B to A and continue with the loop till E becomes 0
- 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.
|2003H||MVI C, 00H|
|2005H||MOV E, A|
|2006H||MOV B, A|
|2007H||LOOP1||MOV D, E|
|2016H||NOTADIVISOR||MOV A, B|
|201BH||MOV A, C|
|201CH||MVI C, 00H|
|2024H||COMPOSITE||MOV A, C|
Store the value of n in 2029H. If 202AH contains 01H, then n is prime, else composite.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- 8085 program to check whether the given 16 bit number is palindrome or not
- 8085 program to check whether both the nibbles of 8 bit number are equal or not
- 8085 program to convert an 8 bit number into Grey number
- 8085 program to count number of ones in the given 8-bit number
- 8085 program to convert an 8 bit BCD number into hexadecimal number
- 8086 program to determine largest number in an array of n numbers
- 8085 program to convert a BCD number to binary
- 8085 program to search a number in an array of n numbers
- 8085 program to find the factorial of a number
- 8085 program to count the number of ones in contents of register B
- 8085 program to find square root of a number
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to convert 8 bit BCD number into ASCII Code
- 8085 program to find 1’s and 2’s complement of 16-bit number
- 8085 program to find sum of digits of 8 bit number
- 8085 program to find nth power of a number
- 8085 program to find square of a 8 bit number
- 8085 program to check whether the given number is even or odd
- 8085 program to find smallest number between two numbers
- 8085 program to reverse 16 bit number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.