Sample Programs and Program Style

This page contains the following sections:

The text below contains links to several Intel assembler programs. The simple programs use DOS services to do character I/O. More complex programs use subroutines to allow I/O using decimal numbers.

Sample Programs - Character I/O

First is a one-page program named onepage.asm that you can select and download to make sure your assembler and linker are working correctly. If you can't assemble and run this simple program without any warnings or errors, something is wrong with your assembler or linker!

The command lines used to assemble and link this program are given in the comments at the beginning of the program. There is also a Web page describing the tools. Either put the ASM and VAL programs in the same directory as your program, or adjust your DOS PATH variable to include where they reside. Read the documentation text files included with ASM and VAL for more details on how the ASM and VAL programs work.

Next is Alan's stars.asm program that prints numbers of asterisks based on one key of input.

The program labels.asm program also prints asterisks but has a bug that causes it to never terminate.

Program noecho.asm is a badly-written piece of code that loops reading characters without echoing them to the screen. You should rewrite this to use a do/while loop and fix the variable names and comments to be more relevant to the algorithm used.

The more complex tail.asm program displays the Command Tail in the Program Segment Prefix. It shows how a set of high-level language statements (e.g. in C language) might be turned into assembler by a compiler (though this program was translated by hand, not by a compiler). You can read more about this program in tail.htm.

Sample Programs - Decimal I/O

Neither the Intel instruction set nor the DOS service routines provide ways of inputting or outputting decimal numbers. (All you can read is ASCII characters, and all you can output is ASCII characters.) The program gcd.asm does decimal-based I/O and is developed in the file 27DevelopingAssemblerPrograms.htm

Since doing decimal I/O was generally useful in multiple programs, Alan Pinck wrote a small I/O Package named dec_io.asm that handles the conversion of ASCII character digits to base-ten numbers for Input and Output, for decimal numbers of four or fewer digits. Here are some sample programs that use Alan's Decimal I/O subroutines.

You will need Alan's dec_io.asm I/O package in the current directory on your disk to assemble these example programs. The I/O package is brought into these programs using an INCLUDE statement in the assembler source code, near the bottom. Look for it.

First is a longer program (mostly comments!) named first.asm. This program is a good test to see if the I/O package downloaded to your computer correctly.

The program series.asm also uses Alan's I/O package and is a fairly direct translation of LMC (Little-Man Computer) mnemonic instructions into Intel mnemonic instructions. See the original LMC page here: 13LMCProgramming.htm

Program addtwo.asm is another simple example that uses a modified version of the I/O package (getshow.asm) to input two decimal numbers (four digits or less), add them together, then output the sum.

The getshow.asm I/O package is based on Alan Pinck's dec_io.asm, but it doesn't output any newlines when reading or showing numbers, so you can use it to format output with more flexibility. (For example, you can output a number in the middle of a line with GETSHOW, but not with DEC_IO.) GETSHOW also uses slightly more obscure names for its internal data, so that you are less likely to get "Symbol already defined" by using the same name in your own program.

Assembly Language Program Style

Programs submitted for marking in this course get maximum marks if they adhere to the following structure:

  1. The comments at the top of the source file for the program contain a copy of the Ian Allen Assignment Submission Label.
  2. Assembler programs are written in four columns: Labels, Mnemonics, Operands, and Comments. Programs that choose to put Labels in column 2 or Mnemonics in column 1 are unreadable and unmarkable. Follow the example of the sample programs very carefully!
  3. Comment your code! Unlike high-level languages where you can see by the names of the variables what manipulation is happening to your data, assembly language programming involves operations on registers whose names have no intrinsic meaning in the algorithm. Use meaningful comments that relate to how the code expresses your algorithm. Do not write comments that simply echo the action of the assembly-language statement:
        ADD    AL,20h  ; add 20h to AL    (pointless comment!)
    Comments must relate to your algorithm:
        ADD    AL,20h  ; make lower-case  (better comment!)
    Often, every line of an assembler program carries some form of simple in-line comment that explains how the operation relates to the algorithm being used. Don't comment the assembler; comment the algorithm.

Text editing your ASM Programs

The ASM and MASM assembler programs both insist that files be well-formed, with proper line end characters on every line, including the last line of the file.

You cannot create a text file using a word processor such as Word or WordPerfect. Even if you explicitly select "Save as text" when you save the file, or you cut-and-paste text from the word processor, chances are the word processor has inserted such non-ASCII characters as "smart quotes" and other things that will cause your program to fail to assemble.

Write your programs in a text editor (VIM, Notepad), not a word processor (Word)!

If you use Windows Notepad to build an ASM file, make sure that the last line of the file ends in a RETURN character. Failure to do this will result in "premature end of file" errors of this type:

 C:> asm /s onepage ;
 End of file encountered on input file
     end start
 onepage.asm(35): ERROR#85 - Premature end of file

Edit the file to add the missing RETURN on the last line of the file. Reading the file into DOS EDIT and writing it out will fix the problem, as will reading the file into VIM (an open source version of Unix VI) and writing it out again.

When in doubt, put a few blank lines at the end of your program source files!

If you see programming as a career, you will do well to pick and learn a proper portable text editor that has cut buffers, key macros, and global search-and-replace capabilities. (I use VIM on both Unix and Windows.)