Assembly Program to check whether the input number is Prime or Not


The program prompts the user to input a number and tells whether the input number is Prime or Not. The program can take a multidigit number as input.

The basic logic in the program is that after taking the number from the user, the program starts iteration from 2 to Number-1 and divide the number with every new value. If the remainder becomes 0 at any division it means that the number is not prime otherwise the number is prime.

 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
.MODEL SMALL
.STACK 100H
.DATA
MSG DB "ENTER A NUMBER: $"
PRIMEMSG DB 0DH, 0AH, "NUMBER IS PRIME $" 
NOTPRIMEMSG DB 0DH, 0AH, "NUMBER IS NOT PRIME $"
TOTAL DB 0
VALUE DB 0
.CODE
MAIN PROC
    
    MOV AX, @DATA
    MOV DS, AX
    
    MOV AH, 9
    LEA DX, MSG
    INT 21H
    
    READ:
    MOV AH, 1
    INT 21H
    
    CMP AL, 13
    JE ENDOFNUMBER
    
    MOV VALUE, AL
    SUB VALUE, 48
    
    MOV AL, TOTAL
    MOV BL, 10
    MUL BL
    
    ADD AL, VALUE
    
    MOV TOTAL, AL
    
    JMP READ
    
    ENDOFNUMBER:
    MOV AH, 0
    MOV AL, TOTAL
    MOV BL, 2
    
    CHECK:
    
    CMP AL, BL
    JE PRIME
    
    DIV BL
    CMP AH, 0
    JE NOTPRIME
    
    INC BL
    MOV AL, TOTAL
    MOV AH, 0
    
    JMP CHECK
    
    PRIME:
    
    MOV AH, 9
    LEA DX, PRIMEMSG
    INT 21H
    
    JMP EXIT
    
    NOTPRIME:
    
    MOV AH, 9
    LEA DX, NOTPRIMEMSG
    INT 21H
    
    EXIT:   

MAIN ENDP

Output:




No comments