UMBC CMSC 211 |
During this process, we have to create several different types of files. When creating the program source code with the editor, we will give the file an extension of .ASM to signify that it is assembly language source. The assembler produces an .OBJ file, which is an intermediate form which has unresolved addresses. In the final step, the linker will produce a .EXE which is the executable image that the operating system can use.
To assemble the program, we will be using Microsoft MASM and LINK programs. They are available in the labs and you can get a free copy from Microsoft.
In our source file, we will want to have comments. Comments are good. Comments are our friend!!! If you want a good grade in CMSC211, you will use a lot of meaningful comments. Comments start with a semicolon and take up the rest of the line. It is normally to have entire lines with the semicolon in column one or to put the comment at the end of instruction.
An instruction can have four parts, a label, operation code (opcode), operand(s), and comment. This is definitely different from the free-flow format of C! Assembly language is fixed field (or nearly so). The label starts in column one, the opcode begins at the first tab stop (I use column 9), the operand(s) begin at the second tab stop (I use column 17) and comment can begin anywhere on the line, but it is the last thing on the line.
Let's look at a sample program, which I typed in with my handi-dandi editor.
;; 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 execution
I made sure that I saved it in text format with the name hello.asm! Now I can assemble the source code with the command:
Now I can link it with the command:
Almost as easy as cc foo.c, but not quite. If you are that lazy, you can use:
Now I can run it and get the following results:
If you get the message:
Note the periods in front of the .MODEL, .STACK, .DATA, and .CODE.