This chapter is a "quick-start" chapter that lets you start writing basic assembly language programs as rapidly as possible. This chapter does the following:
Presents the basic syntax of an HLA (High Level Assembly) program
Introduces you to the Intel CPU architecture
Provides a handful of data declarations, machine instructions, and high-level control statements
Describes some utility routines you can call in the HLA Standard Library
Shows you how to write some simple assembly language programs
By the conclusion of this chapter, you should understand the basic syntax of an HLA program and should understand the prerequisites that are needed to start learning new assembly language features in the chapters that follow.
A typical HLA program takes the form shown in Figure 1-1.
pgmID
in the template above is a user-defined program identifier. You must pick an appropriate descriptive name for your program. In particular, pgmID
would be a horrible choice for any real program. If you are writing programs as part of a course assignment, your instructor will probably give you the name to use for your main program. If you are writing your own HLA program, you will have to choose an appropriate name for your project.
Identifiers in HLA are very similar to identifiers in most high-level languages. HLA identifiers may begin with an underscore or an alphabetic character and may be followed by zero or more alphanumeric or underscore characters. HLA's identifiers are case neutral. This means that the identifiers are case sensitive insofar as you must always spell an identifier exactly the same way in your program (even with respect to upper- and lowercase). However, unlike in case-sensitive languages such as C/C++, you may not declare two identifiers in the program whose name differs only by alphabetic case.
A traditional first program people write, popularized by Kernighan and Ritchie's The C Programming Language, is the "Hello, world!" program. This program makes an excellent concrete example for someone who is learning a new language. Example 1-1 presents the HLA helloWorld program.
Example 1-1. The helloWorld program
program helloWorld; #include( "stdlib.hhf" ); begin helloWorld; stdout.put( "Hello, World of Assembly Language", nl ); end helloWorld;
The #include
statement in this program tells the HLA compiler to include a set of declarations from the stdlib.hhf (standard library, HLA Header File). Among other things, this file contains the declaration of the stdout.put
code that this program uses.
The stdout.put
statement is the print statement for the HLA language. You use it to write data to the standard output device (generally the console). To anyone familiar with I/O statements in a high-level language, it should be obvious that this statement prints the phrase Hello, World of Assembly Language
. The nl
appearing at the end of this statement is a constant, also defined in stdlib.hhf, that corresponds to the newline sequence.
Note that semicolons follow the program
, begin
, stdout.put
, and end
statements. Technically speaking, a semicolon does not follow the #include
statement. It is possible to create include files that generate an error if a semicolon follows the #include
statement, so you may want to get in the habit of not putting a semicolon here.
The #include
is your first introduction to HLA declarations. The #include
itself isn't actually a declaration, but it does tell the HLA compiler to substitute the file stdlib.hhf in place of the #include
directive, thus inserting several declarations at this point in your program. Most HLA programs you will write will need to include one or more of the HLA Standard Library header files (stdlib.hhf actually includes all the standard library definitions into your program).
Compiling this program produces a console application. Running this program in a command window prints the specified string, and then control returns to the command-line interpreter (or shell in Unix terminology).
HLA is a free-format language. Therefore, you may split statements across multiple lines if this helps to make your programs more readable. For example, you could write the stdout.put
statement in the helloWorld program as follows:
stdout.put ( "Hello, World of Assembly Language", nl );
Another construction you'll see appearing in example code throughout this text is that HLA automatically concatenates any adjacent string constants it finds in your source file. Therefore, the statement above is also equivalent to
stdout.put ( "Hello, " "World of Assembly Language", nl );
Indeed, nl
(the newline) is really nothing more than a string constant, so (technically) the comma between the nl
and the preceding string isn't necessary. You'll often see the above written as
stdout.put( "Hello, World of Assembly Language" nl );
Notice the lack of a comma between the string constant and nl
; this turns out to be legal in HLA, though it applies only to certain constants; you may not, in general, drop the comma. Chapter 4 explains in detail how this works. This discussion appears here because you'll probably see this "trick" employed by sample code prior to the formal explanation.