Assignment 4
Due: 9:00am, Thu Feb 16th, 2023
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.
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 9:00am 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.
- Integer array, procedures, MARS syscalls (50 points):
Write a MIPS assembly program in the MARS simulator that performs the
following steps.
The program allocates space for an array of 10 integers (study the example
on page 7 of the MARS overview ).
The array is used to maintain a list of integers, with the list initially
being empty.
The program then executes a loop for 10 iterations.
In each loop iteration, the user
is prompted to either push or pop an integer into the list (see an example
output below). If the user wants to push the value 17, the list grows by
one element, which is assigned the value 17. If the user then chooses to
pop a value, the list shrinks by one element (the number 17 is no longer
considered part of the list). If the list is already empty, a pop does
nothing.
Once the loop is completed, the program calls a procedure "addproc".
This procedure has two arguments - the starting address of our array of
integers and the number of valid entries in our list. The procedure then
executes a for loop that sums up the elements in the list. This sum is
returned to the caller.
The caller program then prints the size and contents of the list, and the sum.
When calling procedure "addproc", you must save/restore at least one register
value on the stack, and you should follow the caller/callee conventions for saving registers.
MARS syscalls only
allow reading in 31-bit positive integers, so we'll only test with positive
integers less than 2 billion.
We will not test your code with invalid inputs, so you don't need to clutter
your code with checks for invalid inputs.
Here's a sample output of the program.
Welcome! The list is currently empty. Ready to push/pop 10 times.
Iteration 1: Enter "u" to push, enter "o" to pop.
u
Enter the number that will be added to the list.
17
Added 17 to the list.
Iteration 2: Enter "u" to push, enter "o" to pop.
u
Enter the number that will be added to the list.
36
Added 36 to the list.
Iteration 3: Enter "u" to push, enter "o" to pop.
o
36 has been removed from the list.
Iteration 4: Enter "u" to push, enter "o" to pop.
o
17 has been removed from the list.
Iteration 5: Enter "u" to push, enter "o" to pop.
o
Nothing to pop.
Iteration 6: Enter "u" to push, enter "o" to pop.
u
Enter the number that will be added to the list.
42
Added 42 to the list.
Iteration 7: Enter "u" to push, enter "o" to pop.
u
Enter the number that will be added to the list.
25
Added 25 to the list.
Iteration 8: Enter "u" to push, enter "o" to pop.
u
Enter the number that will be added to the list.
67
Added 67 to the list.
Iteration 9: Enter "u" to push, enter "o" to pop.
o
67 has been removed from the list.
Iteration 10: Enter "u" to push, enter "o" to pop.
u
Enter the number that will be added to the list.
72
Added 72 to the list.
End of list push/pop operations.
Now saving some registers and calling procedure addproc.
Returned from addproc. Restoring registers.
The list has 3 elements.
The contents of the list are:
1 42
2 25
3 72
The sum is 139.
- 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 an error message
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"). For
example, if you see AAAAA, you would replace them with A5. If you see
BBBBBBBBBBBB, you would replace them with B12.
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 reference, here is an ASCII table.
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:
A2C5GT14A3ab2cd
The compression ratio is 2.0.