8085 program to determine if the number is prime or not - GeeksforGeeks

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:
Take n as inputRun 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 divisorKeep a count of the total number of divisors of nIf the count of divisors is 2, then the number is prime, else composite
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.
Steps:
Load the data from the memory location (2029H, arbitrary choice) into the accumulatorInitialize register C with 00H. This stores the number of divisors of nMove 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 changeMove 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 oneRestore the value of the accumulator by moving the value in B to A and continue with the loop till E becomes 0Now, 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.
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.
microprocessor
system-programming
Computer Organization & Architecture
microprocessor
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Direct Access Media (DMA) Controller in Computer Architecture
Architecture of 8085 microprocessor
Computer Organization and Architecture | Pipelining | Set 2 (Dependencies and Data Hazard)
Pin diagram of 8086 microprocessor
Difference between Hardwired and Micro-programmed Control Unit | Set 2
I2C Communication Protocol
Memory mapped I/O and Isolated I/O
Computer Architecture | Flynn's taxonomy
Computer Organization | Different Instruction Cycles
Introduction of Control Unit and its Design