-
Notifications
You must be signed in to change notification settings - Fork 0
Introduction
In 1984, an article by Andrew Dewdney appeared in Scientific American magazine describing the game Core Wars . The article began with the following words:
Two programs in their natural habitat - computer memory - chase each other from address to address. Sometimes they track down the enemy; sometimes they lay batteries of digital bombs; sometimes they copy themselves to another place of memory to avoid danger or stop to repair the damage done by the enemy. I call this game "Core Wars" ...
Source: Chapter 2. Part 1. The battle on the "ferromagnetic cores" or MARS - the god of war.(www.useless.school)
The Corewar project is based on the popular game and consists of the three mandatory parts:
- The Champion
- The Assembler)
- The Virtual Machine
The objective of this section is to write code in assembly language, which will then be placed in a file with the extension .s
.
In fact, the code is written in a pseudo-assembly language. That is, in a language created specifically for this task, which is similar to a real assembly, but still it is not. For consistency with the text of the assignment, simplicity and in order to save six letters, we will also call this language assembly.
The generated code is our champion. Its' goal is to fight against other champions written by us or by other people.
The champion code has the following structure:
- Name
- Comment
- Executable code
It may look like this:
.name "Batman"
.comment "This city needs me"
loop:
sti r1, %:live, %1
live:
live %0
ld %0, r2
zjmp %:loop
In this project, we have no goal to create the most powerful and invincible champion. This is the task for a completely different project called “Corewar Championship”.
For the Corewar project, we create our champion only to demonstrate understanding of the topic and the ability to write assembly code.
Our task is to write code without errors so that the program asm can turn it into byte-code, which the virtual machine would then execute.
However, our champion should be able to win the zork
, which is the basic living program
.
The objective of this section is to create a program that will translate the champion code written in assembly language into byte-code - a bunch of numbers.
From elvish this task can be interpreted as "translate commands from a language understandable to humans (assembly) into a language understandable to a virtual machine (byte-code)."
Program translation is the conversion of a program presented in one of the programming languages into a program in another language. The translator usually also performs error diagnostics, generates identifier dictionaries, prints program text, etc.
Source: Translator - Wikipedia
That is, we must create a program with a name asm
(from the word “assembly”), which will receive as a parameter a file with assembly language code and create a new file with byte-code.
The file with the code of our champion, written in assembly, must have the extension .s
. On its basis, the program asm
will create a new file with the extension .cor
containing byte-code.
The name of the file itself will remain unchanged. That is, after calling the command ./asm batman.s
, a file batman.cor
should appear next to the file batman.s
, if no errors occurred during the translation, obviously.
After we get the byte-code file, time comes to run the virtual machine.
The executable file of the virtual machine should be called corewar
.
The task of the vm is to allocate memory and place the champions' code in that memory, as well as the cursors which will execute the champions' code.
As always, the number of bonuses and their essence is limited solely by the author’s imagination.
Here are a few that could be implemented:
If an error occurred during the translation, it would be nice the program asm
to behaves like a real translator (which it actually is). That is, to print out a meaningful message - number of line in the file .s
and the type of error.
To make it possible to obtain assembly code from a file with byte code. That is, implement the function inverse to the asm
.
Create a program that will display the state of memory, as well as changing key game parameters during the battle.
You can also add various sound effects to focus on key points such as carriage death or a winner announcement.