| CS 354, version A Fall 2005 | Name:___________________ ID:___________________ Login:_____________________ | |
| 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 = _____ / 3 Q3 = _____ / 7 Q4 = _____ / 6 Q5 = _____ / 8 Total = _____ / 36 |
Question 1 (12 points, 3 points each)
Assume that X is an 8-bit value given by 0x9e,
and that Y is an 8-bit value given by 0x37.
Show your work in doing the following two's complement
integer arithmetic operations.
For any operation that causes overflow, write the word
"OVERFLOW" next to the computed answer.
Part A
X + Y
Part B
X - Y
Part C
X * Y (assume a 16-bit product)
Part D
X xor Y (bitwise exclusive or operation)
Question 2 (3 points)
Write the name of the addressing mode used for the
last operand in each of these MAL instructions.
add $8, $9, $10
register
lw $11, 20($12)
base displacement
and $13, $14, 0x00aa00bb
immediate
Question 3 (7 points)
Integer variables X and Y
as well as the pointer to an integer, PX,
are located in memory.
Fill in the blanks within the code as well as the comments,
such that the MAL code fragment
below does the same thing as the C code fragment.
/* C code fragment */
int X, Y;
int *PX;
PX = &X;
if ( *PX < Y ) {
Y = X + Y;
X = X + 1;
}
# MAL code fragment
___ $8, X # $8 is ___________________
sw $8, PX
lw $9, ($8) # $9 is ___________________
lw $10, Y # $10 is ___________________
____ $9, $10, end_if
add $10, $9, $10
___ $10, Y
add $9, $9, 1
___ $9, X
end_if:
# MAL code fragment
la $8, X # $8 is address of a
sw $8, PX
lw $9, ($8) # $9 is X
lw $10, Y # $10 is Y
bge $9, $10, end_if
add $10, $9, $10
sw $10, Y
add $9, $9, 1
sw $9, X
end_if:
Question 4 (6 points)
The following two MAL code fragments accomplish the
same task.
# code fragment 1
li $8, 0
li $9, 30000000
loop:
beq $8, $9, end_loop
rem $12, $8, 2
beq $12, $0, else
lw $13, X
add $13, $13, $8
sw $13, X
b next_iteration
else:
lw $14, Y
sub $14, $14, $8
sw $14, Y
next_iteration:
add $8, $8, 1
b loop
end_loop:
# code fragment 2
li $8, 0
li $9, 30000000
lw $13, X
lw $14, Y
loop:
beq $8, $9, end_loop
rem $12, $8, 2
beq $12, $0, else
add $13, $13, $8
b next_iteration
else:
sub $14, $14, $8
next_iteration:
add $8, $8, 1
b loop
end_loop:
sw $13, X
sw $14, Y
Part A (2 points)
Which one of the two code fragments is preferable?
The second code fragment is preferable.
Part B (4 points)
(Briefly.) Why is your chosen code fragment preferable?
The second code fragment is preferable because it is more efficient. The load and store are done once, outside the loop.
Question 5 (8 points)
Assume that an array of integers has been declared with
int_array: .word 0:12
Register $8 is presumed to have a legal index of an array element.
Write a MAL code fragment that
places the integer value -3 into the given element of the array.
Include comments to identify the contents of registers.