Why store instructions have their own format
Asked Answered
A

1

6

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).

Antetype answered 9/1, 2020 at 16:31 Comment(1)
Does this answer your question? Why are RISC-V S-B and U-J instruction types encoded in this way?Encroach
P
6

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.

Pampero answered 9/1, 2020 at 18:20 Comment(1)
Note that many other load/store architectures have a flags / condition-code register (e.g. AArch64, PowerPC), and typically have instructions like cmp and tst which don't write any GPRs, only flags. So the key part of that sentence is "like RISC-V". The most well-known such ISAs are RISC-V and MIPS, but there's also HP-PA RISC where there's no FLAGS and conditional branch instructions also include a compare.Thora

© 2022 - 2024 — McMap. All rights reserved.