ISA & Assembly

A byte in memory holds 0x80. Which pair correctly gives the 32-bit register value after loading it with RISC-V lb and with lbu?

  1. Alb gives 0x00000080 (128); lbu gives 0xFFFFFF80 (-128)
  2. Blb gives 0xFFFFFF80 (-128); lbu gives 0x00000080 (128)
  3. CBoth give 0x00000080 (128)
Show answer and explanation

Answer: B. lb gives 0xFFFFFF80 (-128); lbu gives 0x00000080 (128)

lb (load byte) treats the byte as signed and copies its top bit into the upper 24 bits (sign extension). 0x80 has a top bit of 1, so the result is 0xFFFFFF80, which is -128. lbu (load byte unsigned) fills the upper bits with zeros (zero extension), giving 128. When C code reads a signed char versus an unsigned char, the compiler chooses between these two instructions.

  • A. The u means unsigned, so this is reversed.
  • B. Correct. lb sign-extends; lbu zero-extends.
  • C. lb spreads the top bit of 1 upward.
Practice this topic Back to topic

More ISA & Assembly questions

  1. How do RISC-V and x86 differ in their mechanism for conditional branches?
  2. What does the RISC-V instruction "slt t0, a0, a1" do?
  3. When the RISC-V code above finishes, what is the value in t0?
  4. RISC-V conditional branches (B-type) encode the distance as a 13-bit signed byte offset whose lowest bit is always 0 and not stored. Roughly what range can a branch reach directly?
  5. What does the RISC-V instruction "jal ra, func" do?