An Introduction to Corewar
Written by Lawrence Woodman on 30th April 2009     [2 Comments]
Tags: Corewar, Programming, Programming Games, Retro

I remember reading about Corewar roughly 20 years ago and thinking that I will have to have a go at that when I get the time.  As often happens in life, things got in the way and I only recently managed to give it a go.  I love the challenge of programming and the competitive aspect of this programming game really appeals to me.  The object of the game is to write a battle program that will take over a virtual computer and kill the other battle programs running upon it.  Since the game recently celebrated it's 25th birthday, I thought that I would write the following brief introduction to the game to whet peoples' appetite and encourage new interest.

History

The game was first described in Core War Guidelines by D. G. Jones and A. K. Dewdney in March 1984 and was introduced to the public in May 1984 via Dewdney's Computer Recreation column in Scientific American.

There have been several revisions to REDCODE (the form of assembly language in which the programs are written).  The most used is currently The International Core Wars Society (ICWS) 1994 standard with the extensions introduced by pMARS 0.8.  There is also some interest in the older ICWS '88 standard.

The MARS Environment

The programs are run in the MARS (Memory Array Redcode Simulator) environment.  The object of the game is for the programs to take over the environment's memory (Core), and kill the other programs.  The core is circular and memory addresses are relative to the current position.  This is so that it does't matter where a program starts in the core.  The biggest difference from a real processor is that each memory location consists of an instruction and its two operands.

As mentioned above, the programs are written in a form of assembly language called REDCODE.  The most common version of this gives the following instructions:

DATDataKills the current process
MOVMoveCopies data from one address to another
ADDAddAdds one number to another
SUBSubtractSubtracts one number from another
MULMultiplyMultiplies one number by another
DIVDivideDivides one number by another
MODModulusDivides one number by another and gives the remainder
JMPJumpContinues execution from another address
JMZJump if zeroTests a number and jumps to an address if it's 0
JMNJump if not zeroTests a number and jumps if it isn't 0
DJNDecrement and jump if not zeroDecrements a number by one, and jumps unless the result is 0
SPLSplitStarts a second process at another address
CMPCompareSame as SEQ
SEQSkip if equalCompares two locations, and skips the next instruction if they are equal
SNESkip if not equalCompares two locations, and skips the next instruction if they aren't equal
SLTSkip if less thanCompares two locations, and skips the next instruction if the first is less than the second
LDPLoad from p-spaceLoads a number from private storage space
STPStore to p-spaceStores a number to private storage space
NOPNo operationDoes nothing

In addition, there are a number of different addressing modes and instruction modifiers which change the way that they act.  These combine to make REDCODE able to express a lot in very little code.

Example Programs

I have provided slightly altered versions of the standard example programs written by A. K. Dewdney to give you an idea of how the programs work.

IMP

This is one of the simplest program that can be written in REDCODE.  It just replicates itself throughout the core and consists of only one instruction:

    mov.i 0, 1

This copies memory location 0 to memory location 1.  The .i suffix is an instruction modifier which means that mov is to copy the whole of the memory location.  Therefore, in this case the instruction copies the current instruction at location 0 (remember that the addressing is relative and a memory location contains an instruction and it's operands) to location 1 (the next location).  Execution then continues at the next memory location, which now contains the same instruction as the last, so this program rapidly replicates itself throughout the core.

However, the program does not kill the other programs, so where it writes over another program, the other program will just execute this code instead.  This is useful to make another program ineffective, but it still needs to be killed by getting it to execute a DAT instruction, otherwise the outcome is likely to be a tie.

DWARF

As mentioned above the IMP won't kill another process.  To do this you need to get a process to execute a DAT instruction.  This is therefore what the following program does:

    add.ab  #4, 3        
    mov.i   2, @2
    jmp    -2
    dat    #0, #0

The program starts by adding the literal number 4 to memory location 3, which contains the DAT instruction.  This DAT instruction is known as the bomb as it is what will stop another process.  The .ab suffix indicates that the A field (containing #4) of the source should be added to the B field (containing #0) of the destination.  The bomb at memory location 2, is then copied to the memory location pointed to by the B field of the bomb.  In-direct addressing via the B field is indicated by the @ symbol.  Finally the program starts back again at the beginning.  You can therefore see that this copies a bomb to every fourth memory location, and if the core is divisible by four, it will never bomb itself.

Strategies

Strategies range from the examples given above of code that replicates itself or bombs other programs, to code that scans for other programs before disabling them or killing them.  Decoys can be used to fool the scanners and you can combine strategies to increase effectiveness.  The latest developments are self-repairing code and battle programs that have been evolved.  The possibilities really are endless.

Competing and Other Activities

There are a number of ways that you can compete and add interest to REDCODE programming:
  • You can run other peoples' programs against your own via a simulator on your computer.
  • Once you have created an effective battle program you could submit it to a hill such as King of the Hill, where it will compete against other battle programs.
  • Engage in The Mini Challenge, where specific programming challenges are set.
  • You can use the constraints placed upon the programmer by REDCODE as an interesting platform to experiment with.  This is the focus of a lot of the posts on Thoughts on Corewar....

Where Now?

To begin you will need to download a simulator such as pMARS and a tutorial such as The beginners' guide to Redcode.  Further information can be found at the excellent site: Corewar - the war of the programmers.

I must warn you before you try it yourselves; it is incredibly addictive as you will nearly always want to make just one more tweak to improve a battle program.  Have fun, but remember it is only a game.




Creative Commons License
An Introduction to Corewar
by TechTinkering is licensed under a Creative Commons Attribution 2.0 UK: England & Wales License.
Comments


Krylen   (30 April 2009 20:04)
Wow‚ thank you so much for this post. I had briefly read about Corewar some years ago while I was in college‚ but never did get around to actually learning and playing it (much like yourself). I never realized there was such a vibrant community of players and contests available for me to interact with. Now that I know about all these venues for competition (and years of real world experience has made me a much better programmer) I really want to get involved! I have some very good ideas on how to take the advantage..  Mwa-ha-ha-ha..  see you in the cyclic buffer ;)



jungle   (30 April 2009 21:00)
Wow‚ the memories. I did write a couple of Mars simulators (the last one for a Mac Plus)‚ and actually participated in a local tournament with my own creation. I did happen to win with a dull but efficient program (three simple bombers that carpeted the core with co-prime spaced bombs and killed itself in the long run)‚ against crazily imaginative but slow programs that were huge targets. If it was up to me and not to the cold results of the Mars runs‚ they would have won. Also‚ I remember being asked for an interview by a journalist looking for some insight into the mind of a hacker‚ to which I asnwered that we weren't the kind of hackers he was after‚ that our kind achieved nirvana for shaving a bit or a cpu cycle off some code using some obscure trick‚ after which he completely lost interest. Ha!


Add a Comment

Please leave a comment.  I would love to hear what you think.
All comments are checked before displaying to prevent SPAM.

Your Name (Optional)

Your Email (Optional)
Not displayed, just used to pickup a Gravatar

Your Website (Optional)

Comment

All comments preformatted i.e. newlines and spaces display properly.
All HTML will be displayed literally e.g. <br /> will not create a line break.
URLs beginning with http:// or ftp:// will be converted to links.

        Privacy Policy

Contact Details

I would love to hear from you and welcome your feedback on the site.

You can keep in contact with me through the following services:
Click to get email address
Twitter
Delicious
StumbleUpon
YouTube
Technorati
FriendFeed
MyBlogLog