

About this text
If you find this documentation confusing then it might be because my mother
tongue is german. You will need to read and understand this text in order to
play MemWars. As the MemWars Compiler and MemWars Frontend come without
any help, you will either need to print this page or save it to disk.
Contents
1. Virtual Memory
2. MemWars Code
3. Relative addressing
4. How MemWars executes programs
5. Killing other programs
6. Don't think in terms of programs
7. Writing Programs for MemWars
8. Where do I start?
9. Using the DUB instruction
10. Things to keep in mind
For a brief overview of what MemWars is see the introduction.
1. Virtual Memory
If you look at MemWars as game (which it is intended to be) then the
virtual memory is the playing field and the programs are the players. The
virtual memory consists of 80000 memory cells. Each memory cell can contain
a number between -79999 and +79999. When you start a MemWars game using the
MemWars Frontend then all the players' programs will be loaded into this
virtual memory. That means that a players program is nothing else but a
bunch of numbers. Some of these numbers correspond to instructions (see MemWars Code below) and others don't.
Now there is another important thing about virtual memory: it is circular. This means that the address succeeding the greatest address (79999) is 0. You can imagine the virtual memory beeing a circle.
2. MemWars Code
"MemWars Code" defines the instructions you can use when writing a MemWars
program. MemWars Code is made up of 18 very basic, assembler like
instructions each of which has one, two or no parameters. Every instruction
is associated with a certain number. All the MemWars Compiler really does
(apart from handling labels) is to substitute instructions by their
associated numbers. So a program is nothing but a bunch of numbers. When a
program is loaded into virtual memory then every number of the program is
assigned a certain memory cell. The following table lists the instructions
of the MemWars Code:
| Instruction | Number | Parameters | Description |
| NOP | 1 | none | No Operation. Do nothing. |
| CPY_ADR | 2 | Address Address | Copies content of first address to second address. |
| CPY_VAL | 3 | Value Address | Copies first value to second address. |
| ADD_ADR | 4 | Address Address | Adds content of first address and content of second address and writes the result to second address. |
| ADD_VAL | 5 | Value Address | Adds first value and content of second address and writes the result to second address. |
| SUB_ADR | 6 | Address Address | Subtracts content of first address from content of second address and writes the result to second address. |
| SUB_VAL | 7 | Value Address | Subtracts first value from content of second address and writes the result to second address. |
| MUL_ADR | 8 | Address Address | Multiplies content of first address and content of second address and writes the result to second address. |
| MUL_VAL | 9 | Value Address | Multiplies first value and content of second address and writes the result to second address. |
| DIV_ADR | 10 | Address Address | Divides content of second address by content of first address and writes the result to second address. |
| DIV_VAL | 11 | Value Address | Divides content of second address by first value and writes the result to second address. |
| MOD_ADR | 12 | Address Address | Divides content of second address by content of first address and writes the resulting rest to second address. |
| MOD_VAL | 13 | Value Address | Divides content of second address by first value and writes the resulting rest to second address. |
| JMP | 14 | Address | Unconditional Jump to first address. Sets PC to address. |
| JMZ_ADR | 15 | Address Address | Jump to second address if content of first address is equal to zero. |
| JMN_ADR | 16 | Address Address | Jump to second address if content of first address is not equal to zero. |
| EXE | 17 | Address | Creates a new PC and sets it to address contained in parameter. |
| DUB | 18 | Address Address | opies the instruction and all its parameters contained in first address to second address. If content of first address is not a valid association number then DUB acts as NOP. |
When a MemWars source code is compiled then all instructions are substituted by the associated numbers. An instruction like ADD_VAL 255 45 is compiled to 5 255 45.
3. Relative addressing
Another thing you need to understand is "relative addressing". Relative
addressing means that an address is interpreted as an offset to the
instructions address. ADD_ADR 0 2 means
that the value which is contained in address 0 is added to the value which
is contained in address 2 and the result is stored in address 2. Address 0
refers to the memory cell ADD_ADR is contained in. So the value of address
0 is the associated number of the ADD_ADR instruction: 4. The value
contained in address 2 is 2. The result (4+2=6) is stored in the address
parameter 2 addresses. So the instruction changed itself from: ADD_ADR 0 2 to ADD_ADR 0
6! Take a look at the instruction JMP
-3. What does the address -3 refer to? Well, remember relative
addressing: JMP is the instruction the address operand -3 belong to. So the
address of JMP is 0. That means that -3 refers to a memory cell 3 cells
before the cell containing the JMP number.
4. How MemWars executes programs
Using the MemWars Frontend a new game is set up and the players' programs
are loaded into the virtual memory. MemWars decides on the virtual memory
cell your program starts (all programs are equidistant). Each player is
assigned a "program counter" (PC) which points to the address of the next
instruction to execute. At the beginning each player's PC is set to his/her
program start address. After the game is set up you can start execution.
MemWars will now execute one instruction of player 1, then on instruction of
player 2 and so on until one instruction of each player has been executed.
After that the process is repeated. If a player's PC points to an address
which contains a number which is associated to an instruction (1,2,...,18)
then the associated instruction will be executed and the PC moved onwards
to the address MemWars expects the next instruction. This is the memory
cell succeding the last parameter of the executed instruction. So if your PC
points to address 4 and address 4 contains the number 2 then MemWars will
execute the CPY_ADR instruction assuming that address 5 and 6 contain the
parameters. After that MemWars will set your PC to address 7. If your PC
points to an address which does not contain a valid instruction number, say 23,
then no instruction will be executed and the PC will not be moved.
When this happens we say "a program is stuck".
Now this is not totally correct because there is the possibility that a player has more than one program counter. This is the case when dynamic creation of programs is allowed (you can toggle this option in the Frontend) and a program executes an EXE instruction which will lead to the creation of a new PC. Then players' programs can create new programs and players can have more than one PC. MemWars will loop through all program counters of player 1 and try to execute the instructions the point to. Then MemWars will do the same for all other players. After that the process is repeated.
To let you see what is going on in virtual memory it is displayed as 400x200 pixels bitmap in the MemWars Frontend. If an instruction changes the content of a memory cell, then this cell is colored with the color of the player the instruction belongs to.
5. Killing other programs
The aim of a MemWars game is to kill all programs that belong to you. A
game is over if there is only one player left that still has programs
running. A program cannot be killed directly. It can only be forced to get
stuck and self-destruct. So you have to change the code of enemy programs
in such a way that their program counters will point to addresses that do
not contain a valid instruction number. But a program that is stuck is not
dead! If some other program changes the content of the address the stuck
program's PC points to a valid instruction number then the program will not
be stuck anymore. However you can define how often a program can try to
execute an illegal instructions until it dies. So if you set this option
to 5 then a stuck program is allowed to stay stuck for five rounds until
it dies. If a program dies then the according PC is deleted from the players
PC list. So it's more the PC that died than the program.
So far so good. But when does a player win a game? A player wins a game when he is the only one left having one or more PCs.
6. Don't think in terms of programs
Maby you have already realized that programs do not really belong to
players. Players do write programs and programs themselves might create
programs but it is also possible that a another player uses "your" program.
This happens when he sets his PC to an address which contains an
instruction of "your" program. Then he will use your code. So your better
off thinking in terms of PCs rather than programs. If you find this too
confusing then forget about it - you will understand when you play MemWars
some time.
7. Writing Programs for MemWars
I cannot tell you how you can write good programs. You will need to find out for
yourself. In this section I will list a number of rules you should apply to when
writing a MemWars program.
When you finished writing a source file in a text editor of your choicesave it with the extension ".mws" and load it into the MemWars Compiler and press the "Compile" button. The MemWars Compiler will create two files in the same directory your source file is located with the extensions .mwp (MemWars Program) and .mwl (MemWars Logfile). The .mwp file is the program you can load into the MemWars Frontend. The .mwl file contains the pure source (no comments, instructions substituted by associated numbers) of your source file. You can use this file to check if the compiler did the right thing (it's still beta).
8. Where do I start?
If you've made it up to here your bound to be a MemWars player. Before you move
on to writing your own programs I suggest you try to understand and execute
some of the programs listet in the programs section.
9. Using the DUB instruction
The DUB instruction is probably the most powerful instructions of all. With
DUB you can write a very powerful program called "a worm" which consists of
only one instruction: DUB 0 3. Try to
understand what is does. It depends on you wether you allow the DUB command
or not. You can toggle that option in the MemWars Frontend.