ISA & Assembly

After running the RISC-V code above, what is the value in register a2?

addi a0, zero, 5
addi a1, a0, 3
add  a2, a0, a1
  1. A8
  2. B13
  3. C15
Show answer and explanation

Answer: B. 13

Line 1 adds 5 to zero (always 0), giving a0 = 5; line 2 adds 3 to a0, giving a1 = 8; line 3 adds a0 and a1, giving a2 = 5 + 8 = 13. RISC-V arithmetic is written "op destination, source1, source2 (or immediate)", so the first register is where the result goes. zero is an alias of x0 and is often used to build constants.

  • A. 8 is the value of a1.
  • B. Correct. 5 + 8 = 13.
  • C. Recheck a1: it is 5 + 3 = 8.
Practice this topic Back to topic

More ISA & Assembly questions

  1. What does the RISC-V instruction "lw t0, 8(sp)" do?
  2. In the RISC-V conditional branch "beq a0, a1, label", how is the target address of label encoded?
  3. What is an advantage of using PC-relative addressing for branches and jumps?
  4. Which statement about instruction length in the RISC-V base ISA and in x86 is correct?
  5. What is the main advantage of making instructions a fixed length?