esolang

Redcode (Core War)

Assembly for an arena. Two programs share one circular memory, and each tries to make the other run into a DAT.

A. K. Dewdney and D. G. Jones, 1984 · Turing complete: with unbounded memory · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

The idea

Core War is a game for programs, not people. A. K. Dewdney and David Jones designed it in 1983, and Dewdney described it in his "Computer Recreations" column in Scientific American in May 1984. Redcode is its assembly language.

Read the story

Two programs, called warriors, are loaded into the same memory, the core. They take turns, one instruction each. Memory has no protection: anyone can read or overwrite anything. A process that executes a DAT (data) instruction dies. The last warrior with a live process wins.

The trick that makes it work: every address is relative to the instruction that uses it. MOV 0, 1 means "copy this cell to the next cell". So code runs the same wherever it lands, and a program can copy itself somewhere else and keep working there. That one design choice made self-copying programs easy, and Core War became an ancestor of digital evolution: Tierra grew out of the same world of programs fighting over memory.

There are three standards: ICWS-86, ICWS-88, and the 1994 draft, which was never formally adopted but is what most people play today.

The machine

A circular core of cells (8,000 is the classic size). Each cell holds an instruction with an opcode and two fields, A and B. Empty cells hold DAT 0, 0. Each warrior has a queue of processes; SPL adds one.

OpcodeDoes
DATData. Executing it kills the process
MOV A, BCopy cell A to cell B (with #A, write the number A into B's B-field)
ADD / SUBAdd or subtract A into B
JMP AJump to A
JMZ / JMN A, BJump to A if B is zero / not zero
DJN A, BDecrement B; jump to A if it is not zero
CMP A, BSkip the next instruction if A and B are equal
SPL ASplit: start a new process at A, and continue here too
ModeMeaning
#5Immediate: the number 5 itself
5 or $5Direct: the cell 5 ahead
@5Indirect: go 5 ahead, then add that cell's B-field
<5Like @, but first decrement that B-field

Redcode-94 adds more opcodes, more modes and modifiers like MOV.I.

Example

The Imp is one instruction long.

The Imp is one instruction long. It copies itself one cell ahead, then executes the copy:

MOV 0, 1

The Dwarf drops a DAT bomb on every fourth cell, forever:

ADD #4, 3      ; move the target 4 further on
MOV 2, @2      ; copy the bomb to where the bomb's B-field points
JMP -2         ; again
DAT #0, #0     ; the bomb, and the target counter

A replicator in the style of the classic Mice: count down from 7, copying cell after cell 833 cells ahead, split into the copy, aim 653 further on, repeat.

ORG 1           ; start on line 1, not on the data cell
DAT #0, #0      ; counter
MOV #7, -1      ; counter := 7
MOV @-2, <5     ; copy cell (counter) to just before the target, then move the target back
DJN -1, -3      ; counter -1; loop while it is not zero
SPL @3          ; start a process in the fresh copy
ADD #653, 2     ; aim the next copy elsewhere
JMZ -5, -6      ; and start over
DAT #0, #833    ; target

Imp versus Dwarf comes out a tie after 80,000 cycles: the imp crawls over the dwarf's code, and the dwarf's own process then executes MOV 0, 1 and becomes an imp too. The mouse, alone, has 28 copies of itself in core after 1,000 cycles and 128 after 20,000.

Patterns it teaches

  • Self-replication: the imp is the smallest replicator there is; the mouse is a real copy loop, one cell per pass.
  • Self-modifying code: the dwarf's last line is both its bomb and its loop counter, rewritten every pass.
  • Code as data: there is no other kind of data. A bomb is an instruction; a pointer is a field of an instruction.
  • Artificial life: Core War with random mutations was one of the first digital-evolution experiments. See Tierra.

Try this

  1. Change the Dwarf's #4 to #3. Predict what happens (hint: 8,000 is not a multiple of 3), then run it alone.
  2. Change the mouse's 833 and 653. Which values make it overwrite itself and die?
  3. Hard: write a warrior that reliably beats the Imp, not just ties it.

Further

  • esolangs.org/wiki/Redcode
  • A. K. Dewdney, "Computer Recreations: In the game called Core War hostile programs engage in a battle of bits", Scientific American, May 1984.
  • corewar.co.uk: history, standards and the pMARS simulator.

Programming games

Self-modifying

Write your own in Run Convert a program from Redcode (Core War)

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: redcode.

Command line

shib run redcode -e 'DAT #0, #0
END
'
shib manual redcode

Source

Module
langs/museum/ports.js
Interpreter
langs/museum/redcode.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org