Write a program in MIPS that reads a positive integer n that

Write a program in MIPS that reads a positive integer n that is less than or equal to 100 from the user. Find the closest prime number less than or equal to n. The program should reject any number that is out of this range with an appropriate message. Print out the prime number with a message indicating the result. Comment your code appropriately.

Solution

Answer:

MIPS Assembly Language Code :

.zero 1
.LC0:
.string \"The prime numbers between are:\"
main:
push rbp
mov rbp, rsp
sub rsp, 32
mov DWORD PTR [rbp-16], 1
mov DWORD PTR [rbp-20], 100
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*)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
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-16]
mov DWORD PTR [rbp-8], eax
.L7:
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-20]
jg .L2
mov DWORD PTR [rbp-4], 0
mov DWORD PTR [rbp-12], 2
.L5:
mov eax, DWORD PTR [rbp-8]
mov edx, eax
shr edx, 31
add eax, edx
sar eax
cmp eax, DWORD PTR [rbp-12]
jl .L3
mov eax, DWORD PTR [rbp-8]
cdq
idiv DWORD PTR [rbp-12]
mov eax, edx
test eax, eax
jne .L4
add DWORD PTR [rbp-4], 1
jmp .L3
.L4:
add DWORD PTR [rbp-12], 1
jmp .L5
.L3:
cmp DWORD PTR [rbp-4], 0
jne .L6
cmp DWORD PTR [rbp-8], 1
je .L6
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
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> >&))
.L6:
add DWORD PTR [rbp-8], 1
jmp .L7
.L2:
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 .L11
cmp DWORD PTR [rbp-8], 65535
jne .L11
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
.L11:
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

Write a program in MIPS that reads a positive integer n that is less than or equal to 100 from the user. Find the closest prime number less than or equal to n.
Write a program in MIPS that reads a positive integer n that is less than or equal to 100 from the user. Find the closest prime number less than or equal to n.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site