Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Redcode

PietHelzel edited this page Oct 23, 2024 · 13 revisions

Important Links

Guide

Instructions

An instruction looks like the following:

MOV.AB @-2, #3

It is made up of the following parts:

  • Opcode (e.g. MOV): The instruction name.
  • Modifier (e.g. AB): Modifies the instruction by specifying which operands it works on.
  • Source field (e.g. @-2 and destination field (e.g. #3):
    • Each made up of an address mode (like @ or #) followed by a number.
    • The comma separator is necessary!

Address modes

Each instruction can have an address mode for each of its fields. The following is a list of all possible address modes:

  • $ - direct addressing
  • # - immediate
  • * - a-field indirect addressing
  • @ - b-field indirect addressing
  • { - a-field indirect addressing with predecrement
  • < - b-field indirect addressing with predecrement
  • } - a-field indirect addressing with postincrement
  • > - b-field indirect addressing with postincrement

Direct Addressing

Direct addressing directly points to the instruction specified by the number. For example, a MOV $0, $1 copies the 0th instruction (the MOV command itself) one space ahead in memory. The dollar sign is optional, if no address mode is specified, direct addressing is used automatically.

Immediate

A literal number. If decoded as an address, it always points to address 0.

Indirect addressing

Indirect addressing looks into the a- or b-field (whichever is chosen by the specific address mode) of the nth instruction. This value is then used as a direct pointer to the instruction that will actually be used. Here is an example:

-1 | DAT #0, #4 ;  ---.
0  | MOV 0, @-1 ;     |
1  | ...        ;     | +4
2  | ...        ;     |
3  | MOV 0, @-1 ; <---'

When the MOV is executed, it looks at the b-field of instruction -1 (the DAT instruction). The value is 4. It then uses that as a pointer from the DAT instruction (not from the MOV!) to reach the actual destination instruction. In conclusion, the MOV copies itself to instruction 3 (4 ahead of the DAT).

Predecrement and Postincrement

If a predecrement or postincrement addressing mode is used, the intermediate value is either

  • decremented and then used as a pointer in the case of a predecrement.
  • used as a pointer and then incremented in the case of a postincrement.

Instruction Set

DAT

Kill the process. When all processes of a player are killed, they lose.

MOV

Copies data from the source address to the destination address. The specific data is determined by the modifier. For example, the AB modifier copies the a-field from the source instruction to the b-field of the destination instruction. The I modifier copies the entire instruction.

Arithmetic Instructions (ADD, SUB, MUL, DIV, MOD)

ADD, SUB, MUL, DIV and MOD are all very similar, just computing the specified mathematical operation on the arguments specified by the modifier. For example, the ADD.AB instruction would add the a-field of the source instruction and the b-field of the destination instruction and save it to the b-field of the destination instruction. The I and the F modifiers act the same, computing the operation of the a-fields and the b-fields, then saving the result in the respective field in the destination instruction.

Special cases

Division by zero using the DIV or MOD instructions acts like a DAT instruction, killing the process and leaving the destination unchanged. If two divisions are done using the F or X modifiers, and one is a divide by zero, the other division is still performed.

Clone this wiki locally