Homework 1
Exercises from the textbook
33 points total
Exercise 3.1
5 points total
4 points for adding the comments (half a point per meaningful comment)
1 point for describing what the code does
begin: addi $t0, $zero, 0 # Initialize $t0 to 0
addi $t1, $zero, 1 # Initialize $t1 to 1
loop: slt $t2, $a0, $t1 # If $a0 is less than $t1, then set $t2 to 1
# Otherwise set $t2 to 0
bne $t2, $zero, finish # If $t2 is not equal to 0 (i.e. $a0 was less
# than $t1), then branch to the instruction
# labelled finish
add $t0, $t0, $t1 # Increment $t0 by $t1
addi $t1, $t1, 2 # Increment $t1 by 2
j loop # Jump to the instruction labelled loop
finish: add $v0, $t0, $zero # Set $v0 to $t0
This code computes the sum of the odd numbers between 1 and the value in $a0
(which is the same as taking the value in $a0, dividing by 2, rounding up and
taking the square).
Grader's comments:
It is perfectly acceptable to write the line numbers and add comments to these,
rather than write the code out again.
Exercise 3.2
6 points total for the sentence describing what the code does
2 point for mentioning the mode
2 point for mentioning the frequency of the mode
2 points for mentioning the registers the above two values are returned in
This code returns the mode (most frequent value) of the array in $v1 and the
frequency of the mode (how many times the mode occurs in the array) in $v0.
Grader's comment
It was hard enough to work out that the mode was being computed, let alone
that if there's more than one mode in the array, the last one is returned.
So you didn't lose points if you didn't state this.
Exercise 3.4
2 points total
1 point for the correct instruction
1 point for the correct operands
addi $t0, $t1, 100
Grader's comment:
You didn't lose any points for not using $t0 and $t1, which were explicitly
stated in the question. However you will lose points on future homeworks if
you do not abide by given assumptions.
Exercise 3.5
8 points total
2 points for loading the base address into a register
2 points for loading x[11] into a register
1 point for the add instruction
2 points for storing the sum into x[10]
1 point for comments
Assume the array x holds 32 bit integers i.e. words. (You did not need to
state this in order to receive full credit).
lui $t1, 61 # 61 decimal = 0000 0000 0011 1101 binary
addi $t1, $t1, 2304 # 2304 decimal = 0000 1001 0000 0000 binary
# Put the base address (4000000 decimal) in $t1
lw $t2, 44($t1) # Load x[11] into $t2. The offset is 44 = 11 * 4 because
# the array contains 32 bit integers
add $t2, $t2, $t0 # Add c (held in $t0) to $t2, storing the result in $t2
sw $t2, 40($t1) # Store $t2 in x[10]. The offset is 40 = 10 * 4
Grader's comments:
Loading the base address of the array requires two instructions. 4000000 does
not fit in 16 bits and you are not meant to use pseudoinstructions.
In the absence of indications that the value x[11] is going to be reused, we
overwrite $t2 instead of using another register to hold the value of the sum.
It is possible to load the address of an element of the array (say x[10])
rather than the base address of the array. The offsets 44 and 40 have to be
changed accordingly.
Exercise 3.7
12 points total
1/2 point per number, 24 numbers total
op rs rt blah
35 4 3 0
8 2 2 1
43 5 3 0
8 4 4 1
8 5 5 1
5 0 3 -6 OR 5 3 0 -6
blah is the immediate/offset field. All instructions are I-type.
Grader's comment:
Branch offsets are number of instructions from the instruction following the
branch.