ISA & Assembly

What does the RISC-V instruction "lw t0, 8(sp)" do?

  1. AWrites the 4 bytes of t0 to the address sp + 8
  2. BReads 4 bytes from the address sp + 8 and puts them in t0
  3. CReads 4 bytes from the address sp × 8 and puts them in t0
Show answer and explanation

Answer: B. Reads 4 bytes from the address sp + 8 and puts them in t0

lw stands for load word and reads 32 bits (4 bytes) from memory. "8(sp)" means the address formed by adding 8 to sp as a base; this way of specifying an address is called base-plus-offset (register indirect with displacement). It is convenient for reaching stack variables or struct members at a fixed distance from a base. The opposite, writing a register to memory, is sw (store word).

  • A. Writing is done with the sw instruction.
  • B. Correct. It loads from base register plus offset.
  • C. The offset is added, not multiplied.
Practice this topic Back to topic

More ISA & Assembly questions

  1. In the RISC-V conditional branch "beq a0, a1, label", how is the target address of label encoded?
  2. What is an advantage of using PC-relative addressing for branches and jumps?
  3. Which statement about instruction length in the RISC-V base ISA and in x86 is correct?
  4. What is the main advantage of making instructions a fixed length?
  5. In RISC-V instructions, each register field (rd, rs1, rs2) is 5 bits wide. Why?