An important component of running instructions are the registers. As you should know by now, registers are located on the CPU. Registers are very fast memory components. They are much faster than RAM.
This begs the question: why use anything besides registers? Why not have a billion registers and be done with it?
Here's the way to understand memory. If you want more memory, it will be slower to access, but cheaper. As you get more and more registers, you will find it difficult to make them fast, and to keep them as fast as possible will be expensive.
To make it easier to understand registers, we will put them in a black box called a register file. If we didn't do this, there would be a lot of wires to draw, and you would find it very difficult to understand.
We place the 32 integer registers inside a black box. That black box is called a register file.
To design the register file, we'll go through two steps. This will make it easier to follow its construction.
add $r1, $r2, $r3 # R[1] <- R[2] + R[3]What are the individual steps needed to make this happen? To perform the addition, you need the values of registers 2 and 3.
We need to fetch the operands, i.e., the values of register 2 and 3, from the register file. So, what do we need to tell the register file? We need to tell it which registers we want.
There are 32 registers in the register files, so one way to identify the registers is by a bitstring. Recall that labeling N items requires ceil( lg ( N ) ) bits. In this case N is 32. Thus, to label 32 registers, you need ceil( lg( 32 ) ) = 5 bits. We identify the registers using 5 bit UB. Therefore, 00000 represents register 0, and 11111 represents register 31.
We want to access two registers, so we need 10 bits of input. So, the register file should have SRC 1addr, which is a 5 bit input "address" to indicate the first source register, and it should have SRC 2addr, which is another 5 bit input, to indicate the "index" or "address" of the second source register.
In our example, those 10 bits would be 00010 and 00011, which indicate registers 2 and 3, respectively.
But where should the bits come from?
We know, by now, that a CPU contains some hidden registers. In particular, there's a register called IR (short for instruction register). That register stores the instruction currently being executed.
If we're executing the instruction, add $r1, $r2, $r3, we can access the bits for the register directly from the instruction itself. In particular, $r2 is stored in bits B25-21 (which is the rs register) and $r3 is stored in bits B20-16 (which is the rt register).
Here's the encoding for add $r1, $r2, $r3,
| Opcode | Register s | Register t | Register d | Shift | Function |
| 000 000 | 00010 | 00011 | 00001 | 00000 | 100 000 |