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.