Reading — step 1 of 5
Read
~1 min readInstruction Decoding
Loads & Stores
Memory access instructions:
Loads (I-type):
- LB rd, offset(rs1): byte, sign-extend.
- LBU rd, offset(rs1): byte, zero-extend.
- LH rd, offset(rs1): halfword, sign-extend.
- LHU rd, offset(rs1): halfword, zero-extend.
- LW rd, offset(rs1): word.
Stores (S-type):
- SB rs2, offset(rs1): byte.
- SH rs2, offset(rs1): halfword.
- SW rs2, offset(rs1): word.
Address calculation: addr = rs1 + sign_extend(offset).
Implementation:
python
Storage:
python
Endianness: RISC-V is little-endian by default. Byte 0 is LSB.
Common patterns:
- Pointer dereference: LW with offset 0.
- Array access: ADDI to compute pointer + LW.
- Struct field: LW with offset = field's position in struct.
Function prologue uses stores:
addi sp, sp, -16 # allocate stack frame
sw ra, 12(sp) # save return address
sw s0, 8(sp) # save callee-saved
addi s0, sp, 16 # set frame pointer
Epilogue uses loads:
lw s0, 8(sp)
lw ra, 12(sp)
addi sp, sp, 16
ret
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…