Redcode

From Esolang
(Redirected from Core War)
Jump to navigation Jump to search
Redcode
Paradigm(s) imperative
Designed by A. K. Dewdney & David Jones
Appeared in 1983
Computational class Turing-complete
Major implementations pMARS, nMars, CoreWin
Influenced by assembly code
Influenced CoreLife and many others
File extension(s) .red

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. In fact, only one of the instructions is required to achieve that: DJN.

Core War is the most famous programming game, a kind of game where humans don't directly play, but instead write programs (warriors) to compete against other programs on servers called "hills". Another good example of a programming game is BF joust, which is much easier than Redcode.

Redcode is not really esoteric, but closer to assembly code, although it is often considered "weird" for assembly. The latest standard is '94, which is the commonly accepted standard.

Well either by accident or design the rules of corewar has led to a diversity of warriors where any type of warrior always has something which can beat it.

- Scott's Quick Core War Guide

Syntax

Each command is one line consisting of an opcode and two addresses, namely A-field and B-field. All addresses are relative, so for example 1 means the next instruction and -1 means the previous. There are also several addressing modes:

Symbol Decription
# Immediate: This means the "address" is an integer and will be treated as 0 for address-demanding instructions.
$ Direct: This is the default mode which is used when no symbol is specified.
@ Indirect: The final address is the address specified by where the address points to.
< Same as @, but where the address points to is decremented by one before usage.
> Same as <, but it increments instead, and after usage.
* Same as @, but it reads the A-field instead of the B-field like everything above.
{ Same as <, but reads A-field
} Same as >, but reads A-field

The '94 standard also adds instruction modifiers, which modify how the instructions treat the A and B-fields. Valid ones are: .a, .b, .ab, .ba, .f, .x, .i.

Instructions

This table contains heavy ambiguity. For a clearer description, refer to the external resources below.

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.

Non-standard instructions are not listed.

Examples

This section lists the two simplest programs, as well as something to demonstrate Redcode's usage in ordinary programming.

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 (stone). 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.

Hello, World

This program prints out the words Hello, World!, using the non-standard instruction sts:

; 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

See also

External resources

Information sites

Competitive hills

Warning: All hills except for the Beginners' hill are for experts! Do not attempt at the other hills until you have topped the beginners' hill. If you're using an evolver, the Nano hill should be easy.

Important: All of the hills require entries to be sent in email and in plain text. If you're doing it correct but they don't respond after several hours, it means you're not sending the email in plain text format, so look though settings and change them so that your email will be sent in plain text.