---
title: "How a Processor Runs a Program"
book: "Primary & Middle School Computer Science"
subject: computer-science
language: en
chapter: 22
exercises: 12
source: https://one-course.com/books/computer-science/1/en/chapter/22-how-a-processor-runs-a-program
license: CC-BY-NC-SA-4.0
credit: "One Computer Science Book, One Course (one-course.com)"
---

# Chapter 22 — How a Processor Runs a Program

A [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) understands neither Python nor English. It is a chip of silicon that can do a few very simple things, very fast. So how does `3 + 4` become 7 inside it? This chapter opens the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) of a small [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer), the *toy machine* of this series, and follows it step by step.

**You already know.**

The [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) carries out [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction), and the [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) holds the [programs](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) and the data it works on ([Chapter 6](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#ch-g3-inside-the-computer)); [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) is counted in [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) ([Chapter 8](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#ch-g4-memory-and-storage)). A [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) is eight [bits](https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary#def-g5-bits-and-bytes-bit) and holds a whole number from 0 to 255 ([Chapter 11](https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary#ch-g5-bits-and-bytes)). Python [programs](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) are carried out by an [interpreter](https://one-course.com/books/computer-science/1/en/chapter/14-a-first-python-program#def-g6-first-python-program-interpreter) ([Chapter 14](https://one-course.com/books/computer-science/1/en/chapter/14-a-first-python-program#ch-g6-first-python-program)), and a [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) goes back to repeat its block ([Chapter 18](https://one-course.com/books/computer-science/1/en/chapter/18-repeating-with-while#ch-g6-while-loops)).

## 22.1 Instructions are numbers in memory

**Definition 22.1 (Memory cell and memory address).**

The [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) is a long row of *memory cells*, each holding one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte). The cells are numbered from 0; the number of a cell is its *memory address*.

The toy machine has 256 [memory cells](#def-g7-how-a-processor-runs-a-program-cell), with addresses 0 to 255. A real [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) has billions, but they work the same way. The [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) and its data sit side by side in that same [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory): the [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) themselves are numbers.

**Definition 22.2 (Machine instruction and machine language).**

A *machine instruction* is one [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) that the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) carries out directly, written as numbers in [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory). The [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) a [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) understands, with their numbers, are its *machine language*.

Every toy [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) takes two [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte). The first holds the number of the operation, its *operation code*, together with the number of a [register](#def-g7-how-a-processor-runs-a-program-register) (next section): the [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) is $4 \times \text{code} + \text{register}$. The second [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) is a number, an address or a second [register](#def-g7-how-a-processor-runs-a-program-register). People write [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) with short names instead of numbers; this chapter uses nine of them.

| code | written | what it does |
| --- | --- | --- |
| 0 | `HALT` | stop |
| 1 | `LOADI R, n` | put the number $n$ in [register](#def-g7-how-a-processor-runs-a-program-register) `R` |
| 4 | `ADD R, S` | `R` becomes `R + S` |
| 5 | `SUB R, S` | `R` becomes `R - S`; notes whether the result is zero |
| 13 | `CMP R, S` | computes `R - S` only to note whether it is zero or negative |
| 16 | `JNZ a` | if the last result was not zero, jump to address $a$ |
| 17 | `JN a` | if the last result was negative, jump to address $a$ |
| 19 | `IN R` | read a number typed by the user into `R` |
| 20 | `OUT R` | print the number in `R` |

## 22.2 Registers and the program counter

**Definition 22.3 (Register).**

A *register* is a small [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage) place inside the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor), where it keeps the numbers it is working on.

**Definition 22.4 (Program counter).**

The *program counter* is a [register](#def-g7-how-a-processor-runs-a-program-register) that holds the address of the next [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) to carry out.

The toy machine has four [registers](#def-g7-how-a-processor-runs-a-program-register), `R0` to `R3`, of one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) each, and a [program counter](#def-g7-how-a-processor-runs-a-program-pc), `PC`, which starts at 0. Three [one-bit](https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary#def-g5-bits-and-bytes-bit) *flags* remember things about the last result; this chapter only needs `Z`, which is 1 when the last result was zero.

![The toy machine: a processor with four registers, a program counter and flags; a memory of 256 cells (the first 16 drawn, with their addresses in red). Bytes travel between the two.](https://one-course.com/images/onecourse/chapters/computer-science-1/g7-how-a-processor-runs-a-program/fig-5b1eb8c40a33.svg)

*The toy machine: a [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) with four [registers](#def-g7-how-a-processor-runs-a-program-register), a [program counter](#def-g7-how-a-processor-runs-a-program-pc) and flags; a [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) of 256 cells (the first 16 drawn, with their addresses in red). [Bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) travel between the two.*

Here is a first [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program). A `;` starts a [comment](https://one-course.com/books/computer-science/1/en/chapter/14-a-first-python-program#def-g6-first-python-program-comment), as `#` does in Python.

```text
; add_two.toy -- reads two numbers, prints their sum
        IN    R0          ; R0 = the first number
        IN    R1          ; R1 = the second number
        ADD   R0, R1      ; R0 = R0 + R1
        OUT   R0          ; print R0
        HALT
```

***Listing 22.1.** Adding two numbers on the toy machine. code/grade-7/04-how-a-processor-runs-a-program/listings/add_two.toy*

Given 3 and 4 to read, it prints 7. In [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory), these five [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) are ten [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte), from address 0 to address 9. A short Python [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program), using the book’s toy machine as a module, shows them in decimal and in [binary](https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary#def-g5-bits-and-bytes-binary):

```python
from toymachine import assemble

code = assemble(open("add_two.toy").read())
for address in range(0, 10, 2):
    first, second = code[address], code[address + 1]
    print(f"{address:3}: {first:3} {second:3}   {first:08b} {second:08b}")
```

***Listing 22.2.** The [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) of [Listing 22.1](#lst-g7-how-a-processor-runs-a-program-add-two). code/grade-7/04-how-a-processor-runs-a-program/listings/encode.py*

```
$ python encode.py
  0:  76   0   01001100 00000000
  2:  77   0   01001101 00000000
  4:  16   1   00010000 00000001
  6:  80   0   01010000 00000000
  8:   0   0   00000000 00000000
```

***Output 22.1.** Address, the two [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) in decimal, then in [binary](https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary#def-g5-bits-and-bytes-binary).*

`IN R0` is $4 \times 19 + 0 = 76$, `IN R1` is 77, `ADD R0, R1` is $4 \times 4 + 0 = 16$ followed by the second [register](#def-g7-how-a-processor-runs-a-program-register), 1; `HALT` is two zeros.

![in memory: each instruction is a pair of bytes.](https://one-course.com/images/onecourse/chapters/computer-science-1/g7-how-a-processor-runs-a-program/fig-d5a48a391a63.svg)

*[Listing 22.1](#lst-g7-how-a-processor-runs-a-program-add-two) in [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory): each [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) is a pair of [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte).*

## 22.3 Fetch, decode, execute

**Definition 22.5 (Fetch–decode–execute cycle).**

The [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) repeats the same three steps, the *fetch–decode–execute cycle*: *fetch* the [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) at the address in the [program counter](#def-g7-how-a-processor-runs-a-program-pc) and move the [program counter](#def-g7-how-a-processor-runs-a-program-pc) to the next one; *decode* it, that is find which operation and which [registers](#def-g7-how-a-processor-runs-a-program-register) it names; *execute* it.

On the toy machine, fetching reads the two [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) at `PC` and `PC + 1`, then adds 2 to `PC`. A jump simply puts a new address in `PC`, so the next fetch happens there: that is how a [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) goes back.

![The cycle the processor repeats until it meets HALT.](https://one-course.com/images/onecourse/chapters/computer-science-1/g7-how-a-processor-runs-a-program/fig-8719e32f00c1.svg)

*The cycle the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) repeats until it meets `HALT`.*

**Definition 22.6 (Clock and core).**

The *clock* of a [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) is a signal that ticks at a steady rate and sets the pace of its work. A *core* is one part of a [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) that runs its own [fetch–decode–execute cycle](#def-g7-how-a-processor-runs-a-program-cycle); a [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) with several cores runs several [programs](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program), or several parts of one [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program), at the same time.

![A simplified picture: one instruction for each tick of the clock. A real processor spreads an instruction over several ticks, and works on several instructions at once.](https://one-course.com/images/onecourse/chapters/computer-science-1/g7-how-a-processor-runs-a-program/fig-084cbbb2843e.svg)

*A simplified picture: one [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) for each tick of the [clock](#def-g7-how-a-processor-runs-a-program-clock). A real [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) spreads an [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) over several ticks, and works on several [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) at once.*

**Facts as of 2026.**

A desktop [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) on sale in 2026 runs its [clock](#def-g7-how-a-processor-runs-a-program-clock) at up to 5.7 gigahertz, 5.7 billion ticks a second, and has 24 [cores](#def-g7-how-a-processor-runs-a-program-clock). Each [core](#def-g7-how-a-processor-runs-a-program-clock) carries out billions of [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) a second.

## 22.4 Tracing a program by hand

To trace a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) is to play the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor): write a table with one row per step, the [program counter](#def-g7-how-a-processor-runs-a-program-pc) and the [registers](#def-g7-how-a-processor-runs-a-program-register). The toy machine can print that table itself with `--trace`. Each row shows the machine *before* the step, so the effect of an [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) appears on the next row.

```
$ python -m toymachine run --trace add_two.toy < add_two.stdin
step  PC  instruction       R0  R1  R2  R3  Z N C
   1   0  IN R0              0   0   0   0  0 0 0
   2   2  IN R1              3   0   0   0  0 0 0
   3   4  ADD R0, R1         3   4   0   0  0 0 0
   4   6  OUT R0             7   4   0   0  0 0 0
7
   5   8  HALT               7   4   0   0  0 0 0
halted after 5 steps
```

***Output 22.2.** The trace of [Listing 22.1](#lst-g7-how-a-processor-runs-a-program-add-two), reading 3 and 4. The 7 alone on its line is what `OUT` prints.*

The next [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) adds $n + (n-1) + \dots + 1$. It is a [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop): `loop:` is a *label*, a name for the address of the [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) after it, and `JNZ loop` jumps back there while `R0` has not reached zero.

```text
; sum.toy -- reads n, prints 1 + 2 + ... + n
        IN    R0          ; R0 = n
        LOADI R1, 0       ; R1 = the running total
        LOADI R2, 1       ; R2 = the constant 1
loop:   ADD   R1, R0      ; total = total + n
        SUB   R0, R2      ; n = n - 1 (Z is set when n reaches 0)
        JNZ   loop        ; if n is not 0, go back to loop
        OUT   R1
        HALT
```

***Listing 22.3.** The sum $1 + 2 + \dots + n$. code/grade-7/04-how-a-processor-runs-a-program/listings/sum.toy*

```
$ python -m toymachine run --trace sum.toy < sum.stdin
step  PC  instruction       R0  R1  R2  R3  Z N C
   1   0  IN R0              0   0   0   0  0 0 0
   2   2  LOADI R1, 0        4   0   0   0  0 0 0
   3   4  LOADI R2, 1        4   0   0   0  0 0 0
   4   6  ADD R1, R0         4   0   1   0  0 0 0
   5   8  SUB R0, R2         4   4   1   0  0 0 0
   6  10  JNZ 6              3   4   1   0  0 0 0
   7   6  ADD R1, R0         3   4   1   0  0 0 0
   8   8  SUB R0, R2         3   7   1   0  0 0 0
   9  10  JNZ 6              2   7   1   0  0 0 0
  10   6  ADD R1, R0         2   7   1   0  0 0 0
  11   8  SUB R0, R2         2   9   1   0  0 0 0
  12  10  JNZ 6              1   9   1   0  0 0 0
  13   6  ADD R1, R0         1   9   1   0  0 0 0
  14   8  SUB R0, R2         1  10   1   0  0 0 0
  15  10  JNZ 6              0  10   1   0  1 0 0
  16  12  OUT R1             0  10   1   0  1 0 0
10
  17  14  HALT               0  10   1   0  1 0 0
halted after 17 steps
```

***Output 22.3.** The trace for $n = 4$. The label `loop` has become the address 6; when `R0` reaches 0, `Z` becomes 1 and `JNZ` lets the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) go on.*

Each turn of the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) takes three steps (`ADD`, `SUB`, `JNZ`). The same kind of [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) counts down:

```text
; countdown.toy -- prints 3, 2, 1
        LOADI R0, 3
        LOADI R1, 1
again:  OUT   R0
        SUB   R0, R1
        JNZ   again
        HALT
```

***Listing 22.4.** A countdown. code/grade-7/04-how-a-processor-runs-a-program/listings/countdown.toy*

## 22.5 Machine language, and why Python needs an interpreter

The [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor)’s only language is its numbered [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction). A Python [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) is text, which no [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) can carry out. The [interpreter](https://one-course.com/books/computer-science/1/en/chapter/14-a-first-python-program#def-g6-first-python-program-interpreter) of [Chapter 14](https://one-course.com/books/computer-science/1/en/chapter/14-a-first-python-program#ch-g6-first-python-program) is a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) in [machine language](#def-g7-how-a-processor-runs-a-program-machine-instruction) that reads the Python text and does what it says: `print(3 + 4)` becomes, inside it, many [machine instructions](#def-g7-how-a-processor-runs-a-program-machine-instruction) not so different from [Listing 22.1](#lst-g7-how-a-processor-runs-a-program-add-two). A real [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) has hundreds of [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) instead of nine, and [registers](#def-g7-how-a-processor-runs-a-program-register) of eight [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) instead of one, but it runs the same cycle. The next book uses every [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) of the toy machine and writes [programs](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that let people type [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) by name.

![A processor (the purple square) in its socket on an old motherboard. Its machine language has well over a hundred instructions.](https://one-course.com/images/onecourse/chapters/computer-science-1/g7-how-a-processor-runs-a-program/img-ae91fe973714.jpg)

*A [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) (the purple square) in its socket on an old [motherboard](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-motherboard). Its [machine language](#def-g7-how-a-processor-runs-a-program-machine-instruction) has well over a hundred [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction).*

**History — Programs as wiring, then programs in memory.**

ENIAC, the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) of [Chapter 1](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#ch-g1-what-is-a-computer), kept no [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) in its [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory): its operators set it up for each job by plugging in cables and setting switches, which could take days. A report dated 30 June 1945, on the design of its successor EDVAC, described a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) with one large [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) holding the [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) as well as the data. Changing the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) then meant changing numbers in [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory), and almost every [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) since has worked that way.

![Rows of hand-set knobs on one of ENIAC’s function tables, which held numbers for its calculations.](https://one-course.com/images/onecourse/chapters/computer-science-1/g7-how-a-processor-runs-a-program/img-8133e18d5f09.jpg)

*Rows of hand-set knobs on one of ENIAC’s [function](https://one-course.com/books/computer-science/1/en/chapter/19-functions#def-g7-functions-function) tables, which held numbers for its calculations.*

## 22.6 Exercises

**Exercise 22.1 ★.**

All [registers](#def-g7-how-a-processor-runs-a-program-register) start at 0. What do `R0` and `R1` hold when this [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) halts?

```text
        LOADI R0, 5
        LOADI R1, 2
        SUB   R0, R1
        HALT
```

***Listing 22.5.** Three [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction), then `HALT`. code/grade-7/04-how-a-processor-runs-a-program/exercises/three_steps.toy*

**Solution of Exercise 22.1.**

`R0` holds 5, then `R1` holds 2, then `R0` becomes $5 - 2 = 3$: `R0` is 3 and `R1` is 2.

**Exercise 22.2 ★.**

In [Listing 22.3](#lst-g7-how-a-processor-runs-a-program-sum), what does `PC` hold after each of the first four steps?

**Solution of Exercise 22.2.**

Each fetch adds 2: after the steps `PC` holds 2, 4, 6 and 8 (the fourth step is the first `ADD`, which does not jump).

**Exercise 22.3 ★.**

What does [Listing 22.4](#lst-g7-how-a-processor-runs-a-program-countdown) print?

**Solution of Exercise 22.3.**

```
$ python -m toymachine run countdown.toy
3
2
1
```

***Output 22.5.** The countdown.*

`OUT` comes before `SUB`, so 3 is printed first; when `R0` reaches 0 the jump is not taken, and 0 is never printed.

**Exercise 22.4 ★.**

Write a toy [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that reads two numbers and prints their sum plus 1.

**Solution of Exercise 22.4.**

```text
        IN    R0
        IN    R1
        ADD   R0, R1
        LOADI R2, 1
        ADD   R0, R2
        OUT   R0
        HALT
```

*The sum plus 1. code/grade-7/04-how-a-processor-runs-a-program/exercises/sum_plus_one.toy*

With 3 and 4 it prints 8. There is no [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) to add a number directly in the table, so the 1 goes first into a [register](#def-g7-how-a-processor-runs-a-program-register).

**Exercise 22.5 ★★.**

This [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) should print 3, 2, 1. Here is what happens when it runs; the machine stops it after 10 000 steps. What is wrong? Fix it.

```text
; never_ends.toy -- should print 3, 2, 1
again:  LOADI R0, 3
        LOADI R1, 1
        OUT   R0
        SUB   R0, R1
        JNZ   again
        HALT
```

***Listing 22.6.** A countdown that never ends. code/grade-7/04-how-a-processor-runs-a-program/listings/never_ends.toy*

```
$ python -m toymachine run never_ends.toy > printed.txt
$ wc -l printed.txt
2001 printed.txt
$ tail -n 3 printed.txt
3
3
error: no HALT after 10000 steps
```

***Output 22.4.** What it prints is kept in a [file](https://one-course.com/books/computer-science/1/en/chapter/7-files-and-folders#def-g3-files-and-folders-file); `wc -l` counts its lines and `tail` shows the last ones.*

**Solution of Exercise 22.5.**

`JNZ again` jumps back to `LOADI R0, 3`, which puts 3 back in `R0` at every turn: `R0` never reaches 0, and the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) prints 3 forever (2 000 times before the machine stops it). The label belongs on the `OUT` line, after the two `LOADI`, as in [Listing 22.4](#lst-g7-how-a-processor-runs-a-program-countdown).

**Exercise 22.6 ★★.**

Noor forgot the `HALT` at the end of [Listing 22.1](#lst-g7-how-a-processor-runs-a-program-add-two). Her [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) still prints 7 and stops. Why? Why is it still a bug?

**Solution of Exercise 22.6.**

After `OUT R0` the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) fetches the next two cells, 8 and 9. Every cell the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) did not fill holds 0, and the two [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) 0, 0 are the [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) `HALT`: the machine stops by luck. If anything were stored after the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program), data or another part of the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program), the [processor](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-processor) would run it as [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction).

**Exercise 22.7 ★★.**

Write a toy [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that prints 5, 4, 3, 2, 1.

**Solution of Exercise 22.7.**

```text
        LOADI R0, 5
        LOADI R1, 1
again:  OUT   R0
        SUB   R0, R1
        JNZ   again
        HALT
```

*From 5 down to 1. code/grade-7/04-how-a-processor-runs-a-program/exercises/five_to_one.toy*

**Exercise 22.8 ★★.**

Using the table of operation codes, write the two [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) of `LOADI R1, 7` in decimal and in [binary](https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary#def-g5-bits-and-bytes-binary).

**Solution of Exercise 22.8.**

`LOADI` has code 1 and the [register](#def-g7-how-a-processor-runs-a-program-register) is 1: the first [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) is $4 \times 1 + 1 = 5$, `00000101`; the second is the number 7, `00000111`.

**Exercise 22.9 ★★.**

Without the trace, count the steps that [Listing 22.3](#lst-g7-how-a-processor-runs-a-program-sum) takes for $n = 4$; then check your answer on [Output 22.3](#out-g7-how-a-processor-runs-a-program-sum-trace).

**Solution of Exercise 22.9.**

Three steps before the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) (`IN` and two `LOADI`), three per turn for 4 turns, then `OUT` and `HALT`: $3 + 12 + 2 = 17$, as the trace says.

**Exercise 22.10 ★★★.**

The toy [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) has 256 cells and each [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) takes two. How many [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) can the longest [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) have? Why would a real [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) be shorter still?

**Solution of Exercise 22.10.**

$256 / 2 = 128$ [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction). A real [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) also needs cells for its data, and the last [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) must be `HALT`, so fewer [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) do useful work.

**Exercise 22.11 ★★★.**

Find an expression for the number of steps [Listing 22.3](#lst-g7-how-a-processor-runs-a-program-sum) takes for any $n$, and check it with a Python [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that runs the toy machine for $n = 1$ to 10.

**Solution of Exercise 22.11.**

Three steps before the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop), three per turn for $n$ turns, two at the end: $3n + 5$ steps.

```python
from toymachine import Machine, assemble

code = assemble(open("sum.toy").read())
for n in range(1, 11):
    machine = Machine(code, inputs=[n])
    machine.run()
    print(f"n = {n:2}   total = {machine.outputs[0]:2}   steps: {machine.steps}")
```

*Steps of [Listing 22.3](#lst-g7-how-a-processor-runs-a-program-sum). code/grade-7/04-how-a-processor-runs-a-program/listings/sum_steps.py*

```
$ python sum_steps.py
n =  1   total =  1   steps: 8
n =  2   total =  3   steps: 11
n =  3   total =  6   steps: 14
n =  4   total = 10   steps: 17
n =  5   total = 15   steps: 20
n =  6   total = 21   steps: 23
n =  7   total = 28   steps: 26
n =  8   total = 36   steps: 29
n =  9   total = 45   steps: 32
n = 10   total = 55   steps: 35
```

***Output 22.6.** From 8 steps for $n = 1$, three more for each extra turn.*

**Exercise 22.12 ★★★.**

Write a toy [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that reads two numbers below 128 and prints the larger one. Use `CMP` and `JN` from the table.

**Solution of Exercise 22.12.**

`CMP R0, R1` computes $R0 - R1$; it is negative exactly when `R0` is the smaller one, and then the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) jumps to print `R1`.

```text
        IN    R0
        IN    R1
        CMP   R0, R1      ; compute R0 - R1, set the flags, keep R0
        JN    second      ; negative: R0 is smaller than R1
        OUT   R0
        HALT
second: OUT   R1
        HALT
```

*The larger of two numbers. code/grade-7/04-how-a-processor-runs-a-program/exercises/larger.toy*

With 9 and 14 it prints 14. (Numbers of 128 or more would need the flag `C`, which the next book explains.)

## 22.7 Problem: Multiplying on a Machine That Cannot Multiply

**Problem 22.1.**

Weekend problem — multiplying with additions

The toy machine can add and subtract, but it has no [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) to multiply. Yet $6 \times 4$ is $6 + 6 + 6 + 6$.

**Part I — By hand.**

1. Write $6 \times 4$ and $4 \times 6$ as sums. Which one has fewer additions?
2. Here is a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that reads $a$ and $b$ and adds $a$ to a total, $b$ times. Trace it by hand for $a = 6$ and $b = 4$ , in a table with the columns step, `PC` , `R0` , `R1` , `R2` .

```text
; multiply.toy -- reads a and b, prints a x b by adding a, b times
        IN    R0          ; R0 = a
        IN    R1          ; R1 = b (how many additions are left)
        LOADI R2, 0       ; R2 = the total
        LOADI R3, 1       ; R3 = the constant 1
again:  ADD   R2, R0      ; total = total + a
        SUB   R1, R3      ; one addition fewer to do
        JNZ   again
        OUT   R2
        HALT
```

***Listing 22.7.** Multiplying by repeated addition. code/grade-7/04-how-a-processor-runs-a-program/listings/multiply.toy*

3. How many steps did your trace take? Which [register](#def-g7-how-a-processor-runs-a-program-register) holds the answer?
4. What does `R1` count?

**Part II — Counting the steps.**

```python
from toymachine import Machine, assemble

code = assemble(open("multiply.toy").read())
for b in range(1, 11):
    machine = Machine(code, inputs=[6, b])
    machine.run()
    print(f"6 x {b:2} = {machine.outputs[0]:3}   steps: {machine.steps}")
```

***Listing 22.8.** Running [Listing 22.7](#lst-g7-how-a-processor-runs-a-program-multiply) for $b = 1$ to 10. code/grade-7/04-how-a-processor-runs-a-program/listings/multiply_steps.py*

5. Run the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) . Does it agree with your trace for $b = 4$ ?
6. By how much does the number of steps grow when $b$ grows by 1? Why?
7. Which steps are done only once, whatever $b$ is? How many are they?

**Part III — The cost of a multiplication.**

8. Write the number of steps as an expression in $b$ .
9. Predict the steps for $3 \times 50$ and for $50 \times 3$ , then run them.
10. To multiply two numbers, which one should be read second, as $b$ ?
11. A [register](#def-g7-how-a-processor-runs-a-program-register) holds one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) , at most 255. What goes wrong with $7 \times 200$ ?
12. State the result.

**Solution of Problem 22.1.**

**1.** $6 \times 4 = 6 + 6 + 6 + 6$ (three additions to a first 6, or four to a total starting at 0); $4 \times 6 = 4 + 4 + 4 + 4 + 4 + 4$, with more additions.

**2.** The first four rows read $a$, $b$ and set up `R2` = 0 and `R3` = 1 (`PC` 0, 2, 4, 6). Then each turn runs `ADD`, `SUB`, `JNZ` at addresses 8, 10 and 12: `R2` goes 6, 12, 18, 24 while `R1` goes 3, 2, 1, 0. Finally `OUT` at 14 and `HALT` at 16.

**3.** $4 + 3 \times 4 + 2 = 18$ steps; the answer is in `R2`, and the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) prints 24.

**4.** `R1` counts the additions that are still to be done.

**5.** It agrees: 18 steps for $b = 4$.

```
$ python multiply_steps.py
6 x  1 =   6   steps: 9
6 x  2 =  12   steps: 12
6 x  3 =  18   steps: 15
6 x  4 =  24   steps: 18
6 x  5 =  30   steps: 21
6 x  6 =  36   steps: 24
6 x  7 =  42   steps: 27
6 x  8 =  48   steps: 30
6 x  9 =  54   steps: 33
6 x 10 =  60   steps: 36
```

***Output 22.7.** The steps for $6 \times b$.*

**6.** By 3 each time: one more turn of the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop), which is three [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction).

**7.** The four [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) before the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) and the two after it: 6 steps.

**8.** $3b + 6$ steps.

**9.** $3 \times 50 + 6 = 156$ steps for $3 \times 50$, and $3 \times 3 + 6 = 15$ for $50 \times 3$:

```
$ python orders.py
3 x 50 = 150   steps: 156
50 x 3 = 150   steps: 15
7 x 30 = 210   steps: 96
30 x 7 = 210   steps: 27
7 x 200 = 120   steps: 606
```

***Output 22.8.** The order matters for the time, not for the answer (except on the last line).*

**10.** The smaller one, since the number of turns is $b$.

**11.** $7 \times 200 = 1\,400$ does not fit in one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte). The machine prints 120 instead: it kept only what fits in a [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte), $1\,400 - 5 \times 256$. The next book explains what happens when a number goes past 255.

**12.** **Result.** The multiplication of $a$ by $b$ takes $3b + 6$ steps: the same 3 more for each extra unit of $b$, so $3 \times 50$ takes 156 steps and $50 \times 3$ only 15. A [one-byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) [register](#def-g7-how-a-processor-runs-a-program-register) cannot hold a product above 255.
