Memory Instructions

In this section, we will briefly describe the necessary hardware and its interconnection in order to realise load and store instructions.

Which instructions?

In this section, we will implement two new instructions:

  • lw (load word)
  • sw (store word)

Here, lw is of the already known I-type format:

In contrast, sw uses the new S-type format:

What do they do?

These two new instructions access the data memory: lw fetches a word from memory and stores it in a register, and sw stores the value of a register in memory. In both cases, the memory address is calculated from the value of the first source register plus an immediate offset. So for example

lw x2, 40(x6)

will load the memory entry at the address formed by the value of register x6 plus the constant 40, and store it in register x2. In the same way

sw x4, -12(x3)

will store the value of register x4 at the address formed by the value of register x3 minus 12.

Note that the S-type format has been introduced in order to keep the source and destination register indices always at the same positions within the instruction word. As a downside, the position of the immediate value is different in the sw instruction, a fact that the decoding needs to take care of.

What do we need?

For both instructions, we need to perform the same address computation, adding a constant offset to a register value. This should sound familiar to you: It's actually the same operation performed by the addi instruction. So we can reuse the data path of the immediate instructions. However, the decode unit needs to take care of the different positions of the immediate field.

Secondly, we now have encountered the first instruction that does not write its result into a register: sw writes into the data memory. So instead of setting the write signal of the register file to constant one, the decode unit will produce a new output named write that indicates wether the instruction updates the destination register.

Finally, we need to integrate the data memory. In particular, we need to be able to choose the value to be written to the register file: Either the result of the ALU (for all R-type and I-type instructions), or the output of the data memory (for the lw instruction). The data to be written to the memory by the sw instruciton can be just taken from the register file outputs. Furthermore, the decoder needs to tell the data memory wether to read or write (load or store, respectively).

The data path

The figure below shows the new components and connections:

For the memory address, we reuse the ALU output (immediate addition). The second register output is wired to the write data input of the memory. The read data output of the memory is fed into a new multiplexer, which selects the result to be written back to the register file: The ALU output or the data read from memory. For this purpose, the decode unit procuces two new output signals: load indicates that we are loading data from memory (selecting the write back input), store indicates that we are storing data in the the memory (used as write signal for the data memory).