Only putting preset values into registers can’t do anything useful. We need to add two numbers in registers with an adder and put the result back into a register, or fetch numbers from memory and write results back to memory. A machine that reads instructions from memory in order and drives various parts like this is called a CPU (Central Processing Unit).
So how should we extend our instructions?
The jobs a CPU does, like loading a value, adding, or fetching from memory, can be fixed to a handful, so let’s give each job a number and write it in the instruction.
Let’s start by numbering the “put a value into a register” job from the instructions post. The CPU in this series writes one instruction in 8 bits, and the first 4 bits and the last 4 bits have different roles.
How the last 4 bits are read can differ by job. They can hold two registers, a register and a number, or a memory address.
Two registers · Add R1 and R2
A register and a number · Put 3 into R0
A memory address · Fetch memory slot 5
So let’s give jobs whose last 4 bits are read the same way the same first 2 bits. We’ll call these first 2 bits the job’s kind. Then the first 2 bits alone tell how to read the last 4 bits, which keeps the circuit that decodes instructions simple.
A number written right inside the instruction, like the number in a load, is
called an immediate. Let’s make jobs that use an immediate kind 01, and
give loading a value 0110 within it.
01100011: put 3 into R0 (LDI R0, 3)
A number that chooses the job, like the first 4 bits, is called the opcode, and the part that says where and what, like the last 4 bits, is called the operand. A list of instructions written in bits like this is a machine code program.
Bits alone are hard to read, so each job gets a short name that’s easy to
remember, a mnemonic. The mnemonic for loading a value is LDI (Load
Immediate), and 01100011 is written as LDI R0, 3. The two are two ways of
writing the same instruction, and the bits the CPU reads directly are the
machine code.
Let’s extend the machine from the instructions post to 8-bit instructions. Since LDI is the only job, Write is always on, and the job number (OP) isn’t connected.
An instruction is a number given to a job, plus its targets. But the machine doesn’t look at the job number yet, so whatever we write, it loads a value. From the next post, let’s add jobs one at a time.
01100011 means 'put 3 into R0' (LDI) and is also the number 99. If we treat it as a number, add 1, and read it as an instruction again, what does it mean?