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.

Thursday, 10 February 2011

An Early Description of Core War

Until I saw this description of Core War I assumed Dewdney and Jones were the first to use the term in the Core War Guidelines.

Core war is a game surreptitiously played by systems programmers on large installations, where a player's goal in each fixed time slice of real time is to propagate his program elsewhere in memory, while doing as much “damage” (read: clearing to zero) as possible at random places in the hopes of causing the opponent's program to blow up. The game of core war is rarely mentioned with more than a whisper, and thus tends to be lost amid the din of easier and less abstract games such as Star Trek, Adventure or Dungeons and Dragons
-- BYTE Magazine, July 1978, page 106-107


This brief description was published 6 years before A. K. Dewdney's first Core War article in Scientific American (May 1984, page 14-22).

Have you seen any other early mentions of Core War?

Monday, 17 January 2011

Revisiting the Dragon Curve

A few days ago I published code to draw the Heighway Dragon Curve. Here's a variation that's 3 lines shorter and plots the curve in 655360 cycles. The pMARS command line is pmarsv -s 90000 -c 1000000.

;redcode-fractal
;name Dragon Curve 2
;author John Metcalf

        width  equ 315
        stack  equ dragon+100
        plot   equ direct+width+1
        first  equ 80*width+50

        mov    #first,   plot
dragon  mov.ab count,    <plot
test    mov.b  @plot,    #0
        add.ba @plot,    direct
        div    #2,       @plot
        mod    #2,       test
        jmz    test,     test
        mod.a  #4,       direct
direct  add.b  3,        width+1
count   sub.ba #65536,   #1+1
        jmp    dragon,   -width+1

Sunday, 9 January 2011

Dragon Curve in Redcode

dragon curve in redcode

The Heighway Dragon Curve is fractal line than goes through a series of 90° turns, creating a pattern which fills a 2 dimensional space. The program plots 32768 points in 458741 cycles. The pMARS command line is pmarsv -s 90000 -c 500000.

;redcode-fractal
;name Dragon Curve
;author John Metcalf

        width  equ 315
        stack  equ dragon+100

dragon  mov.x  paira,    <stack
recur   djn    dragon,   #15
        mod.a  #4,       direct
        add.b  *direct,  plot
plot    mov    >recur,   80*width+110
        mov.ba >stack,   ret
ret     jmp    0,        }stack
turn    add.a  @stack,   direct
        mov.x  pairb,    <stack
        jmp    recur

direct  dat    3,        width
paira   dat    turn-ret, -1
        dat    0,        -width
pairb   dat    plot-ret, 1