Check Prime or Not - Assembly Language Program - MASM


FIND WHETHER A NUMBER IS PRIME OR NOT
Program:

.model small
.stack 200H
.data
 msg1 DB 10,13,'Enter the number: $'
 msg2 DB 10,13,'The number is prime: $'
 msg3 DB 10,13,'The number is not prime: $'
 newline DB 10,13,'$'
 num dw ?
 que dw ?
.code
 print MACRO msg
 PUSH AX
 PUSH DX
 MOV AH,09H
 MOV DX,offset msg
 INT 21H
 POP DX
 POP AX
 ENDM
.startup
 print newline
 print msg1
 call readnumtoAX
 mov cx,2
 mov num,ax
 mov ax,num
 mov dx,0
 div cx
 mov que,ax
 mov ax,dx
NNN:
 cmp cx,que
 ja skip1
 cmp dx,00
 je skip2
 inc cx
 mov ax,cx
 mov dx,0
 mov ax,num
 div cx
 mov ax,dx
 jmp NNN
skip1:
 print newline
 print msg2
 jmp exit
skip2:
 print newline
 print msg3
exit:
.exit
readnumtoAX PROC NEAR
                                            ;STORE NUM. TO AX PROCEDURE DEF.
 PUSH BX
 PUSH CX
 MOV CX,10
 MOV BX,00
back:
 MOV AH,01H
 INT 21H
 CMP AL,'0'
 JB skip
 CMP AL,'9'
 JA skip
 SUB AL,'0'
 PUSH AX
 MOV AX,BX
 MUL CX
 MOV BX,AX
 POP AX
 MOV AH,00
 ADD BX,AX
 JMP back
skip:
 MOV AX,BX
 POP CX
 POP BX
 RET
readnumtoAX ENDP
displayAX PROC NEAR ;DISPLAY PROCEDURE CONTENTS
 PUSH DX
 PUSH CX
 PUSH BX
 PUSH AX
 MOV CX,0
 MOV BX,10
back1:
 MOV DX,0
 DIV BX
 PUSH DX
 INC CX
 OR AX,AX
 JNZ back1
back2:
 POP DX
 ADD DL,30H
 MOV AH,02H
 INT 21H
 LOOP back2
 POP AX
 POP BX
 POP CX
 POP DX
 RET
 displayAX ENDP
END

Output:
F:\>prm
Enter the number: 9
The number is not prime:
F:\>prm
Enter the number: 46
The number is not prime:
F:\>prm
Enter the number: 17
The number is prime:

2 comments:

Related Posts Plugin for WordPress, Blogger...