Reference no: EM133218692
Consider this series of numbers: 3 10 5 16 8 4 2 1 Starting with a positive integer N, this series is computed with a simple iterative computation:
If the current integer N is even, the next number is N / 2
If the current integer N is odd, the next number is N * 3 + 1
Repeat until N equals 1 This is called the Hailstone sequence and it is related to the Collatz conjecture. The conjecture states that eventually this series will converge to 'one' no matter what positive starting integer is chosen. Here is another example of the series: 26 13 40 20 10 5 16 8 4 2 1 implement MIPS assembly language program that does the following:
Prompt the user for an integer N.
Compute the hailstone sequence for the given number N and count the number of iterations required to converge to 1. You do not need to store the sequence of numbers, just compute them, print them and count the iterations.
It is possible, for large N, the computation may overflow. That is, N * 3 + 1 may produce a number larger than can be contained in a 32 bit word. Be aware of this possibility, but you do not need to detect or handle overflow in your program for this assignment.
When the sequence reaches 1, print out the number of iterations and terminate the program. • Feel free to use snippets of my multiplication code (posted on the website) to read and write to the terminal, multiply and to terminate your program. Note: there is a much easier way to multiply by 3.
Write (at least) one function that computes n*3+1. (The parameter n must be passed to the function in $a0, and the function should return n*3+1 in $v0. Properly use this function in the part of your code that computes the Hailstone sequence (using jal to call the function and jr to return from it).
You may NOT use the built-in MIPS instructions for multiply or divide. Use addition and shift instructions for multiplication and division, and the AND instruction for odd/even testing.
You must comment your code