ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Adam's Assembler Tutorial 1.0 ÇÄ¿ º º ³ º PART VIII º ³ ÈÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Revision : 1.4 Date : 28-06-1996 Contact : blackcat@faroc.com.au http://www.faroc.com.au/~blackcat Note : Adam's Assembler Tutorial is COPYRIGHT, and all rights are reserved by the author. You may freely redistribute only the ORIGINAL archive, and the tutorials should not be edited in any form. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Well, welcome back assembler coders. This tutorial is _really_ late, and would have been a lot later were it not for Bj”rn Svensson, and many others like him, who thanks to their determination to get Tutorial 8, persuaded me to get this thing written. Of, course, this means I've probably failed all my exams over the past two weeks, but such is life. :) Okay, this week we're really going to learn something. We're going to take a much closer look at how we can declare variables, and delve into the world of structures. You'll learn how to create arrays in Assembler, and this concept is reinforced with the demo program I included - a fire routine! ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ DATA STRUCTURES IN ASSEMBLER ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Okay, by now you should know that you can use the DB, (Declare Byte) and DW, (Declare Word) to create variables. However, up until now we have been using them as you would use the Const declaration in Pascal. That is, we have been using it to assign a byte or word with a value. EG: MyByte DB 10 -- which is the same as -- Const MyByte : Byte = 10; However, we could just have easily said: MyByte DB ? ...and then later on said: MOV MyByte, 10 In fact DB is very powerful indeed. Several tutorials ago when you were learning to put strings on the screen, you saw something along the lines of this: MyString DB 10, 13 "This is a string$" Now the more inquisitive of you would have probably said to yourselves, "Hang on... that tutorial guy said that DB declares a BYTE. How can DB declare a string then?" Well, DB has the ability to reserve space for multiple byte values - from 1 to as many bytes as you need. You may also have wondered what the 10 and 13 before the text stood for. Well, dig out your ASCII chart and have a look at character 10 and character 13. You'll notice that 10 is Line Feed and 13 is Carriage Return. Basically, it's just like saying: MyString := #10 + #13 + 'This is a string'; in Pascal. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Okay, so you've seen how to create variables properly. But what about constants? Well, in Assembler, constants are known as Equates. Equates make Assembler coding much more easy, and can simplify things greatly. For instance, if I were to have used the following in previous tutorials: LF EQU 10 CR EQU 13 DB LF, CR "This is a string$" ...people would have got the 10, 13 thing straight away. However, just to make things a little more complicated, there is yet another way that you can assign values to identifiers. You can do things just like you would in BASIC: Population = 4Ch Magnitude = 0 Basically, you can bear the following points in mind: þ Once you use EQU to assign a value to an identifier, you can not change it. þ EQU can be used to define just about any type - including strings. You cannot, however, do this when you use a '='. An '=' can only define numeric values. þ You can use EQU almost anywhere in your program. þ Values defined with '=' can be changed. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ And now on with one of the trickier aspects of Assembler coding - structures. Structures are not variables themselves, they are a TYPE - basically a schematic for a variable. As an example, if you had the following in Pascal: Type Date = Record; Day : Byte; Month : Byte; Year : Word; End; { Record } You could represent this in Assembler as follows: Date STRUC Day DB ? Month DB ? Year DW ? Date ENDS However, one of the advantages of Assembler is that you can initialize all or some of the fields of the structure before you even refer to the structure in your code segment. That structure above could easily be written as: Date STRUC Day DB ? Month DB 6 Year DW 1996 Date ENDS Some important points to remember are as follows: þ You can declare a structure anywhere in your code, although for good program design, you should really put them in the data segment, unless they will only be used by a subroutine. þ Defining a structure does not reserve any bytes of memory. It is only when you declare a structured variable that memory is allocated. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ REFERENCING DATA STRUCTURES IN ASSEMBLER ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Well, you've seen how to define structures, but how do you actually refer to them in your code? All you have to do, is place a few lines like the ones below somewhere in your program - preferably in the data segment. Date STRUC Day DB 19 Month DB 6 Year DW 1996 Date ENDS Date_I_Passed_Physics Date <> ; I hope! At this point in time, Date_I_Passed_Physics has all three of its fields assigned. Day is set to 19, Month to 6 and Year to 1996. Now, what are those brackets, "<>", doing after date you ask? The brackets present us with yet another chance to alter the contents of the variable's fields. If I had written this: Date_I_Passed_Physics Date <10,10,1900> ...then the fields would have been changed to the values in the brackets. Alternatively, it would have been possible to do this: Date_I_Passed_Physics Date <,10,> ; And now only the Month field has been changed. Note that in this example, the second comma was not needed as we did not go on to change further fields. It is your choice, (and the compiler's!), whether to leave the second comma in. Now all this is very well, but how do you use these values in your code? It is simply a matter of saying: MOV AX, [Date_I_Passed_Physics.Month] ; or something like MOV [Date_I_Passed_Physics.Day], 5 ; or maybe even CMP [Date_I_Passed_Physics.Year], 1996 Simple, huh? ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ CREATING ARRAYS IN ASSEMBLER ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Okay, arrays are pretty easy to implement. As an example, let's say you had the following array structure in Pascal: Var MyArray : Array[0..19] Of Word; To create a similar array in Assembler, you must use the DUP operator. DUP, or DUPlicate Variable, has the following syntax: þ