Intcode
A machine language that grew one puzzle at a time, in Advent of Code 2019.
2019 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 5 known programs · Test method
Run it
The idea
Intcode came from Advent of Code 2019, where it was featured on 12 of the 25 days, and every contestant had to write an interpreter for it. The specification says it was created purely to annoy them. Day 2 had only add, multiply and halt. Day 5 added input, output, immediate mode, two jumps and two tests. Day 9 added the relative base.
Read the story
A program is a list of integers separated by commas. It is copied to the start of memory and runs from address 0, so code and data share one memory and a program can rewrite itself.
The machine
An instruction is one number. Its last two digits are the opcode, and the digits above them give the mode of each parameter, from the hundreds up. Mode 0 reads an address, mode 1 takes the value itself and mode 2 reads an address counted from the relative base.
| Opcode | Parameters | Does |
|---|---|---|
1 2 | 3 | Add or multiply the first two into the third |
3 | 1 | Read an integer into it |
4 | 1 | Output it |
5 6 | 2 | Jump to the second if the first is not 0, or if it is 0 |
7 8 | 3 | Write 1 to the third if the first is less than, or equal to, the second, else 0 |
9 | 1 | Add it to the relative base |
99 | 0 | Halt |
Cells hold integers without bound. The input is the integers in the input text, and each output is printed in decimal on its own line.
Example
The factorial reads n and multiplies down from it.
The factorial reads n and multiplies down from it. It gives 1 for 0, and 25 shows that the numbers have no bound.
The Hello World outputs numbers, which the specification reads as ASCII. They spell Hello, world! and a line feed.
It keeps the character at address 3, which is also the first operand of its own add. Each lap adds the next difference from a list that starts at address 14, a second add moves the list along, and the loop ends when the character reaches 0. It takes 57 instructions.
Patterns it teaches
- Self-modifying code: the Hello World rewrites its own add.
- Code as data: one memory holds the program and its numbers.
Further
See also
Self-modifying
Write your own in Run Convert a program from Intcode
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: intcode.
Command line
shib run intcode -e '4,17,4,19,1001,17,1,17,8,17,18,16,1006,16,0,99,-1,1,11,32'shib manual intcodeSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/intcode.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org