Stored Program
Before 1945 a machine's program lived in plugboards, switches or paper tape, and its numbers lived somewhere else entirely. The First Draft of a Report on the EDVAC put both in the same memory, in the same binary storage. Not in the same layout: the First Draft divides a stored minor cycle into numbers and orders by its first bit, and what the remaining bits mean depends on which it is. The consequence is not tidiness: it is that a program can read and write itself, so an instruction can be loaded, added to and stored back like any other number. This memory holds twelve numbers. Some of them are the program.
A memory of twelve cells, some of which are the program
Two programs that add the same three numbers. The first writes out one instruction per number. The second uses one ADD and rewrites it with its own STORE.
The memory. Every cell is read twice: once as the number it is, once as the instruction it would be.
| cell | as a number | as an instruction |
|---|---|---|
| 0 | 109 | LOAD 9 |
| 1 | 210 | ADD 10 |
| 2 | 211 | ADD 11 |
| 3 | 307 | STORE 7 |
| 4 | 0 | HALT |
| 5 | 0 | HALT |
| 6 | 0 | HALT |
| 7 | 60 | HALT |
| 8 | 1 | HALT |
| 9 | 10 | HALT |
| 10 | 20 | HALT |
| 11 | 30 | HALT |
What each pass did to the instruction at cell 1.
| pass | instruction | what it read | rewritten to |
|---|---|---|---|
| This program never writes to a cell it also executes, and it stops on its own HALT at cell 4. | |||
- the instruction at cell 1
- ADD 10, and after the whole program ran it is still ADD 10
- times the program rewrote itself
- none
- the total in cell 7
- 60
Three numbers, one LOAD and two ADDs written out by hand. The program grows by one instruction for every number it adds, which is the problem the other one solves.
What this page checked when it loaded, rather than what it claims.
| claim | held | measured |
|---|---|---|
| every opcode is a single digit | yes | |
| all 1,000 words decode and re-encode to themselves | yes | |
| both programs total the same three numbers | yes | 60 and 60 |
| the spelled-out program writes no cell it executes, and stops on its own HALT | yes | it stopped at cell 4 (halt) |
| the loop's own STORE rewrote its ADD, and the loop then ran the rewritten one | yes | ADD 9 became ADD 12, written 3 times by the STORE at cell 5 |
| on the first pass the accumulator held the ADD as the number 209 | yes | it read 209, added one, and stored 210 |
All 6 checks held when this page loaded.
What is real here, and what is not
This is not EDVAC, and not the Baby either
It is a decimal machine with twelve cells, one accumulator and six instructions, invented for this page so that every value on the screen is a number you can read. EDVAC was binary, had a completed acoustic mercury-delay store of 1,024 words of 44 bits, which is 128 delay lines of eight words, rather than the larger memory the report proposes. It was not running in 1945 either: the report describes a machine that did not yet exist. The first stored program actually executed ran on the Manchester Baby in June 1948, and it was a division by repeated subtraction, not this.
The encoding is one table and everything derives from it
A word is the opcode times a hundred plus the operand, so 209 is ADD from cell 9 and is also just the number 209. Two digits would have been neater, and they reach only ten cells, two short of a loop that rewrites itself with its own instructions. Both columns of the memory table are read out of that single word, the instruction column being decoded from it rather than stored beside it, so the two cannot disagree. All thousand possible words are decoded and re-encoded when the page loads, and the result is in the table above.
The loop does not know when to stop, and that is the honest part
After three passes the ADD instruction reads cell 12, and there is no cell 12; a fourth pass stops the machine with that fault. A program that ends such a loop has to test the modified instruction against a constant, which makes the stopping condition itself a number in memory that can be got wrong. The page stops the loop at three rather than pretend a mechanism it does not have. Until 2026-09-11 the page did more than stop it: its JavaScript moved the operand between passes, and a check that said the loop edited its own instruction passed on that edit. The loop now makes the edit with its own STORE, and the check asks which instruction wrote the cell.
Self-modifying code did not last, and it is worth saying why
Index registers later did the same job without a program writing to itself, and no source archived for this page dates them; instruction caches, shared code pages and memory protection later made it expensive and carefully controlled rather than impossible. Intel and ARM both document how to do it, and a JIT compiler does it every time it runs: it writes instructions as data and then executes them. What survived is the part underneath: instructions are still data, which is why a compiler can write a program, and why a program can be read off a disk and run.
Sources
- J. von Neumann, First Draft of a Report on the EDVAC, 1945, reprinted in IEEE Annals of the History of Computing, 1993. The report that put instructions and data in one memory.
- F. C. Williams and T. Kilburn, Electronic Digital Computers, Nature 162, 487, 1948. The Manchester Baby, and the first program actually stored and run.
- Logical Art, the studio this belongs to.