* -------------------------------------------------------------------------------------------- * | ChaoZers 98/05/13 | | Assembler Tutorial #04 | | -------------------------------------------------------------------------------------------- | | | Basic writing of assembler code in AsmOne/AsmPro. | *----------------------------------------------------------------------------------------------* Introduction ------------ This time i will show you how to write your code in AsmOne/AsmPro. First thing to learn -------------------- Remember to write alot of comments in you code, because if you dont, other people than yourself wont understand a thing of what you are doing. You can write labels, instructions, variable definitions and even use procedures like IF/ENDIF & REPEAT/ENDREPEAT in AsmOne/AsmPro. Lables ------ Ok, before you begin reading about the instructions of the MC680x0, i will teach you how to write assembly code. Lets begin with labels! Now what the hell is a label? Well, labels are used to jump to, in different ways. I will not go through the full usage of them yet, only how to write them. A label is always written at the very begining of a line. Here is a piece of code. MyCode: CLR.L d1 CLR.L d2 MOVE.W #$2000,d1 MOVE.B #4,d2 RTS In the above example "MyCode:" is the label, dont bother about the rest of the example, as i will explain MOVE, CLR & RTS in future turtorials. Instructions ------------ Instructions are written att the first TAB of the line that the command is written on. Here is a piece of code to illustrate what i mean. MyCode: CLR.L d1 TAB>CLR.L d2 MOVE.W #$2111,d1 MOVE.B d1,d2 RTS So, CLR, MOVE & RTS are all instructions and they are therefore written on the first TAB of their line. Variables --------- So how do variables work in assembler? Well, its pretty much the same as in BASIC, i will show you an example of how to use variables in your assembler code. Five =5 MyCode: CLR.L d1 CLR.L d2 MOVE.W #$2111,d1 MOVE.B #Five,d2 RTS See how it works? Above i define the decimal value 5 to the variable "Five", later on i move the byte value of "Five" into d2. So the thing i really do on the row where i write "MOVE.B #Five,d2" is that i move the value 5 into d2, its very simple. If i would have written "MOVE.B Five,d2" in the above example, i would not have moved the value 5 into d2, but instead i would have moved the value att the address $5, so be very careful so that you dont do a misstake here. I will explain more about what an address really is, in a future tutorial. Compiler direct --------------- Theese labels we are using are "compiler direct", so it may not work properly if you change theese within your code, it will only work if there is only one value to change to. Lets take an example... You have the label "number", and you want to do a random number generator and put the number you generate into "number". And the value will be different everytime you execute your code. Now lets say that you assemble your program and write an executable in "RAM:". When you assembled & wrote an executable, the random number generator, generated the number "34". This information now lies within the exe file, and each time you execute it from wb/cli you will get the number 34, so things like theese arent possible when using such Labels as here, because the compiler saves the infomrtaion in them. To solve this you have to make use of memory, (DC.X X=B, W, L), i will tell you about DC in the next part of this tutorial. Procedures ---------- Ok, now i will explain how to make use of the REPEAT/ENDREPEAT procedure in AsmOne/AsmPro. The thing that the assembler does when doing this is that it transforms the code in a way. So if you have a repeat of 3, the assembler will simply do the same thing three times when compiling, ehrm... I better show you what i mean :). Five =5 MyCode: CLR.L d1 CLR.L d2 REPT 3 MOVE.W #$2111,d1 MOVE.B #Five,d2 ENDR RTS Here is a piece of code, and you can see that i repeat two instructions three times. Now look closely because the above code is the same thing as the one i am gonna show you now. Five =5 MyCode: CLR.L d1 CLR.L d2 MOVE.W #$2111,d1 MOVE.B #Five,d2 MOVE.W #$2111,d1 MOVE.B #Five,d2 MOVE.W #$2111,d1 MOVE.B #Five,d2 RTS Now you should see how it works. As you can understand this makes the compiled file really big, because imagine a repeat on, for example 2000! This is the same as writing the same thing 2000 times, and the compiled executable just grows. Therefore there are special processor instructions that get rid of this problem, but more about that in a future tutorial. Ok, lets go right along, and look at the IF/ENDIF procedure. But, to be able to REALLY use IF/ENDIF, we need to learn the SET command in AsmOne/AsmPro. So first, let me show you how to use the SET command. Five SET 5 MyCode: CLR.L d1 CLR.L d2 REPT 3 MOVE.W #$2111,d1 MOVE.B #Five,d2 ENDR RTS This piece of code is the same as the one in the previous example, so writing "Five SET 5" is the same thing as writing "Five =5". BUT, There is a difference between theese two, and this is what i will show you now! :) Here follows a code that AsmOne/AsmPro will never accept. Five =5 MyCode: CLR.L d1 CLR.L d2 REPT 3 Five SET Five*2 MOVE.W #$2111,d1 MOVE.B #Five,d2 ENDR RTS In the above example, AsmOne/AsmPro will complain about a "double symbol" as it thinks that you are trying to define the variable "Five" twice... This is because Five =5 works more like a constant, while when using the SET command you can go changing the variable again, and again. So here follows the correct piece of code... Five SET 5 MyCode: CLR.L d1 CLR.L d2 REPT 3 Five SET Five*2 MOVE.W #$2111,d1 MOVE.B #Five,d2 ENDR RTS If you do like this, AsmOne/AsmPro will accept it, and compile it properly. Ok, now that you are familiar with the SET command, i will explain how to use the IF/ENDIF procedure, it is VERY simple. Lets include a IF/ENDIF procedure in our previous code! Five SET 5 MyCode: CLR.L d1 CLR.L d2 REPT 3 Five SET Five*2 IF Five=10 MOVE.W #$2111,d1 MOVE.B #Five,d2 ENDIF ENDR RTS If you know BASIC properly, you can see that I only do the two isntructions within the IF/ENDIF procedure, when Five=10, its very simple! Comments -------- Comments in assembler are written exactly like in a AmigaDos script. That means that you write ;, very simple indeed. Here is an example, just to be sure you get it =). Five SET 5 ;Five=5 MyCode: ;This is a label. CLR.L d1 ;Clear register d1. CLR.L d2 ;- | - d2. REPT 3 ;Repeat 3times. Five SET Five*2 ;Five=Five*2 IF Five=10 ;IF Five=10 Then... MOVE.W #$2111,d1 ;Move value to d1. MOVE.B #Five,d2 ;Move value to d2. ENDIF ;ENDIF ENDR ;End Repeat RTS ;return to subroutine. (End?) Above I have written alot of comments, in the future tortorials, this is exactly what i am going to do, so get used to it. Closing words ------------- I hope that was not to hard to understand. In the next part, we will talk mainly about the MOVE command, so look out for that one. It would be good if you experimeted a litle with the MOVE command, by writing it into AsmOne/AsmPro, because this will give you a litle head-start, and you will understand everything easier! Exercises --------- 01.Where on a line are labels written? 02.Where on a line are the instructions written? 03.What sign do you use to define where a comment begins? 04.What is the difference between the SET command and the = sign? Answers to Exercises --------------------- 01.At the very begining of the line! 02.At the first TAB of the line. 03.The ; sign. 04.The = sign works more like a constant, and if you use the SET command you can redefine the same value over and over again, this is not possible with the = sign. Email&More ---------- Want to get in touch with me? My email is "chaozer@algonet.se". My homepage is at "http://www.algonet.se/~chaozer/" You can also find me on IRC, almost everywhere, under the nickname ChaoZer.