yeolyi.comSeongyeol's CS

Machine Code

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?

Numbering the jobs

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

0110Job number
RegisterR1
RegisterR2

A register and a number · Put 3 into R0

0011Job number
RegisterR0
Number3

A memory address · Fetch memory slot 5

0101Job number
Memory addressslot 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.

Job numberWhere · what
Kinduses an immediate
Jobload value
RegisterR0
Number3

01100011: put 3 into R0 (LDI R0, 3)

  1. Change the bits in the register and number cells. Where to put it and what to put change.
  2. Change the bits of the job number. No job has that number yet, so the machine can’t understand it.

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.

A machine that reads 8-bit instructions

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.

Change CLK to 1 and back to 0, four times over. R0~R3 become 3, 2, 1, and 3.

Wrapping up

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.

Challenge

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?