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 MASM32 and LINK programs.

In the latest edition of the textbook, you get the software that is necessary for you work on your own computer, if you wish to.

Creating the source file

Creating a file is easy and I assume you know how to do it.

In our source file, we will want to have comments. Comments are good. Comments are our friend!!! If you want a good grade in CMSC313, 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, mnemonic, 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 handy-dandy editor.

First, let's look that the C version of this program.

#include <stdio.h>
int main( void )
{
    printf("Hello, World!\n" );
    return 0;
}
That should be completely understood by everyone, since CSMC201 is a prerequisite for this course. ;-)

; #########################################################################

      .386
      .model flat, stdcall
      option casemap :none   ; case sensitive

; #########################################################################

      include \masm32\include\windows.inc
      include \masm32\include\user32.inc
      include \masm32\include\kernel32.inc

      includelib \masm32\lib\user32.lib
      includelib \masm32\lib\kernel32.lib

; #########################################################################

    .code

start:

    jmp @F
      szDlgTitle    db "Minimum MASM",0
      szMsg         db "     Hello, World!",0
    @@:

    push MB_OK
    push offset szDlgTitle
    push offset szMsg
    push 0
    call MessageBox

    push 0
    call ExitProcess

    ; --------------------------------------------------------
    ; The following are the same function calls using MASM
    ; "invoke" syntax. It is clearer code, it is type checked
    ; against a function prototype and it is less error prone.
    ; --------------------------------------------------------

    ; invoke MessageBox,0,ADDR szMsg,ADDR szDlgTitle,MB_OK
    ; invoke ExitProcess,0

end start

I made sure that I saved it in text format with the name hello.asm! I have a ".bat" file (batch file of commands) called build.bat that I use to save typing a lot of stuff.

@echo off

if exist minimum.obj del minimum.obj
if exist minimum.exe del minimum.exe

\masm32\bin\ml /c /coff /nologo minimum.asm
\masm32\bin\Link /SUBSYSTEM:WINDOWS /MERGE:.rdata=.text minimum.obj > nul

dir minimum.*

pause

Now I can link it with the command: build

build

Almost as easy as gcc -Wall -ansi foo.c. Now I can run it and get the following results:

Required Comments

Every project in this class must have the following comments:
;; Filename:       hello.asm
;; Name:           Ima Student
;; email:          imas1@umbc.edu
;; Date:           7 May 2007
;; Course:         CMSC313
;; Description:    (Your psuedocode goes here.  Must be detailed)
;; Notes:          (As needed, such has how to compile)
;;
;;