; PROGRAM: FIRST N PRIME NUMBERS
read macro num
mov ah, 01h
int 21h
mov bh, al
int 21h
mov bl, al
sub bh, 30h
mov al, 0ah
mul bh
sub bl, 30h
add al, bl
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg db 'Enter Limit: $'
seris db 'Series is: $'
comma db ', $'
nl db 0dh,0ah,'$'
count db 00h
nextpr db 02h
pnum db 4 dup('$')
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
print msg
read count
print nl
mov cl, count
mov ch, 00h
mov al, nextpr ;initialize first prime
mov ah, 00h
NEXTPRIME: ;check next prime
mov bh, 02h
div bh
mov bl, al ;inner loop counter
mov dl, 02h
cmp dl, bl
jg PRIME
REDO:
mov al, nextpr
mov ah, 00h
div dl
cmp ah, 00h
jz NOTPRIME
cmp bl, dl ;if div done upto count
jz PRIME ;and rem not 0, prime number
inc dl
jmp REDO
PRIME:
mov si, offset pnum
mov al, nextpr
mov ah, 00h
mov count, cl
call hex2ascii ;converts hex in ax
print pnum ;to ascii saves in si loc
print comma
mov cl, count
NOTPRIME:
mov al, nextpr ;get current num
add al, 01h ;find next num
mov ah, 00h
mov nextpr, al ;update memory
loop NEXTPRIME
mov ah, 4ch
int 21h
hex2ascii proc near
mov cx, 0000h
REPDIV:
mov dx, 0000h
mov bx, 000ah
div bx
add dl, 30h
push dx
inc cx
cmp ax, 000ah
JGE REPDIV
cmp al, 00h
je SKIP0
add al, 30h
mov [si], al
inc si
SKIP0:
SAVEASCI:
pop dx
mov [si], dl
inc si
loop SAVEASCI
STOP1:
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>prime_series
Enter Limit: 05
1, 2, 3, 5,
Z:\S5IT\masm>prime_series
Enter Limit: 20
1, 2, 3, 5, 7, 11, 13, 17, 19,
**************************
0 comments:
Post a Comment