ECE 3035 1:00p.m.
Computing Mechanisms
Spring 2007
5 problems, 6 pages
Final Exam Solutions
3 May 2007
1
Problem 1
(1 part,
2 points)
Compiling MIPS
Part A
(20 points) Write an efficient MIPS program fragment that is equivalent to the C program
fragment below.
Place the output variable Sum in register $4 and use $3 for
i
. Use temporary registers as
needed.
int Array[] = {10, 20, 30, 40, 50, 60, 70};
int i, Sum = 0;
for (i = 0; i < 7; i++) {
if ((i % 2) == 0)
continue;
Sum =+ Array[i];
}
Sum = Sum + 100;
Assume Array has been declared as a label in the
.data
section as follows:
Array:
.word
10, 20, 30, 40, 50, 60, 70
label
data/instruction
comment
add $4, $0, $0
# clear Sum
add $3, $0, $0
# clear i
Loop:
slti $5, $3, 7
# is i < 7?
beq $5, $0, End
# if not, exit loop
andi $5, $3, 1
# check last bit of i
beq $5, $0, Continue
# if it’s 0 then continue
sll $5, $3, 2
# scale i by 4
lw
$5, Array($5)
# Lookup Array[i]
add $4, $4, $5
# Sum =+ Array[i]
Continue:
addi $3, $3, 1
# i++
j Loop
# loop back
End:
addi $4, $4, 100
# Sum = Sum + 100
Part B
(10 points)
Write an efficient MIPS program fragment that is equivalent to the C program
fragment below. Assume
X
is in register $1 and
Y
is in register $2. Use temporary registers as needed.
if (X < Y)
X = Y - X;
else
Y = X - Y;
label
instruction
comment
slt $5, $1, $2
# is x < y?
beq $5, $0, Else
# if not, jump to Else
sub $1, $2, $1
# X = Y – X
j
End
# exit if-then-else
Else:
sub $2, $1, $2
# Y = X – Y
End: