In: Computer Science
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.
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.