Corewar

See all articles from CATS v6 n9/10
Author(s): Tim Swenson
Publication: CATS Newsletter
Pub Details: CATS v6 n9/10 (Volume 6, Issue 9-10)
Date: February/March 1989
Pages: 20

In May 1984 issue of Scientific American, A. K. Dewdney in his Computer Recreations column describes a game called Core War. The game is run by a monitor program called MARS (Memory Array Redcode Simulator). Essentially what the program does is to control two other programs written in an assembly-like language called REDCODE. The two programs are designed to try to destroy each other. They do this by somehow stopping the other program from running. They can put bad data in the middle of the program, take over control of the program, etc.

MARS is a computer simulation of a computer. It uses an array of strings to simulate memory spaces and executes programs that run in these spaces. The program executes one line of code from one Redcode program and then another line from the other. This continues until MARS can not execute a command. The program that bombs out is the program that loses.

Redcode is comprised of the following commands:

MOV A B – Move contents of address A to address B ADD A B – Add contents of address A to address B and put results in address B SUB A B – Subtract contents of address A from address B and put results in address B JMP A – Transfer control (jump) to address A JMZ A B – Jump to address A if contents of address B is equal to zero JMG A B – Jump to address A if contents of address B is greater than zero DJZ A B – Subtract one from contents of address B and jump to address A if contents of address B is equal to zero CMP A B – Compare contents of address A and B then skip next instruction if unequal DAT A – Non-executabel statement, used for storage of numbers RET – Used to end program. Not really part of REDCODE but it is used by the program to know the end of your program when reading it in from the data statements.

All of the addresses in Redcode are relative. PC is the Program Counter, this controls what line MARS is executing. MOV 0 1 means to put the contents of address PC+0 (in other words the current line) and put it in address PC+1 (the next line). Negative numbers are allowed to mean addresses before the PC.

Redcode does make provisions for direct and indirect addressing. A # before a number is direct addressing and an @ is used for indirect. MOV #0 1 means to put the number 0 in the address 1+PC. In indirect the commands:

DAT 20 MOV 0 @-1 means to put the contents of address 0+PC and store it at the address pointed at by the number at PC-1 (the previous line). MARS goes and gets the number stored at PC-1 and finds 20, it then puts the data at that address, PC+20.

Indirect addressing may be used for both A and B arguements, direct may be used for A, but direct may only be used for B with the CMP command. All other commands may not use direct addressing of B.

I must spend a minute and talk about the DAT command. The command itself does not execute, but it tells MARS that data is stored at this address. DAT statements are used similarly to variables. If you are going to add the contents of two addresses then they must be DAT statements. If a number is stored in an address that is not preceded by DAT it is considered a bug and the program bombs out.

The MOV and DAT commands interact different together. If you MOV #0 20 and address PC+20 is a DAT statement the 20 is added so that at PC+20 is stored DAT 0. But if PC+20 is not a data statement then just 0 is stored at PC+20. This is used to put “bombs” in programs. If you can drop a 0 in the middle of the other program then MARS will halt when it gets to that line causing the other program to crash.

Here are a few example programs:

IMP – copies itself to the next address and plows through memory.

MOV 0 1 RET

Dwarf – This puts 0’s in every 5th address, laying down a barrage of 0’s.

DAT -1 ADD #5 -1 MOV #0 @-2 JMP -2 RET

Gemini – This program copies itself forward 100 addresses in memory and then moves control to the copy.

DAT -2 DAT 99 MOV @-2 @-1 CMP -3 #8 JMP 4 ADD #1 -5 ADD #1 -5 JMP -5 MOV #-2 93

MOV #99 93 JMP 93 RET

To run the program, type the two programs you want into the data statements in lines 2000 (prog #1) and 3000 (prog #2). Then RUN the program. As each line of code is executed it is printed out on the screen along with the address at which it is at. This way you can the progress of the two programs. Also included in the program is a procedure called list_memory. This procedure copies out the contents of the memory array to the screen. This way you can see what is in memory.

There is a constant called top_mem. This defines how big the memory array is. If you find memory too small, just increase this constant.

100 DEFine PROCedure rs: DELETE flp2_Core_War_bas: SAVE flp2_Core_War_bas: END DEFine
110 top_mem=1000
120 DIM memory$(top_mem,14)
130 prog1=2000
140 prog2=3000
150 pc_prg1 = RND(1 TO 1000)
160 pc_prg2 = RND(1 TO 1000)
170 IF ABS(pc_prg1-pc_prg2)[illegible]100 THEN GO TO 150
180 load_prog
190 CLS #2: CLS #0 : CLS
200 PRINT #2," P R O G R A M # 1"
210 PRINT " P R O G R A M # 2"
220 REPeat main_loop
230 pc_main = pc_prg1
240 prog=1

Continued in the next issue

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.