Core War

See all articles from CATS v6 n12
Author(s): Tim Swenson
Publication: CATS Newsletter
Pub Details: CATS v6 n12 (Volume 6, Issue 12)
Date: April 1989
Pages: 5-7

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)=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
250 cmd$=memory$(pc_main)
260 PRINT #2,pc_main;" ";cmd$
270 sep_linc
280 evaluate
290 IF pc_prg1=pc_main THEN
300 pc_prg1=pc_prg1+1
310 ELSE
320 pc_prg1=pc_main
330 END IF
340 pc_main = pc_prg2
350 prog=2
360 cmd$=memory$(pc_main)
370 PRINT pc_main;" ";cmd$
380 sep_line
390 evaluate
400 IF pc_prg2=pc_main THEN
410 pc_prg2=pc_prg2+1
420 ELSE
430 pc_prg2=pc_main
440 END IF
450 IF pc_prg1>top_mem THEN pc_prg1=pc_prg1-top_mem
460 IF pc_prg2>top_mem THEN pc_prg2=pc_prg2-top_mem
470 END REPeat main_loop
480 STOP
490 DEFine FuNction get_num(address)
500 LOCal temp$
510 temp$=memory$(address)
520 IF temp$="" THEN goto_error
530 IF temp$(1 TO 3) <> "DAT" THEN goto_error
540 RETurn temp$(5 TO)
550 END DEFine get_num
560 DEFine PROCedure sep_line
570 LOCal blank
580 IF LEN(cmd$)<5 THEN goto_error
590 arg2 = 0
600 opcode$=cmd$(1 TO 3)
610 cmd$=cmd$(5 TO)
620 opmodea=1 : opmodeb=1
630 IF cmd$(1)="#" THEN opmodea=0
640 IF cmd$(1)="@" THEN opmodea=2
650 IF opmodea<>1 THEN cmd$=cmd$(2 TO)
660 IF (opcode$="JMP") OR (opcode$="DAT") THEN arg1=cmd$ : RETurn
670 blank = " " INSTR cmd$
680 arg1 = cmd$(1 TO blank-1)
690 cmd$=cmd$(blank+1 TO)
700 IF cmd$(1)="#" THEN opmodeb=0
710 IF cmd$(1)="@" THEN opmodeb=2
720 IF opmodeb<>1 THEN cmd$=cmd$(2 TO)
730 arg2=cmd$
740 END DEFine sep_line
750 DEFine PROCedure evaluate
760 IF opmodea=0 THEN a=arg1
770 IF opmodea=1 THEN a=pc_main+arg1
780 IF opmodea=2 THEN a=pc_main+get_num(pc_main+arg1)
790 IF opmodeb=0 AND opcode$<>"CMP" THEN PRINT #0,"Direct mode not allowed to second argguement ": STOP
800 IF opmodeb=0 THEN b=arg2
810 IF opmodeb=1 THEN b=pc_main+arg2
820 IF opmodeb=2 THEN b=pc_main+get_num(pc_main+arg2)
830 IF a>top_mem THEN a=a-top_mem
840 IF b>top_mem THEN b=b-top_mem
850 IF opcode$="MOV" THEN MOV: RETurn
860 IF opcode$="ADD" THEN ADD: RETurn
870 IF opcode$="SUB" THEN SUBT: RETurn
880 IF opcode$="JMP" THEN JMP: RETurn
890 IF opcode$="JMZ" THEN JMZ: RETurn
900 IF opcode$="JMG" THEN JMG: RETurn
910 IF opcode$="DJZ" THEN DJZ: RETurn
920 IF opcode$="CMP" THEN CMP: RETurn
930 IF opcode$="DAT" THEN RETurn
940 goto_error
950 END DEFine evaluate
960 DEFine PROCedure load_prog
970 LOCal count, prog$
980 RESTORE prog1
990 count = pc_prg1
1000 REPeat loop
1010 READ prog$
1020 IF prog$="RET" THEN EXIT loop
1030 IF count>top_mem THEN count=count-top_mem
1040 memory$(count)=prog$ 1050 count=count+1
1060 END REPeat loop
1070 RESTORE prog2
1080 count = pc_prg2
1090 REPeat loop
1100 READ prog$ 1
110 IF prog$="RET" THEN EXIT loop
1120 IF count>top_mem THEN count=count-top_mem
1130 memory$(count)=prog$
1140 count=count+1
1150 END REPeat loop
1160 END DEFine load_prog
1170 DEFine PROCedure goto_error
1180 IF prog=1 THEN PRINT #2," E R R O R"
1190 IF prog=2 THEN PRINT "E R R O R"
1200 CLS #0 1210 PRINT #0,"A Winner has been found."
1220 PRINT #0,"Player #";prog;" has
lost."
1230 STOP
1240 END DEFine goto_error
1250 DEFine PROCedure MOV
1260 IF opmodea=0 THEN
1270 temp$=memory$(b)&" "
1280 IF temp$(1 TO 3)="DAT" THEN
1290 memory$(b)="DAT "&a
1300 ELSE
1310 memory$(b)=a
1320 END IF 1330 ELSE
1340 memory$(b)=memory$(a)
1350 END IF
1360 END DEFine MOV
1370 DEFine PROCedure ADD
1380 LOCal temp
1390 IF opmodea=0 THEN
1400 temp=get_num(b)+a
1410 ELSE 1420 temp=get_num(b)+get_num(a)
1430 END IF
1440 memory$(b)="DAT "&temp
1450 END DEFine ADD
1460 DEFine PROCedure SUBT
1470 LOCal temp
1480 IF opmodea=0 THEN
1490 temp=get_num(b)-a
1500 ELSE
1510 temp=get_num(b)+get_num(a)
1520 END IF
1530 memory$(b)="DAT "&temp
1540 END DEFine SUBT
1550 DEFine PROCedure JMP
1560 pc_main = a
1570 END DEFine JMP
1580 DEFine PROCedure JMZ
1590 LOCal temp
1600 temp = get_num(b)
1610 IF temp=0 THEN JMP
1620 END DEFine JMZ
1630 DEFine PROCedure JMG
1640 LOCal temp
1650 temp = get_num(b)
1660 IF temp>0 THEN JMP
1670 END DEFine JMG
1680 DEFine PROCedure DJZ
1690 LOCal temp
1700 temp = get_num(b)
1710 temp=temp-1
1720 memory$(b)="DAT "&temp
1730 IF temp=0 THEN JMP
1740 END DEFine DJZ
1750 DEFine PROCedure CMP
1760 LOCal tempa,tempb
1770 IF opmodea=0 THEN
1780 tempa=a
1790 ELSE
1800 tempa=get_num(a)
1810 END IF
1820 IF opmodeb=0 THEN
1830 tempb=b
1840 ELSE
1850 tempb=get_num(b)
1860 END IF
1870 IF tempa<>tempb THEN pc_main=pc_main+2
1880 END DEFine CMP
1890 DEFine PROCedure list_memory
1900 FOR x=1 TO top_mem
1910 PRINT memory$(x)
1920 NEXT x
1930 END DEFine list_memory
2000 DATA "DAT -2","DAT 99","MOV @-2 @-1","CMP -3 #8","JMP 4","ADD #1 -5"
2010 DATA "ADD #1 -5","JMP -5","MOV #-2 93","MOV #99 93","JMP 93","RET"
3000 DATA "DAT -1","ADD #5 -1","MOV #0 @-2","JMP -2","RET"

Transcription note: Line 170 prints =100 where the program logic suggests <100. The stray 1 at the end of line 1100 is probably the first digit of line 1110, displaced in typesetting. Several pairs of lines print run together, as the typesetter left them.

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