I. Introduction
Recently, I ran across a programming-oriented game by the name of "Core War."
The objective of Core War is to create a program (called a "warrior") that survives the
longest within the Memory Array Redcode Simulator, or MARS for short. However, creating
a warrior is a daunting task, and has a steep learning curve for beginners. In this brief
guide, I shall explain the basics of the Redcode assembly language, and give you some tips
on how to create a successful warrior.
II. The Redcode Intsruction Set
The following section will describe each instruction that is available within the
Redcode assembly language. The syntax of any instruction is as follows:
CODE :
name source, dest
A. The ADD instruction
Well, if you haven't figured out what this one does yet, I'm not sure if
you should be reading this guide. ADD adds together the source and dest,
and stores it in dest.
B. The CMP instruction
CMP compares the instruction at source and the instruction at dest, and
skips the next instruction if they are equal.
C. The DAT instruction
This instruction is vital to winning any Core War game. Upon executing a
DAT, the warrior will terminate, and will be out of the game. This is one
of the only ways to kill a warrior. The arguments to this instruction have
no effect on how it executes.
D. The DIV instruction
As you might have guessed, DIV divides the source by dest, and stores it in
dest. If the divisor is 0, the warrior will terminate, as if it has received
a DAT.
E. The DJN instruction
DJN is a conditional jump, the opposite of an unconditional one. Conditional
jumps only jump if a certain condition is met. DJN decrements src by 1, and
if it is not zero, then it jumps to dest.
F. The JMP instruction
This is an unconditional jump. That means, no matter what, the next instruction
executed will be at src.
G. The JMN instruction
JMN is a conditional jump that checks to is if src is 0, and if it isn't, jumps
to dest.
H. The JMZ instruction
JMZ is a conditional jump that checks to is if src is 0, and if it IS,
(the opposite of JMN) then it jumps to dest.
I. The MOD instruction
MOD is a modulus function that takes the remainder of a division between src
and dest and stores it in dest. If the divisor is 0, the same action will be taken as
if it were a DIV.
J. The MOV instruction
MOV, well, moves the instruction at src to dest. It's easily the most simple instruction
in any assembly language.
K. The MUL instruction
MUL multipilies src by dest, and stores the result in dest.
L. The SEQ instruction
SEQ is exactly the same as the aforementioned CMP instruction.
M. The SNE instruction
SNE compares the instruction at source and the instruction at dest, and
skips the next instruction if they are not equal.
N. The SPL instruction
SPL is the most complicated instruction within all of Redcode. It starts a new
process at the location given by src, and the current process continues towards
the next instruction.
O. The SUB instruction
SUB subtracts src from dest and stores it in dest.
III. Facts about the MARS system
MARS works very differently than a regular PC. Its memory wraps around, which means
once you reach the limit of the memory, you start back again at 0. Also, MARS uses what is called
"relative addressing." This means that in your instructions, you refer to other instructions relative to
your location. An example would be in the first warrior created by A.K. Dewdney, known as the "Imp."
The code for the Imp is as follows:
CODE :
MOV $0, $1
Let's dissect what exactly this instruction is doing. First, it's a MOV instruction, so we know that it's
moving an instruction from one place to another. The $ sign before the numbers simply means it's "direct",
which we will cover in the next chapter. This code moves the instruction at the current location, 0,
(which is MOV $0, $1) to 1, which is 1 instruction above the current location. So, the Imp simply moves
itself to the next instruction indefinitely. This tactic easily overwrites the opposing warrior, but will not
kill it, so the game will end in a tie.
IV. The addressing modes
Addressing modes are certain modes applied to src and dest (from now on known as the A-field
and the B-field, respectively) that change how an instruction executes. When the rules for Core War
were first written in 1988, only 4 addressing modes were established. Later, in 1994, 4 more addressing modes
were added. The changes present in the 1994 addition to the Core War rules are known as the '94 standard.
A. The # (immediate) addressing mode
# is used to show that the number is actually a number, and not a reference to another
area. ADD #2, @1 would add the number 2 to the B-Field of the instruction at 1.
B. The $ (direct) addressing mode
$ is to display that you explicitly specify the whole instruction. MOV $0, $1 moves the
whole instruction to the location in front of it. Usually $ can be omitted in your code.
C. The * (A-field indirect) addressing mode
* shows that you want to take the value stored in the A-field of that instruction.
MUL *1, #2 will multiply the A-field of the instruction at 1 by the number 2.
D. The @ (B-field indirect) addressing mode
This is the exact same as the *, except instead of referencing the value in the A-field,
you're referencing the one in the B-field.
E. The { (A-field indirect with predecrement) mode
This is similar to the * mode, except that the value stored in the A-field of that instruction
is decremented before the current instruction is executed.
F. The < (B-field indirect with predecrement) mode
Again, this is exactly the same as the { addressing mode, but with the B-field instead.
G. The } (A-field indirect with postincrement) mode
This is the same as the * mode, except after the current instruction is executed, the
value stored at the A-field of that instruction is inremented by 1.
H. The > (B-field indirect with postincrement) mode
I'm sure by now you can guess what this mode does. :)
V. A final warrior example
This warrior is the second one created by A.K. Dewdney, and known as the "Dwarf." The dwarf
places DATs at intervals of 4 within the core. The following is the code for the dwarf.
CODE :
ADD #4, $3
MOV 2, @2
JMP -2
DAT #0, #0
Firstly, 4 is added to the B-field of the DAT instruction. Then, the DAT is copied to 4 instructions in front
of itself. Finally, the jump moves back 2 instructions, and 4 is added to the B-field in the DAT again. So, the Dwarf
simply copies DATs at 4, 8, 12, etc until it wraps around and starts copying all over again. This type of warrior
is known as a "bomber." There are other types of warriors that will be discussed in a later article.
VI. Epilogue
Redcode is a very interesting language, to say the least. It allows a programmer to hone his skills and
compete at the same time. Hopefully, you've learned a thing or two from this article. If not, I'm sure it was at least
a good review.
Cast your vote on this article *Note: the order of the votes has been reversed.
Comments: Published: 10 comments.
By: greyhat_i386 - 02:13 am Friday June 20th, 2008
Good work, must've taken a little while to write and put together. A thing or two was learned indeed.
Hey, apples, there is an error in "Dwarf" example: the first line of code should be "ADD #4, 3", not "ADD #4, @3". That's how it was in the original "Dwarf" code. This way the code will not run properly.
By: webdracula - 04:07 am Friday August 29th, 2008
Really helpfull, thank you.
This site is the collective work of the
HackThisSite staff. Please don't reproduce in part or whole without permission.
Page Generated: Tue, 14 Oct 2008 01:27:56 -0500 Exec:
9