ECS 50 (Winter 2021) Exam #3 Solutions Changelog
v.1: Initial version.
Remarks
Please don’t ask me questions along the lines of, “How much partial credit will I get if I answered in this way or that way?” You’ll find out once it’s all graded.
There may be — and for some questions, definitely will be — different correct answers besides the ones shown below.
Solutions
Q1
free points
Q2
Wrong answer: 2/6.
The fraction 2/6 is 1/3, a non-terminating decimal.
Correct answer: 0.375
0.375 (base 10) -> 0.011 (base 2) -> 000110 (representation)
Wrong answer: 22.
The base 2 representation of 22 is 10110, which tells us that 3 mantissa bits
(not counting the implicit 1) would be needed. Correct answer: -2.5
-2.5 (base 10) -> -10.1 (base 2) -> 110001 (representation)
Supporting the memory-mapped I/O approach instead of the I/O address space approach means that RISC-V does not need to support a family of instructions similar to the IN/OUT family of instructions that x86-64 supports, helping to keep the number of instructions low.
Q4
1.25:
Q3
Sign: 0
Integer component: 01 Fractional component: 010
-2.375:
Sign: 1
Integer component: 10 Fractional component: 011
Q5
You should have assumed 2’s complement was used, since no serious architecture would use signed magnitude. However, it doesn’t change the answer, and if you did assume signed magnitude, then — depending on your explanation — you could still get full credit on this problem.
If 2’s complement: 24 values need to be supported. 4 bits would only give us 2^4 = 16 bits, but 5 bits would work, since 2^5 = 32.
If signed magnitude: The highest magnitude we would need to represent is 12, and we would need 4 bits for this. The sign would be an additional bit. Thus, 5 bits would work.
Q6
True.
Q7
Below are the steps to left-rotate an n-bit string: (there are multiple correct answers)
1. Use bitwise AND with the mask 1 << (n - 1) to determine if the MSB is set.
2. Left shift the n-bit string. (Note that this will make the LSB become 0.)
3. If the MSB was determined to be 1 in step #1, then use bitwise OR to set the LSB to 1 by OR-ing the shifted bit string with 1.
With enough thought, you can do something like the above in one line of C code and without a conditional statement. If you didn't do this during the exam, then you should try to do it on your own time, as such an exercise is somewhat similar to the kinds of questions you could get asked in a technical interview when the interviewer really wants to make you think.
Q8
It's impossible to tell, because labels are not maintained at the machine code level in MiniCUSP.
Q9
Q9.1
[ ] IF [X] ID
[ ] EX
[ ] MEM [X] WB
Q9.2
Structural hazard.
Q10
foo() would take better advantage of spatial locality of references to data if arr were stored in column-major order. The code is traversing down every other column, finishing one column before it goes on to the next one. If arr were stored in row-major order, the elements within a given column are not adjacent, which means that spatial locality would not be taken advantage of unless nc happened to be small (which we can't assume). Conversely, if arr were stored in column-major order, then the elements within a given column would be adjacent, so there would be many more cache hits in the inner for loop. (The student does not need to mention caches to get full credit on this problem.)
Q11
[ ] The CPU stops doing the instruction cycle entirely until the interrupt is resolved, e.g. by an interrupt service routine (ISR).
- The instruction cycle NEVER stops as long as the computer is on. The computer is always fetching, decoding, and executing something. It's just a question of what that something is.
[X] The CPU takes the address of the instruction that it would have fetched next (if there had been no interrupt) and stores this address somewhere, e.g. in the stack.
- This is what is meant by preserving the PC.
[ ] The CPU finishes whatever program it is currently running and then jumps to the ISR of the device that triggered the interrupt.
[ ] None of the above.
Q12
Below is one solution.
lw s0, a
lw s1, b
addw s0, s0, s1
lw s1, c
lw s2, d
addw s1, s1, s2
blt s1, s0, res1
lui s0, %hi(result)
addi s0, s0, %lo(result) li s1, 0
sw s1, 0(s0)
j end
res1:
lui s0, %hi(result)
addi s0, s0, %lo(result) li s1, 1
sw s1, 0(s0)
end:
Q13
Below is one full solution. The format string variable would need to be added. (You don't need to have provided the .data, .text, or .globl directives.)
.data
...
format_str:
.string "%d\n"
.text
.globl main
main:
li s0, 0
li s1, 4
lui s2, %hi(arr)
addi s2, s2, %lo(arr)
lui a0, %hi(format_str)
addi a0, a0, %lo(format_str) li a1, 0
loop:
bge s0, s1, end
slli t0, s0, 2 add t0, t0, s2 lw t1, 0(t0) add a1, a1, t1 addiw s0, s0, 1 j loop
end:
call printf
Q14
Below is one solution. No additional variables are needed.
li s0, 0
lw s1, arrlen
lui s2, %hi(arr)
addi s2, s2, %lo(arr)
loop:
bge s0, s1, afterLoop slli t0, s0, 2
add t0, t0, s2
lw a2, 0(t0)
slli a2, a2, 1
sw a2, 0(t0)
addiw s0, s0, 1
j loop
Q15
[X] An instruction that pushes the operands' values onto the stack, changing the stack pointer in the process.
- This would involve writing to memory (stack memory) without a store instruction.
[ ] An instruction that triples the operand's value.
[ ] An instruction that rotates the bits in the operand.
[X] An instruction that copies the contents of the memory location referenced by the first second operand to the memory location referenced by the second operand.
[ ] None of the above. (In other words, all of the above instruction could be allowed.)
Q16
x10 and a0 are the same register. Thus, when the program loaded the starting address of the format string into a0, it overwrote my favorite number. An easy fix would be to do the mv instruction before the lui instruction.
Q17
The most obvious example is the fixed-length instructions.
Note that the question is not saying that MiniCUSP would fall more into the RISC categorization than into the CISC one. An example of a CISC-like (or, at least, a non-RISC-like) characteristic of the MiniCUSP assembly language is that MiniCUSP is not a load-store architecture.
Q18
Due to a semi-significant typo, this question has been removed and will be free points.
Copyright Statement
This content is protected and may not be shared, uploaded, or distributed.