3.2

This code returns the word appearing most frequently in the array in $v1 and its multiplicity in $v0.

3.13

Let IC be the instruction count, then the total number of cycles on the unmodified machine is

(0.48*1.0 + 0.33*1.4 + 0.17*1.7 + 0.02*1.2)*IC = 1.255*IC

If 25% of the data transfer instructions are changed, then the total number of cycles on the modified machine is

((0.48 - 0.33*0.25)*1.0 + 0.33*1.4 + 0.17*1.7 + 0.02*1.2)*IC = 1.1725*IC

If the cycle time of the unmodified machine is C, then the total time on the unmodified machine is 1.255*IC*C, the total time on the modified machine is 1.1725*IC*1.1*C = 1.28975*IC*C. The unmodified machine is

1 - 1.28975/1.255 = 2.77% faster.

4.13

The problem is that lw sign extends the offset A_lower. So,

A_upper_adjusted = A_upper + bit 15 of A (the "sign" bit of A_lower)

4.17

Let A and B be the numbers (unsigned) in $t3 and $t4 respectively. If there is a carry out, then A+B >= 2^n. So, if we do

    addu $t2, $t3, $t4

The number in $t2 is A+B-2^n. Since both A and B are less than 2^n, we conclude that $t2<$t3 and $t2<$t4. If there is no carry out, we have $t2>=$t3 and $t2>=$t4. Thus, we have

    addu $t2, $t3, $t4
    sltu $t2, $t2, $t3

alternatively, 1's complement is the largest (unsigned) number which doesn't cause a carry out when it is added to the original number

    nor  $t2, $t3, $t3   #1's complement
    sltu $t2, $t2, $t4

4.23

Fig.4.4 on page 222 shows that if overflow and OldSet = 1, then A >= 0 and B < 0 which means A < B is false; if overflow and OldSet = 0, then A < 0 and B >= 0 which means A < B is true. Thus we have the following truth table:


        OldSet  overflow        NewSet
        1       0               1
        0       0               0
        0       1               1
        1       1               0

That is

        NewSet = OldSet XOR overflow