| CS 354, version A
Fall 2007 | 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 = _____ / 10 Q2 = _____ / 10 Q3 = _____ / 20 Total = _____ / 40 |
Question 1 (10 points total)
An array of integers is declared with
array: .word 0:40
Part A (4 points)
How many elements does this array hold?
la $8, array
register $8 has the (hexadecimal) value 0x004000c0.
At what (hexadecimal) address will the 31st element be?
This is the element at index 30, where the first
element of the array has index 0.
Question 2 (10 points, plus a bonus of 4 points, to
those students with correct implementations that add the fewest number
of instructions)
Write MIPS assembly language code that completes the following
code fragment.
The code fragment writes out elements of an array of integers
such that
arr[0] = 0 arr[1] = 1 arr[index] = arr[index-1] + arr[index-2]Here is a diagram of the contents of a portion of this array after the code fragment executes.
0 1 2 3 4 5 6 7 8 (index) --------------------------------------------------------- | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | . . . ---------------------------------------------------------Make sure you provide a comment identifying the role of each register you use in the loop. It is fine to change any register value.
la $8, arr # base address of the array
li $9, 0 # set arr[0] and arr[1]
sw $9, 0($8)
li $9, 1
sw $9, 4($8)
li $10, 2 # index of first array element to set
li $11, 26 # index of last array element to set
add $12, $8, 8 # address of arr[2] in register $12
# (not all correct code fragments will use this value)
loop:
add $10, $10, 1 # increment element index for next iteration
b loop
Question 3 (20 points, 10 points each part) The following function is supposed to follow all the same MIPS register usage and parameter passing conventions that we have used this semester. It does not do this correctly.
# swap
# Using pointers to integers, swap the 2 integers
# function swap receieves 2 parameters (via the stack)
# the first parameter is a pointer to one of the integers
# the second parameter is a pointer to the other of the integers
swap: # prologue
sub $sp, $sp, 12 # allocate AR
sw $ra, 12($sp) # save $ra
sw $8, 4($sp) # save callee saved registers
sw $9, 8($sp)
lw $8, 8($sp) # $8 is pointer (first parameter)
lw $9, 4($sp) # $9 is pointer (second parameter)
lw $10, ($8) # follow pointer to load $10, first int
lw $11, ($9) # follow pointer to load $11, second int
sw $11, ($8) # store second int at first int's address
sw $10, ($9) # store first int at second int's address
# epilogue
lw $8, 4($sp) # restore callee saved registers
lw $9, 8($sp)
lw $ra, 12($sp) # restore $ra
add $sp, $sp, 12 # deallocate AR
jr $ra
Part A
On the code, identify and correct the items that do not
follow the conventions.
Add instructions, where necessary, to correctly implement
the register usage and parameter passing conventions.