Practice

RISC-V and digital logic exercises

Each exercise poses a concrete problem. Type your answer and press “Check”: the result is computed in your browser by the same code the tools use, and the solution explains the reasoning. “Open in the tool” loads the same example so you can explore it.

RISC-V

All you need is paper and the format table from the RISC-V path.

Decode an instruction with a negative immediate

Convert 0xFFF28293 to binary and split it into fields. Give the format, the opcode, the registers, funct3 and the decimal value of the immediate.

0xFFF28293

Open in the tool

Decode an R-type instruction

Identify the fields of 0x40B50533. Pay attention to funct7: it is what tells add and sub apart.

0x40B50533

Open in the tool

Identify the format from the opcode

Look only at the low 7 bits of each word (the last hex digit and a half) and say which format it belongs to.

Open in the tool

Trace the registers

Run the program by hand. What are t2 and a0 at the end?

li   t0, 7
li   t1, 3
sub  t2, t0, t1
slli t2, t2, 2
xor  a0, t2, t0
Open in the tool

Follow a loop

What is a0 when the loop exits? How many instructions run in total (counting every iteration)?

li   a0, 1
li   t0, 4
loop:
  add  a0, a0, a0
  addi t0, t0, -1
  bnez t0, loop
Open in the tool

Count the cycles of a load-use hazard

In the simulator’s 5-stage pipeline, how many cycles does this sequence take with and without forwarding? How many bubbles are there with forwarding?

lw   t0, 0(a0)
add  t1, t0, t0
sub  t2, t1, a1
Open in the tool

Your first program

Two instructions load constants and the third adds them. What value ends up in a0?

addi t0, zero, 5
addi t1, zero, 7
add  a0, t0, t1
Open in the tool

An unconditional jump changes the PC

What is a0 at the end and how many instructions run?

li   a0, 1
j    skip
li   a0, 2
skip:
  addi a0, a0, 10
Open in the tool

Two’s complement overflow

lui loads 0x80000 into the upper 20 bits of t0. What are t1 and t2 in decimal?

lui  t0, 0x80000
addi t1, t0, -1
slt  t2, t1, t0
Open in the tool

Call a function with jal and return with ret

The triple routine takes a number in a0 and returns the result in a0. What are a0 and ra at the end?

li   a0, 6
jal  ra, triple
j    end
triple:
  add  a1, a0, a0
  add  a0, a1, a0
  ret
end:
  nop
Open in the tool

Reorder to remove the bubble

In the original order (lw, add, addi, sw) there is a load-use bubble and the sequence takes 9 cycles with forwarding. Here addi has been moved between lw and add. How many cycles does it take now with forwarding, and how many bubbles remain?

lw   t0, 0(a0)
addi t2, a1, 4
add  t1, t0, t0
sw   t1, 0(a2)
Open in the tool

The penalty of a branch

The simulator resolves branches in EX and treats them as taken. How many cycles does the sequence take with forwarding and how many instructions are discarded?

addi t0, t0, -1
bne  t0, zero, loop
addi a0, a0, 1
addi a1, a1, 1
Open in the tool

Digital logic

Answers are compared with the full truth table, so any equivalent minimal expression is accepted. Review the digital logic path.

Place the minterms on the map

Mark the cells of F(A,B,C,D) = Σm(3, 6, 11, 12, 14) on the 4-variable map. Rows are AB and columns are CD, both in Gray code.

AB \ CD00011110
00
01
11
10
Open in the tool

Find the adjacent cells

On a 4-variable map, which cells are adjacent to m8? Mark all of them: remember that opposite edges also touch.

AB \ CD00011110
00
01
11
10
Open in the tool

Minimize using don’t cares

Write a minimal sum of products for F(A,B,C,D) = Σm(1, 3, 7, 11, 15) + d(0, 2, 5). Use A, B, C, D and an apostrophe for NOT (for example A'B).

Syntax: products side by side (AB), sum with +, NOT with an apostrophe (A') or a leading !.

Open in the tool

Check someone else’s solution

A classmate proposes F = A'B' + BC' + AB for F(A,B,C) = Σm(0, 1, 2, 5, 6, 7). Is it correct? If not, mark a minterm where it fails.

F = A'B' + BC' + AB

Is the expression correct?
Open in the tool