I need the code for this program. It has to be in MIPS Assembly language. Whethe
ID: 3776345 • Letter: I
Question
I need the code for this program. It has to be in MIPS Assembly language. Whether or not a number is prime.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Write a program that reads in a number and prints a message telling whether the number is prime or not. Recall that a prime number is divisible only by itself and 1.
An easy (but not efficient) method to determine whether a number N is prime is to check whether any number smaller than N (other than 1) divides N evenly. For example, if N is 20, you need to check whether 2, 3, ... , 19 divide 20 evenly. Actually, you only need to check those numbers less than sqrt(n) (think about why this is true). If one of these numbers does divide n evenly, then N is not prime.
It can be shown that all primes, other than 2 and 3, are of the form 6k - 1 or 6k + 1 for some k. (We will not prove this here.) This means that we can use the following algorithm, which is a little more efficient than the brute force method described above:
So a sample execution of your program might look like:
Another sample execution of your program might look like:
Explanation / Answer
Answer:
MIPS Assembly Language Code :
.zero 1
.LC0:
.string "Enter a number: "
.LC1:
.string " is a prime number"
.LC2:
.string " is not a prime number"
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov BYTE PTR [rbp-5], 1
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
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*)
lea rax, [rbp-12]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov DWORD PTR [rbp-4], 2
.L4:
mov eax, DWORD PTR [rbp-12]
mov edx, eax
shr edx, 31
add eax, edx
sar eax
cmp eax, DWORD PTR [rbp-4]
jl .L2
mov eax, DWORD PTR [rbp-12]
cdq
idiv DWORD PTR [rbp-4]
mov eax, edx
test eax, eax
jne .L3
mov BYTE PTR [rbp-5], 0
jmp .L2
.L3:
add DWORD PTR [rbp-4], 1
jmp .L4
.L2:
cmp BYTE PTR [rbp-5], 0
je .L5
mov eax, DWORD PTR [rbp-12]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC1
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*)
jmp .L6
.L5:
mov eax, DWORD PTR [rbp-12]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
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*)
.L6:
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 .L10
cmp DWORD PTR [rbp-8], 65535
jne .L10
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L10:
nop
leave
ret
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret