Jump to content

Core War/Printable version

From Wikibooks, open books for an open world


Core War

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Core_War

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.

Redcode

Core War programs are written in a programming language known as Redcode. Redcode does not have access to direct input or output.

Opcodes

[edit | edit source]

Each opcode in Corewar contains three components: The instruction, register A, and register B. In the core, each memory section contains exactly one opcode, and provides no independent numbers.

The registers A and B are relative addresses, described in the Address modes below.

Opcode Description
DAT A,B Data; executing this instruction kills the task.
MOV #A,B If one is immediate, copied immediate number to B.
MOV A,B Moves instruction at A to B. However, if A is immediate, copies the immediate number to B.
ADD A,B Adds A to B.
SUB A,B Subtracts A from B.
MUL A,B Multiplies A and B to produce the result.
DIV A,B Divides B by A. Division by zero kills the process.
MOD A,B Modulus of B in A. Division by zero kills the process.
JMP A,B Jumps to A.
JMZ A B If *B is 0, jumps to A.
JMN A,B If *B is not 0, jumps to A.
DJN A,B Decreases *b. If non zero, jumps to A.
CMP A,B If *a == *B (or if immediate, #A == *B), skips an instruction
SPL A,B Creates a new task, which starts at B. Newer task starts first.
SEQ A,B Skips past the next instruction if A and B are equal.
SNE A,B Skips past the next instruction if A and B are not-equal.
SLT A,B Skips if *A < *B. (88 only)
XCH A,B At A, swaps A and B. (extended only)
PCT A,B At A, Protects operands from changing, until an instruction is written to that address.
NOP A,B Has no special effect (although operands are still evaluates.)
STP A,B Stores A into P-Space at location specified by B.
LDP A,B Retrieves data from P-Space at location specified by B into A.

Address modes

[edit | edit source]

Each opcode listed above contains two registers. These registers are composed of both a number and an address mode:

# Immediate. The number is directly in the opcode.
$ (or none) Direct. The opcode points to a cell relative to the current cell.
@ Indirect. The opcode points to a cell relative to the current cell. That cell's B value is added to the indirect pointer, to provide the target.
< Indirect, but the intermediate register is decreased before use.
> Indirect, the intermediate register is increased after use.
* A-field Indirect. The opcode points to a cell relative to the current cell. That cell's A value is added to the indirect pointer, to provide the target.
{ A-field Indirect, but the intermediate register is decreased before use.
} A-field Indirect, the intermediate register is increased after use.

Special commands

[edit | edit source]
END Stops compilation, further lines are treated as comments.
ORG Takes one parameter, which identifies the start location.
PIN Specifies P-Space identifier. If equal, the two programs share P-Space.
<label> EQU <A> Replaces all instances of <label> with <A>.

Instruction modifiers

[edit | edit source]
.A A -> A
.B B -> B
.AB A -> B
.BA B -> A
.F A->A and B->B
.X A->B and B->A
.I Entire instruction.

P-Space

[edit | edit source]

P-Space is a private storage used by programs across multiple runs of a program, that cannot be directly accessed by the opponent. However, attacks by programs can trick other programs into corrupting the P-Space region

In the opcode listing above, there are two instructions that can read and write to P-Space: STP and LDP.


Imp

The Imp is the simplest program that exists in Core War, consisting of one instruction:

MOV 0,1

The imp places focus on one task: survival. By continuously copying the current instruction to the next cell to be executed, it keeps it self alive simply by never encountering a DAT cell. If left untouched, it will eventually fill the entire core with the same instruction, causing other programs to become imps as well. However, under the rules of Core War, the Imp will never win a match - only draw it.

Since the Imp is a simple program, it can be countered. The basic method is to place either an "Imp gate" or "Alarm" that detects incoming movement. When it tripped, a nearby region is continuously bombed with a data instruction to prevent it from continuing.


Dwarf

Dwarf is a simple yet classic warrior. This program "bombs" the core at regularly space locations with DATs, at the same time making sure it doesn't hit itself.


        ADD #4, 3        ; execution begins here
        MOV 2, @2
        JMP -2
        DAT #0, #0