Assembler and runner for the Intcode computer from Advent of Code 2019.
The following program outputs "Hello World!".
ARB #message ; move the relative base to the beginning of our message
loop:
OUT rb ; output the current character in the message
ARB #1 ; move the relative base to the next character
JNZ rb, #loop ; if the next character is non-zero then go back to `loop`
HLT
message:
DB "Hello World!\n"The compiler can assemble the following instruction set specification into an Intcode program.
Intcode assembly must be written in a UTF-8 encoded file with Unix line endings.
Comments start with a semicolon ;.
There are two types of operands.
-
Label
A label refers to an address in a program. For example:
endin the following program refers the address of theHLTinstruction.JZ #0, #end end: HLT
-
Number
A binary, octal, decimal, or hexadecimal number. This can be used for specifying manual addresses, address offsets, or exact values. For example: the following reads in a value, minuses 3 from it, and outputs the result.
IN x ADD x, #-0b11, x+1 OUT x+0x1 HLT
There are three ways to specify the operands for different instructions.
-
Positional
Specifies a value by specifying the address it should be read from. For example:
19specifies the value at address 19.x+3specifies the value at the labelxwith an offset of 3.
-
Immediate
Specifies a value by specifying the exact value. For example:
#19specifies the exact value 19.#x+3specifies the exact label addressxwith an offset of 3.
-
Relative
Specifies a value by specifying the address it should be read from as an offset of the relative base. For example:
rb+3specifies the value at the relative base address with an offset of 3.
The following operations are supported, roughly described in the order they are introduced in Advent of Code.
-
ADDAdds the first two operands and stores the result in the third. For example: increment the value at
x:ADD x, #1, x
-
MULMultiplies the first two operands and stores the result in the third. For example: multiply the value at
xby 2:MUL x, #2, x
-
INReads a single number and stores it in the first operand. For example: store input at
x:IN x
-
OUTOutputs a single number and stores it in the first operand. For example: output the value at
x:OUT x
-
JNZChecks if the first operand is non-zero and then jumps to the value of the second operand. For example: set the instruction pointer to
labelif the value atxis non-zero:JNZ x, #label
-
JZChecks if the first operand is zero and then jumps to the value of the second operand. For example: set the instruction pointer to
labelif the value atxis zero:JZ x, #label
-
LTChecks if the first operand is less than the second. If true, stores 1 in the third operand else stores 0. For example: check if the value at
xis less than 7 and store the result inresult:LT x, #7, result
-
EQChecks if the first operand is equal to the second. If true, stores 1 in the third operand else stores 0. For example: check if the value at
xis equal to 7 and store the result inresult:EQ x, #7, result
-
ARBAdjusts the relative base to the value of the first operand. For example: sets the relative base to the
messageaddress:ARB #message -
HLTHalts the program. For example:
HLT
-
DBPlaces raw data into the program. This must be a sequence of numbers or strings. Strings will be encoded as UTF-8. A label on a
DBinstruction will refer to the start of the data. For example the following specifies the string "Hello World!" with a newline.message: DB "Hello World!", 10
-
_Refers to an undefined address. This should be used to indicate that the address will be set at runtime.
-
ipRefers to the address of the next instruction. This can be used to dereference a pointer.
Consider the following example where ptr refers to some address and we want to
read a value into that address. _ is used because the value of ptr will be
filled as the parameter for the IN instruction by the ADD instruction.
ADD ptr, #0, ip+1
IN _
HLTLicensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.