Home/Instructions/Load Byte
LB

RISC-V LB Instruction Details

Instruction ManualI-type

Load an 8-bit byte from memory, sign-extend it to XLEN, and write rd

Instruction Syntax

lb rd, offset(rs1)
Operand Breakdown
Destination rd: general-purpose register receiving the result.
Source rs1: register holding the first operand.
Immediate imm: 12-bit signed value, sign-extended before operation with rs1.
RV32IRV64IMemory Load

Instruction Behavior

LB is an RV32I/RV64I I-type load (opcode=0000011, funct3=000). Its effective address is rs1 plus a sign-extended 12-bit offset; it reads 8 bits there, sign-extends the value to XLEN, and writes rd. A byte access is naturally aligned at every byte address; the EEI defines legal addresses and other access restrictions.

LB Decode And Execute Animation

Starts with machine-code field decoding, then shows operand reads, instruction-specific execution, and ISA-visible state update.

rd
imm
rs1
lb
,
(
)
Execution Context

This example uses fixed initial register values and PC=0x2000; instruction fetch succeeds, with no asynchronous interrupt or extra extension checks. Exception branches stop ordinary commit; post-trap PC and handler state are EEI-dependent.

Assumes readable ordinary memory, with the displayed sample value at each selected effective address. Only misaligned EEI outcomes are modeled, not permissions or device side effects. Byte accesses are always naturally aligned; successful loads advance PC by 4, while exceptions stop ordinary writeback.

a0(x10)
0x00001000
mem[0x00001000]
0x000000F2
31..20
19..15
14..12
11..7
6..0
000000000000
imm[11:0]
01010
rs1
000
funct3
00101
rd
0000011
opcode
Execution Data Path
instruction
0x00050283
opcode
0000011 -> LOAD
funct3
000 -> LB
rd / base
t0(x5) / a0(x10)=0x00001000
offset
000000000000 -> 0; ea=0x00001000 (naturally aligned (1-byte))
load
Not computed
x5
t0(x5) = 0x00003001; pc = 0x00002000
Current Step

Concept Step: receive the 32-bit instruction encoding

The machine code is split by the current instruction format; the animation starts from encoding/decode.

encoding: 0x00050283
syntax : lb x5, 0(x10)
Pending result: Not computed
Current architectural state
t0(x5) = 0x00003001; pc = 0x00002000

This animation shows ISA-visible decode and state changes, not any specific CPU pipeline, cache, prediction, or timing implementation.

Quick Understanding & Search Notes

LB reads an 8-bit byte from the effective address formed by rs1 plus a signed 12-bit offset, then sign-extends the value into rd.

All base loads form an effective byte address as rs1 plus a sign-extended 12-bit offset.
A byte access is naturally aligned at every byte address; this does not guarantee a legal address or suppress other access exceptions.
With rd=x0, the loaded value is discarded, but access exceptions and other side effects still apply.

Common Usage Scenarios

Byte data loads

Understand this scenario with real code like «lb x5, 0(x10) # x5 = sign_ext(mem[x10+0][7:0])».

Sign extension

Understand this scenario with real code like «lb x5, 0(x10) # x5 = sign_ext(mem[x10+0][7:0])».

Pre-Use Checklist

Syntax Check
  • Use rd, offset(rs1); offset is a signed byte displacement from -2048 through 2047.
  • rd and rs1 select integer registers x0 through x31 or their ABI names.
Semantic Check
  • LB/LH sign-extend the loaded 8/16-bit value to XLEN.
  • Byte accesses are always naturally aligned, but EEI address/access restrictions still apply; rd=x0 does not suppress exceptions or other side effects.

Pitfalls / Common Confusions

Sign extension: if byte MSB=1, upper bits filled with 1
Byte accesses are naturally aligned, but rd=x0 does not suppress access exceptions or other side effects.

FAQ

What offset range does LB use?

The sign-extended 12-bit byte offset ranges from -2048 through 2047.

What is the difference between signed and unsigned loads?

LB/LH/LW sign-extend the loaded value to XLEN; LBU/LHU zero-extend. On RV64, use LWU when a 32-bit word should be zero-extended.