Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write the following code in MIPS: There are several ways to find prime numbers. A standard one is to divide the number x by the
Write the following code in MIPS:
There are several ways to find prime numbers. A standard one is to divide the number x by the numbers sqrtx and if it divides evenly by any of them, it is not prime. However, you are to write rather different algorithm, discovered by the ancient Greek polymath Eratosthenes of Cyrene. There are several variants on this algorithm, and you are to implement the following one. Note that each bit represents a number, such that the first bit is the second bit is etc.
Request an integer from the user. If it is less than or greater than show an error message and go back to
Allocate ceilingn bytes of memory and fill it with all one bits. That is put hex FF in each byte, meaning that initially we assume all numbers are prime. If n is not an even multiple of round up Use the SBRK SysAlloc in my SysCalls.asm file system call to allocate memory. Do not allocate it using the space directive in your program.
Starting with bit representing the number set each bit to zero that is a multiple of but do not set bit to zero, since is prime. See example below. The bit may already be zero, but that doesnt matter.
Find the next nonzero bit in the array, compute its position as a number this will be and set every third bit to zero. The array is zero origin, so as shown, the first bit is numbered zero, then etc. Whether you consider the high bit or the low bit in the byte to be the zeroth bit in that byte is up to you as long as you are consistent. Im showing it with the high bit as zero for convenience because that is also the byte order.
The algorithm terminates when you have used bits up to n Go through the array and compute the bit position of every bit, and print the position. Those will be your primes. Print one prime number per line, or if youre ambitious, print per line separated by spaces.
Stop
SAMPLE OUTPUT Example, where the user entered :
Bit position:
Initial array:
First pass: # This uses
Second pass: # This uses what will the third pass use?
Prints starting with bit :
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started