S09 Exam 1 retake answers - Missouri State University

NAME: ________________________________
Exam 1 retake
MSU CSC 285
Spring, 2009
(70 pts.)
1. The sll instruction shifts the bits of a register to the left by a specified immediate
(constant) number of bit positions, and the srl instruction is similar. For this question,
use the MIPS Reference Data (“green sheet”) shown on p. 2 of this exam.
1a. (5 pts.) Show the decimal values of the fields of the instruction below. Use Fig.
A.6.1 shown at the top of the next page..
sll $s1, $t2, 9
rd
rt
0
6 bit
rs ignored
0
5 bit
rt is $t2
10
5 bit
rd is $s1
17
5 bit
shamt
9
5 bit
0
6 bit
1b. (5 pts.) Show the 32-bit hexadecimal form of that instruction.
decimal
Binary
Bits
regrouped
for hex
Hex
0
000000
0
00000
0000
0
0
10
01010
0000
0000
0
1010
A
17
10001
1000
8
9
01001
1010
0100
A
0
000000
0000
4
0
Page 1
This chart is from the “green sheet” MIPS Reference Data of the COD textbook.
2. (10 pts.) Suppose MIPS assembly code is used to implement a timing loop (that is, a
loop whose only purpose is to pause while time passes). Suppose every MIPS instruction
takes 1 millisecond to execute, and you want to pause for 1000 milliseconds.
Write MIPS code that pauses for as-close-as-you-can-get to1000 milliseconds. Do not
assume any register has any useful value already in it. State how many instructions are
executed by your code.
(Many answers are possible.)
addi $s0, $zero, 499
# Initialize counter. This is needed
# because “Do not assume any register has
# any useful value already in it.”
top: addi $s0, $s0, -1
bne $s0, $zero, top
# Decrement counter
# if (timer > 0) repeat
This example executes 1 + (499 * 2) = 999 times
Page 2
3. (20 pts.) The following is valid, tested MIPS assembly code. Draw a diagram of the
contents of MIPS memory in the neighborhoods of array and the stack after the execution
of the code. Include addresses of that memory (as best as you can with the information
given) and the “top” and “bottom” of the stack.
.data
array: .word 1, 2, 3, 4, 5, 6
.text
la
$s0, array
addi $s1, $zero, 6
loopA:
lw
$t1, 0($s0)
addi $sp, $sp, -4
sw
$t1, 0($sp)
addi $s0, $s0, 4
addi $s1, $s1, -1
bne $s1, $zero, loopA
0x10010000
(original $sp) - 18
...
(original $sp) - 8
(original $sp) - 4
1
2
...
6
6
...
2
1
Top of stack
Bottom of stack
4. (5 pts.) Suppose a MIPS subroutine calls another MIPS subroutine. Explain why the
second call cannot use the instruction jal $ra, and the solution to that problem.
Register $ra contains the address of the “calling execution point,” to which the $pc
register should be restored to continue execution at that point. Every call to jal $ra
replaces the value in register $ra. A second call to jal $ra overwrites the first call’s
value, and the problem is that execution cannot resume at that point (since the point is
unknown). The solution is to use a stack to contain many values of return addresses.
Page 3
5. (25 pts. total) After the execution of line 2 below, the value of $t0 is 0x10010000 and
the value of $t1 is 0x10010028.
.data
array_a:
array_b:
.word 0x1111, 0x2222, 0x3333, 0x4444, 0x5555,
0x6666, 0x7777, 0x8888, 0x9999, 0xaaaa
.word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 # Ten words
.text
la
la
top:
$t0, array_a
$t1, array_b
addi $t3, $zero, 5
# Line 3
lw $t6, 0($t0)
# Line 4
sw $t6, 0($t1)
# Line 5
addi $t0, $t0, 8
# Line 6
addi $t1, $t1, 4
# Line 7
addi $t3, $t3, -1
# Line 8
bottom: bne $t3, $zero, top
5a. (10 pts.) After the execution of line
2, show the contents of the initialized
portion of the Data Segment of memory,
and at least one address of this data. You
may use ellipses “…” as long the
meaning is clear. (Do not show the
initialized portion of the Text Segment,
which is the program itself.)
0x10010000
0x10010028
# Line 1
# Line 2
1111
2222
....
aaaa
0
0
…
0
# Line 9
5b. (15 pts.) Complete all rows of a chart with
the values of registers at each iteration of the
loop, at the point just after the execution of line
9.
$t3
$t6
$t0
4
1111
0x10010008
3
3333
0x10010010
2
5555
0x10010018
1
7777
0x10010020
0
9999
0x10010028
Page 4