Skip to content

Introduction

gmolin edited this page Apr 7, 2020 · 4 revisions

A short digression into history

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-chasing 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 in 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 is the god of war.] (Https://www.useless.school/single-post/2017/06/01/Chapter-2- Battle- on - "ferromagnetic-cores" -or-MARS --- god-war-Part-1)

Overview of the Corewar project

It is on the basis of this rather popular game in narrow circles that the ** “Corewar” ** project was built, which consists of three mandatory parts:

  • Champion (The Champion)
  • Assembler (The Assembler)
  • Virtual Machine (The Virtual Machine)

Champion

The essence of this section is to write assembly language code, which will then be placed in a file with the extension .s.

In fact, we are writing code in a pseudo-assembler language. That is, in a language created specifically for this task, which is similar to a real assembler, but still it is not. But for consistency with the text of the assignment, simplicity and in order to save six letters, we will also call this language assembler.

The generated code is our champion, whose goal is to fight with other champions written by us or other people.

The code of each champion has the following structure:

  1. Name
  2. Commentary
  3. Executable code

It may look, for example, 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 a task for a completely different project called «Corewar Championship».

In «Corewar» we create our champion only to demonstrate understanding of the topic and the ability to write code in assembly language. And not so that he could defeat someone.

Our task is to write the code without errors so that the asm program can turn it into bytecode, which the virtual machine would then execute.

The goals to win the battle or to demonstrate at least some worthy result in the battle are not before us.

Assembler

The objective of this section is to create a program that will translate the champion code written in assembly language into bytecode - a bunch of numbers in hexadecimal notation.

From Elvish this task can be interpreted as "translate commands from a language understandable to humans (assembler) into a language that is understandable to a virtual machine (byte code)."

Translation of the program - 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, issues program text for printing, etc.

Source: [Translator - Wikipedia] (https://ru.wikipedia.org/wiki/Translator)

That is, we must create a program with the name asm (from the word" assembler "), which will receive a file with assembly language code as a parameter and create a new file with byte code based on it.

The file with the code of our champion, written in assembler, must have the extension .s. Based on it, the program asm will create a new file with the extension .cor, where the created bytecode will be located.

The name of the file itself will remain unchanged. That is, after calling the ./asm batman.s command, the batman.cor file should appear next to the batman.s file. Of course, if during the broadcast there is no error.

Virtual machine

After we received the file with bytecode, the virtual machine’s running time comes.

A virtual machine is also a program whose executive file should be called corewar.

Its task is to allocate a specific piece of memory, place the code of champions and the carriage that will execute it on this section.

And then follow the progress of the battle to declare the champion champion after its completion.

Bonuses

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:

Extended error message

If an error occurred during the generation of the bytecode, it is advisable that in this case the asm program behaves like a real translator, which it actually is. That is, it produced a meaningful message - on which line when working with the .s file an error occurred and what its type is.

Ability to disassemble bytecode

To make it possible to obtain source code in assembler by having a file with byte code. That is, implement the function inverse to the one for which the asm program is intended.

Visualizer

Create a program that will display the state of memory, as well as changing key game parameters during the battle.

Various sound effects can also be added to focus on key points such as carriage death or a winner announcement.

Useful links

The University of Useless Knowledge website has a series of three articles that describes the ideas and principles of the Core Wars game. Also in these articles there are examples of the code of champions and analysis of how this same code can fight and cause damage. True, all this is in the language of this assembler.

But in general, this is a good introduction to the topic, which tells in detail what the essence of the "battle in memory" is: