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 this homework, please upload three separate files - the first is a typed up solution to Q1, the other two are separate .asm files for Q2 and Q3. The TAs will download and test your .asm files on MARS. 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.
Your program must have at least one procedure call with at least one save/restore of a register, while following the save/restore conventions. Before you start, go through the MARS overview in detail. For example, the code on page 7 and 8 will be useful as you go about allocating space for an array of characters and reading a string. For reference, here is an ASCII table.
| 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 |
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.
While it was laborious to read this question, you have learned an important computer hardware concept. Modern processors use variants of this algorithm to identify patterns and make predictions about future behavior, e.g., whether a conditional branch will be true or false. We will study this further when we get to Branch Predictors.