home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / pascal / corwar.zip / CORWAR.DOC < prev    next >
Text File  |  1992-03-15  |  8KB  |  173 lines

  1.      Core War is a computer game played both with and BY  compu-
  2. ters.  In Core War,  two player-written computer programs operate
  3. concurrently in a "circular"  memory  array.  A program "loses" 
  4. when it hits an instruction it cannot execute. 
  5.  
  6.      Core War is described more fully by A. K. Dewdney in the 
  7. "Computer Recreations" column of Scientific  American, May 1984.
  8. A short synopsis follows:
  9.  
  10.      Core War battle programs are written in a special assembler-
  11. style  language called Redcode,  which defines only 9  operations.
  12. Unlike  regular  assembly code,  any arguments  that  an 
  13. instruction  may  have are considered to be contained in a single 
  14. memory location,  so addressing is very simple.  For example, 
  15. the instruction JMP -2 simply means jump back 2 locations  before 
  16. this  one -- JMP 0 would be an endless loop on that one location, 
  17. and  JMP 1 simply moves to the next  location  -- essentially,  a 
  18. "No-operation" Instruction.
  19.  
  20.      Neither  program originally knows where in memory the  other 
  21. is,  or even where the program itself has started -- however, the 
  22. memory array  used by Core War is "circular," and all addressing
  23. is relative, so absolute memory addresses are not important.
  24.  
  25.      The nine Redcode instructions are:
  26.  
  27.      Instr:    Arguments:     Description/Result:
  28.  
  29.      MOV       A    B         B will equal A (A is unchanged)
  30.  
  31.      ADD       A    B         B will equal B + A (A unchanged)
  32.  
  33.      SUB       A    B         B will equal B - A (A unchanged)
  34.  
  35.      JMP       A              Program will jump to A
  36.  
  37.      JMZ       A    B         Program will jump to A if B is zero
  38.  
  39.      JMG       A    B         Program will jump to A if B > 0
  40.  
  41.      DJZ       A    B         B will equal B - 1; if this is 
  42.                               zero, program jumps to A
  43.  
  44.      CMP       A    B         Skip the next instruction unless A = B
  45.  
  46.      DAT            B         B is data.  A PROGRAM LOSES WHEN IT TRIES
  47.                               TO EXECUTE A "DAT" STATEMENT.
  48.                               
  49.      There  are  also three addressing modes,  or ways  in  which 
  50. arguments  can  be presented.  They  are  Direct,  Indirect,  and 
  51. Immediate.  In Direct mode, the argument is taken as an offset to 
  52. the current address.  This is the default mode.  Thus, the state-
  53. ment DJZ 12 -1 would decrement the previous location, and if that 
  54. result  were  zero,  would jump forward 12 locations (or  if  not 
  55. zero, it would move ahead one, normally).
  56.  
  57.      In Indirect mode,  denoted by the "@" symbol,  the indicated 
  58. relative  address contains a further relative  address.  Consider 
  59. the four-statement sequence:  JMP   @1
  60.                               DAT           1
  61.                               JMP    12
  62.                               CMP   -10    -11 
  63.       The first  jump statement will look for its argument in 
  64. relative address  +1.  In this example, it finds a 1,  which
  65. RELATIVE TO THAT ADDRESS is 1 -- it points to the statement JMP 12.
  66. Had it been a 2,  it would have  pointed to the CMP instruction.
  67. A -1 would have pointed to 
  68. the original JMP @1, and been an endless loop (assuming the enemy 
  69. program never changes either location).  Had it been 0,  the pro-
  70. gram  would  have jumped to the DAT ststement  and  been  halted, 
  71. having "committed suicide."
  72.  
  73.      Finally,  Immediate  mode takes the argument as an  absolute 
  74. number,  and is denoted by the "#" sign.  CMP #2 -3 will check to 
  75. see  if the value at offset -3 is equal to 2.  Immediate mode has 
  76. two special cases which are very important to Core Wars -- first, 
  77. a MOV statement with an Immediate argument in the first  argument 
  78. will  cause  the  argument addressed in the  second  argument  to 
  79. become  a  DAT instruction. (Had the argument not been  Immediate, 
  80. MOV would instead have copied everything,  arguments and instruc-
  81. tions.  Thus, a battle program can move itself around in memory.)
  82. This  is a very handy way of planting "bombs" in the  enemy  pro-
  83. gram's code.
  84.  
  85.      The  second  important thing to realize is that you can  NOT 
  86. have an Immediate,  or absolute, address. If the operating system 
  87. sees an Immediate-mode argument as an offset,  it will automatic-
  88. ally assume an offset of zero, which can have disastrous results.
  89.  
  90.      Which argument is which?   The second,  or "B" argument,  is 
  91. the one that contains data for ADD, SUB, CMP, and the conditional 
  92. jump  instructions.  Thus,  when  the first  instruction  of  the 
  93. Redcode sequence ADD #2   1
  94.                  CMP  3  15 is performed, the second line will be 
  95. changed to compare locations 3 and 17. The first, or "A" argument 
  96. is almost never altered by Redcode operations (except for MOV).
  97.      Both battle programs are executed by the Core War operating 
  98. system,  MARS.  In  the version presented here,  MARS is also the 
  99. program  loader;  programs may be loaded either from the keyboard 
  100. or from disk (when you give the program a name, it will check the 
  101. directory for that name -- if it finds it,  it will load it  from 
  102. disk,  assuming the program to be an ASCII file). The MARS inter-
  103. preter  simply keeps switching its program counter from one  pro-
  104. gram  to another -- ABABABABABABAB....  until either one  program 
  105. loses,  some maximum number of instruction cycles have been  per-
  106. formed  (as a safeguard against endless loops),  or until you hit 
  107. <ESC> to abort.
  108.  
  109.      Here is the shortest possible battle program, called "Imp":
  110.  
  111.                          MOV  0    1
  112.  
  113.      Imp  just copies the current location to the next  location, 
  114. then  advances  to the next location,  etc.  While  the  original 
  115. program  is  short,  it  will eventually gobble up  every  memory 
  116. location if unchecked,  thus becoming the largest possible battle 
  117. program as well.  It can even spread to its opponent,  since  any 
  118. program  that  jumps to a location written by Imp will become  an 
  119. identical clone of Imp.
  120.  
  121.      Here is another program, "Anti.Imp":
  122.  
  123.                          MOV  #0   -5
  124.                          CMP  #0   -6
  125.                          JMP  -1
  126.                          MOV  #0   -5
  127.                          MOV  #0   -6
  128.                          MOV  #0   -7
  129.                          MOV  #0   -8
  130.                          JMP  -7
  131.  
  132.      Anti.Imp sets up a "marker" byte at -5 relative to its first 
  133. byte,  then waits for Imp to come along. When the marker changes, 
  134. it bombards the area that Imp is movinbg into with DAT 0 instruc-
  135. tiomns, which Imp can't execute and thus bites it.
  136.  
  137.      Here is "AntiAnti.Imp":
  138.  
  139.                          MOV   4   @3
  140.                          ADD  #1    2
  141.                          JMP  -2
  142.                          DAT        2
  143.                          MOV   0    1
  144.  
  145.      AntiAnti.Imp writes a block of code that LOOKS like Imp into 
  146. progressively higher memory locations.  When Anti.Imp senses this 
  147. "drone" Imp,  it will attack it, but to no avail -- it will still 
  148. get over-written,  and then become a clone of Imp (at which point 
  149. it  turns around and wreaks havoc on AntiAnti.Imp,  which has  no 
  150. protection against Imp itself as shown here....).
  151.      Other examples are given in Dewdney's article,  such 
  152. as Dwarf:    dat        -1
  153.             add    #5    -1
  154.             mov    #0    @-2
  155.             jmp    -2
  156. which fires "Zero Bombs" in a fashion similar to Anti-
  157. Anti.Imp;  Gemini,  which simply runs away;  or Raidar,  which is 
  158. able  to "leapfrog" over advancing attacks. 
  159.  
  160.      COREWARS.C was written by Kevin A. Bjorke 5/25/84, in Small-C 
  161. version 2.03 (M80),  which is  available  to the public domain
  162. through RCP/M's as well as MicroCornucopia. The addition 
  163. of  a few << and >> operations will speed it up a bit. 
  164.  
  165.     A C86 version of the original Small-C (CPM) program has been 
  166. written by R. Green for use with MS-DOS, and a DeSmet C version
  167. with an optional split-screen "picture" of core has been written
  168. by R. Sawyer.
  169.  
  170.  
  171.  
  172.  
  173.