CS 354 | Computer Organization and Systems | Fall 2019 | |
Project 3 - Hailstone Numbers in MIPSGoalsIn this project you will gain familiarity with basic MIPS assembly programming including program format, simple I/O, and basic MIPS operations. We will be using the MARS (MIPS Assembler and Runtime Simulator) IDE for our MIPS programming. Hailstone NumbersHailstone numbers arise from the Collatz problem. This is a very simply stated problem which has no know solution. Given a positive integer n, the next number in the sequence is given by n/2 if n is even or 3n+1 if n is odd. This is sometimes called the 3n+1 problem. To illustrate this sequence, consider n=8. The sequence generated will be 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, and so on. It becomes more interesting for values that are not powers of 2. When n=7, the sequence is 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, and so on. Note that whenever a power of 2 occurs in the sequence, the sequence will rapidly progress to the 4, 2, 1 loop. For other numbers, the value bounces up and down, like a hailstone, giving rise to their name. It has been experimentally verified that all values of n < 263-1 will eventually terminate in the 4, 2, 1 loop. From a mathematical standpoint, it is a complete mystery if given an arbitrary starting value of n whether or not the sequence always will terminate in the 4, 2, 1 loop. I/O in MIPSI/O is a messy affair (consider babies) and assembly language programming in MIPS is no exception. Our MIPS environment hides some of the mess from us by acting a broker between us and the simulated MIPS computer, similar to an operating system but on a much smaller scale. System services are called to perform the desired operation as described in Appendix B. Read this section carefully, including the table and examples on page B-44. Here is an example of a MIPS program that prompts the user for two integers, reads the two integers, adds them, and outputs the result. You can download it as add2.s, load it into MARS, run it, and observe the behavior of MARS.
TaskWrite a MIPS program that prompts the user for an input integer value of n and reports the number of steps required to reach the value of 1 in the generated hailstone sequence. For example, with an input of n=8, output a value of 3. For n=7, output a value of 16. Let's assume an input of 1 reports 0 steps. Only the MIPS assembly language instructions found in the example program listed above plus the instructions listed in Figure 2.1 (page 78) are allowed. For example, there is no division instruction allowed, so you will need to use a shift instruction. DeliverablesSend me an email with the MIPS file containing your program. Comment each line and code segment explaining its function. Humor?References
Copyright © 2019, David A. Reimann. All rights reserved. |