Question

In: Computer Science

Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...

Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values.

This program should be in assembly language.

Solutions

Expert Solution

Assembly Code:

.LC0:

.string " "

SieveOfEratosthenes(int):

push rbp

mov rbp, rsp

push rbx

sub rsp, 56

mov DWORD PTR [rbp-52], edi

mov rax, rsp

mov rbx, rax

mov eax, DWORD PTR [rbp-52]

add eax, 1

cdqe

lea rcx, [rax-1]

mov QWORD PTR [rbp-40], rcx

mov rax, rcx

add rax, 1

mov r10, rax

mov r11d, 0

mov rax, rcx

add rax, 1

mov r8, rax

mov r9d, 0

mov rax, rcx

lea rdx, [rax+1]

mov eax, 16

sub rax, 1

add rax, rdx

mov esi, 16

mov edx, 0

div rsi

imul rax, rax, 16

sub rsp, rax

mov rax, rsp

add rax, 0

mov QWORD PTR [rbp-48], rax

mov rax, rcx

lea rdx, [rax+1]

mov rax, QWORD PTR [rbp-48]

mov esi, 1

mov rdi, rax

call memset

mov DWORD PTR [rbp-28], 2

.L5:

mov eax, DWORD PTR [rbp-28]

imul eax, eax

cmp DWORD PTR [rbp-52], eax

jl .L2

mov rdx, QWORD PTR [rbp-48]

mov eax, DWORD PTR [rbp-28]

cdqe

movzx eax, BYTE PTR [rdx+rax]

movzx eax, al

cmp eax, 1

jne .L3

mov eax, DWORD PTR [rbp-28]

imul eax, eax

mov DWORD PTR [rbp-24], eax

.L4:

mov eax, DWORD PTR [rbp-24]

cmp eax, DWORD PTR [rbp-52]

jg .L3

mov rdx, QWORD PTR [rbp-48]

mov eax, DWORD PTR [rbp-24]

cdqe

mov BYTE PTR [rdx+rax], 0

mov eax, DWORD PTR [rbp-28]

add DWORD PTR [rbp-24], eax

jmp .L4

.L3:

add DWORD PTR [rbp-28], 1

jmp .L5

.L2:

mov DWORD PTR [rbp-20], 2

.L8:

mov eax, DWORD PTR [rbp-20]

cmp eax, DWORD PTR [rbp-52]

jg .L6

mov rdx, QWORD PTR [rbp-48]

mov eax, DWORD PTR [rbp-20]

cdqe

movzx eax, BYTE PTR [rdx+rax]

test al, al

je .L7

mov eax, DWORD PTR [rbp-20]

mov esi, eax

mov edi, OFFSET FLAT:_ZSt4cout

call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

mov esi, OFFSET FLAT:.LC0

mov rdi, rax

call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

.L7:

add DWORD PTR [rbp-20], 1

jmp .L8

.L6:

mov rsp, rbx

nop

mov rbx, QWORD PTR [rbp-8]

leave

ret

.LC1:

.string "Following are the prime numbers smaller "

.LC2:

.string " than or equal to "

main:

push rbp

mov rbp, rsp

sub rsp, 16

mov DWORD PTR [rbp-4], 1000

mov esi, OFFSET FLAT:.LC1

mov edi, OFFSET FLAT:_ZSt4cout

call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

mov esi, OFFSET FLAT:.LC2

mov rdi, rax

call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

mov rdx, rax

mov eax, DWORD PTR [rbp-4]

mov esi, eax

mov rdi, rdx

call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

mov esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

mov rdi, rax

call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

mov eax, DWORD PTR [rbp-4]

mov edi, eax

call SieveOfEratosthenes(int)

mov eax, 0

leave

ret

__static_initialization_and_destruction_0(int, int):

push rbp

mov rbp, rsp

sub rsp, 16

mov DWORD PTR [rbp-4], edi

mov DWORD PTR [rbp-8], esi

cmp DWORD PTR [rbp-4], 1

jne .L13

cmp DWORD PTR [rbp-8], 65535

jne .L13

mov edi, OFFSET FLAT:_ZStL8__ioinit

call std::ios_base::Init::Init() [complete object constructor]

mov edx, OFFSET FLAT:__dso_handle

mov esi, OFFSET FLAT:_ZStL8__ioinit

mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev

call __cxa_atexit

.L13:

nop

leave

ret

_GLOBAL__sub_I_SieveOfEratosthenes(int):

push rbp

mov rbp, rsp

mov esi, 65535

mov edi, 1

call __static_initialization_and_destruction_0(int, int)

pop rbp

ret

IF YOU HAVE ANY QUERY PLEASE COMMENT BELOW.

PLEASE GIVE A THUMBS UP.


Related Solutions

Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Write a program that prints the count of all prime numbers between A and B (inclusive),...
Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7,...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit. Aim Develop code for implementing the sieve of Eratosthenes. Steps for Completion Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. Consider building the sieve in the class constructor. CODE GIVEN public class SieveOfEratosthenes { public SieveOfEratosthenes(int maxValue) { // Build the sieve here } public boolean isPrime(int value) { // Write...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
Write an algorithm that print all numbers that are divisible by 3 between 1000 and 2000
Write an algorithm that print all numbers that are divisible by 3 between 1000 and 2000
Use "do...while" loops to write a program that finds all prime numbers less than a specified...
Use "do...while" loops to write a program that finds all prime numbers less than a specified value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT