-
Notifications
You must be signed in to change notification settings - Fork 26
/
Prime-Check.asm
57 lines (48 loc) · 1.78 KB
/
Prime-Check.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
;##################################################
;# 8086 Program to check the given no for Prime #
;# Author: kingspp #
;##################################################
;NUM is assigned to the Number to be checked
;AL is used as the default no
;BL is used as Dividing Variable. It is Incremented in each loop
;BH is used to know the number of variables which can divide the Number. BH>02, The number is not prime
;BL is made to run x number of times, where x is the given number.
; Declaration Part
.MODEL SMALL
.DATA
MSG DB "The Give No is a Prime No$"
NMSG DB "The Given No is not a Prime No$"
NUM DB 71H ;Enter the required no here
.CODE
START: MOV AX,@DATA
MOV DS,AX
MOV AL,NUM
MOV BL,02H ; The Dividing starts from 2, Hence BH is compare to 02H
MOV DX,0000H ; To avoid Divide overflow error
MOV AH,00H ; To avoid Divide overflow error
;Loop to check for Prime No
L1:DIV BL
CMP AH,00H ; Remainder is compared with 00H (AH)
JNE NEXT
INC BH ; BH is incremented if the Number is divisible by current value of BL
NEXT:CMP BH,02H ; If BH > 02H, There is no need to proceed, It is not a Prime
JE FALSE ; The no is not a Prime No
INC BL ; Increment BL
MOV AX,0000H ; To avoid Divide overflow error
MOV DX,0000H ; To avoid Divide overflow error
MOV AL,NUM ; Move the Default no to AL
CMP BL,NUM ; Run the loop until BL matches Number. I.e, Run loop x no of times, where x is the Number given
JNE L1 ; Jump to check again with incremented value of BL
;To display The given no is a Prime No
TRUE: LEA DX,MSG
MOV AH,09H ; Used to print a string
INT 21H
JMP EXIT
;To display The given no is not a Prime No
FALSE: LEA DX,NMSG
MOV AH,09H ; Used to print a string
INT 21H
EXIT:
MOV AH,4CH
INT 21H
END START