Assignment 4

Due: 10:45am, Thu Feb 13th, 2025

Note: Make reasonable assumptions where necessary and clearly state them. Feel free to discuss problems with classmates, but the only written material that you may consult while writing your solutions are the textbook and lecture slides/videos/notes.

While all other homeworks are worth 100 points, this one is worth 150 points, i.e., it offers 50 points of extra credit.

Every homework has an automatic penalty-free 1.5 day extension to accommodate any health/family-related disruptions. In other words, try to finish your homework by Thursday 10:45am to keep up with the lecture content, but if necessary, you may take until Friday 11:59pm.

For the MARS programs, please upload separate .asm files in Gradescope that can be easily tested by the TAs. Note that your MARS programs will be graded on readability and user friendliness, as well as correctness. That means LOTS OF COMMENTS!! Again, here's the document that provides an overview of MARS. The examples at the end of that doc will be especially useful as you write these longer programs.

  1. Word Count (40 points): Write a MIPS assembly program in the MARS simulator that performs the following steps. The program allocates space for an array of 64 characters (study the example on page 7 of the MARS overview ). The program then prompts the user to enter a string of size less than 64 characters; it reads the string input by the user (we will not test with invalid inputs). The program then iterates through the string (until the null character is encountered) to perform a word count. A word starts with an alphabet a-z or A-Z and ends when a non-alphabet (not a-z and not A-Z) is encountered. The program ends by printing the string "Word count: " followed by the number of words in the string. For reference, here is an ASCII table. Here is an example run of the program:

    Enter a string of size less than 64 characters:
    NGL. Programming in assembly is insanely fun!
    Word count: 7

  2. Fibonacci Sequence with Recursion (50 points): Write a recursive MIPS assembly program in the MARS simulator to print the Nth integer in the Fibonacci sequence. Prompt the user for N and read the input value N (we will only test with N greater than 0). Recall that procedures are invoked with the jal instruction and you must perform appropriate saves/restores before/after the procedure invocation. You should follow the caller/callee conventions for saving registers. The pseudo code for procedure Fib is shown below:
    procedure Fib(N)
    if (N == 0) return 0
    if (N == 1) return 1
    return Fib(N-1) + Fib(N-2)

    An example run of the program:
    Enter an integer greater than zero:
    7
    Element 7 of the Fibonacci sequence is: 13

  3. Pattern Prediction (60 points):
    In this problem, you will be given a string of 0's and 1's as input. Using the algorithm below, your program will predict the next bit in the string. Given an input string, your program will first scan the string to train itself, culminating in a prediction of the next bit. The training is done with an array of 32 counters with values between 0 and 7. All 32 counters are initialized to the value 4. Let's take the following 24-bit input string as an example to explain the algorithm:
    Bit value 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0
    Bit-id 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    This string is a recurring pattern of 1110, so it should be easy for us humans to guess that the next bit should be a 1. The algorithm starts its training from bit-id 4. It looks at the prior 5 bits (in this case, 11101) and converts that 5-bit binary number into a decimal number (in this case, 29). It then looks at bit-id 5. If bit-id 5 is 1, then (in this case) it increments counter[29]; if bit-id 5 is 0, then it decrements counter[29]. These are "saturating" increments and decrements, i.e., if the counter was already at 7, the increment operation should keep it at 7, and if the counter was already at 0, a decrement operation should keep it at 0. Note that counter[29]'s value is being trained to remember the common-case next bit -- if counter[29] is a high value (greater than 3), then the model thinks the next bit after sequence 11101 (the number 29) will be 1 -- if counter[29] is a low value (less than 4), then the model thinks the next bit after sequence 11101 will be 0.

    After this first training step, the algorithm advances to bit-id 5. It looks at the prior 5 bits (in this case, 11011), converts it into a decimal number (27), uses that number to index the counter array (accesses counter[27]), and examines bit-id 6 to decide whether to increment/decrement that counter (in this case, bit-id 6 is 1, so counter[27] is incremented).

    This training repeats until we reach the end of the string, i.e., as soon as the next bit-id is not 0 or 1, we switch from training to prediction. In the above example, this happens when we reach bit-id 23. So we look at the last 5 bits (bit-id 19-23, which in this case is 01110) and convert it to a decimal number (14 in this case). That decimal number is used to index our array (in this case, we'll examine counter[14]). If counter[index] is greater than 3, we print our predicted next bit as 1. If counter[index] is less than 4, we print our predicted next bit as 0.

    Note that the program accepts the input as a string. You will need code to convert a string of 0 and 1 characters into a number when generating the index. We will not feed your program with invalid inputs, i.e., the input string will only have 0 or 1, and the input string length will remain between 6 and 100. A correct implementation of the above algorithm will do a decent job detecting most recurring patterns that are shorter than 6 characters.