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 one memory in one format. The consequence is not tidiness: it is that a program can read and write itself, and the first thing anybody did with that was walk an array by adding one to an instruction. This memory holds eight numbers. Some of them are the program.

A memory of eight cells, some of which are the program

Two programs that add the same three numbers. The first writes out one ADD per number. The second uses one ADD and edits it.

The memory. Every cell is read twice: once as the number it is, once as the instruction it would be.

Each cell, the number it holds, and that same number read as an instruction
cellas a numberas an instruction
015LOAD 5
126ADD 6
227ADD 7
334STORE 4
46060 — not an instruction
510LOAD 0
620ADD 0
730STORE 0

What each pass did to the instruction at cell 1.

Each pass: the instruction before it ran, the cell it read, and the instruction after the program edited it
passinstructionwhat it readrewritten to
This program never writes to a cell it also executes.

the instruction at cell 1
ADD 6, and after the whole program ran it is still ADD 6
times the program rewrote itself
none
the total in cell 4
60

Three numbers, three ADD instructions written out by hand. The program is exactly as long as the data it adds, which is the problem the other one solves.

What this page checked when it loaded, rather than what it claims.

Each claim, whether it held, and the values behind it
claimheldmeasured
every opcode is a single digityes
all 100 words decode and re-encode to themselvesyes
both programs total the same three numbersyes60 and 60
the loop edited its own instructionyesADD 5 became ADD 8

What is real here, and what is not

This is not EDVAC, and not the Baby either

It is a decimal machine with eight 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 four thousand words of mercury delay line, and was not running in 1945 — 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 ten plus the operand, so 25 is ADD from cell 5 and is also just the number 25. Both columns of the memory table are read out of that single word — the instruction column is decoded from it rather than stored beside it — so the two cannot disagree. All hundred 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 8, and there is no cell 8. Real programs of the period ended the loop by testing the modified instruction against a constant, which meant the terminating condition was itself a number in memory that could be got wrong. The page stops the loop at three rather than pretend a mechanism it does not have.

Self-modifying code did not last, and it is worth saying why

Index registers arrived within a few years and did the same job without a program writing to itself; instruction caches, shared code pages and memory protection later made it impractical and then impossible on ordinary hardware. 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