Skip to content
Tactiverge

The heartbeat of a processor

14Fetch, decode, execute

  1. Stage 1
  2. Stage 2
  3. Stage 3
  4. Stage 4
  5. Stage 5
  6. Stage 6
  7. Stage 7
  8. Stage 8
  9. Stage 9
  10. Stage 10
  11. Stage 11
  12. Stage 12
  13. Stage 13
  14. Stage 14 (this one)
  15. Stage 15

Explanation

A processor repeats one loop forever. Fetch: send the program counter to memory and copy the instruction into the instruction register, then add one to the PC. Decode: raise the control signals for that opcode. Execute: move data, use the ALU, or change the PC.

Bench-8 does this in conceptual clock cycles: four to fetch the two bytes, one to decode, then up to three to execute. Step through one cycle at a time and watch the bus carry each value.

Program (Bench-8 assembly)

Assembles: 13 bytes. Loaded.

Instruction set (16 instructions)
opcodedoescycles
NOP00—. Do nothing for one instruction.5
LDI n10A ← n. Load the number n itself into A.6
LDA a20A ← M[a]. Load the byte stored at address a into A.7
STA a30M[a] ← A. Store A into memory at address a.7
ADD a40A ← A + M[a]. Add the byte at a to A (8-bit wraparound, C = carry out).8
SUB a50A ← A − M[a]. Subtract the byte at a from A (C = 1 means a borrow: A was smaller).8
AND a60A ← A AND M[a]. Bitwise AND with the byte at a.8
OR a70A ← A OR M[a]. Bitwise OR with the byte at a.8
XOR a80A ← A XOR M[a]. Bitwise XOR with the byte at a.8
NOT90A ← NOT A. Flip every bit of A.6
JMP aA0PC ← a. Jump: the next instruction comes from address a.6
JZ aB0if Z: PC ← a. Jump to a if the last result was zero.6
JC aC0if C: PC ← a. Jump to a if the last ADD carried (or the last SUB borrowed).6
IND0A ← input. Read the next number from the input tape (0 if the tape is empty).6
OUTE0output ← A. Write A to the output display.6
HALTF0stop. Stop the clock.6
bus idlePC0MAR0MemoryM[0] = 16IR00 NOPOperand0Control / decoderreadyA (accumulator)0B latch0ALUidleFlagszcnvINnext 3OUT–FETCHDECODEEXECUTEcycle 0 · instructions 0
  • no signals yet: press Step

Next: F1 MAR ← PC.

Machine state as text

Cycle 0, 0 instructions completed. Next step: F1 (fetch) MAR ← PC. PC 0, IR 0 (NOP), operand 0, MAR 0. A 0, B 0. Flags Z 0 C 0 N 0 V 0. Output: nothing yet.

Output

–

Instructions

0

Cycles

0

CPI

–

Listing and breakpoints

Assembled program; press a row's button to toggle a breakpoint
bpaddrbytessource
0010 05▶ LDI 5
02B0 0Aloop: JZ done ; stop when A is 0
04E0 00OUT
0650 0CSUB one
08A0 02JMP loop
0AF0 00done: HALT
0C01one: DB 1

Memory (256 bytes)

Memory contents in hex; PC and MAR cells are marked
+0123456789ABCDEF
0010 (PC)05B00AE000500CA002F00001000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

blue = PC, lime = MAR. Program and data share this memory.

Cycle-by-cycle trace

cyclestepphasetransferbusAPC

Nothing yet. Step the clock.

Saved programs stay in this browser only (localStorage). Nothing is uploaded.

Check yourself

Marked instantly in your browser. Use the interactive above to work it out.

  1. 1. Bench-8 starts with PC = 0. After fetching one two-byte instruction (no jump), what is the PC?
  2. 2. How many conceptual cycles does Bench-8 take for ADD (4 fetch + 1 decode + 3 execute)?