CS 354 Computer Organization and Systems Fall 2019 

Project 3 - Hailstone Numbers in MIPS

Goals

In 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 Numbers

Hailstone 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 MIPS

I/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.

        .data
        .align 0
p1:     .asciiz "Please enter the 1st integer: "
p2:     .asciiz "Please enter the 2nd integer: "
r1:     .asciiz "The sum of "
r2:     .asciiz " and "
r3:     .asciiz " is "
r4:     .asciiz ".\n"


		.text
        .globl  main
main:
        li      $v0, 4             # Load 4=print_string into $v0
        la      $a0, p1            # Load address of first prompt into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 5             # Load 5=read_int into $v0
        syscall                    # Read an integer via syscall
        add     $s0, $v0, $zero    # Copy from $v0 to $s0

        li      $v0, 4             # Load 4=print_string into $v0
        la      $a0, p2            # Load address of second prompt into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 5             # Load 5=read_int into $v0
        syscall                    # Read an integer via syscall
        add     $s1, $v0, $zero    # Copy from $v0 to $s1


        add     $s2, $s0, $s1      # Add $s0 and $s1 and store in $s2


        li      $v0, 4             # Load 4=print_string into $v0
        la      $a0, r1            # Load address of 1st result string into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 1             # Load 1=print_int into $v0
        add     $a0, $s0, $zero    # Load first number into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 4             # Load 4=print_string into $v0
        la      $a0, r2            # Load address of 2nd result string into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 1             # Load 1=print_int into $v0
        add     $a0, $s1, $zero    # Load second number into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 4             # Load 4=print_string into $v0
        la      $a0, r3            # Load address of 3rd result string into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 1             # Load 1=print_int into $v0
        add     $a0, $s2, $zero    # Load sum of inupt numbers into $a0
        syscall                    # Output the prompt via syscall

        li      $v0, 4             # Load 4=print_string into $v0
        la      $a0, r4            # Load address of 4th result string into $a0
        syscall						# Output the prompt via syscall

		li   $v0, 10				# system call for exit
		syscall						# we are out of here.




Task

Write 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.

Deliverables

Send me an email with the MIPS file containing your program. Comment each line and code segment explaining its function.

Humor?

References

  1. MARS (MIPS Assembler and Runtime Simulator)
  2. Appendix B of our text.
  3. Eric W. Weisstein. "Hailstone Number." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/HailstoneNumber.html
  4. Eric W. Weisstein. "Collatz Problem." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/CollatzProblem.html


Copyright © 2019, David A. Reimann. All rights reserved.