Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. model small . data prime _ numbers db 2 0 dup ( ? ) comma db ' , ' , ' $ ' newline

.model small
.data
prime_numbers db 20 dup(?)
comma db ',','$'
newline db 13,10,'$' ; Newline character for printing
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
mov cx,20 ; Number of primes to find (0-19)
mov bx,2 ; Start checking from 2
mov si,0 ; Initialize index for prime_numbers array
find_primes:
mov ax, bx ; Load the current number to check
xor dx, dx ; Clear DX for division
check_divisors:
div bl ; Divide AX by BL (quotient in AX, remainder in DX)
cmp dx,0 ; Check remainder
jnz not_prime ; If not prime, jump to not_prime
; If prime, store it in the array
mov [prime_numbers+si], bl ; Store prime number at offset (si)
inc si ; Move to the next index in the array
not_prime:
inc bx ; Move to the next number
dec cx ; Decrement counter
jcxz end_find_primes ; If counter is zero, end finding primes
jmp find_primes ; Repeat until all primes are found
end_find_primes:
; Print prime numbers separated by comma (,)
mov si, offset prime_numbers ; Point SI to prime_numbers array
mov cx,20 ; Number of primes to print
jmp print_loop ; Jump to print_loop to print the prime numbers
print_loop:
mov al,[si] ; Load prime number from array
cmp al,0 ; Check if prime number is zero (end of array)
je print_newline ; If zero, print newline and exit loop
add al,'0' ; Convert to ASCII
mov ah,02h ; Function to print character
int 21h ; Print the prime number
inc si ; Move to the next prime number
dec cx ; Decrement counter
jz print_newline ; If counter is zero, print newline
mov dl,',' ; Load comma character into DL
mov ah,02h ; Function to print character
int 21h ; Print the comma
jmp print_loop ; Continue printing the next prime number
print_newline:
mov dx, offset newline ; Load newline character into DX
mov ah,09h ; Function to print string
int 21h ; Print the newline
; Exit the program
mov ax,4C00h ; AH =4Ch for DOS exit
int 21h
end main
why this code print ,,,,,,,,,,,,,,,,,,,,,,,,,,,,, only i want tp print PNUM

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions

Question

Synthesis

Answered: 1 week ago