Write a MIPS program that accomplishes the following high-level code: // User pr
ID: 3795256 • Letter: W
Question
Write a MIPS program that accomplishes the following high-level code:
// User provides a value. Program determines if it is a prime number.
int num, ii, rem;
output(“Enter a Number: “);
input(num);
ii = 2;
while (ii < num/2)
{
rem = num % ii;
if (rem == 0)
ii = num/2;
ii++;
}
if (rem != 0)
output(“Number is prime! ”);
else
output(“Sorry, number is not prime. ”);
Advice
It is never a bad idea to write these programs first in a high-level language, such as Java, then to begin
translating that code to assembly language.
This program requires a number of new features (looping, decisions, I/O). I would suggest you
incrementally develop it. Like, start by simply accomplishing input and output. Then add looping, etc.,
one step at a time.
Explanation / Answer
This can be the logic to find if the number is prime.
testprime:
beq $s2, $s3, prime
div $s2, $s3
mfhi $t1
beq $t1, $0, noprime
addi $s3, $s3, 1
j testprime
noprime:
ori $t0, $0, 0
addi $s2, $s2, 1
j loop
prime:
ori $t0, $0, 1
sub $s0, $s0, $s4
addi $s2, $s2, 1
j loop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.