| CS 354, version A Spring 2009 | Name:___________________ ID:___________________ | |
| Exam 2 | ||
|
No electronic devices may be used while taking this exam.
Examples of devices not allowed are calculators, pagers, cell phones,
wrist calculators/computers, laptop computers, pocket computers.
Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes.
The notes may be on both sides of the paper.
Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used. |
Exam Score Q1 = _____ / 12 Q2 = _____ / 18 Q3 = _____ / 6 Q4 = _____ / 10 Total = _____ / 46 |
Question 1 (12 points)
Write a MIPS assembly language code fragment that
implements the following C for loop.
Assume that all variables are integers.
Further assume that the variables and constants are to reside in the following
registers. The code fragment must initialize register values.
$8 contains variable i
$9 contains variable result
$10 is to contain the constant 23
$11 is to contain the constant 3
result = 1;
for ( i = 5; i < 23; i++ ) {
result = result * (i + 3);
}
Question 2 (18 points total)
The following MAL code fragment is written to initialize an array
of 30 integer elements defined by:
array[0] = 0 array[1] = 1 array[i] = array[i-1] + array[i-2]Here is a diagram of what the contents of a portion of this array are expected to be after the code fragment executes.
0 1 2 3 4 5 6 7 8 (index) --------------------------------------------------------- | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | . . . ---------------------------------------------------------
.data
array: .word 0:30
.text
la $8, array # $8 is the address of array[i]
sw $0, ($8) # set array[0]
li $9, 1 # $9 is the constant value 1
sw $9, 1($8) # set array[1]
add $8, $8, 2 # $8 still has the address of an array element,
# now it is the address of array[2]
li $10, 30 # $10 is a loop counter, 30 down to 1
loop:
beq $10, $9, end_loop
sub $11, $8, 1 # $11 has address of array[i-1]
lw $12, ($11) # $12 has the value of array[i-1]
sub $13, $8, 2 # $13 has address of array[i-2]
lw $14, ($13) # $14 has the value of array[i-2]
add $15, $12, $14 # $15 has (array[i-1] + array[i-2])
sw $15, ($8) # put sum into array[i]
add $8, $8, 1 # update array[i] address for next iteration
sub $10, $10, 1 # update loop counter
b loop
end_loop:
Question 2, Part A (5 points)
Unfortunately, this code does not work correctly.
It gets a runtime error.
Identify which instruction causes the runtime error by
drawing an arrow on the code fragment
that points to the instruction that causes the error.
Question 2, Part B (4 points)
(Approximately) What error message will the simulator print?
Question 2, Part C (9 points)
Assume that all the comments correctly indicate what is
expected to happen in the code.
On the code fragment,
show any and all changes that need to be made to fix the code.
Question 3 (6 points)
1.
2.
3.
For a function that returns a value,
list 3 things that the epilogue of the function must do.
Since there are more than 3,
only the first 3 you list will be considered as your answer.
Question 4 (10 points total)
Function A requires
an activation record of
12
words.
Part A (3 points)
Write the MIPS instruction that allocates A's
activation record space.
Part B (3 points)
A copy of A's return address needs to be placed
into this activation record
as part of A's prologue.
Write the MIPS instruction that follows the conventions and places
the return address into A's activation record space.
Part C (4 points)
Assume that A receives
5
parameters from its parent (caller), and that A calls
B and passes 2 parameters to B.
Given that this code follows the same
register usage and parameter passing conventions used by 354 this semester,
how many registers out of the set $8-$25 does A use?
Show how you calculated this value.