UMBC CMSC 211

CSEE | 211 | Current | lectures | news | help


Creating an Assembly Language Program

The process of creating a program with assembly language is very similar to creating a C language program. You have to: If there was an error of any kind, the programming finds the problem, and how to fix the "bug" and go back the first step to correct the program.

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.

Which programs should I use?

Obviously, you need an editor. So long as the editor produces ASCII or text files, you can use any editor you want, such as: Remember that the key is the file must be all text. Microsoft Word and WordPerfect allow you to "Save As" and select the text only option!

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.

Creating the source file

Creating a file is easy and I assume you know how to do it. If not, see the Consultants in UCS for help.

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:

c:>masm hello.asm

Now I can link it with the command:

c:>link hello.obj

Almost as easy as cc foo.c, but not quite. If you are that lazy, you can use:

ml hello.asm

Now I can run it and get the following results:

c:>hello
Hello, my name is Gary

c:>

If you get the message:

Bad command or file name you either misspelled the command or misspelled the file name or the operating system does not know where the command is located. Make sure that your path variable is set correctly. You can check your path like this: E:\cmsc211\Spring00\burt\lectures\lec03> path PATH=C:\ORAWIN95\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\;C:\DOS;C:\MASM611\BIN;C:\MASM611\BINR E:\cmsc211\Spring00\burt\lectures\lec03> If the path is not correct, you must edit the autoexec.bat file and put in the statement: PATH C:\ORAWIN95\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\;C:\DOS;C:\MASM611\BIN;C:\ MASM611\BINR You must put in your own settings, but this is a copy of mine.

Note the periods in front of the .MODEL, .STACK, .DATA, and .CODE.


CSEE | 211 | Current | lectures | news | help