Hardware, software and von Neumann
Hardware and software, and the stored program concept in a tiny computer.
Do this lesson in the simulatorA computer is two things that are useless apart. Hardware is the physical parts: the processor, the memory, the motors and sensors on BugBot. Software is the programs: the instructions that tell the hardware what to do. This lesson is about how they fit together, and the idea at the heart of almost every computer, including the one in the robot: the stored program.
Hardware and software
| Hardware | Software |
|---|---|
| the physical parts you could touch | programs and the data they use |
| BugBot's processor, memory, camera, motors, battery | your Python program, the robot's firmware, the Python interpreter |
| does nothing on its own | does nothing without hardware to run on |
Software comes in two kinds. System software runs the computer itself: the operating system, device drivers, and utilities. Application software does jobs for the user: a web browser, a game, or the lessons you are reading.
The stored program concept
Early computers were rewired by hand for each new job. In the 1940s, John von Neumann and others described a better design: keep the program in the computer's memory, in the same place and the same form as the data. To do a new job, load a new program. No rewiring.
Every computer built this way has the same parts, called the von Neumann architecture:
- a processor (CPU) that fetches instructions from memory and carries them out, one at a time;
- memory that holds both the program's instructions and the data they work on, each at a numbered address;
- input and output devices, such as BugBot's sensors and motors;
- buses: sets of wires that carry addresses, data and control signals between them.
Program and data in one memory
Here is a tiny computer's memory, made in Python as a list. Some addresses hold instructions and some hold data, and nothing marks which is which: it is all just values. The processor only knows an address holds an instruction because the program counter sends it there.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
memory = [
"LOAD 6", # 0: copy the value at address 6 into the processor
"ADD 7", # 1: add the value at address 7
"STORE 8", # 2: store the answer at address 8
"OUT 8", # 3: output the value at address 8
"HALT", # 4: stop
"", # 5: unused
20, # 6: data
22, # 7: data
0, # 8: the answer goes here
]
acc = 0 # the accumulator: the processor's working value
pc = 0 # the program counter: the address of the next instruction
while True:
instruction = memory[pc]
pc = pc + 1
op, *arg = instruction.split()
if op == "LOAD":
acc = memory[int(arg[0])]
elif op == "ADD":
acc = acc + memory[int(arg[0])]
elif op == "STORE":
memory[int(arg[0])] = acc
elif op == "OUT":
print("output:", memory[int(arg[0])])
elif op == "HALT":
break
print("memory now:", memory)
Change the data at addresses 6 and 7 and run again: same program, different answer. Change ADD to a different instruction and it is a different program. Program and data live side by side.
Why it matters
Because programs are just data in memory, a computer can load any program, a program can be copied or downloaded like any other file, and your Python program can be sent to BugBot and run. It also means a mistake can make a processor treat data as instructions, which is how some security attacks work.
Task: a stored program
Add a SUB instruction to the tiny computer, which subtracts the value at an address from the accumulator. Then put a program in memory that loads 50 from address 6, subtracts 8 from address 7, stores the answer at address 8 and outputs it, so the program prints output: 42. The robot should then drive forward the answer in centimetres, divided by 2, read from memory address 8.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
memory = ["LOAD 6", "ADD 7", "STORE 8", "OUT 8", "HALT", "", 50, 8, 0]
Challenges
- Add a
JUMP ainstruction that sets the program counter toa, and use it to make a program that outputs forever. Then stop it. - What happens if the program counter reaches address 6? Try removing the
HALT. - List five pieces of software BugBot uses, and say whether each is system or application software.