=========================preview======================
(COMP180)midterm-01-sol.pdf
Back to COMP180 Login to download
======================================================
Q1.
(a)
li $s0,4000 lw $t1,44($s0) add $t1, $t1,$t0 sw $t1, 40($s0)
(b)
80000 is too large to represent with 16 bits (the length of the immediate .eld in a sw instruc-tion)
(1c) 2 instructions are suf.cient
addui $t1, $t0, 65535
sw $a0, 14465($t1)
Q2. The code detrmies the most frequent word in an array and returns it $v1 and its multiplicity in $v0
Q3. Solution:
(a)
A B Out0 Out1 Out2 Out3
0 0 1 0 0 0
0 1 0 1 0 0
1 0 0 0 1 0
1 1 0 0 0 1
This circuit is a 2:4 decoder (a decoder is suf.cient)
(b)
(c)
a b Cin Cout Sum
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
Sum = (a . b . Cin) + (a . b . Cin) + (a . b . Cin) + (a . b . Cin) Cout = (a . b . Cin) + (a . b . Cin) + (a . b . Cin) + (a . b . Cin) which can be simpli.ed into the following
Cout = (b . Cin) + (a . Cin) + (a . b)
(d)
Cin
(e)
Result = S1 . S0 . D0 + S1 . S0 . D1 + S1 . S0 . D2
S0 S1 D0 D1 D2 Result
0 0 1 X X 1
1 0 X 1 X 1
0 1 X X 1 1
1 1 X X X X
(f)
Q4. Solution:
(a) CPI for P = 2*0.4 + 3*0.3 + 4*0.2 + 5*0.1 = 3.0
CPI for P = 2*0.4 + 2*0.3 + 3*0.2 + 4*0.1 = 2.4
(b)
Therefore P is faster than P by the ratio 50/33.3 = 1.5 or about 50%
(c)
CPI of P with new compiler = 2*0.5 + 3*0.35 + 4*0.1 + 5*0.05 = 2.7
(d)
with the new compiler, P is faster by
37.04/33.3 = 1.112 or about 11%
Q5 addu $t2, $t3, $t4 sltu $t2, $t2, $t3
Q6.
(a) In both cases
step1: the address of the following instruction (i.e., 00004116 respect. 00004212) is stored in the
return address register $ra ($31 is also considered correct)
step 2: The PC is loaded with the address of the instruction labelled term, respectively fact (i.e.,
00004200 respect. 00004440).
NB: if answered by: the execution jumps to ... it should also be considered correct
(b)
$ra = 00004116
(c)
$ra = 00004212
(d)
$ra = 00004212
(e)
$pc = 00004212
Yes there is a problem. The program has an in.nite loop in procedure term. This occured
because the .rst return address has been wiped by the second return address (the action of the
.rst jal in question a.step1 has been erased by the action of the second jal).
(f)
register $ra needs to be pushed (saved) on the stack at the beginning of the procedures and
poped (restored) just before the instruction jr $ra.
NB: if they do not mention the stack yet mention save and restore it is also correct.