In this tutorial we will write our very first program in assembler and compile it. We will also meet the two most basic instructions in assembler, from our point of view. These are the MOV instruction which is used to transfer data, and the INT instruction which calls a DOS interrupt.
The MOV instruction is the instruction which will appear more than any other in an assembler program. All that is does is it copies a piece of data from one location to another. It is similar in concept to the MOVE operator in the COBOL language, but it is used far more frequently. Here are a few examples of MOV instructions.
|; This copies the contents of the AX register into the BX register.
; This copies the top byte of the DX register into the top byte of the CX register.
; This copies the bottom byte of the DX register into the top byte of BX.
; This puts the value 12 decimal into the top half of the AX register.
; This does the same as the above except that the number is given in hexadecimal.
; Hexadecimal numbers MUST begin with a digit and end with a "h".
; This puts the character "*" into DL (lower half of DX).
; This does the same thing, as characters are stored as numbers. ( ASCII char 42 = "*" )
The above are all prefectly legal assembler statements ( notice that comments are preceded by a ";". This is the same as "//" in C++ or "*" in COBOL). In fact you could type the above statements into a text file and it would assemble with A86. (If you do try to do this, do NOT run the .com file generated as it will not terminate!). There are a number of things that you cannot do with the MOV instruction:
|; Invalid operation, as you cannot move an 8 bit quantity to a 16 bit one.
; Similar to above, you cannot put a 16 bit quantity into an 8 bit one.
; You cannot put a value into the number 12. If you see this is a program what
; is probably meant is MOV DL,12 - put 12 into DL.
; You cannot have 3 operands!
; Neither can you have only 1! You must have exactly 2: destination and source
The INT instruction is the instruction which does the most work in any assembler program. What it does is it calls a DOS interrupt (like a function) to perform a special task. When one wants to read from the keyboard or disk or mouse, or write to the screen, one uses an interrupt. When using DOS, there are a over 50 different interrupts available. Of these the programmer will only use a few. Each interrupt though, has a number of sub-functions which select the individual task that the function has to do. For example, there is just one interrupt for accessing the mouse INT 33h, but there are separate subfunctions available to see if a BUTTON has been clicked, to see how far the mouse has moved, to display or hide a mouse pointer etc. An assembler programmer's best friend is an list of interrupts and their subfunctions, as whenever you want to do some input or output you can simply go down the list until you find the interrupt subfunction which does what you want, and use it. I, being the helpful chap that I am, have provided a brief interrupt list here which should be sufficient for most of your needs.
By now I'm sure you are asking, how do I use these wonderful interrupts? Thankfully, it is not difficult. One goes down the list until one find the appropriate interrupt subfunction and moves the subfunction number to AH. One then looks at the input required by the function and moves the appropriate values to the registers stated.
Example: I you go down the list you will see that interrupt 21h (The DOS interrupt), subfunction 2, outputs a character. So let us write a code extract which will output the character "!".
|; To select subfunction 2, move the appropriate number, 2, to AH.
; In the interrupt list, it says that the character to output should be
; in register DL. So we move the character to DL.
; Finally when all the registers are set as required, we call the interrupt.
Perhaps the most inportant of all the interrupt subfunctions is INT 21h, subfunction 4Ch. This is the function which terminates the program and returns the user to the operating system. Every assembler program you write should end with the following lines.
; Select the subfunction
; Select a return value (optional but recommended)
; Call the DOS interrupt.
Now we can write compile and run our first assembler program. Using a text editor, MS-DOS Edit, or Windows Notepad, type in the following lines ( same as above ) and save it as prog1.asm in the same directory as A86:
MOV AH,02 ; Function to output a char MOV DL,"!" ; Character to output INT 21h ; Call the interrupt to output "!" MOV AH,04Ch ; Select exit function MOV AL,00 ; Return 0 INT 21h ; Call the interrupt to exitAt the DOS command line then type in the following command: "A86 prog1.asm". A86 assembler should start up ( I am assuming you are already in the directory where A86.com, A86.lib and prog1.asm are) adn assemble your program. If you then type "dir" you will see that the file prog1.com has been generated. Type "prog1" to run the program, and volia!
To continue clickHERE