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
Store the results in array
Print the prime numbers separated by comma
dont use procedure,loop, pop
push
load
use only basics
I work on X
model small
stack h
data
primes db dup ; Array to store prime numbers from to
comma db ; Comma separator for printing
code
main:
mov ax @data
mov ds ax
mov cx ; Number of prime numbers to find
mov si ; Start checking from first prime number
checkprimeloop:
mov ax si ; Load the current number
call isprime ; Check if it's prime
test ax ax ; Check if AL result of isprime is zero not prime
jz notprime
; If prime, store it in the array
mov di si ; Move current number to DI
sub di ; Adjust index for zerobased indexing
mov primes di al ; Store prime number at offset
; Print the prime number
mov dl al ; Load prime number into DL
add dl ; Convert number to ASCII
mov ahh ; DOS function: print character
int h ; Call DOS interrupt
; Print comma if not the last prime
cmp si ; Check if this is the last prime
je done ; If yes, exit loop
mov dl ; Otherwise, print a comma
mov ahh
int h
notprime:
inc si ; Move to the next number
cmp si ; Check if we've reached
jle checkprimeloop ; If not, continue checking for primes
done:
mov axCh ; Exit the program
int h
isprime:
mov cx ; Start checking divisors from
cmp ax ; Special case: is prime
je prime
jbe notprime ; If the number is 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 isprime
prime:
mov ax si ; Set AL to to indicate prime
ret
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
