8085 Micro-Processor Programs


Check whether a number is Prime using 8085

  • An Assembly Language Program to check whether a number is prime or not using 8085?
Note: A number is said to be prime, if it has only two factors, ie 1 and the number itself. Eg: factors of 7 = 1 and 7.

Algorithm
1.   Start the program by loading the number to be checked in to the memory location, 4500h.
2.   Copy the number to the registers A, B, D and E.
3.   Initialize the count with 0, C=00h.
4.  During the execution of the program all of the contents of these registers are overwritten except that in the Register E.
5.   Register E will hold the number in it throughout the execution of the program.
6.   Programs contains mainly two loops, on OUTLOOP and an INLOOP.
7.  The OUTLOOP updates the operand values and the INLOOP checks whether the remainder after division is zero or not.
8.   If the remainder is zero, then the count, C is incremented by 1.
9.   Else copy the content of register E (contains the number to be checked) to the Accumulator.
10. Decrement D by 1 and copy it to register B (second operand).
11. Continue the process until D becomes zero.
12. Store the count in to the location 4501 from register C.
13. If the value in the location, 4501 is 02h then the number in the location 4500 is a prime number, else it is not a prime number.
14. Terminate the program.

Example:
Suppose that you want to check whether 3 is a prime number or not.
Enter the number, 03h to the memory location 4500.
Initialize the count as 0, C=00h
Now copy the number to the registers A, B, D and E.
ie. A=03h              B=03h             D=03h             E=03h
--------------------------------------------------------------------------------------
After the INLOOP executes,       //Dividing 3 by 3
A=00h       (remainder)      B=03h             D=03h             E=03h
After the OUTLOOP executes,
C is incremented by 1, since 4 is divisible by 4 and remainder is 0.
Therefore, C=01h
A=03h (Copied from register E)   B=02h             D=02h             E=03h
----------------------------------------------------------------------------------
INLOOP------Dividing 3 by 2
A=01h (remainder)                       B=02h             D=02h             E=03h

OUTLOOP
C is not incremented since the remainder is not 0.
C=01h
A=03h                   B=01h             D=01h             E=03h

---------------------------------------------------------------------------------------

INLOOP------Dividing 3 by 1
A=00h                   B=01h             D=01h             E=03h

OUTLOOP
C=02h (since the remainder is zero)
A=03h                   B=01h             D=00h             E=03h
----------------------------------------------------------------------------------------
Program terminates since D=00h.
4501: 02h --------Indicates that 3 has only 2 factors and hence it is prime.

----------------------------------------------------------------------------------------
Program

MEMORY
LABEL
MNEMONIC
HEX  CODE
COMMENT
4200

LXI H,4500
21
Load the number from 4500 to HL pair
4201


00
4202


45
4203

MOV A,M
7E
Copy the number to accumulator
4204

MVI C,00
0E
Initialize C register with 00
4205


00
4206

MOV D,A
57
Copy the number to D
4207

MOV E,A
5F
Copy the number to E
4208
OUTLOOP
MOV B,D
42
Copy the content of D to B
4209
INLOOP
CMP B
B8
Compare accumulator and register B
420A

JC GO
DA
Jump on carry to the label GO
420B


11
420C


42
420D

SUB B
90
Subtract B from A
420E

JNZ INLOOP
C2
Jump on non-zero to the label INLOOP
420F


09
4210


42
4211
GO
CPI 00
FE
Compare accumulator content with 00
4212


00
4213

JNZ AHEAD
C2
Jump on non-zero to the label AHEAD
4214


17
4215


42
4216

INR C
0C
Increment C
4217
AHEAD
MOV A,E
7B
Copy the contents of E to A
4218

DCR D
15
Decrement D
4219

JNZ OUTLOOP
C2
Jump on non-zero to the label OUTLOOP
421A


08
421B


42
421C

MOV A,C
79
Copy the count to A
421D

STA 4501
32
Store the count in 4501
421E


01
421F


45
4220

HLT
76
Program ends


Observation

Input at     4500    :           07H
Output at   4501    :           02H      -------The number is prime.

-----------------------------------------------------------------------------------


No comments:

Post a Comment