Self-modifying code
A program that rewrites its own instructions while it runs. Forbidden in polite software, the whole point in many esolangs.
●●○ moderate · where to see it
The idea
In most computers, code and data sit in the same memory. That is the von Neumann (stored-program) design. It means nothing stops a program from writing over its own instructions. Early programmers did this on purpose, to save memory or to patch a jump address. Today it is mostly banned; memory pages are marked read-only, and compilers assume code never changes.
Read the story
Esolangs bring it back. Sometimes as a puzzle (Befunge's p), sometimes as a punishment (Malbolge scrambles each instruction after it runs), and sometimes as the engine of life itself, since a program that writes a copy of itself into memory is modifying the memory it lives in.
There are three flavours worth telling apart.
- Patching. The program edits a few of its own cells to change what happens next.
- Forced change. The language edits the program by itself.
- Writing a successor. The program writes a new program (a copy, or a child) somewhere else.
How it works
Befunge-93 has a 2D grid of characters, and the instruction p ("put") writes a character into that grid.
Befunge-93 has a 2D grid of characters, and the instruction p ("put") writes a character into that grid. It pops y, then x, then a value v, and stores the character with code v at column x, row y.
"@"80p1.
Trace it, starting at the top left, moving right.
| Step | Instruction | Stack after | Note |
|---|---|---|---|
| 1 | "@" | 64 | string mode pushes the code of @ |
| 2 | 8 0 | 64 8 0 | |
| 3 | p | (empty) | writes @ at column 8, row 0 |
| 4 | 1 . | (empty) | prints 1 |
| 5 | @ | the cell it just wrote; stop |
The source contains no @. Without step 3 the pointer would wrap around the 80-column line and print 1 forever. The program writes its own stop sign.
The Redcode "Imp" from Core War is the smallest self-modifier that moves.
MOV 0, 1
MOV 0, 1 copies the instruction at offset 0 (itself) to offset 1 (the next cell). Then the program counter moves to the next cell, which now holds MOV 0, 1. It does the same. The Imp crawls through memory forever, overwriting whatever was there, including enemy code.
Step by step
00g reads the character at column 0, row 0, which is the program’s own first character, 0.
Read your own first character
00g reads the character at column 0, row 0, which is the program’s own first character, 0. , prints it.
Expected output
0
Output
Write the stop command ahead of yourself
Expected output
(nothing)
Output
"@"pushes 64, the code of@.60pushes 6 and 0.pwrites 64 at column 6, row 0, the cell right after it, which was a space.- The cursor moves on into the new
@and stops. Withoutpit would loop forever.
A program that edits the digit it will print
The program writes the character 5 over the 0 at column 7. When the cursor gets there, it pushes 5 instead of 0, and . prints 5.
Expected output
5
Output
Side by side
In Self-modifying Brainfuck the program sits on the tape, so writing a cell rewrites the code.
Self-modifying Brainfuck
In Self-modifying Brainfuck the program sits on the tape, so writing a cell rewrites the code. This program prints itself.
Expected output
<[<]>[.>]Quines are easy!
Output
In the kernel, the same idea is DEFINE, which rebinds a word or a reading rule while the program runs. See code as data.
Try this
- Change the Befunge example so it prints
1 2before stopping. Where must the@go now? - Write a Befunge program that uses
gto read a digit from its own source and print it. - Hard: design a two-instruction Redcode warrior that copies itself two cells ahead each turn. What does it leave behind?
Seen in
- Befunge.
pandgread and write the program grid. Puzzles often build code at run time. - Malbolge. After each instruction runs, it is replaced through a fixed permutation table. You must plan for the program you will have, not the one you wrote.
- Redcode. Warriors in Core War share one memory and win by overwriting each other. The Imp is self-modification as movement.
- Tierra. Creatures copy their own instructions into a new block of memory. Copy errors are mutations.
- bff. Code and data are the same 64-byte tape, so a program that writes bytes is rewriting code, its own or its neighbour's. Replicators arise from exactly this.
Further
- Self-modifying code on Wikipedia, and the esolangs.org category Self-modifying.
- A. K. Dewdney, "Computer Recreations" on Core War, Scientific American, May 1984.
- See also code as data, self-replication, quine.