| CS 354, version A ANSWER KEY Fall 2003 | 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 Q 1 = _____ / 6 Q 2 = _____ / 8 Q 3 = _____ / 12 Q 4 = _____ / 10 Q 5 = _____ / 10 Total = _____ / 46 |
Question 1 (6 points)
(A) Identify a program error that the simulator catches at execution time.
While a program is executing, the simulator may catch
(B) Identify a program error that the simulator catches at assembly time.
While a program is being assembled, the simulator may catch
(C) Why would we choose to use a stack instead of a queue in a program?
We choose to use a stack instead of a queue because of the
ordering in which data is generated and used.
A stack gives us last-in-first-out, where a queue gives us
first-in-first-out.
Question 2 (8 points)
Give a TAL synthesis for each of the MAL instructions.
Assume that variable X has been assigned
address 0xabcdef00 and contains the value 0xfedcba00.
sw $8, X rem $8, $9, $10
lui $1, 0xabcd div $9, $10
ori $1, $1, 0xef00 mfhi $8
sw $8, 0($1)
li $12, -1 add $12, $15, 3
addi $12, $0, -1 addi $12, $15, 3
Question 3 (12 points total) Consider this MAL program fragment:
.
.
.
move $a0, $t3
jal X
li $t1, -20
beq $t0, $t1, print_error_msg
.
.
.
X: sub $sp, $sp, 28
sw $ra, 28($sp)
sw $s0, 20($sp)
sw $s1, 24($sp)
sw $a0, 32($sp)
li $s0, -50
li $a0, -1
jal Y
lw $a0, 32($sp)
addi $s1, $s0, 100
sw $s1, ($a0)
lw $s0, 20($sp)
lw $s1, 24($sp)
lw $ra, 28($sp)
add $sp, $sp, 28
jr $ra
(A) (6 points)
List all cases where this program fragment does NOT
follow the MIPS procedure call, register usage, and parameter passing conventions.
There are 2 cases where this code fragement does not follow the conventions.
X.
Therefore, it must be saved/restored.
X, but are not properly
saved/restored.
(B) (6 points)
In the space to the right of the code,
show modifications of the code for only procedure X such that
procedure X does follow the conventions.
Question 4 (10 points total)
(A) (6 points) Give machine code (in binary, and in hexadecimal)
for the TAL assembly language instruction
bltz $10, loop
Assume that the branch instruction is to be placed at
address 0x88110024, and the label loop is assigned
address 0x88110000.
The machine code format for bltz:
0000 01ss sss0 0000 iiii iiii iiii iiii
$s is $10, 01010
The immediate field is computed from
byte offset = target addr - ( 4 + bltz addr )
0x 0x88110000 - ( 4 + 0x88110024 )
order this operation to get a positive result, then find the
additive inverse in 2's complement representation.
0x88110028
- 0x88110000
-------------
0x00000028, which in binary is 0000 0000 0010 1000
take the 2's complement:
0000 0000 0010 1000
1111 1111 1101 0111 + 1 = 1111 1111 1101 1000
Eliminate the least significant 2 zeros (because we needed
a word offset, not a byte offset), AND sign extend to form
a 16-bit value.
0000 0101 0100 0000 iiii iiii iiii iiii
becomes
0000 0101 0100 0000 1111 1111 1111 0110
0x 0 5 4 0 f f f 6
(B) (4 points) What addressing modes are used for (each of) the operands
in the machine code version of the branch instruction?
Question 5 (10 points)
(A) (4 points) A 2-dimensional array of integers is stored in row major order.
Assume that row and column indices start at 0.
Assume the following MAL declarations,
and write an equation for the address
of element [Y, X].
.data
array: .word 0:200
num_rows: .word 20
num_cols: .word 10
addr of [Y,X] = base address + size of an element *
( num_cols * Y + X )
= base address + 4 ( 10 * Y + X )
(B) (6 points) Write a MAL code fragment that places the integer value
33
into this array
at location [Y, X], where the
value for Y is in $8,
and the value for X is in $9.
Assume values for these variables
have been set at some earlier point within the program.
The code fragment will NOT check for indices out
of range.
Use comments to identify the contents of registers within
the code fragment.
Choose registers without regard of the MIPS conventions for register
usage.
# addr of [Y,X] = base address + size of an element * # ( num_cols * Y + X ) la $10, array # $10 is the base address lw $11, num_cols # $11 is num_cols mul $12, $11, $8 # $12 will be the address of the word change add $12, $12, $9 mul $12, $12, 4 # multiply by the size of an element in bytes add $12, $12, $10 # add in the base address of array li $13, 33 # $13 is the value to be placed at [Y,X] sw $13, 0($12)