Question: Write a full assembly program that finds the prime numbers from 0 - 2 0 . 1 . Store the results in array 2 .

Write a full assembly program that finds the prime numbers from
0
-
2
0
.
1
.
Store the results in array
2
.
Print the prime numbers separated by comma
(
,
)
dont use procedure,loop, pop
,
push
,
load
use only basics
I work on X
8
0
8
6
.model small
.stack 100h
.data
primes db 8 dup(0) ; Array to store prime numbers (from 2 to 19)
comma db ',' ; Comma separator for printing
.code
main:
mov ax, @data
mov ds, ax
mov cx,8 ; Number of prime numbers to find
mov si,2 ; Start checking from 2(first prime number)
check_prime_loop:
mov ax, si ; Load the current number
call is_prime ; Check if it's prime
test ax, ax ; Check if AL (result of is_prime) is zero (not prime)
jz not_prime
; If prime, store it in the array
mov di, si ; Move current number to DI
sub di,2 ; Adjust index for zero-based indexing
mov [primes + di], al ; Store prime number at offset
; Print the prime number
mov dl, al ; Load prime number into DL
add dl,'0' ; Convert number to ASCII
mov ah,02h ; DOS function: print character
int 21h ; Call DOS interrupt
; Print comma if not the last prime
cmp si,19 ; Check if this is the last prime
je done ; If yes, exit loop
mov dl,',' ; Otherwise, print a comma
mov ah,02h
int 21h
not_prime:
inc si ; Move to the next number
cmp si,20 ; Check if we've reached 20
jle check_prime_loop ; If not, continue checking for primes
done:
mov ax,4C00h ; Exit the program
int 21h
is_prime:
mov cx,2 ; Start checking divisors from 2
cmp ax,2 ; Special case: 2 is prime
je prime
jbe not_prime ; If the number is 1 or less, it's not prime
xor dx, dx ; Clear DX for division
div cx ; Divide AX by CX
test dx, dx ; Check remainder (DX)
jz prime ; If remainder is zero, it's prime
inc cx ; Increment divisor
cmp cx, ax ; Compare divisor with the number
jae prime ; If divisor exceeds or equals the number, it's prime
jmp is_prime
prime:
mov ax, si ; Set AL to 1 to indicate prime
ret

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!