Friday, 1 July 2011

A Brief Introduction to Core War

What is Corewar?


Corewar is a programming game in which the aim is to eliminate all opponents from the memory of a virtual computer. Players write short battle programs to compete against each other.

Battle programs are written in Redcode, the assembly language of the MARS virtual processor. Redcode has 18 opcodes. Each Redcode instruction is made up of an opcode, a source pointer, a destination pointer, a source addressing mode and a destination addressing mode. For example, the mov instruction copies information from the source to the destination:

mov 10, 20

In the example, the source is 10 and the destination is 20. When the instruction is executed, it take the information in the cell 10 locations after the mov and copies it to the cell 20 locations after.

Here's another example, known as an imp:

mov 0, 1

When this executes, it copies the information from 0 locations ahead (i.e. itself) to 1 location ahead:

mov 0, 1
mov 0, 1 <--- the copy

Execution continues with the next instruction, so the program executes the copy it just made. Addresses in Corewar are relative to the cell they're stored in, so when the copy executes it makes a new copy one location ahead:

mov 0, 1
mov 0, 1 <--- the copy
mov 0, 1 <--- the second copy

The addressing mode affects how the address is interpreted. If no addressing mode is specified, direct addressing is used as in the examples above. The alternatives are immediate and indirect addressing.

Immediate addressing is indicated by an octothorpe (#). Instead of using data a number of cells away, it uses the data stored in the current instruction. For example:

add #10, 20

The # shows the data to be used is 10, not the value from the cell 10 locations away. add #10, 20 adds 10 to the value in the cell 20 locations ahead.

Indirect addressing is indicated by one of the following symbols, < @ > { * }. Instead of using data from a number of cells away, that data is used as a pointer to the actual data to be used. For example:
add #10, @1
dat 0,   19

When the add instruction executes, it adds 10 to the cell 20 locations ahead. @1 tells the Redcode processor to use the destination of the cell 1 location away as a pointer. This contains 19, so the destination is 19 locations after the dat, or 20 after the add.

Here's a quick run down of addressing modes. A-field is another name for the source pointer and b-field is for the destination pointer:

  • < use the b-field as a pointer after subtracting 1 from it
  • @ use the b-field as a pointer
  • > use the b-field as a pointer and afterwards add 1 to it
  • { use the a-field as a pointer after subtracting 1 from it
  • * use the a-field as a pointer
  • } use the a-field as a pointer and afterwards add 1 to it
  • # use the data in the currently executing instruction

Here are the 18 opcodes supported by the Redcode processor:

  • mov - copy data from source to destination
  • add - add value from source to destination
  • sub - subtract value at source from destination
  • div - divide destination by source
  • mod - destination = destination MODULO source
  • mul - multiply destination by source
  • seq - skip next instruction if source equal to destination
  • sne - skip next instruction if source not equal to destination
  • slt - skip next instruction if source less than destination
  • jmp - jump to a-field
  • jmn - jump to a-field if b-field non-zero
  • jmz - jump to a-field if b-field zero
  • djn - subtract 1 from b-field. if result non-zero, jump to a-field
  • spl - start a new thread at a-field
  • dat - destroy thread when executed
  • stp - store source to private memory
  • ldp - load private memory to destination
  • nop - no operation

Top Corewar Links


  • Beginners' Guide to Redcode
    Ilmari's guide to Redcode provides a gentle introduction to the art of Corewar.
  • Corewar Bibliography
    The Corewar Bibliography is a comprehensive index of Corewar articles and the ideal starting point to research any Corewar topic.
  • Corewars - King of the Hill
    KOTH.org is the provider of several online hills. Submit your program by email to compete against others.
  • KOTH@SAL Corewar Hills
    The hills at SAL present some alternative settings, including the beginner hill and the popular nano hill.
  • CoreWar "Koenigstuhl" Page
    Koenigstuhl is the Valhalla of Corewar! When an author publishes their battle programs, they're archived on the Koenigstuhl infinite hills.
  • Programming in Corewar
    In Corewar a battle is played out between computer programs, which attempt to eliminate all opponents within the memory of the MARS virtual computer.
  • Fizmo's Corewar Info
    The ultimate source for any information regarding Corewar, a programming game where small programs fight each other.

No comments: