UMBC CMSC 211 |
;; FIRST.ASM -- Our first Assembly Language Program. This program ;; displays the line 'Hello, my name is Gary' on the CRT. .MODEL SMALL .STACK 100H .DATA Message DB 'Hello, my name is Gary', 13, 10, '$' .CODE Hello PROC mov ax, @data mov ds, ax mov dx, OFFSET Message mov ah, 9h ;Function code for 'display string' int 21h ;Standard way to call MSDOS mov al, 0 ;Return code of 0 mov ah, 4ch ;Exit back to MSDOS int 21h Hello ENDP END Hello ;Tells where to start executionOK, what else did you see? Some of the operations are started with a period, .MODEL, .STACK, .DATA., and .CODE. These and the operations of DB, PROC, ENDP, and END are not really computer instructions and are known as pseudo-operations. These are actually directives to the assembler. As you will see in this course, the other lines are converted to a single machine instruction.
Labels are identifiers. They are names invented by the programmer, in an attempt to provide documentation about what is happening, just as the identifiers in C language are. Labels contain letters, digits, and the special characters @ $ _ ?. Labels can have up to 31 character and the first character must not be a digit. Upper and lower case are normally equivalent. It is best to have the first character be a letter and that is a requirement for this class.
The actual program is only eighteen bytes long, or eight instructions. The program will use 25 bytes of data.
To help you put it into a proper frame of reference, here is the corresponding C program:
#include <stdio.h> int main( void ) { printf("'Hello, my name is Gary\n"); return 0; }
The author gives a boilerplate that I recommend you put into a file and that will become a template for each program that you write.
When you are declaring a multi-byte location, the value is stored in little-endian format:
myInt DW 1234h ; two-byte valuewill be stored in consequentive memory locations as:
34 12A four-byte value looks like:
myBigInt DD 12345678h ; four-byte valuebecomes in memory:
78 56 34 12
This reversal only occurs with 16-, 32-, and 64- byte declarations (DW, DD, and DQ). It does not happen when you declare variables with DB!
When reserving the data space, you can use several forms:
As we start our study of assembly language, there a couple of things to clarify. We need to understand the internals of the CPU better than we do now. Also, some work is done by causing a interrupt after we have first set up the CPU.
Hopefully, you have already looked at the information on the registers. That leaves to describe how to output a message to the screen and how to exit the program.
We can prepare for an interrupt by putting data into specified registers. Then we simply activate the interrupt that we want.
To move data around, there has to be a source and a destination. The assembly language instruction is mov destination, source. Note that they both can not be references to a memory address. The destination and the source can be memory or a register. The source can also be a constant. This instruction can be either word or byte, but the source and destination must agree in size.
mov AX, 345;
To display a message on the screen, we have to use the interrupt instruction, as well as the function code and the address of the message. In our example we see:
mov dx, OFFSET Message ;This is where in the Data Segment to find Message mov ah, 9h ;Function code: Display String int 21h ;Cause the right interrupt
We exit the program with a different Function Code for INT 21:
mov al, 0h; ;Return Code mov ah, 4ch ;Function code: Exit to DOS int 21h ;Cause the right interrupt
The return code is identical to in C when main has:
int main() { return 0; }