Sunday 29 January 2012

Masm program to find prime numbers upto a limit

printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

data segment
num dw ?
i dw 02
j dw ?
n dw ?

msg1 db "Enter a limit$"
msg2 db 0ah,0dh,"Prime nos:$"
msg3 db 0ah,0dh,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax

printmsg msg1

mov dh,02
mov ah,01h
int 21h
mov ah,00
sub al,30h

mov cl,10
mul cl
mov n,ax
mov ah,01h
int 21h
mov ah,00
sub al,30h

add ax,n
inc ax
mov n,ax

printmsg msg2


mov ah,00
mov cx,n
l3:
cmp cx,i
je l1

mov j,02
mov ax,i
mov dh,02
div dh
mov bl,al
l6:cmp j,bl
jnc l2
l7:mov ax,i
mov dl,j
div dl
cmp ah,00
je l9
inc j
jmp l6

l2:
jz l7
printmsg msg3
calcnum i
l9:inc i
jmp l3

l1:mov ah,04h
int 21h
code ends
end start

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More