< Core War

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

Opcodes

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.

OpcodeDescription
DAT A,BData; executing this instruction kills the task.
MOV #A,BIf one is immediate, copied immediate number to B.
MOV A,BMoves instruction at A to B. However, if A is immediate, copies the immediate number to B.
ADD A,BAdds A to B.
SUB A,BSubtracts A from B.
MUL A,BMultiplies A and B to produce the result.
DIV A,BDivides B by A. Division by zero kills the process.
MOD A,BModulus of B in A. Division by zero kills the process.
JMP A,BJumps to A.
JMZ A BIf *B is 0, jumps to A.
JMN A,BIf *B is not 0, jumps to A.
DJN A,BDecreases *b. If non zero, jumps to A.
CMP A,BIf *a == *B (or if immediate, #A == *B), skips an instruction
SPL A,BCreates a new task, which starts at B. Newer task starts first.
SEQ A,BSkips past the next instruction if A and B are equal.
SNE A,BSkips past the next instruction if A and B are not-equal.
SLT A,BSkips if *A < *B. (88 only)
XCH A,BAt A, swaps A and B. (extended only)
PCT A,BAt A, Protects operands from changing, until an instruction is written to that address.
NOP A,BHas no special effect (although operands are still evaluates.)
STP A,BStores A into P-Space at location specified by B.
LDP A,BRetrieves data from P-Space at location specified by B into A.

Address modes

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

ENDStops compilation, further lines are treated as comments.
ORGTakes one parameter, which identifies the start location.
PINSpecifies P-Space identifier. If equal, the two programs share P-Space.
<label> EQU <A>Replaces all instances of <label> with <A>.

Instruction modifiers

.AA -> A
.BB -> B
.ABA -> B
.BAB -> A
.FA->A and B->B
.XA->B and B->A
.IEntire instruction.

P-Space

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.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.