5.4 Reading the Instructions Stored in Memory

In Section 5.3 we said that an instruction is an agreed-upon binary number placed in memory. In this section, let’s lay out actual memory contents and learn how to read the binary numbers in memory as instructions. Once we can read instructions here, we’ll be able to follow, without getting stuck, how the CPU executes them one by one in the next section.

First, let’s settle two things in advance. One: to keep the explanation simple, we’ll assume a computer that handles both data and instructions in 16-bit (2-byte) units (8 bits are called 1 byte). Two: for convenience of notation, addresses are written in hexadecimal and the values stored in memory in binary.

Figure 5-6 below shows memory prepared with the values (data) to be used in a calculation and the instructions that say what to do with them. Looking only at what’s stored in memory, it’s just a sequence of binary numbers, but in this example we’ve placed data starting at 0x0000 and instructions starting at 0x1000.

Memory with the data 1, 5, 2, 3 at addresses 0x0000 to 0x0006 and the instructions LOAD, LOAD, ADD, STORE at 0x1000 to 0x1006, each stored as a 16-bit binary number
Figure 5-6. Data and instructions prepared in memory

The addresses go up by 2: 0x0000, 0x0002, 0x0004, and so on. Memory has an address for every byte and can be accessed one byte at a time, but here we treat 2 bytes (16 bits) as one unit. So the first box spans two addresses, 0x0000 and 0x0001, and the next box starts at the following address, 0x0002. Each box is a 16-bit space; you can think of it as 16 of the 1-bit DRAM cells we saw in Section 4.8 put together.

Let’s read the data side first. What sits in this region is data, so we read it as numeric values. Converting the 16-bit binary number in each box to decimal, we can think of 0x0000 as holding 1, 0x0002 5, 0x0004 2, and 0x0006 3. Of these, 1 and 5 will be the two values added in the next section — the operands.

Next is the instruction side, starting at address 0x1000. As we said in Section 5.3, from memory’s point of view these are just 16-bit binary numbers, exactly like the data before them. But whereas data reads straight away as familiar numbers, instructions have to be interpreted according to the agreement. Let’s start with the structure. Figure 5-7 below shows the structure of a 16-bit instruction.

A 16-bit instruction divided into four 4-bit fields: OPCODE (the instruction type) and rs, rt, rd (the numbers of the registers to use)
Figure 5-7. Instruction format

First, the 16 bits are divided into four fields of 4 bits each. The OPCODE at the front corresponds to the “what to do” from Section 5.3, and the remaining rs, rt, and rd correspond to “with which values.” However, what’s written in rs, rt, and rd isn’t the values themselves but the numbers of the registers to use. Depending on the instruction, a value may be read from that register or a result stored in it. Since there are only a few registers (Section 4.8), referring to them by number rather than by name is enough, and the circuit picks out the right register using this number, like the MUX from Section 4.6.

For example, if we split the instruction 0010 0001 0010 0100 at 0x1004 in Figure 5-6 according to this structure, the OPCODE is 0010, rs is 0001, rt is 0010, and rd is 0100. The 0001 in the rs field points to register 1, the 0010 in the rt field to register 2, and the 0100 in the rd field to register 4. In other words, this instruction says to do whatever OPCODE 0010 means, using registers 1, 2, and 4. What that job is, and how each of the three registers is used, is set by the instruction definitions below.

Now that we know the structure, we need the agreement that says what the value in each field means — the instruction definitions. Table 5-1 below defines three instructions (in reality there are many more).

InstructionOPCODEWhat it doesrsrtrd
LOAD0000Fetch a value from memory into a register — store the memory value at the address held in register rs into register rtNumber of the register holding the memory address of the value to fetchNumber of the register to store the fetched value in(unused)
STORE0001Write a register’s value to memory — store the value of register rt in memory at the address held in register rsNumber of the register holding the memory address to store toNumber of the register holding the value to store in memory(unused)
ADD0010Add the values of two registers — add the values of registers rs and rt and store the result in register rdNumber of the register holding a value to addNumber of the register holding the other value to addNumber of the register to store the result in
Table 5-1. Instruction definitions by OPCODE (an example agreement)

As Table 5-1 shows, instructions are distinguished by their OPCODE, and how the values in the rs, rt, and rd registers are used differs from one instruction to another. Now let’s read the instruction at 0x1004 that we split up earlier. Interpreting it, OPCODE 0010 is ADD in Table 5-1 — addition. According to Table 5-1, for ADD, rs and rt are the registers holding the two values to add and rd is the register to store the result in, so this instruction means: take the value in register 1 (0001) and the value in register 2 (0010), add them, and store the result in register 4 (0100).

Let’s read the instruction at 0x1000, 0000 0000 0001 0000, the same way. Splitting it by the structure, the OPCODE is 0000, rs is 0000 (register 0), rt is 0001 (register 1), and rd is 0000. OPCODE 0000 is LOAD, and for LOAD, rs is the register holding the memory address of the value to fetch and rt is the register to store the fetched value in. So this instruction means: use the value in register 0 as an address, read the value from memory there, and store it in register 1. LOAD doesn’t use rd, so the 0000 in the rd field goes unused. For STORE too, as with LOAD, the value in the rs register is the memory address to access.

The instruction format and codes shown here resemble real ones, but they are an example made up for this book to aid understanding. Real CPU instructions are the same in that they carry both “what to do” and “with which values,” but how many bits are used, how they’re divided, and which codes are used differ from CPU to CPU. We’ll look at real CPU instructions in Chapter 6.

In this way, data and instructions sit together in memory as the same kind of binary numbers, but depending on how they’re used, they’re read either as values or as instructions. Now we can split an instruction according to the agreement, read it, and work out what job it asks for and which registers it uses. In the next section, let’s follow the CPU as it executes, in order, the four instructions placed starting at 0x1000.

Leave a question or comment

Questions, corrections, or thoughts about this section are welcome. Your first comment will appear after review. (Privacy Policy)