| |
Sample Programs and Program Style
This page contains the following sections:
The sidebar and text below contain links to several Intel assembler programs. These
simple programs use DOS services to do character I/O. More complex
programs use subroutines to allow I/O using decimal numbers.
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
them. 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 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.
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).
Neither the Intel instruction set nor DOS provide ways of inputting or
outputting decimal numbers. (All you can read is ASCII characters, and all
you can output is ASCII characters.)
Alan Pinck wrote a small
I/O Package that handles the conversion of ASCII character digits to
base-ten numbers for Input and Output. Here are some sample
programs that use Alan's Decimal I/O subroutines.
Programs submitted for marking in this course get maximum marks if they
adhere to the following structure:
- The first line of each program must contain its name. This enables
the instructor to find the program on your diskette, if the assignment
requires you to submit the program on diskette.
- The comments at the top of the source file for the program contain a
copy of the Ian Allen Assignment Submission Label.
- 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!
- 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 (good 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.
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, 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, not a word processor!
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 text editor that has cut buffers, key macros, and global
search-and-replace capabilities. (I use VIM on both Unix and
Windows.)
Keyboard and editing skills should not stand in your way of a
good job!
|