Introduction to programming by the means of Corewars and Redcode
This chapter is a discussion about the basic principles, how computers work in view of Core War programming. If you are experienced with Core Wars, you don't need to read these pages. If you already know what machine language is, you might want to read about the specifications of a MARS-CPU.
To understand this tutorial, you should know about computer basics already. This introduction only tries to show some specific aspects of the way computers work. If you are not familiar with writing programs, you came to the right place.
This chapter explains the following topics:
Machine Language and Assembler
Processor, ALU, Register
Instruction Pointer, Multitasking and Task Queue (FIFO)
Differences between the MARS-processor and a real processor
How to win a Core War battle
About Programming Languages
Most programs today are written in high level programming languages, except for special programs like hardware drivers. If you want your computer to display a text, you can write the BASIC command: PRINT "Hello, World!". In machine language this task is more complex; The program has to copy each character of the text into the memory of the video hardware. You won't let every character be transferred by an individual MOV command, but you will create a certain mechanism, that can copy any text from any location of the memory to another. Thus you will get a sub-routine that can be reused for displaying any text.
Those of you, who already have written programs in a high level language, may wonder, what programming in assembly language is useful after all. The simple answere is: You can play Core War with it. Just kidding. Machine programs are written by humans or compiler programs. If written by an expert, they are as fast (or memory saving) as they can be. At least they should be. Assembler is mostly used when a program intensively has to communicate with certain hardware components or in time critical applications like Games, 3D graphic applications, mathematical software or specific parts of an operating system. Modern high level programming languages allow to assembler code sections to be included in the program source code. (Inline Assembler)
Most examples of this tutorial can not be entered to the simulator's core in the way, they are presented. They contain additional information for the preprocessor. Such programs need to be stored in text files, because they are read in and translated into the Core Memory by the assembler of ARES before a battle starts.
Parts of these source files, which are written behind a semicolon (";"), are ignored by the assembler. They are meant as documentary comments for the program writer. There is an exception for this rule - so called "assembler directives" provide further information for certain core war programs.
By default, ARES displays the contents of the "Monitor-View" in a certain shortened way. The ICWS-88 standard did not allow to use every possible part of the memory, like in the command JMP +1. Most opcodes in Redcode use two operands, but JMP only takes one: the target address ("+1"). But ICWS-94 compatible programs are allowed to use these "invisible" memory gaps, so you might want to see the whole content of every memory location. You can switch between ICWS-88 and ICWS-94 view by hitting Strg-O. The examples of this tutorial are shown in their short version, when the full '94-features aren't used. This difference affects the "Monitor-View" mainly: If a value is not displayed, it doesn't mean, that it is not there. If you type in an opcode, the possible missing second operand is automatically created corresponding to the opcode modifiers.