Download COMP 411 20-min Sakai Quiz #2 1) What is the hex

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP 411 20-min Sakai Quiz #2 1) What is the hex representation of the following instruction (opcode for ori = 001101)? ori $v0, $zero, 9 a) 0x35000949 b) 0x34020009 c) 0x20490009 d) 0x00110109
Explain:
The ori instruction is an I-type. An I-type looks like:
By knowing that the opcode is at the front and the immediate constant is at the end, you can eliminate all
choices except b.
2) What is the hex representation of the following instruction (func for add =100000)? add $t1, $t2, $t3 a) 0x014B4820 b) 0x21490009 c) 0x11010203 d) 0x80010203
Explain:
The add instruction is an R-type. An R-type looks like:
Since add uses a func, the opcode is 000000. Because the opcode is at the front, you can eliminate all
choices except a.
3) What is the hex representation of the following instruction (opcode for j = 000010)? j 12 a) 0x0000100C b) 0x08000012 c) 0x08000016 d) 0x0800000C
Explain:
The j instruction is a J-type. A J-type looks like:
Since the opcode is already given, we just pick the choice that has the opcode and the hex representation
of 12. The answer is d.
4) Which of the following CANNOT be compiled as a single real MIPS instruction (not
psuedoinstruction): a) addu $s0, $s1, $s2 b) mul $t2, $t3, $t4 c) lw $t2, -3($t4) d) la $t2, 3($t4)
Explain:
The mul instruction is a pseudoinstruction because you can represent it using several basic
instructions. The mult and the multu instructions put the result into MIPS registers hi and lo. The results
may be moved out of these registers using mfhi and mflo. The three operand pseudoinstruction mul d,s,t
uses those basic instructions. It is used when the product of registers $s and $t is a value that fits into the
32 bits of lo. For example:
mul $v2, $s3, $t0
is equivalent to
mult $s3, $t0
mflo $v2
All the other choices are not pseudoinstructions so the answer is b.
5) In the MIPS instruction set, why is there a "sub" instruction but not a "subi" instruction? a) addi can read signed value thus you can add with a negative value b) subtracting in MIPS isn't very useful to start with c) sub instruction can already take in constants because it is an I-type d) Boolean logic instructions such as ori can be used to perform subtraction Explain:
The addi instruction is an I-type and the immediate can be negative. The sub instruction is an R-type so
there is no dedicated operand for immediates. You could use Boolean logic instructions as an alternate for
subtraction but that would be too complicated. The best answer is a.
6) Choose the best statement below regarding these three MIPS instructions: lw $t2, x($0) lb $t3, y($0) lui $t4, z a) The value of x, y and z must all be multiples of 4 b) Only x and y (and not z) must be a multiple of 4 c) Only x (and neither y nor z) must be a multiple of 4 d) None of them are required to be a multiple of 4
Explain:
A word is 4 bytes, thus x must be a multiple of 4 in order to load the words from memory. Bytes can be
loaded from any address because they are the smallest unit you can load from memory, thus y doesn’t
have to be a multiple of 4. The lui instruction takes in an immediate so z can have any value. Thus the
answer is c.
7) Suppose you execute the following instruction sequence: addi $t0, $0, -1 sll $t1, $t0, 16 srl $t2, $t0, 16 What is the value of $t2 after execution in hex? a) 0xffff0000 b) 0x0000ffff c) 0xffffffff d) 0x00000000
Explain:
After the first line, $t0 has a value of 0xffffffff. After the second line, $t1 has a value of
0xffff0000 while $t0 keeps its value. After the third line, you take the value of $t0 and shift it right by 16,
resulting to the value 0x0000ffff. This result is stored in $t2. Thus the answer is b.
8) Lets say we want make a "doub rA, rB" instruction that doubles the value in rA and stores in rB.
Which of the following instructions is its equivalent? a) sll rB, rA, 1 b) srl rB, rA, 1 c) sra rB, rA, 1 d) add rA, rA, rB
Explain:
So this is something to note! If you are given a binary number and shift it left by 1, the resulting
value is the original * 2. If you shift it left by 1, you are dividing by 2. For example:
00001010
//original val = 1*23 + 1*21 = 10
00010100
//after shift, val = 1*24 + 1*22 = 20
For every shift left, you multiply by 2 and for every shift right, you divide by 2. Try this yourself to
understand the concept behind it.
9) If you want the instruction lw $t1, x($t2) to be compiled as a single real MIPS instruction, what range
of values must "x" lie within? a) 0...15 b) 0...215-1 c) 0...216-1 d) -215...215-1 Explain:
The lw instruction is an I-type which has an immediate that is 16 bits long. Since the immediate
can be signed, the lowest number is -215. Since we must also include nonnegative numbers (the positives
and 0), the highest number is 215-1.
10) Let's say we are implementing a stack in MIPS. What does the following piece of code do? addi $sp, $sp, -4 sw $r3, 0($sp) a) pops value from the stack and stores into $r3 b) push value of $r3 into stack c) push the value -4 into the stack d) pop the value -4 from the stack and store into $r3
Explain:
The stack pointer ($sp) points at the top of the stack. By moving the address by 4 and storing a
value into memory, you are pushing into the stack. The stack pointer doesn’t carry the value you push into
or pop from the stack. It keeps track of the address in memory of what is considered the top. The answer
is b.
11) Why is the register $sp important? a) it describes the size of the overall stack b) it is intermediate register that carries the resulting value of pop action c) it is the stack pointer and it keeps track of what is the next top of the stack d) it is the intermediate register that carries the inputting value of a push action Explain:
Like the given explanation in question 10, the stack pointer ($sp) keeps track of the address of the
top of the stack. For every push or pop, you would need to take into consideration what the address of the
new top be. The answer is c.