Machine Problem 4, due Apr 14

Part of the homework for 22C:60 (CS:2630), Spring 2014
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Assignment:

A popular screensaver from the past used to plot an animated "worm" on the the screen. If you constrain yourself to the ASCII display of the Hawk, this such a worm might look like the following:

        * * *
            *   *
            *   *
            * * *

Here, we have spaced the cells so that the x coordinate is always even in order to make the x and y spacing look uniform when characters are about twice as high as they are wide.

In each generation of the animation:

The worm starts out 10 asterisks long somewhere on the screen. Since a new asterisk is added to grow the worm each time an asterisk is removed, the worm stays 10 asterisks long until the program terminates.

Randomness:

Software cannot generate truly random numbers without help from hardware, since the whole point of our programming environment is to produce programs that produce deterministic output -- that is, given the program and the input, the output is completely determined.

What we can do is generate pseudo-random numbers -- they seem random, but in fact, they are not. It is very difficult to write a good pseudorandom number generator, and it is worth noting that many widely used pseudorandom number generators are not very good. Here is one that is adequate for many purposes (but not good enough for security-critical applications):

        /* Park and Miller's Minimal Standard Pseudorandom Number Generator */

        #define A 16807       /* = 0x41A7 */
        #define M 2147483647  /* = 0x7FFFFFFF */
        #define Q M/A         /* = 127773 = 0x1F31D */
        #define R M%A         /* = 2836 = 0xB14 */

        int seed; /* the value used to create successive random numbers */
                  /* at all times, (0 < seed < M) */

        setseed( int i )
        /* sets the seed for random number generation */
        {
                if ((i < 1) || (i >= M)) error();
                seed = i;
        }

        int random()
        /* advance the seed through the pseudorandom sequence
           and the next value from the sequence */
        {
                int t = A * (seed % Q) - R * (seed / Q);
                if (t > 0) {
                        seed = t;
                } else {
                        seed = t + M;
                }
                /* seed = (A * seed) % M is equivalent! */
                return seed;
        }

Requirements:

Grading

As mentioned above, the assignment number mp4 and your name must appear on the title of the listing. In addition, the file name of the file you submit must be mp4.a. No upper case, no alternatives.

Notes

If you ask for help debugging your code, your code must be well commented. We will not look at "naked" assembly code with no comments to suggest what it is intended to do, and when we find poor comments or discrepancies between the comments and the code, we may ask you to fix those before we look at the code in any detail. Of course, if you ask for help with the comments themselves, we will be glad to offer suggestions.