An Introduction to Assembly Language Using CoreWar 1.03d By David Wright Anyone who has dabbled with programming in BASIC has probably realized that all of those nifty arcade games such are obviously written in another language. Languages such as Pascal, BASIC, and COBOL are what is known as "High-Level" languages, because they are close to (if you believe this, you haven't programmed in them) human languages. Other popular languages on the Amiga such as C, FORTH, Icon, Lisp, Modula II, and even Draco, while sometimes called high-level languages are actually somewhere in between BASIC and assembly language, which is the only "true" low-level language (although some Apple II owners stuck in the neolithic era actually enter programs in hex). One of the advantages to high-level languages is "portability", which means the ability to run the same program and run it on several different machines with little or no changes. Assembly language is specific to one type of processor, and generally not that portable even to another computer running with the same CPU. Does this mean that assembly is a waste of time? Not at all. Programming is about learning and using concepts, and once those are learned, moving to a different CPU is usually just a matter of learning the differences between the two. You can think of this in terms of BASIC using WRITE or DISPLAY instead of PRINT. The same action is accomplished with a slightly different command. Why would anyone want to learn a language that used arcane commands, wasn't portable, and was more prone to errors when fine languages such as BASIC, C, or FORTH are available? Size and Speed. All other languages at some point convert your program into assembly, or use assembly to carry out your instructions, so writing in assembly produces the smallest, fastest code. Some languages such as C and FORTH (and the less popular Modula-II) can produce small code, but the same program written in assembly will usually be smaller or faster. BASIC may fool you into thinking it is producing small programs, because the files are small, but each one requires a 200K+ overhead to load the BASIC interpreter. The title of this article says that this is an introduction to assembly using CoreWar. You may be wondering what CoreWar is, so let's diverge a minute. By now every Amiga owner has heard of "virus" programs. These are really VERY old, and not new at all. Over 13 years ago (maybe even 20 or more, I didn't get into computers until '74 or '75) there were programs called "trojan horses" or "worm" programs that would replicate on punch cards without anyone noticing. But now that many people have computers, and most of the computers have modems, a virus can spread quite far and quite rapidly. CoreWar is a program that functions as a "host" computer for 2 virus programs that you write yourself. There is no danger to your Amiga as the programs are written in a language that the Amiga does not understand, and they only have access to a limited area of memory. If you would like more information on the history of CoreWar, see the CoreWar documentation. The object of CoreWar is to have your program "bomb" the other program by making it execute instruction 0 (end) or to merely stay alive for the length of the game by hiding. Because the number of instructions CoreWar understands is small (9) and it has only 3 addressing modes, it is a good tool for learning assembly language. If your program is small and fast you win, if not you lose (of course strategy helps too). The 9 commands represent the most basic "logical" and "conditional" commands found in "real" assembly language. These include commands to move, add, subtract, and compare numbers. Let's take a look at one of the demo programs that comes with CoreWar, imp: 500 MOV 0,1 501 END MOV is a "redcode" instruction to "move" (really copy) a byte. As you would expect, you must tell it what to move and where. This line is using the addressing mode known as "relative" (the numbers are relative to the current instruction). The first number is the FROM location, the next is the TO location. You do not enter the line numbers to the left. They are added when the program is loaded. To make this easier, let's replace the 0 and 1 with the location as it appears when the computer "sees" it: 500 MOV 500,501 501 END You can see that what this really means is to move line 500 to line 501 (500+0, 500+1). Here is what the 2 lines would look like after 500 runs: 500 MOV 0,1 501 MOV 0,1 They are now the same. Now comes the interesting part. When a computer runs a program, it does what the instruction says, and then goes to the next instruction. This means it will now run line 501: 500 MOV 0,1 501 MOV 0,1 502 END ~~~~~~~~~~~ 500 MOV 0,1 501 MOV 0,1 502 MOV 0,1 This program will go on forever. What happens if it copies over part of the other program? The other program becomes "infected" and either dies (you win) or you become a part of the other program (you tie). I have heard of programs that check their DNA and kill viral infections, but I have yet to see any. When running CoreWar you will see a continual output of numbers, which are the programs running. It displays both relative and absolute" addresses, which will help you spot errors. Most good assembler packages also have a real-time dissassembler such as this, so learning how to read this screen will be useful for real programming later. Because assembly IS a matter of learning concepts it would be impossible to teach you how to do it in one article. I would suggest getting a book on 68000 assembly language to learn what it is like, and using CoreWar as a learning tool without needing a tutor, although it is quite possible to use CoreWar on it's own to learn assembly and "graduate" to 68000 assembly language later.