Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🎁 Assembler and runner for the Intcode computer from 🎄 Advent of Code 2019

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

rossmacarthur/intcode

Repository files navigation

Assembler and runner for the Intcode computer from Advent of Code 2019.

Hello World! program

The following program outputs "Hello World!".

    ARB #message  ; move the relative base to the beginning of our messageloop:OUT rb        ; output the current character in the message    ARB #1        ; move the relative base to the next characterJNZ rb, #loop ; if the next character is non-zero then go back to `loop`HLTmessage:DB"Hello World!\n"

Assembly language

The compiler can assemble the following instruction set specification into anIntcode program.

General

Intcode assembly must be written in a UTF-8 encoded file with Unix line endings.Comments start with a semicolon;.

Operand types

There are two types of operands.

  • Label

    A label refers to an address in a program. For example:end in the following program refers the address of theHLT instruction.

    JZ #0, #endend:HLT
  • Number

    A binary, octal, decimal, or hexadecimal number. This can be used forspecifying manual addresses, address offsets, or exact values. For example:the following reads in a value, minuses 3 from it, and outputs the result.

    IN  xADD x, #-0b11, x+1OUT x+0x1HLT

Operand modes

There are three ways to specify the operands for different instructions.

  • Positional

    Specifies a value by specifying theaddress it should be read from. Forexample:

    • 19 specifies the value at address 19.
    • x+3 specifies the value at the labelx with an offset of 3.
  • Immediate

    Specifies a value by specifying the exact value. For example:

    • #19 specifies the exact value 19.
    • #x+3 specifies the exact label addressx with an offset of 3.
  • Relative

    Specifies a value by specifying theaddress it should be read from as anoffset of therelative base. For example:

    • rb+3 specifies the value at the relative base address with an offset of 3.

Opcodes

The following operations are supported, roughly described in the order they areintroduced in Advent of Code.

  • ADD

    Adds the first two operands and stores the result in the third. For example:increment the value atx:

    ADD x, #1, x
  • MUL

    Multiplies the first two operands and stores the result in the third. Forexample: multiply the value atx by 2:

    MUL x, #2, x
  • IN

    Reads a single number and stores it in the first operand. For example: storeinput atx:

    IN x
  • OUT

    Outputs a single number and stores it in the first operand. For example:output the value atx:

    OUT x
  • JNZ

    Checks if the first operand is non-zero and then jumps to the value of thesecond operand. For example: set the instruction pointer tolabel if thevalue atx is non-zero:

    JNZ x, #label
  • JZ

    Checks if the first operand is zero and then jumps to the value of the secondoperand. For example: set the instruction pointer tolabel if the value atx is zero:

    JZ x, #label
  • LT

    Checks if the first operand is less than the second. If true, stores 1 in thethird operand else stores 0. For example: check if the value atx is lessthan 7 and store the result inresult:

    LT x, #7, result
  • EQ

    Checks if the first operand is equal to the second. If true, stores 1 in thethird operand else stores 0. For example: check if the value atx is equalto 7 and store the result inresult:

    EQ x, #7, result
  • ARB

    Adjusts the relative base to the value of the first operand. For example: setsthe relative base to themessage address:

    ARB #message
  • HLT

    Halts the program. For example:

    HLT

Pseudo-opcodes

  • DB

    Places raw data into the program. This must be a sequence of numbers orstrings. Strings will be encoded as UTF-8. A label on aDB instruction willrefer to the start of the data. For example the following specifies the string"Hello World!" with a newline.

    message:DB"Hello World!",10

Special labels

  • _

    Refers to an undefined address. This should be used to indicate that theaddress will be set at runtime.

  • ip

    Refers to the address of the next instruction. This can be used to dereferencea pointer.

Consider the following example whereptr refers to some address and we want toread a value into that address._ is used because the value ofptr will befilled as the parameter for theIN instruction by theADD instruction.

ADD ptr, #0,ip+1IN  _HLT

License

Licensed under either of

at your option.

About

🎁 Assembler and runner for the Intcode computer from 🎄 Advent of Code 2019

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp