8085 program to determine if the number is prime or not


In this program we will see how to check a number is prime or not using 8085.

Problem Statement

Write 8085 Assembly language program to check whether a given number is prime or not.

Discussion

Here the approach is little bit different. We are actually counting the number of unique factors. For prime numbers the factors are only two. The 1 and the number itself. So if the result is 02H, then it is prime, otherwise not a prime number. As there is no division operation, we have to perform division by subtracting repeatedly.

Input

Address
Data
F100
07

 

Address
Data
F100
FF

 

Address
Data
F100
2F

 

Flow Diagram

 

Program

Address
HEX Codes
Labels
Mnemonics
Comments
F000
21, 00, F1
 
LXI H,F100
Point to F100 to take the number
F003
FE
 
MOV A,M
Take the number into Accumulator
F004
0E
 
MVI C,00
Clear C register
F006
57
 
MOV D,A
Copy A to D
F007
5F
 
MOV E,A
Copy A to E
F008
42
L2
MOV B,D
Load B with D
F009
B8
L1
CMP B
Compare B with A
F00A
DA, 11, F0
 
JC LABEL
if carry is generated, jump to Label
F00D
90
 
SUB B
Subtract B from A
F00E
C2, 09, F0
 
JNZ L1
Jump to L1
F011
FE, 00
LABEL
CPI 00
Compare A with 00H
F013
C2, 17, F0
 
JNZ SKIP
If Z = 0, jump to SKIP
F016
0C
 
INR C
Increase C by 1
F017
7B
SKIP
MOV A,E
Load A with E again
F018
15
 
DCR D
Decrease D by 1
F019
C2, 08, F0
 
JNZ L2
Jump to L2 label if Z = 0
F01C
79
 
MOV A,C
Load C to A
F01D
32, 01, F1
 
STA F101
Store result into F101
F020
76
 
HLT
Terminate the program

 

Output

Address
Data
F150
02

This is Prime

Address
Data
F150
08

This is not prime

Address
Data
F150
02

This is Prime

 

 

 

 

 

 

 

 

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements