Redcode
Redcode is an esoteric programming language designed by A. K. Dewdney and David Jones in autumn 1983 and first described in the May 1984 issue of Scientific American. The purpose of Redcode is to provide an environment for Core War, in which programs attempt to crash their opponents. Redcode is (almost) Turing Complete in the same sense as C, where any given program must run within finite memory space but may be agnostic to the exact size.
Instructions
Opcode | Description |
---|---|
DAT | When executed, the current process is terminated |
MOV | Copies the contents of the A-field to the B-field |
ADD | Adds the contents of the A-field to the contents of the B-field |
SUB | Subtracts the contents of the A-field from the contents of the B-field |
MUL | Multiplies the contents of the A-field by the contents of the B-field. Redcode-94 only. |
DIV | Divides the contents of the A-field by the contents of the B-field. Division by zero terminates the executing process. Redcode-94 only. |
MOD | Divides the contents of the A-field by the contents of the B-field and sets the B-field to the remainder. Modulus by zero terminates the executing process. Redcode-94 only. |
JMP | Transfers control to the location specified in the A-field |
JMZ | Transfers control to the A-field location if the B-field is 0 |
JMN | Transfers control to the A-field location if the B-field is not 0 |
DJN | Decrements the B-field. Transfers control to the A-field location if the B-field is not 0 |
SEQ | Skips the next instruction if the A-field and B-field are equal. CMP is a synonym |
SNE | Skips the next instruction if the A-field and B-field are not equal. Redcode-94 only. |
SLT | Skips the next instruction if the A-field is less than the B-field |
SPL | Start a new process at the location specified by the A-field |
NOP | Do nothing |
STP | Copies the A-field to the private memory specified by the B-field. Redcode-94 only, and only on hills where P-space is allowed. |
LDP | Copies the private memory specified by the A-field to the B-field. Redcode-94 only, and only on hills where P-space is allowed. |
STS | Write the character in the A-field to STDOUT. Non-standard. |
LDS | Read a character from STDIN and store in the A-field. Non-standard. |
Example
Hello, World
This program prints out the words Hello, World!:
; Redcode Hello World, John Metcalf write sts.a hello, 0 sts.b }write, 0 djn write, #7 hello dat 72, 101 ; He dat 108, 108 ; ll dat 111, 44 ; o, dat 32, 87 ; W dat 111, 114 ; or dat 108, 100 ; ld dat 33, 10 ; !\n
Imp
This program copies itself forward forever, turning other processes into copies of itself. Somewhat hard to kill, but always overwrites enemies with executable instructions. Note that this program relies on the fact that all Redcode addressing is relative--in fact, no position in memory is privledged in any way as the "first", since Redcode memory is circular.
; "Imp", A. K. Dewdney mov 0, 1 ; Alternate versions using redcode-94 instruction modifier semantics: mov.i #8, 1 ; A-field can store arbitrary data. mov.i #1, *0 ; Copies itself via A-field indirection.
Dwarf
This was the first offensive program. It scores 25% wins and 75% ties against Imp, and 37.5% wins, 37.5% losses, and 25% ties against itself. Additionally, in cores of size divisible by four (which are very common), it will never overwrite its execution footprint with a terminal DAT. By modern standards, though, it is very weak.
; "Dwarf", A. K. Dewdney mov 4, @4 ; Move the bomb to the location pointed to by the bomb. (B-indirect.) add #4, 3 ; Immediate add adds the immediate A-operand to the B-field of the cell pointed to by the B-operand. jmp -2 ; Jump back two steps (to the MOV) dat #4 ; The bomb. After enough cycles, dwarf will overwrite the bomb with itself, harmlessly. It then restarts the bombing run.
External resources
- Annotated Draft of the Proposed 1994 Core War Standard
- Thoughts on Corewar..., impomatic's Redcode programming blog
- Core War (Wikipedia), including an overview of different strategy types