Fetch, Decode, and Execute in a Simple Processor

A basic processor reads an instruction from an address, interprets its fields, and performs the operation specified by those fields.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a stored instruction
  2. Keep program position explicit
  3. Keep program bytes and operand effects on separate lines
  4. Test control-flow and invalid-opcode outcomes too

Trace a stored instruction

If the current instruction selects the addition operation and a source register containing 4, its execution can add that value to a destination holding 3. The observed result is 7. An instruction field layout determines which operand register is named.

Keep program position explicit

Ordinary sequential execution advances to the next instruction address. A branch can select another position. Follow program-counter updates when tracing code with decisions.[1]

Keep program bytes and operand effects on separate lines

In a toy machine with one-word instructions, let the word at program address five encode adding three to R2. If R2 held six, execution writes nine to R2 and normally advances the program counter to six. Fetching, decoding and arithmetic answer different questions. See toy instruction fields.

Fetch instruction at PC=5
Decode opcode ADD, destination R2, immediate 3
Execute: R2=6+3=9
Next PC=6

Test control-flow and invalid-opcode outcomes too

A branch can choose a next program counter other than the sequential one. Some instruction encodings are reserved and require a defined fault. Keep instruction length in the model when calculating the next address.

References

  1. Noam Nisan and Shimon Schocken, From Nand to Tetris: Course Projects ↩