CS 230 Winter 2024
Assignment 4
Coverage: Arrays, Standard Input/Output, and the Stack
Due Date: Sunday, March 10, 11:59 p.m.
. All submissions are to be completed through MarkUs
(https://markus.student.cs.uwaterloo.ca/markus_cs230_w/en/assignments)
. Read the policy described in Learn regarding late submissions.
Check MarkUs to see how many 12-hour grace periods you have remaining.
. Solutions for assignments will not be posted.
. Your programs must assemble and run properly with the given MIPS assembler binasm in the student.cs environment.
. The majority of the marks for this assignment will based on correctness. If your program does not assemble, then the correctness marks will be 0. Test your submission thoroughly before submitting.
. Correctness marks are based on passing test cases. No part marks are assigned to the code that is written. Part marks are assigned based on the number of test cases passed.
All test data will match the restrictions described in the problem specification. In other words, you do not need to check for input errors.
. Registers are a limited resource. They provide very fast access to data, so programs
should optimize their use. A few marks for Question 1 will be assigned to the use of registers. There are no restrictions on the number of registers used for Questions 2 or 3.
. Your solution should include comments at the beginning that include your name, your Quest ID, and a brief description of the program.
. You are not required to comment every line of your program. However, you should include inline comments that highlight the logical steps of your solution.
. You must not change the values of registers $26 through $29 in your programs.
However, you are allowed to use any other registers while completing your solutions. The stack ($30) should be restored to its original state at the end of your programs. The contents of registers that are explicitly mentioned in the problem description must meet the specification, however other registers (less than $26) may have random values as required by your solution.
. You may use subprograms in your solutions if you wish, however they are not required. For this assignment only, if you do use subprograms, you are not required to follow the register conventions described in Module 02, Slide 74.
. Double check your submissions in MarkUs after you have uploaded them. It is your responsibility to ensure that the uploaded version is readable in MarkUs.
. Questions 1, 2, 3 are weighted approximately equally.
1. Assume that an array has been initialized with integers, where the array location is in register $1 and the array length is in register $2. Write an assembly program that
updates the contents of the array as follows:
. negative numbers are replaced by the square of the number
. positive numbers are replaced by 10 times the number
. zero is unchanged
For example, if the array started with values [-7, 0, 15, -3, 6], when the program is done the array will contain the values [49, 0, 150, 9, 60] .
The contents of registers $1 and $2 must be the same at the end of your program as they were at the beginning; however their values may change within the body of the program. If the values of registers $1 and $2 are incorrect, then correctness marks for this question will be 0.
Notes:
. Test your solution using the arraydump front end. For more information about how arraydump works, see the solutions for Tutorial 06 .
. You must use as few registers as possible to receive full marks. The exact number required for full marks will not be published; this is something you need to judge for yourself.
. You should not avoid using registers by temporarily saving values to memory.
Memory access with the lw and sw instructions is very slow compared to accessing data in registers, so it would be counter-productive to save a value to memory rather than use a register.
. You cannot use the stack for this question.
Submit the file a4q1.asm.
2. Credit card numbers include a check digit that can be used to detect errors when someone enters the card number for online purchases. One common method for generating a check digit is the Luhn formula. A description of the algorithm can be found here:
https://en.wikipedia.org/wiki/Luhn_algorithm
Assume that an array has been initialized with non-negative integers, where the array location is in register $1 and the array length is in register $2. The contents of the array represent all the digits of a credit card except the check digit. In other words, each element of the array will be a single digit in the range 0 to 9 inclusive.
Write an assembly program that computes the check digit based on Luhn’s algorithm and places the check digit in register $3. The leftmost digit of the credit card will appear at index position 0 in the array. The rightmost digit of the credit card will appear in the last index position in the array.
Examples:
. The array with values [1, 2, 3, 4] has check digit of 4, since
1 + 2 × 2 + 3 + 4 × 2 = 16. You can find the check digit by taking this sum, multiplying by 9, and looking at the ones digit of the product:
i.e. 16 × 9 = 144, and the remainder when you divide 144 ÷ 10 is 4. . The array with values [5, 4, 3, 2, 1] has a check digit of 5, since 5 × 2 = 10 and the sum of the digits of 10 is 1 + 0 = 1, and
1 + 4 + 2 × 3 + 2 + 1 × 2 = 15, and
15 × 9 = 135, and
the remainder when you divide 135 ÷ 10 is 5.
. The array with values [7, 9, 9, 2, 7, 3, 9, 8, 7, 1] has check digit of 3 (Wikipedia example) .
Test your solution using the array front end.
Submit the file a4q2.asm.
3. Hangman is a simple 2-player game where one player picks a word or phrase and the other player tries to guess it. The word to guess is marked out by underscores, where each underscore represents one letter. The guesser picks one letter at a time. If the letter is in the word then all occurrences of that letter replace the dash(es) at the appropriate place(s). The game continues until either the guesser picks all the letters in the word, or until they reach a predetermined limit of incorrect guesses.
(a) Write an assembly program that reads a word entered by the player. The word is composed of one or more lowercase letters only followed by a new line character. Then read a single, lowercase letter followed by a newline character which is a guess. Print the state of the game after one guess, where if the guessed letter appears in the word then it is printed in its correct spot(s), otherwise a underscore is printed in place of the letter in the word. A newline character should be printed at the end of the state. The ASCII code value of a newline character is the hex number 0xA. For example, if the user enters the word waterloo, then enters the letter o, the following characters will be printed on their own line:
oo
Test your solution using the noargs front end.
Submit the file a4q3a.asm.
(b) Write a more complete version of the game that reads a word entered by the player, and accepts multiple guesses one letter at a time. This continues until either the player guesses all the letters in the original word or until they have made six guesses where the letter guessed is not in the word. You are not required to keep track of which letters have been guessed. After each guess, the state of the game as described in part (a) should appear on its own line. If the player guesses all the letters, then print an uppercase W, followed by a newline character. If the player runs out of guesses, then print an uppercase L, followed by a newline character.
Here are two sample interactions of the game. For clarity, the user input appears in italics and output appears in bold.
Sample 1: Player wins
wow
a
w
ww
o
wow
W
Sample 2: Player loses
wow
a
w
ww
w
ww
x
ww
x
ww
x
ww
x
ww
x
ww
L
Note that the player makes a guess before the first state of the game is printed. Test your solution using the noargs front end.
Submit the file a4q3b.asm.
You will need to save the initial word in memory before you start guessing. Each part of this question is approximately equally weighted.