solution - UCSB Computer Science

advertisement
CS64 Spring 2011
NAME__________________________
Midterm Exam
PERM__________________________
Midterm
LAST NAME
FIRST NAME
PERM Number
n
2n
n
2n
Please turn off all pagers, cell phones and
1
2
17
131072
beepers. Remove all hats & headphones.
2
4
18
262144
Place your backpacks, laptops and jackets
3
8
19
524288
at the front. Sit in every other seat. Nothing
4
5
6
16
32
64
20
21
22
1048576
2097152
4194304
7
8
128
256
23
24
8388608
16777216
9
512
25
33554432
10
1024
26
67108864
11
12
13
2048
4096
8192
27
28
29
134217728
268435456
536870912
14
15
16
16384
32768
65536
30
31
32
1073741824
2147483648
4294967296
Instructions
•
may be placed in the “no fly zone” spare
seat/desk between students.
•
You have 75 minutes to complete this
exam.
•
This exam is closed book, closed notes,
no computers, PDAs or calculators.
•
WRITE YOUR NAME on EACH PAGE OF
THIS TEST. You will be deducted -1 points
you can not finish this task.
CS64 Spring 2011
NAME__________________________
Midterm Exam
PERM__________________________
Important – please note: The MIPS instructions shown in this table are
the ones that you must use on the entire exam. Do not use any
instructions that are not in this table. If you use any instructions not
listed below, you will lose points.
Name
Syntax
Meaning
add
add rd, rs, rt
rd=rs+rt
sub
sub rd, rs, rt
rd=rs-rt
and
and rd, rs, rt
rd=rs AND rt
or
or rd, rs, rt
rd=rs OR rt
sll
sll rd, rt, shamt
rd=logic shift rt left shamt bits
srl
srl rd, rt, shamt
rd=logic shift rt right shamt bits
sra
sra rd, rt, shamt
rd=arithmetic shift rt right shamt bits
slt
slt rd, rs, rt
If rs<rt, rd=1, else rd=0
slti
slti rd, rs, imm
If rs<imm, rd=1, else rd=0
jr
jr rs
jump to the instruction held by the memory
location indicated by rs
addi
addi rt, rs, imm
rt=rs+imm
andi
andi rt, rs, imm
rt=rs AND imm
ori
ori rt, rs, imm
rt=rs OR imm
lw
lw rt, imm(rd)
rt=MEMORY[rd+imm]
sw
sw rt, imm(rd)
MEMORY[rd+imm]=rt
beq
beq rs, rt, label
Branch if rs==rt
bne
bne rs, rt, label
Branch if rs != rt
j
j label
Jump to label
jal
jal label
Jump to label and link
CS64 Spring 2011
NAME__________________________
Midterm Exam
PERM__________________________
1. [5pt] Big Ideas
We’ve discussed two design principles that guide the authors of instruction sets (and
played a part in MIPS design). Choose the corresponding item from below and map it
to the right design principle (one answer per principle)
Design Principle
Simple is faster
How was the MIPS design affected
a
Smaller is faster
c
a) Each MIPS native instruction performs one function and requires one cycle to
run
b) Each MIPS native instruction requires multiple cycles to run
c) MIPS has 32 registers, rather than many more
d) MIPS instructions only work with hexadecimal numbers
2. [5pt] Number representation, assuming a 8-bit system
d
Consider the following bit pattern
11111111
What is the (decimal) value of this bit pattern assuming that it is in two’s
complement format
a)
0
b)
-255
c)
255
d)
-1
e) Can’t tell since this representation has the property of a dirty zero
3. [15pt] Write the value of each of these binary integer numbers in DECIMAL
(assuming 8-bit system)
(in sign magnitude format)
10000000
_________0______________
(in one’s complement format) 10000000
_________-127_____________
(in two’s complement format) 10000000
_________-128______________
CS64 Spring 2011
Midterm Exam
NAME__________________________
PERM__________________________
(in one’s complement format) 10000001
_______-126_______________
(in two’s complement format) 10000001
________-127_______________
4. [5pt] Number representation, assuming a 32-bit system
What is the smallest two’s complement format number that can be stored in one 32bit word? Express your answer in both decimal (base 10) and hex (base 16) format.
-2^31
0x80000000
5. [20pt] MIPS Arithmetic and Logic Instructions
Assume that
Register $a0 contains
0x00000FFF
Register $a1 contains
0x10001011
Register $a2 contains
0x00FFFFFF
at the beginning of each of the following instructions. For each instruction, give the
contents of the destination register in hex format
a) and $s0, $a0, $a1
0x00000011
b) add $s1, $a0, $a1
0x10002010
c) addi $s2, $a1, 0xFFFF
0x10001010 (note: oxFFFF is sign
extended to oxFFFFFFFF)
d) or $s3, $a1, $a2
0x10FFFFFF
CS64 Spring 2011
NAME__________________________
Midterm Exam
PERM__________________________
6. [20pt] Assembly Programming
Decoding Assembly Code: The following piece of MIPS code tries to reverse the
contents of an array of words (base address of the array in register $a0, length of the
array in register $a1). Fill in the blanks.
reverse: add
$t0, $zero, $a0
add
$t1, $zero, $a1
addi
$t1, $t1, -1
loop:
# t1 = a1 - 1
sll
$t1, $t1, _2__
# Now t1= t1 x 4
add
$t1, $a0, $t1
lw
$t2, 0($t0)
# First element of array pointed to by $t0
lw
$t3, 0($t1)
# Last element of the array pointed to by $t1
sw
_$t2___, 0($t1)
sw
_$t3___, 0($t0)
addi
$t1, $t1, _-4__
addi
$t0, $t0, _4__
bgt
$t1, $t0, loop
# $t1 points to the end of the array
# Loads first - then do the stores
# Update first and last element addresses
# Go on till $t0 >= $t1
CS64 Spring 2011
NAME__________________________
Midterm Exam
PERM__________________________
7. [25pt] Implementing RightRotate in MIPS
Imagine that there is a MIPS instruction:
sllv rd, rt, rs
that causes the bits in register rt to be left-shifted by the amount indicated in
register rs, and the result is put into register rd. Likewise, imagine that there is a
similar instruction called "srlv", i.e. shift to the right by the amount indicated in
register rs. For srlv, when shifting to the right, the content of rt is zero-extended.
Using these new variable shift instructions, write a MIPS FUNCTION called
"RightRotate" by filling the blanks. It accepts two arguments:
•
•
$a0 contains the bit string to be rotated.
$a1 contains the number of bits by which to rotate the string. (Assume that
$a1 contains a positive integer between 0 and 31: 0 <= $a1 <= 31.)
The bits that are rotated out of the number are again inserted to the beginning of
the results. For example, rotating to the right by a single bit is shown in the
diagram below:
In this case, the last “0” was inserted to the beginning of the result.
Your function should adhere to all function conventions and return properly with the
rotated result in the proper register.
RightRotate:
srlv
$t0, $a0, $a1
# shift to the right by the amount defined by $a1.
addi
$t1, $0, __32___
sub
$t1, $t1, $a1
sllv
$v0, $a0, _$t1___
or
__$v0__, _$v0___, _$t0__ # combine two results
__jr__
___$ra___
# shift to the left by some amount
together
# function returns
CS64 Spring 2011
Midterm Exam
NAME__________________________
PERM__________________________
8.[5pt] Assembly Programming – Instructions to perform divisions
Assume that we want to divide the number contained in register $s0 by 16 and save
the result in register $s1. Choose the right instruction(s) to do that ( Select all that
apply)
a) addi $t0, $zero, 16
a) & d) div
$s0, $t0
mflo $s1
b) sll
$s1, $s0, 4
c) sra
$s1, $s0, 2
d) sra
$s1, $s0, 4
e) none of the above
Download