Courses & Projects by Rob Marano

Assignment 3

<5 points>

Homework Pointing Scheme

Total points Explanation
0 Not handed in
1 Handed in late
2 Handed in on time, not every problem fully worked through and clearly identifying the solution
3 Handed in on time, each problem answered a boxed answer, each problems answered with a clearly worked through solution, and less than majority of problems answered correctly
4 Handed in on time, majority of problems answered correctly, each solution boxed clearly, and each problem fully worked through
5 Handed in on time, every problem answered correctly, every solution boxed clearly, and every problem fully worked through.

Reading

Chapter 2, sections 2.7-2.10, 2.12-2.14, 2.21, 2.22 of our textbook Read 2.20 if GPUs or parallel computing is of interest to you.

Problem Set

§ means “section,” and §§ means “sections”

These are tailored to test you understanding of loops, memory addresses, and the stack.


1: The Multiplier (Arithmetic & Logic)

Task: Write a program that takes two integers from the user, multiplies the first by 8 using a shift instruction (not mul), adds the second integer to that result, and stores the final sum in a memory location labeled final_val.

2: The Negative Counter (Control Flow)

Task: Given an array of signed integers, write a loop that counts how many numbers in the array are negative (less than 0). Store the final count in register $s0.

3: The Leaf Procedure (Basic Functions)

Task: Write a function calc_perimeter that takes two arguments (length and width in $a0 and $a1$) and returns the perimeter of a rectangle (2 x (length + width)).

4: The Power Function (Procedures & Stack)

Task: Create a procedure called square_plus_one. It should take one argument (n), calculate n^2 + 1, and return the result.

5: The Nested Procedure (Functions calling Functions)

Task: Write a procedure average_of_squares.

  1. It takes two arguments (a, b).
  2. It calls a second procedure, square, for each argument.
  3. It calculates the average of those two squares: (a^2 + b^2)/2.

6: The Recursive Procedure (Self-Calling)

Task: Implement the Factorial function (n!).

Checklist: “The Debugging Lab”

Use this checklist when you get “stuck” on the exercises:

  1. Infinite Loop? Check if you forgot to increment your iterator (e.g., addi $t0, $t0, 1).
  2. Wrong Memory Value? Did you remember that each .word is 4 bytes? (Your offset must be i * 4 because MIPS is byte-addressable).
  3. Register Overwrite? If your main values change after calling a function, check if you saved your $s registers on the stack.
  4. Crash on jr $ra? Ensure you restored $ra from the stack if your procedure called another procedure (nested calls).