Write a program that generates all prime numbers between 2 and 1000, using the S
ID: 3717351 • Letter: W
Question
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can ?nd many articles that describe the method for ?nding primes in this manner on the Internet (example: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes (Links to an external site.)Links to an external site.). Display all the prime values in this range.
Hint: you can create a procedure called "MarkMultiples" to mark numbers that are not prime in your array and another called "PrintPrimes", to display the numbers not marked.
Your array could be create like:
.data?
sieve BYTE 1000 DUP(?)
the position in the area corresponds to the number and the value is 0 for primes and 1 for not primes... Initialize the array with all 0s and the "MarkMultiples" procedure would change to 1 the value for the numbers that are not prime
WRITE THE CODE IN ASSEMBLY LANGUAGE AND ALSO WRITE THE COMMENTS AFTER EACH LINE SO THAT I CAN UNDERSTAND PROPERLY AND ALSO SCREENSHOT THE OUTPUT.
Explanation / Answer
INCLUDE Irvine32.inc
PrintPrimes PROTO,
count: DWORD; number of values ??to display
FIRST_PRIME = 2
LAST_PRIME = 1000;
.data
commaStr BYTE ",", 0
.data?
sieve BYTE LAST_PRIME DUP (?)
.code
main PROC
; Initialize the array to zeros
mov ecx, LAST_PRIME
mov edi, OFFSET sieve
mov al, 0
cld
rep stosb
mov esi, FIRST_PRIME
.WHILE Esi <LAST_PRIME
.IF Sieve [esi * TYPE sieve] == 0; is current entry prime?
call MarkMultiples; yes: mark all of its multiples
.ENDIF
inc esi; move to next table entry
.ENDW
INVOKE PrintPrimes, LAST_PRIME; display all primes found
exit
main ENDP
; ------------------------------------------------- -
MarkMultiples PROC
;
; Mark all multiples of the value passed in ESI.
; Notice we use ESI as the prime value, and
; Take advantage of the "scaling" feature of indirect
; Operands to locate the address of the indexed item:
; [Esi * TYPE sieve]
; ------------------------------------------------- -
push eax
push esi
mov eax, esi; prime value
add esi, eax; start with first multiple
L1: cmp esi, LAST_PRIME; end of array?
ja L2; yes
mov sieve [esi * TYPE sieve], 1; no: insert a marker
add esi, eax
jmp L1; repeat the loop
L2: pop esi
pop eax
ret
MarkMultiples ENDP
; ------------------------------------------------- -
PrintPrimes PROC,
count: DWORD; number of values ??to display
;
; Display the list of prime numbers
; ------------------------------------------------- -
mov esi, 1
mov eax, 0
mov ecx, count
L1: mov al, sieve [esi * TYPE sieve]
.IF Al == 0
mov eax, esi
call WriteDec
mov edx, OFFSET commaStr
call WriteString
.ENDIF
inc esi
loop L1
ret
PrintPrimes ENDP
END main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.