Assignment 4
Due: 10:45am, Thu Feb 15th, 2024
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 covid/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.
- Word Count (50 points):
Write a MIPS assembly program in the MARS simulator that performs the
following steps.
The program allocates space for an array of 80 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
80 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 80 characters:
I never realized it would be this insanely fun to program in assembly.
Word count: 13
- Data Compression (50 points):
Write a MIPS assembly program in the MARS simulator that accepts an
input string of size less than 50 characters, and applies the following
compression algorithm to the string, and then prints the resulting
compressed string.
The input string will only consist of alphabets, i.e., a-z and A-Z. First
check the string to make sure it's a valid input (if not, print "Invalid Input"
and quit). Then walk through the string looking for consecutive occurrences
of the same character and replace them with the character and a count
(called a "run length encoding"). Limit yourself to run lengths of less than 10.
For example, if you see AAAAA, you would replace it with A5. If you see
BBBBBBBBBBBB, you would replace it with B9B3. If you see CC, you would replace
it with C2. Single character occurrences do not need a count.
At the end, print the compression ratio, which is a floating point number = (size of input string) /
(size of output string). For floating point conversion, see the FAQ at the
end of the MARS google doc.
Here is an example run of the program:
Provide an input string with less than 50 characters and only containing a-z or A-Z:
AACCCCCGTTTTTTTTTTTTTTAAAabbcd
The compressed string is:
A2C5GT9T5A3ab2cd
The compression ratio is 1.875.
- 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