Our processor is fairly simple. It consists of a set of 4 general
purpose registers. There are two more registers that are special purpose,
the Instruction Pointer register and the Status Register. We'll call
these IP and S, respectively. That means we have our 4 registers, which
we'll call R1, R2, R3, and R4, the IP and the S registers.
Remembering that the processor is just a set of circuits toggled on and
off, let's look at how we might arrange a set of instructions. The first
four bytes might be the "prefix" and all instructions have it. It decides
if we're going to use the arithmetic circuts, or a logical operation, or a
jump operation. We might decide on the following:
+---------+------------------------------------+
| Field | Meaning |
+---------+------------------------------------+
| - - - | Jump to a specific memory address |
| - - X | Compare |
| X X - | Add |
| - X X | Subtract |
| X X X | Multiply |
| X - X | Divide |
| X - - | Input Output |
| - X - | Load / Shift |
+---------+------------------------------------+
Simple Instruction Set - The instruction type field
The first three bits of the instruction tell the processor to switch on a
different circuit depending on the instruction. For example, if all three
bits are off, then the processor will load the contents of a register into
the instruction pointer, causing the program to jump to a new instruction.
(The instruction pointer contains the location in memory where the next
instruction resides. If we load a new value into the instruction pointer,
it will start fetching instructions for that location.
For example, if we're executing an ADD instruction, we need to be able to
say from what register we're reading the numbers to add and where we're
writing the result. We decide to use three groups of four bits to
indcate the two source registers and the destination register.
+---------------------------------------------------------------------+
+ | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| 13| 14| 15| 16|
+---------------------------------------------------------------------+
| ADD | X | X | - | - | X | - | - | - | - | X | - | X | - | - | - | - |
+---------------------------------------------------------------------+
Add the contents of R3 to R2 and put the result in R4
Bits 1, 2, and 3 tell us it's an add instruction. Bits 4, 5, 6, and 7,
indicate the first source register is 3. Bits 8, 9, 10, and 11, indicate
the second source is register 2. The bits 12, 13, 14, and 15 indicate the
destination is R4. The 16th bit is ignored. Inside the CPU the ADD
circuit is turned on. The lines connecting R2 and R3 as inputs are
turned on. The lines connecting R4 as the output are turned on. The
addition circuit runs.
A different instruction might have a different layout. For example, the
LOAD instruction loads a number into a register.
For this instruction the first three bits indicates is a load or a shift
instruction. The next bit indicates if it is actually a load or a shift.
The next four bits indicate the register. The last eight bits are the
value to load or how many times to shift. Again, the CPU is bascially
turning some circuits on or off depending on the values.
The Jump instructions operate on the Instruction Pointer register, causing
the program to execute instructions stored in a different location in
memory. In addition, some instructions may take input from the status
register. For example, a Compare instruction may set the Zero flag on the
status register. Most computers have a Jump if Zero to execute a jump
only if the last instruction set the zero flag. That allows the programmer
to compare a pair of values and jump to a new location if they are equal.
In this example, all the instructions were the same length, 16 bits. But
some processors, such as the Intel, have variable length instructions.
For now, though, we're going to build our mental model using this simple
CPU and its instructions are all the same length.