Load and store instructions have the same requirements for encoding: two registers and a 12-bit immediate. However store instructions (sb, sh, sw) have a dedicated format that is called S-type whereas load instructions use the I-type format which is same as addi instruction.
I don't understand why load and stores don't share the instruction format but stores have a dedicated instruction format only for themselves (S-type).
Instruction format supports the design of bits in an instruction. It contains fields including opcode, operands, and addressing mode. The instruction length is generally preserved in multiples of the character length, which is 8 bits.
The lw instruction loads a 32-bit value from memory into rd. So, it is not only a simple 'address' vs 'value' problem, but also depends on the platform and symbol.
I believe the two formats are different to simplify decoding. Looking at the formats of I and S instructions, you'll see:
I: | imm[11:0] | rs1 | funct3 | rd | opcode |
S: | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
Load's rd
is in the same bit positions of the instruction as in all other instructions that need an rd
(I, R, U and UJ formats all put rd
in the same place). In contrast, Store instructions don't have a destination register, but rather have an address register and a register whose value we're storing, so the bits needed for rd
are instead used as part of an immediate encoding.
If you play with implementing RISC-V yourself (in FPGA, Logisim, or however you choose), you'll see how convenient it is that rd
is always in the same place.
To summarize why S instructions have a unique format: they're the only kind of instructions, in a load/store
architecture like RISC-V, that do not modify a register's contents. The rest of the instructions do modify a register, so need an rd
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With