In this assignment you will learn/practice:
- write an assembly program that uses a conditional loop, conditional statements,
and integer arithmetic
- explore an interesting open problem in mathematics
Make sure you have the most recent version of the homework repository and do
your work in the hw04 directory.
Write an assembly language program that calculates the length of the so-called
hailstone sequence for the numbers 2 to 100,000 and displays the numbers whose
sequence is 300 or more.
The hailstone function for a positive integer
is defined as:
To obtain the hailstone sequence simply plug a number into the function to
get a new number. Then plug that number into the function. Continue this process
until the function produces 1. It has been conjectured that the sequence
will coverge to 1 for all positive integers. This is still an open question.
As an example, the hailstone sequence for the number three is:
For our purposes we'll say this sequence has a length of 7. This sequence is less than 300
so we would not include it in our output. Here is a partial sample of the output from my program:
26623 has length 307
34239 has length 310
35497 has length 310
35655 has length 323
. . . .
. . . .
. . . .
When completed, push your committed work to bitbucket.
One of the challenges of writing assembly code is managing complexity. It can be
tempting to try to write the entire program and then start debugging. As an
alternative, try this sequence, stopping to test and verify after each step:
- Write and debug this program in Java. This will establish that you
have a correct algorithm to work from.
- Start your assembly program. Put some number a register and see
if you can print "Even" if the number in that register is even and "Odd"
if it is odd.
- Replace that statement that prints odd with code to multiple the number
by 3 and then add 1 to it. Print the new number.
- Replace that statement that prints even with code to divide the number
by 2. Print the new number.
- Add code around these statements to loop if the new number is greater
than 1 and to drop out if the number is equal to 1. Make sure the new
number is the one that is used in the calculations on subsequent trips
through the loop. Print each new number so you can see the sequence.
- Add code on the outside of the working loop to act like a for loop
going from 2 to 5 using the index as the starting value for the inner
loop.
- Add a counter in the inner loop to keep track of how many numbers appear
in the sequence. Display that number when the inner loop finishes. Be sure
to clear the counter before the inner loop begins.
- Add conditional code to only print the sequence length if it exceeds 5.
- Modify the various conditional statements to meet the assignment requirements.