esolang

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

Expected: 120

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.

OpcodeParametersDoes
1 23Add or multiply the first two into the third
31Read an integer into it
41Output it
5 62Jump to the second if the first is not 0, or if it is 0
7 83Write 1 to the third if the first is less than, or equal to, the second, else 0
91Add it to the relative base
990Halt

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.

Expected: 15511210043330985984000000

The Hello World outputs numbers, which the specification reads as ASCII. They spell Hello, world! and a line feed.

Expected: 72 101 108 108 111 44 32 119 111 114 108 100 33 10

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

Further

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 intcode

Source

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