Asl
Asl
Preface
xi
Introduction
11
1 Basic Principles
1.1 Assembler Operation
1.1.1 The source line
13
13
13
20
24
28
30
32
34
vi
Contents
35
59
2.1
2.2
2.3
2.4
2.5
66
3 Directives
3.1 Introduction
69
3.2 Program Identication Directives
73
3.3 Source Program Control Directives
74
3.4 Machine Identication Directives
76
3.5 Loader Control Directives
77
3.6 Mode Control Directives
77
3.7 Block Control & LC Directives
79
3.8 Segment Control Directives
85
3.9 Symbol Denition Directives
86
3.10 Base Register Denition Directives
89
69
Contents
vii
106
4 Macros
4.1 Introduction
109
4.1.1 The Syntax of macro denition and expansion
112
4.2 Macro Parameters
114
4.2.1 Properties of macro parameters
116
4.3 Operation of Pass 0
121
4.4 MDT Organization
122
4.4.1 The REMOVE directive
123
4.4.2 Order of search of the MDT
123
4.5 Other Features of Macros
124
4.5.1 Associating macro parameters with their arguments.
4.5.2 Delimiting macro parameters
125
4.5.3 Numeric values of arguments
125
4.5.4 Attributes of macro arguments
125
4.5.5 Directives related to arguments
126
4.5.6 Default arguments
127
4.5.7 Automatic label generation
127
4.5.8 The IRP directive
128
4.5.9 The PRINT directive
129
4.5.10 Comment lines in macros
129
4.6 Nested Macros
130
4.6.1 Nested macro expansion
130
4.7 Recursive Macros
133
4.8 Conditional Assembly
134
4.8.1 Global SET symbols
139
109
124
viii
Contents
A 6800 Example
160
A VAX Example
164
A MASM Example
169
An MPW Example
172
Review Questions and Projects
5.5.1 Project 51
175
159
175
177
194
7 Loaders
7.1 Assemble-Go Loaders
196
7.2 Absolute Loaders
198
7.3 Linking Loaders
199
7.4 The Modify Loader Directive
7.5 Linkage Editors
211
7.6 Dymanic Linking
212
7.7 Loader Control
213
7.8 Library Routines
214
7.9 Overlays
215
7.10 Multiple Location Counters
195
209
219
Contents
ix
229
References
A Addressing Modes
A.1 Introduction
254
A.2 Examples of Modes
256
A.2.1 The Direct mode
256
A.2.2 The Relative mode
257
A.2.3 The Immediate mode
257
A.2.4 The Index mode
258
249
254
Contents
B Hexadecimal Numbers
B.1 Review Questions
268
270
C Answers to Exercises
Index
271
281
Preface
My computing experience dates back to the early 1960s, when higher-level languages were fairly new. It is therefore no wonder that my introduction to computers
and computing came through assembler language; specically, the IBM 7040 assembler language. After programming in assembler exclusively (and enthusiastically)
for more than a year, I nally studied Fortran. However, my love aair with assemblers has continued, and I very quickly discovered the lack of literature in this
eld. In strict contrast to compilers, for which a wide range of literature exists,
very little has ever been written on assemblers and loaders. References [1,2,3,64]
are the best ones known to me, that describe and discuss the principles of operation of assemblers and loaders. Assembler language textbooks areof coursevery
common, but they only talk about what assemblers do, not about how they do it.
One reason for this situation is that, for many yearsfrom the mid 1950s to
the mid 1970sassemblers were in decline. The development of Fortran and other
higher-level languages in the early 1950s overshadowed assemblers. The growth of
higher-level languages was taken by many a programmer to signal the demise of
assemblers, with the result that the use of assemblers dwindled. The advent of the
microprocessor, around 1975, caused a signicant change, however.
Initially, there were no compilers available, so programmers had to use assemblers, even primitive ones. This situation did not last long, of course, and today, in
the early 1990s, there are many compilers available for microcomputers, but assemblers have not been neglected. Virtually all software development systems available
xii
Preface
for modern computers include an assembler. The assemblers described in Ch. 8 are
typical examples.
References [57] list three Z-80 assemblers running under CP/M. In spite of
being obsolete, they are good examples of modern assemblers. They are all state
of the art, relocatable assemblers that support macros and conditional assembly.
References [5,6] also include linking loaders. These assemblers reect the interest
in the Z-80 and CP/M in the early 80s. Current processors, such as the 80x86 and
the 680x0 families, continue the tradition. Modern, sophisticated assemblers are
available for these processors, and are used extensively by programmers who need
optimized code in certain procedures.
The situation with loaders is dierent. Loaders have always been used. They
are used with as well as with assemblers, but their use is normally transparent to
the programmer. The average programmer hardly notices the existence of loaders,
which may explain the lack of literature in this area.
This book diers from the typical assembler text in that it is not a programming
manual, and it is not concerned with any specic assembler language. Instead
it concentrates on the design and implementation of assemblers and loaders. It
assumes that the reader has some knowledge of computers and programming, and it
aims to explain how assemblers and loaders work. Most of the discussion is general,
and most of the examples are in a hypothetical, simple, assembler language. Certain
examples are in the assembler languages of actual machines, and those are always
specied. Some good references for specic assembler languages are [5, 6, 7, 13, 26,
27, 30, 31, 32, 35, 37, 39, 101].
This work has its origins at a point, a few years ago, when my students started
complaining about a lack of literature in this eld. Since I include assemblers and
loaders in classes that I teach every semester, I responded by developing class notes.
The notes were an immediate success, and have grown each semester, until I had
enough material for an expository paper on the subject. Since I was too busy to
polish the paper and submit it, I pretty soon found myself in a situation where the
work was too large for a paper. So here it is at last, in the form of a book.
This is mostly a professional book, intended for computer professionals in general, and especially for systems programmers. However, it can be used as a supplementary text in a systems programming or computer organization class at any
level.
Chapter 1 introduces the one-pass and two-pass assemblers, discusses other
important conceptssuch as absolute- and relocatable object lesand describes
assembler features such as local labels and multiple location counters.
Data structures for implementing the symbol table are discussed in chapter 2.
Chapter 3 presents many directives and dicusses their formats, meaning, and
implementation. These directives are supported by many actual assemblers and,
while not complete, this collection of directives is quite extensive.
The two important topics of macros and conditional assembly are introduced
in chapter 4. The treatment of macros is as complete as practically possible. I have
Preface
xiii
tried to include every possible feature of macros and the way it is implemented,
so this chapter can serve as a guide to practical macro implementation. At the
same time, I have tried not to concentrate on the macro features and syntax of any
specic assembler.
Features of the listing le are outlined, with examples, in chapter 5, while
chapter 6 is a general description of the properties of disassemblers, and of three
special types of assemblers. Those topics, especially meta-assemblers and high-level
assemblers, are of special interest to the advanced reader. They are not new, but
even experienced programmers are not always familiar with them.
Chapter 7 covers loaders. There is a very detailed example of the basic operation of a one pass linking loader, followed by features and concepts such as dynamic
loading, bootstrap loader, overlays, and others.
Finally, chapter 8 contains a survey of four modern, state of the art, assemblers.
Their main characteristics are described, as well as features that distinguish them
from their older counterparts.
To make it possible to use the book as a textbook, each chapter is sprinkled
with exercises, all solved in appendix C. At the end of each chapter there are review
problems and projects. The review questions vary from very easy questions to tasks
that require the student to nd some topic in textbooks and study it. The projects
are programming assignments, arranged from simple to more complex, that propose
various assemblers and loaders to be implemented. They should be done in the order
specied, since most of them are extensions of their predecessors. Some instructors
would nd appendix A, on addressing modes, useful.
References are indicated by square brackets. Thus [14] (or Ref. [14]) refers to
Grishmans book listed in the reference section.
This is the attention symbol. It is placed in front of paragraphs that require
special attention, that present fundamental concepts, or that are judged important
for other reasons.
Acknowledgement: I would like to acknowledge the help received from B. A.
Wichmann of the National Physical Laboratory in England. He sent me information
on the PL516 high-level assembler, the BABBAGE language, and the GE 4000
family of minicomputers. His was the only help I have received in collecting and
analyzing the material for this book. Johnny Tolliver, of Oak Ridge National Labs,
should also be mentioned. His version of the MakeIndex program proved invaluable
in preparing the extensive index of this book.
Christopher Morley
Introduction
A work on assemblers should naturally start with a denition. However, computer science is not as precise a eld as mathematics, so most denitions are not
rigorous. The denition I like best is:
An assembler is a translator that translates source instructions (in symbolic language) into target instructions (in machine language), on a one to one basis.
This means that each source instruction is translated into exactly one target instruction.
This denition has the advantage of clearly describing the translation process
of an assembler. It is not a precise denition, however, because an assembler can
do (and usually does) much more than just translation. It oers a lot of help to
the programmer in many aspects of writing the program. The many types of help
oered by the assembler are grouped under the general term directives (or pseudoinstructions). All the important directives are discussed in chapters 3 and 4.
Another good denition of assemblers is:
An assembler is a translator that translates a machineoriented language into machine language.
Introduction
Introduction
It tells the assembler to create a pass 1 listing (/D), to create a variable opt and
set its value to 5, to convert all letters read from the source le to upper case (MU),
and to include certain information in the listing le (the V, or verbose, option).
The second input is the source le. It includes the symbolic instructions and
directives. The assembler translates each symbolic instruction into one machine
instruction. The directives, however, are not translated. The directives are our
way of asking the assembler for help. The assembler provides the help by executing
(rather than translating) the directives. A modern assembler can support as many
as a hundred directives. They range from ORG, which is very simple to execute,
to MACRO, which can be very complex. All the common directives are listed and
explained in chapters 3 and 4.
The rst and most important output of the assembler is the object le. It
contains the assembled instructions (the machine language program) to be loaded
later into memory and executed. The object le is an important component of the
assembler-loader system. It makes it possible to assemble a program once, and later
load and run it many times. It also provides a natural place for the assembler to
leave information to the loader, instructing the loader in several aspects of loading
the program. This information is called loader directives and is covered in chapters
3 and 7. Note, however, that the object le is optional. The user may specify no
object le, in which case the assembler generates only a listing.
The second output of the assembler is the listing le. For each line in the source
le, a line is created in the listing le, containing:
The Location Counter (see chapter 1). The source line itself. The machine
instruction (if the source line is an instruction), or some other relevant information
(if the source line is a directive).
The listing le is generated by the assembler, sent to the printer, gets printed,
and is then discarded. The user, however, can specify either not to generate a listing
le or not to print it. There are also directives that control the listing. They can
be used to suppress parts of the listing, to print page headers, or to control the
printing of macro expansions.
Introduction
Most assemblers today are of the two-pass variety. They generate an object le
that is relocatable and can be linked and loaded by a loader.
A loader, as the name implies, is a program that loads programs into memory.
Modern loaders, however, do much more than that. Their main tasks (chapter
7) are loading, relocating, linking and starting the program. In a typical run,
a modern linking-loader can read several object les, load them one by one into
memory, relocating each as it is being loaded, link all the separate object les into
one executable module, and start execution at the right point. Using such a loader
has several advantages (see below), the most important being the ability to write
and assemble a program in several, separate, parts.
Writing a large program in several parts is advantageous, for reasons that will
be briey mentioned but not fully discussed here. The individual parts can be
written by dierent programmers (or teams of programmers), each concentrating
on his own part. The dierent parts can be written in dierent languages. It is
common to write the main program in a higher-level language and the procedures in
assembler language. The individual parts are assembled (or compiled) separately,
and separate object les are produced. The assembler or compiler can only see one
part at a time and does not see the whole picture. It is only the loader that loads
the separate parts and combines them into a single program. Thus when a program
is assembled, the assembler does not know whether this is a complete program or
just a part of a larger program. It therefore assumes that the program will start at
address zero and assembles it based on that assumption. Before the loader loads the
program, it determines its true start address, based on the memory areas available
at that moment and on the previously loaded object les. The loader then loads the
program, making sure that all instructions t properly in their memory locations.
This process involves adjusting memory addresses in the program, and is called
relocation.
Since the assembler works on one program at a time, it cannot link individual
programs. When it assembles a source le containing a main program, the assembler
knows nothing about the existence of any other source les containing, perhaps,
procedures called by the main program. As a result, the assembler may not be
able to properly assemble a procedure call instruction (to an external procedure) in
the main program. The object le of the main program will, in such a case, have
missing parts (holes or gaps) that the assembler cannot ll. The loader has access
to all the object les that make up the entire program. It can see the whole picture,
and one of its tasks is to ll up any missing parts in the object les. This task is
called linking.
The task of preparing a source program for execution includes translation (assembling or compiling), loading, relocating, and linking. It is divided between the
assembler (or compiler) and the loader, and dual assembler-loader systems are very
common. The main exception to this arrangement is interpretation. Interpretive
languages such as BASIC or APL use the services of one program, the interpreter,
for their execution, and do not require an assembler or a loader. It should be clear
from the above discussion that the main reason for keeping the assembler and loader
Introduction
separate is the need to develop programs (especially large ones) in separate parts.
The detailed reasons for this will not be discussed here. We will, however, point out
the advantages of having a dual assembler-loader system. They are listed below, in
order of importance.
It makes it possible to write programs in separate parts that may also be in
dierent languages.
It keeps the assembler small. This is an important advantage. The size of the
assembler depends on the size of its internal tables (especially the symbol table and
the macro denition table). An assembler designed to assemble large programs is
large because of its large tables. Separate assembly makes it possible to assemble
very large programs with a small assembler.
When a change is made in the source code, only the modied program needs to
be reassembled. This property is a benet if one assumes that assembly is slow and
loading is fast. Many times, however, loading is slower than assembling, and this
property is just a feature, not an advantage, of a dual assembler-loader system.
The loader automatically loads routines from a library. This is considered by
some an advantage of a dual assembler-loader system but, actually, it is not. It
could easily be done in a single assembler-loader program. In such a program, the
library would have to contain the source code of the routines, but this is typically
not larger than the object code.
Introduction
The words of the wise are as goads, and as nail fastened by masters of
assemblies
Ecclesiastes 12:11
A Short History of
Assemblers and Loaders
One of the rst stored program computers was the EDSAC (Electronic Delay Storage Automatic Calculator) developed at Cambridge University in 1949 by
Maurice Wilkes and W. Renwick [4, 8 & 97]. From its very rst days the EDSAC
had an assembler, called Initial Orders. It was implemented in a read-only memory
formed from a set of rotary telephone selectors, and it accepted symbolic instructions. Each instruction consisted of a one letter mnemonic, a decimal address, and
a third eld that was a letter. The third eld caused one of 12 constants preset by
the programmer to be added to the address at assembly time.
It is interesting to note that Wilkes was also the rst to propose the use of
labels (which he called oating addresses), the rst to use an early form of macros
(which he called synthetic orders), and the rst to develop a subroutine library [4].
Reference [65] is a very early description of the use of labels in an assembler The
IBM 650 computer was rst delivered around 1953 and had an assembler very similar
to present day assemblers. SOAP (Symbolic Optimizer and Assembly Program) did
symbolic assembly in the conventional way, and was perhaps the rst assembler to
do so. However, its main feature was the optimized calculation of the address of
the next instruction. The IBM 650 (a decimal computer, incidentally), was based
on a magnetic drum memory and the program was stored in that memory. Each
instruction had to be fetched from the drum and had to contain the address of
its successor. For maximum speed, an instruction had to be placed on the drum
in a location that would be under the read head as soon as its predecessor was
completed. SOAP calculated those addresses, based on the execution times of the
individual instructions. Chapter 7 has more details, and a programming project,
on this process.
One of the rst commercially successful computers was the IBM 704. It had
features such as oating-point hardware and index registers. It was rst delivered
in 1956 and its rst assembler, the UASAP-1, was written in the same year by Roy
Nutt of United Aircraft Corp. (hence the name UASAPUnited Aircraft Symbolic
Assembly Program). It was a simple binary assembler, did practically nothing
but one-to-one translation, and left the programmer in complete control over the
program. SHARE, the IBM users organization, adopted a later version of that
assembler [9] and distributed it to its members together with routines produced
and contributed by members. UASAP has pointed the way to early assembler
writers, and many of its design principles are used by assemblers to this day. The
UASAP was later modied to support macros [62].
In the same year another assembler, the IBM Autocoder was developed by R.
Goldnger [10] for use on the IBM 702/705 computers. This assembler (actually
several dierent Autocoder assemblers) was apparently the rst to use macros. The
Autocoder assemblers were used extensively and were eventually developed into
large systems with large macro libraries used by many installations.
Another pioneering early assembler was the UNISAP, [47] for the UNIVAC I
& II computers, developed in 1958 by M. E. Conway. It was a one-and-a-half pass
assembler, and was the rst one to use local labels. Both concepts are covered in
chapter 1.
By the late fties, IBM had released the 7000 series of computers. These came
with a macro assembler, SCAT, that had all the features of modern assemblers.
It had many directives (pseudo instructions in the IBM terminology), an extensive
macro facility, and it generated relocatable object les.
The SCAT assembler (Symbolic Coder And Translator) was originally written
for the IBM 709 [56] and was modied to work on the IBM 7090. The GAS (Generalized Assembly System) assembler was another powerful 7090 assembler [58].
The idea of macros originated with several people. McIlroy [22] was probably
the rst to propose the modern form of macros and the idea of conditional assembly.
He implemented these ideas in the GAS assembler mentioned above. Reference [60]
is a short early paper presenting some details of macro denition table handling.
One of the rst full-feature loaders, the linking loader for the IBM 704709
7090 computers [59], is an example of an early loader supporting both relocation
and linking.
The earliest work discussing meta-assemblers seems to be Ferguson [24]. The
idea of high-level assemblers originated with Wirth [61] and had been extended,
Machine Language
Assembler Language
Absolute Assembler
Directives
Relocation
Bits
External
Routines
External
Relocatable
Routines
Absolute Assembler
with
Library Routines
Relocatable
Assembler
and Loader
Linking Loader
Macros
Macro Assembler
Conditional Assembly
Full-Feature, Relocatable
Macro Assembler, with
Conditional Assembly
Phases in the historical development of assemblers and loaders.
a few years later, by an anonymous software designer at NCR, who proposed the
main ideas of the NEAT/3 language [85,86].
The diagram summarizes the main phases in the historical development of
assemblers and loaders.
Types of
Assemblers and Loaders
A One-pass Assembler: One that performs all its functions by reading the source
le once.
A Two-Pass Assembler: One that reads the source le twice.
A Resident Assembler: One that is permanently loaded in memory. Typically
such an assembler resides in ROM, is very simple (supports only a few directives
and no macros), and is a one-pass assembler. The above assemblers are described
in chapter 1.
A Macro-Assembler: One that supports macros (chapter 4).
A Cross-Assembler: An assembler that runs on one computer and assembles programs for another. Many cross-assemblers are written in a higher-level language to
make them portable. They run on a large machine and produce object code for a
small machine.
A Meta-Assembler: One that can handle many dierent instruction sets.
A Disassembler: This, in a sense, is the opposite of an assembler. It translates
machine code into a source program in assembler language.
1. Basic Principles
The basic principles of assembler operation are simple, involving just one problem, that of unresolved references. This is a simple problem that has two simple
solutions. The problem is important, however, since its two solutions introduce,
in a natural way, the two main types of assemblers namely, the one-pass and the
two-pass.
14 Basic Principles
Ch. 1
The comment is for the programmers use only. It is read by the assembler, it
is listed in the listing le, and is otherwise ignored.
The label eld is only necessary if the instruction is referred to from some
other point in the program. It may be referred to by another instruction in the
same program, by another instruction in a dierent program (the two programs
should eventually be linked), or by itself.
The word mnemonic comes from the Greek oo, meaning pertaining to
memory; it is a memory aid. The mnemonic is always necessary. It is the operation.
It tells the assembler what instruction needs to be assembled or what directive to
execute (but see the comment below about blank lines).
The operand depends on the mnemonic. Instructions can have zero, one, or
two operands (very few computers have also three operand instructions). Directives
also have operands. The operands supply information to the assembler about the
source line.
As a result, only the mnemonic is mandatory, but there are even exceptions
to this rule. One exception is blank lines. Many assemblers allow blank linesin
which all elds, including the mnemonic, are missingin the source le. They make
the listing more readable but are otherwise ignored.
Another exception is comment lines. A line that starts with a special symbol (typically a semicolon, sometimes an asterisk and, in a few cases, a slash) is
considered a comment line and, of course, has no mnemonic. Many modern assemblers (see, e.g., references [37], [99][102]) support a COMMENT directive that has the
following form:
COMMENT delimiter text delimiter
Where the text between the delimiters is a comment. This way the programmer
can enter a long comment, spread over many lines, without having to start each
line with the special comment symbol. Example:
COMMENT =This is a long
comment that . . .
.
.
. . . sucient to describe what you want=
Old assemblers were developed on punched-card based computers. They required the instructions to be punched on cards such that each eld of the source
line was punched in a certain eld on the card. The following is an example from
the IBMAP (Macro Assembler Program) assembler for the IBM 7040 [12]. A source
line in this assembler has to be punched on a card with the format:
Sec. 1.1
Assembler Operation
Columns
Field
1-6
7
8-
Label
Blank
Mnemonic
15
The semicolon guarantees that the word ENABLE will not be considered an
operand by the assembler. This is why many assemblers require that comments
start with a semicolon.
Exercise 1.1 Why a semicolon and not some other character such as $ or @ ?
Many modern assemblers allow labels without an identifying :. They simply
have to work harder in order to identify labels.
The instruction sets of some computers are designed such that the mnemonic
species more than just the operation. It may also contain part of the operand.
The Signetics 2650 microprocessor, for example, has many mnemonics that include
one of the operands [13]. A Store Relative instruction on the 2650 may be written
STRR,R0 SAV; the mnemonic eld includes R0 (the register to be stored in location
SAV), which is an operand.
On other computers, the operation may partly be specied in the operand
eld. The instruction IX7 X2+X5, on the CDC Cyber computers [14] means: add
register X2 and register X5 as integers, and store the sum in register X7. The
operation appears partly in the operation eld (I) and partly in the operand eld
(+), whereas X7 (an operand) appears in the mnemonic. This makes it harder
for the assembler to identify the operation and the operands and, as a result, such
instruction formats are not common.
16 Basic Principles
Ch. 1
Exercise 1.2 What is the meaning of the Cyber instruction FX7 X2+X5?
To translate an instruction, the assembler uses the OpCode table, which is a
static data structure. The two important columns in the table are the mnemonic
and OpCode. Table 11 is an example of a simple OpCode table. It is part of the
IBM 360 OpCode table and it includes other information.
mnemonic
A
AD
ADR
AER
AE
OpCode
5A
6A
2A
3A
1A
type length
RX
RX
RR
RR
RR
4
4
2
2
2
Table 11
The mnemonics are from one to four letters long (in many assemblers they
may include digits). The OpCodes are two hexadecimal digits (8 bits) long, and the
types (which are irrelevant for now) provide more information to the assembler.
The OpCode table should allow for a quick search. For each source line input,
the assembler has to search the OpCode table. If it nds the mnemonic, it uses the
OpCode to start assembling the instruction. It also uses the other information in
the OpCode table to complete the assembly. If it does not nd the mnemonic in the
table, it assumes that the mnemonic is that of a directive and proceeds accordingly
(see chapter 3).
The OpCode table thus provides for an easy rst step of assembling an instruction. The next step is using the operand to complete the assembly. The OpCode
table should contain information about the number and types of operands for each
instruction. In table 11 above, the type column provides this information. Type
RR means a Register-Register instruction. This is an instruction with two operands,
both registers. The assembler expects two operands, both numbers between 0 and
15 (the IBM 360 has 16 general-purpose registers). Each register number is assembled as a 4 bit eld.
Exercise 1.3 Why does the IBM 360 have 16 general purpose registers and not a
round number such as 15 or 20?
Example: The instruction AR 4,6 means: add register 6 (the source) to register 4 (the destination operand). It is assembled as the 16-bit machine instruction
1A46, in which 1A is the OpCode and 46, the two operands.
Type RX stands for Register-indeX. In these instructions the operand consists
of a register followed by an address.
Example: BAL 5,14. This instruction calls a procedure at location 14, and
saves the return address in register 5 (BAL stands for Branch And Link). It is
assembled as the 32-bit machine instruction 4550000E in which 00E is a 12-bit
Sec. 1.1
Assembler Operation
address eld (E is hexadecimal 14), 45 is the OpCode, 5 is register 5, and the two
zeros in the middle are irrelevant to our discussion. (A note to readers familiar
with the IBM 360This example ignores base registers as they do not contribute
anything to our discussion of assemblers.)
Exercise 1.4 What are the two zeros in the middle of the instruction used for?
This example is not a typical one. Numeric addresses are rarely used in assembler programming, since keeping track of their values is a tedious task better left to
the assembler. In practice, symbols are used instead of numeric addresses. Thus the
above example is likely to be written as BAL 5,XYZ, where XYZ is a symbol whose
value is an address. Symbol XYZ should be the label of some source line. Typically
the program will contain the two lines
XYZ
17
A
.
.
BAL
4,ABC
5,XYZ
Besides the basic task of assembling instructions, the assembler oers many
services to the user, the most important of which is handling symbols. This task
consists of two dierent parts, dening symbols, and using them. A symbol is
dened by writing it as a label. The symbol is used by writing it in the operand
eld of a source line. A symbol can only be dened once but it can be used any
number of times. To understand how a value is assigned to a symbol, consider the
example above. The add instruction A is assembled and is eventually loaded into
memory as part of the program. The value of symbol XYZ is the memory address of
that instruction. This means that the assembler has to keep track of the addresses
where instructions are loaded, since some of them will become values of symbols.
To do this, the assembler uses two tools, the location counter (LC), and the symbol
table.
The LC is a variable, maintained by the assembler, that contains the address
into which the current instruction will eventually be loaded. When the assembler
starts, it clears the LC, assuming that the rst instruction will go into location 0.
After each instruction is assembled, the assembler increments the LC by the size
of the instruction (the size in words, not in bits). Thus the LC always contains
the current address. Note that the assembler does not load the instructions into
memory. It writes them on the object le, to be eventually loaded into memory by
the loader. The LC, therefore, does not point to the current instruction. It just
shows where the instruction will eventually be loaded. When the source line has
a label (a newly dened symbol), the label is assigned the current value of the LC
as its value. Both the label and its value (plus some other information) are then
placed in the symbol table.
The symbol table is an internal, dynamic table that is generated, maintained,
and used by the assembler. Each entry in the table contains the denition of a
symbol and has elds for the name, value, and type of the symbol. Some symbol
18 Basic Principles
Ch. 1
tables contain other information about the symbols. The symbol table starts empty,
labels are entered into it as their denitions are found in the source, and the table
is also searched frequently to nd the values and types of symbols whose names are
known. Chapter 2 discusses various ways to implement symbol tables.
In the above example, when the assembler encounters the line
XYZ A 5,ABC ;THE SUBROUTINE STARTS HERE
it performs two independent operations. It stores symbol XYZ and its value (the
current value of the LC) in the symbol table, and it assembles the instruction.
These two operations have nothing to do with each other. Handling the symbol
denition and assembling the instruction are done by two dierent parts of the
assembler. Many times they are performed in dierent phases of the assembly.
If the LC happens to have the value 260, then the entry
name
XYZ
value
0104
type
REL
will be added to the symbol table (104 is the hex value of decimal 260, and the type
REL will be explained later).
When the assembler encounters the line
BAL 5,XYZ
it assembles the instruction but, in order to assemble the operand, the assembler
needs to search the symbol table, nd symbol XYZ, fetch its value and make it part
of the assembled instruction. The instruction is, therefore, assembled as 45500104.
Exercise 1.5 The address in our example, 104, is a relatively small number. Many
times, instructions have a 12-bit eld for the address, allowing addresses up to
212 1 = 4095. What if the value of a certain symbol exceeds that number?
This is, in a very general way, what the assembler has to do in order to assemble
instructions and handle symbols. It is a simple process and it involves only one
problem which is illustrated by the following example.
XYZ
BAL
.
.
A
5,XYZ
4,ABC
In this case the value of symbol XYZ is needed before label XYZ is dened. When the
assembler gets to the rst line (the BAL instruction), it searches the symbol table
for XYZ and, of course, does not nd it. This situation is called the future symbol
problem or the problem of unresolved references. The XYZ in our example is a future
symbol or an unresolved reference.
Sec. 1.1
Assembler Operation
19
Obviously, future symbols are not an error and their use should not be prohibited. The programmer should be able to refer to source lines which either precede
or follow the current line. Thus the future symbol problem has to be solved. It
turns out to be a simple problem and there are two solutions, a one-pass assembler
and a two-pass assembler. They represent not just dierent solutions to the future
symbol problem but two dierent approaches to assembler design and operation.
The one-pass assembler, as the name implies, solves the future symbol problem
by reading the source le once. Its most important feature, however, is that it
does not generate a relocatable object le but rather loads the object code (the
machine language program) directly into memory. Similarly, the most important
feature of the two-pass assembler is that it generates a relocatable object le, that
is later loaded into memory by a loader. It also solves the future symbol problem
by performing two passes over the source le. It should be noted at this point that
a one-pass assembler can generate an object le. Such a le, however, would be
absolute, rather than relocatable, and its use is limited. Absolute and relocatable
object les are discussed later in this chapter. Figure 11 is a summary of the most
important components and operations of an assembler.
Pass
indicator
Source
file
Location counter
Error
proc.
Source line
buffer
Main
Program
Lexical scan
routine
Object
code
assembly
area
Object
file
Opcode
table
Directive
table
Symbol
table
20 Basic Principles
Ch. 1
Sec. 1.2
21
A copy of the source line. Notice that a label, if any, is not use by pass 2 but
must be included in the intermediate le since it is needed in the nal listing.
Fig. 12 is a ow chart summarizing the operations in the two passes.
There can be two problems with labels in the rst pass; multiply-dened labels
and invalid labels. Before a label is inserted into the symbol table, the table has to
be searched for that label. If the label is already in the table, it is doubly (or even
multiply-) dened. The assembler should treat this label as an error and the best
way of doing this is by inserting a special code in the type eld in the symbol table.
Thus a situation such as:
AB
ADD 5,X
.
.
AB
SUB 6,Y
.
.
JMP AB
will generate the entry:
name
value
type
AB
MTDF
22 Basic Principles
Ch. 1
pass 1
read line
from source file
eof
?
yes
no
label
defined
?
yes
pass 2
no
determine size
of instruction
LC:=LC+size
Sec. 1.2
23
pass 2
2
read next line from
intermediate file
yes
stop
eof
?
no
assemble
instruction
24 Basic Principles
Ch. 1
67
89
126
AB
BEQ
.
.
BNE
.
.
JMP
.
.
anything
AB
;BRANCH ON EQUAL
AB
AB
;UNCONDITIONALLY
Symbol AB is used three times as a future symbol. On the rst reference, when
the LC happens to stand at 36, the assembler searches the symbol table for AB, does
not nd it, and therefore assumes that it is a future symbol. It then inserts AB into
the symbol table but, since AB has no value yet, it gets a special type. Its type is
U (undened). Even though it is still undened, it now occupies an entry in the
symbol table, an entry that will be used to keep track of AB as long as it is a future
symbol. The next step is to set the value eld of that entry to 36 (the current
value of the LC). This means that the symbol table entry for AB is now pointing
to the instruction in which AB is needed. The value eld is an ideal place for the
pointer since it is the right size, it is currently empty, and it is associated with
AB. The BEQ instruction itself is only partly assembled and is stored, incomplete,
in memory location 36. The eld in the instruction were the value of AB should be
stored (the address eld), remains empty.
When the assembler gets to the BNE instruction (at which point the LC stands
at 67), it searches the symbol table for AB, and nds it. However, AB has a type
of U, which means that it is a future symbol and thus its value eld (=36) is not
a value but a pointer. It should be noted that, at this point, a type of U does not
necessarily mean an undened symbol. While the assembler is performing its single
pass, any undened symbols must be considered future symbols. Only at the end of
the pass can the assembler identify undened symbols (see below). The assembler
handles the BNE instruction by:
Partly assembling it and storing it in memory location 67.
Copying the pointer 36 from the symbol table to the partly assembled instruction
in location 67. The instruction has an empty eld (where the value of AB should
have been), where the pointer is now stored. There may be cases where this eld
Sec. 1.3
25
in the instruction is too small to store a pointer. In such a case the assembler must
resort to other methods, one of which is discussed below.
Copying the LC (=67) into the value eld of the symbol table entry for AB,
rewriting the 36.
When the assembler reaches the JMP AB instruction, it repeats the three steps
above. The situation at those three points is summarized below.
memory
loc
contents
36
BEQ .
.
symbol
table
n
v
t
AB
.
.
36
.
.
memory
loc
contents
36
BEQ .
.
BNE 36
U
67
symbol
table
n
v
t
AB
.
.
67
.
.
memory
loc
contents
36
BEQ .
.
BNE 36
.
.
JMP 67
U
67
89
symbol
table
n
v
t
AB
.
.
89
.
.
26 Basic Principles
Address
Contents
Contents
Contents
36
BEQ .
.
BNE 36
.
.
JMP 126
BEQ -
BEQ 126
BNE 126
BNE 126
JMP 126
JMP 126
temp=67
temp=36
temp=/
Step 1
Step 2
Step 3
67
89
Ch. 1
Sec. 1.3
start.
lc=0
6
1
read line
from
source
eof
enter name,
pointer &
type of U
3
yes
5
7
no
label
defined
27
yes
copy pointer
from s.t. to
instruction
being
assembled
no
scan line
a
symbol
used
place LC in
s.t. to point
to current
instruction
being
assembled
no
3
3
assemble line
yes
search
symbol
table
not
found
load in memory
6
print LC, source
& object codes
found
3
yes
type
=D
no
28 Basic Principles
Ch. 1
search
s.t.
found
no
type
=U
Error!
label is
doubly
defined
yes
enter
name
LC &
type=D
in s.t.
follow pointer
in value field.
complete all
instr. waiting
for value of
the symbol
no
stop
U
symbol
?
yes
store LC in
value field of
s.t., change
type to D
Error!
undefined
symbol(s)
scan
s.t.
stop
104
TO
JMP TO
.
.
ADD 1,2
The JMP instruction is assembled as JMP 104 and is written onto the object le.
When the object le is loaded starting at address 0, the JMP instruction is loaded
at location 86 and the ADD instruction, at location 104. When the JMP is executed,
it will cause a branch to 104, i.e., to the ADD instruction.
Sec. 1.4
symbol table
n
AB
symbol table
AB
36
29
symbol table
AB
67
t
U
89
36
67
36
LC=36
LC=67
LC=89
symbol table
n
AB
36
89
67
30 Basic Principles
Ch. 1
In its simplest form, agging each item is done by adding an extra bit, a
relocation bit, to it. The relocation bit is set by the assembler to 0, if the item
is absolute and to 1, if it is relocatable. The loader, when reading the object le
and loading instructions, reads the relocation bits. If an object instruction has a
relocation bit of 0, the loader simply loads it into memory. If it has a relocation bit
of 1, the loader relocates it by adding the start address to it. It is then loaded into
memory in the usual way. In our example, the JMP TO instruction will be relocated
by adding 500 to it. It will thus be loaded as JMP 604 and, when executed, will
branch to location 604 i.e. to the ADD instruction.
The relocation bits themselves are not loaded into memory since memory should
contain only the object code. When the computer executes the program, it expects
to nd just instructions and data in memory. Any relocation bits in memory would
be interpreted by the computer as either instructions or data.
This explains why a one-pass assembler cannot generate a relocatable object
le. The type of the instruction (absolute or relocatable) can be determined only
by examining the original source instruction. The one-pass assembler loads the
machine instructions directly in memory. Once in memory, the instruction is just
a number. By looking at a machine instruction in memory, it is impossible to tell
whether the original instruction was absolute or relocatable. Writing the machine
instructions from memory to a le will create an object le without any relocation
bits, i.e., an absolute object le. Such an object le is useful on computers were the
program is always loaded at the same place. In general, however, such les have
limited value.
Some readers are tempted, at this point, to nd ways to allow a one-pass
assembler to generate relocation bits. Such ways exist, and two of them will be
described here. The point is, however, that the one-pass assembler is a simple,
fast, assemble-load-go program. Any modications may result in a slow, complex
assembler, thereby losing the main advantages of one-pass assembly. It is preferable
to keep the one-pass assembler simple and, if a relocatable object le is necessary, to
use a two-pass assembler (see also the discussion of a one-and-a-half pass assembler
below).
Another point to realize is that a relocatable object le contains more than
relocation bits. It contains loader directives and linking information (covered in
chapter 7). All this is easy for a two-pass assembler to generate but hard for a
one-pass one.
1.4.2 One-pass, relocatable object les
Two ways are discussed below to modify the one-pass assembler to generate a
relocatable object le.
1. A common approach to modify the basic one-pass assembler is to have it generate
a relocation bit each time an instruction is assembled. The instruction is then
loaded into memory and the relocation bit may be stored in a special, packed array
outside the program area. When the object code is nally written on the object
Sec. 1.4
31
le, the relocation bits may be read from the special array and attached each to its
instruction.
Such a method may work, but is cumbersome, especially because of future symbols. In the case of a future symbol, the assembler does not know the type (absolute
or relocatable) of the missing symbol. It thus cannot generate the relocation bit,
resulting in a hole in the special array. When the symbol denition is nally found,
the assembler should complete all the instructions that use this symbol, and also
generate the relocation bit and store it in the special array (a process involving bit
operations).
2. Another possible modication to the one-pass assembler will be briey outlined.
The assembler can write each machine instruction on the object le as soon as it is
generated and loaded in memory. At that point the source instruction is available
and can be examined, so a relocation bit can be prepared and written on the object
le with the instruction. The only problem is, as before, instructions using future
symbols. They must go on the object le incomplete and without relocation bits.
At the end of the single pass, the assembler writes the entire symbol table on the
object le.
The task of completing those instructions is left to the loader. The loader
initially skips the rst part of the object le and reads the symbol table. It then
rereads the le, and loads instructions in memory. Each time it comes across an
incomplete instruction, it uses the symbol table to complete it and, if necessary, to
relocate it as well.
The trouble with this method is that it shifts assembler tasks to the loader,
forcing the loader to do what is essentially a two-pass job.
None of these modications is satisfactory. The lesson to learn from these
attempts is that, traditionally, the one-pass and two-pass assemblers have been
developed as two dierent types of assemblers. The rst is fast and simple; the
second, a general purpose program which can support many features.
Chapter 7 discusses typical formats of relocatable object les and other items
added by the assembler to those les, to be used by the loader.
1.4.3 The task of relocating
The role of the loader is not as simple as may seem from the above discussion.
Relocating an instruction is not always as simple as adding a start address to it.
On the IBM 7090/7094 computers [65,66], for example, many instructions have the
format:
Field
OpCode
Decrement
Tag
Address
Size (in bits)
3
15
3
15
The exact meaning of the elds is irrelevant except that the Address and Decrement
elds may both contain addresses. The assembler must determine the types of both
elds (either can be absolute or relocatable), and prepare two relocation bits. The
loader has to read the two bits and should be able to relocate either eld. Relocating
the Decrement eld means adding the start address just to that eld and not to the
entire instruction.
32 Basic Principles
Ch. 1
Exercise 1.9 How can the loader add something to a eld in the middle of an
instruction ?
The discussion of separate assembly in chapter 3 (the EXTRN and ENTRY directives) shows that each eld can in fact have three dierent types, Absolute,
Relocatable, and special relocation. Thus the assembler generally has to generate
two relocation bits for each eld which, in the case of the IBM 7090/7094 (or similar computers), implies a total of four relocation bits. Chapter 7 shows how those
pairs of relocation bits are used as identication bits, identifying each line in the
relocatable object le as one of four types: an absolute instruction, a relocatable
instruction, an instruction requiring special relocation, or as a loader directives.
On the IBM PC, an absolute object le uses the extension .COM, and a relocatable object le, the extension .EXE.
Sec. 1.6
Forcing Upper
33
SB5 A6
SB6 X5
SX6 A6+B6
SA1 5
15.0
15.1
16.0
16.1
L1
SB5 A6
SB6 X5
L2 SX6 A6+B6
SA1 5
The P part of the LC indicates the position in the current word. The example
on the left does not have any labels but still has some forcing upper. Three 15-bit
instructions are loaded into location 15, positions 0, 1, 2. The next instruction,
SA1 5, is 30 bits long and does not t in the rest of the word. It is forced into
position 0 of word 16, and position 3 of location 15 is padded with a NO instruction
(which is not shown). In the example on the right, label L2 causes a forcing up of
the third instruction, resulting in two NO instructions padding location 15.
Note. The position counter in the Cyber actually indicates the number of
bits remaining in the current word. It is initialized to 60 and is decremented by the
size of the instruction, in bits. The programmer can explicitly indicate a forcing
upper of an instruction by placing a plus + in the label eld of the instruction. A
hyphen - in this eld suppresses an automatic forcing upper.
Exercise 1.10 What are other ways to explicitly request a forcing upper?
Exercise 1.11 What other case causes a forcing upper situation?
34 Basic Principles
Ch. 1
60
30
30
30
15
15
15
15
30
15
15
15
15
30
15
15
The assembler has to generate one of the values 05 as a 3-bit relocation eld
attached to each word as it is written on the object le. The loader reads this eld
and uses it to perform the actual relocation.
60
0
1
30
30
30
15
15
15
15
15
15
15
15
30
30
15
15
Sec. 1.7
35
27
A LOD
.
.
B STO
The value of A is 16 and its type is relative (meaning A is a regular label, dened
by writing it to the left of an instruction). Thus A represents address 16 from the
start of the program. Similarly B is address 27 from the start of the program. It is
thus reasonable to dene the expression B-A as having a value of 27 16 = 11 and
a type of absolute. It represents the distance between the two locations, and that
distance is 11, regardless of where the program starts.
Exercise 1.12 What about the expression A-B? is it valid? If yes, what are its
value and type?
On the other hand, an expression of the form rel + rel has no well-dened
type and is, therefore, invalid. Both A & B above are relative and represent certain
addresses. The sum A+B, however, does not represent any address. In a similar
way abs abs is abs, rel abs is rel but rel rel is invalid. abs/abs is abs, rel/abs
is rel but rel/rel is invalid. All expressions are evaluated at the last possible
moment. Expressions in any pass 0 directives (see Ch. 4 for a discussion of pass 0)
36 Basic Principles
Ch. 1
are evaluated when the directive is executed, in pass 0. Expressions in any pass 1
directives are, similarly, evaluated in pass 1. All other expressions (in instructions
or in pass 2 directives) are evaluated in pass 2.
An extreme example of an address expression is A-B+C-D+E where all the
symbols involved are relative. It is executed from left to right (((A-B)+C)-D)+E,
generating the intermediate types: (((rel rel) + rel) rel) + rel ((abs + rel)
rel) + rel (rel rel) + rel abs + rel rel. A valid expression.
In general, expressions of the type X+A-B+C-D+ +M-N+Y are valid when X,Y
are absolute and A,B, ,M,N are relative. The relative symbols must come in
pairs like A-B except the last one M-N, where N may be missing. If N is missing, the
entire expression is relative, otherwise, it is absolute.
Exercise 1.13 How does the assembler handle an expression such as A-B+K-L in
which all the symbols are relative but K,L are external?
1.7.1 Summary
The two-pass assembler generates the machine instructions in pass two, where it
has access to the source instructions. It checks each source instruction and generates
a relocation bit according to:
If the instruction uses a relative symbol, then it is relocatable and the relocation
bit is 1.
If the instruction uses an absolute symbol (see the discussion of EQU in chapter 3)
or uses no symbols at all, the instruction is absolute and the relocation bit is 0.
An instruction in the relative mode contains an oset, not the full address, and
is therefore absolute (see App. A for the ralative mode).
The one-pass assembler generates the object le at the end of its single pass,
by dumping all the machine instructions from memory to the le. It has no access
to the source at that point and therefore cannot generate relocation bits.
As a result, those two types of assemblers have evolved along dierent lines,
and represent two dierent approaches to the overall assembler design, not just to
the problem of resolving future symbols.
Sec. 1.8
Local Labels
37
is justied since it is only used to jump over the array DDCT. (Note that the arrays
name is descriptive, possibly meaning deductions or double-dictionary) The DS directive is explained in chapter 3. We say that A1 is used only locally, to serve a
limited purpose.
As a result, many assemblers support a feature called local labels. It is due
to M. E. Conway who used it in the early UNISAP assembler for the UNIVAC I
computer [47]. The main idea is that if a label is used locally and does not require
a descriptive name, why not give it a name that will signify this fact. Conway used
names such as 1H, 2H for the local labels. The name of a local label in our examples
is a single decimal digit. When such a label is referred to (in the operand eld), the
digit is followed by either B or F (for Backward or Forward).
LC
.
.
13
1:
...
.
.
17
JMP 1F
.
.
24 1: LOD R2,1B
.
.
31
1: ADD R1,2F
.
.
102 2: DC 1206,-17
.
.
115
SUB R3,2B-1
jump to 24
2F is address 102
102-1=101
13
24
31
102
38 Basic Principles
Ch. 1
The order of the labels in the symbol table is important. If the symbol table is
sorted between the two passes, all occurrences of each local label should remain
sorted by value. In pass 2, when an instruction uses a local label such as 1F, the
assembler identies the specic occurence of label 1 by comparing all local labels
1 to the current value of the LC. The rst such instruction in our example is the
JMP 1F at LC=17. Clearly, the assembler should look for a local label with the
name 1 and a value 17. The smallest such label has value 24. In the second
case, LC=24 and the assembler is looking for a 1B. It needs the label with name 1
and a value which is the largest among all values < 24. It therefore identies the
label as the 1 at 13.
Exercise 1.14 If we modify the instruction at 24 above to read 1:
would the 1F refer to address 31 or 24?
LOD R2,1F
In a one-pass assembler, again the labels are recognized and put into the symbol
table in the single pass. An instruction using a local label iB is no problem, since
is needs the most recent occurence of the local label 1 in the table. An instruction
using an iF is handled like any other future symbol case. An entry is opened in the
symbol table with the name iF, a type of U, and a value which is a pointer to the
instruction.
In the example above, a snapshot of the symbol table at LC=32 is:
Symbol Table
n
v
t
1
1
1
2
13
24
31
31
D
D
D
U
An advantage of this feature is that the local labels are easy to identify as such,
since their names start with a digit. Most assemblers require regular label names
to start with a letter.
In modern assemblers, local labels sometimes use a syntax dierent from the
one shown here. See Ch. 8 for examples.
1.8.1 The LC as a local symbol
Virtually all assemblers allow a notation such as BPL *+6 where * stands
for the current value of the LC. The operand in this case is located at a point 6
locations following the BPL instruction.
The LC symbol can be part of any address expression and is, of course, relocatable. Thus *+A is valid if A is absolute, while *-A is always okay (and is absolute
if A is relative, relative if A is absolute). This feature is easy to implement. The
address expression involving the * is calculated, using the current value of the LC,
and the value is used to assemble the instruction, or execute the directive, on the
current source line. Nothing is stored in the symbol table.
Sec. 1.8
Local Labels
39
Some assemblers use the asterisk for multiplication, and may designate the
period . or the $ for the LC symbol.
ADD D, ...
USE DATA
DS 12
USE *
.
.
This directive directs the assembler to start using (or to resume the use of) a new
location counter. The name is specied in the operand eld, so an empty operand
means the main LC. The asterisk * implies the previous LC, the one that was used
before the last USE.
40 Basic Principles
Ch. 1
Exercise 1.16 The previous section discusses the use of asterisk as the LC value.
When executing a USE *, how does the assembler know that the asterisk is not the
LC value?
The USE directives divide the program into several sections, which are loaded,
by the loader, into separate memory areas. The sections are loaded in the order in
which their names appear in the source. Fig. 18 is a good example:
USE
USE
USE
USE
USE
USE
.
. (1)
.
DATA
.
. (2)
.
*
.
. (3)
.
BETA
.
. (4)
.
DATA
.
. (5)
.
<space>
.
. (6)
.
GAMMA
.
. (7)
.
END
At load time, the dierent sections would be loaded in the order MAIN, DATA,
BETA, GAMMA or 1,3,6,2,5,4,7. Chapter 7 explains the details of such a load, which
involves an additional loader pass.
Exercise 1.17 Can we start a program with a USE ABC? in other words, can the
rst section be other than the main section?
Another example of the same feature is procedures. In assembler language, a
procedure can be written as part of the main program. However, the procedure
Sec. 1.9
41
should only be executed when called from the main program. Therefore, it should
be separated from the main instruction stream, since otherwise the hardware would
execute it when it runs into the rst instruction of the procedure. So something
like:
.
0
15
16
17
45
46
104
.
LOD ...
.
.
SUB ...
CALL P
P ADD R5,N
.
.
RET
CLR ...
.
.
END
is wrong. The procedure is dened on lines 1745 and is called on line 16. This
makes the source program more readable, since the procedure is written next to its
call. However, the hardware would run into the procedure and start executing it
right after it executes line 16, i.e., right after it has been called. The solution is to
use a new LCnamed, perhaps, PROCby placing a USE PROC between lines 16, 17
and a USE * between lines 45, 46.
1.9.2 COMMON blocks
Fortran programmers are familiar with the COMMON statement. This is a block
of memory reserved in a common area, accessible to the main program and to all
its procedures. It is allocated by the loader high in memory, overlaying the loader
itself. The common area cannot be preloaded with constants, since it uses the same
memory area occupied by the loader. In many assemblers, designed to interface with
Fortran programs, there is a preassigned LC, called //, such that all data declared
under it end up being loaded in the common area. The concept of labeled common
in Fortran also has its equivalent in assembler language. The Fortran statement
COMMON/NAM/A(12),B(5) can be written in assembler language as:
A
B
C
.
USE /NAM/
DS 12
DS 5
USE DAT
DC 56,-90
USE
.
.
42 Basic Principles
Ch. 1
The two arrays A, B would be loaded in the labeled common /NAM/, while the
constants labeled C would end up as part of section DAT.
The IBM 360 assembler has a CSECT directive, declaring the start of a control
section. However, a control section on the 360 is a general feature. It can be used to
declare sections like those described here, or to declare sections that are considered
separate programs and are assembled separately. They are later loaded together,
by the loader, to form one executable program. The dierent control sections are
linked by global symbols, declared either as external or as entry points. The entire
concept is explained in chapter 3, as part of the discussion of the EXTRN, ENTRY
directives.
The VAX Macro assembler (see Ch. 8) [77] has a .PSECT directive similar to
CSECT, and it does not support multiple LCs. A typical VAX example is:
.TITLE CALCULATE PI
.PSECT DATA, NOEXE,WRT
A=2000
B: .WORD 6
C: .LONG 8
.PSECT CODE, EXE,NOWRT
.ENTRY PI,0
.
.
<instructions>
.
.
$EXIT
.PSECT CONS, NOEXE,NOWRT
K: .WORD 1230
.END
PI
Each .PSECT includes the name of the section, followed by attributes such as
EXE, NOEXE, WRT, NOWRT.
The memory on the 80x86 microprocessors is organized in 64k (highly overlapping) segments. The microprocessor can only generate 16-bit addresses, i.e., it
can only specify an address within a segment. A physical address is created by
combining the 16-bit processor generated address with the contents of one of the
segment registers in a special way (see refs. [38, 57] for the details). There are four
such registers: The DS (data segment), CS (code segment), SS (stack segment) and
ES (extra segment).
When an instruction is fetched, the PC is combined with the CS register and
the result is used as the address of the next instruction (in the code segment). When
an instruction species the address of a piece of data, that address is combined with
the DS register, to obtain a full address in the data segment. The extra segment
is normally used for string operations, and the stack segment, for stack-oriented
instructions (PUSH, POP or any instructions that use the SP or BP registers).
Sec. 1.9
43
1.10 Literals
Many instructions require their operands to be addresses. The ADD instruction
is typically written ADD AB,R3 or ADD R3,AB where AB is a symbol and the instruction adds the contents of location AB to register 3. Sometimes, however, the
programmer wants to add to register 3, not the contents of any memory location
but a certain constant, say the number 7. Modern computers support the immediate mode which allows the programmer to write ADD #-7,R3. The number
sign # indicates the immediate mode and it implies that the instruction contains
the operand itself, not the address of the operand. Most old computers, however,
do not support this mode; their instructions have to contain addresses, not the
operands themselves. Also, in many computers, an immediate operand must be a
small number.
To help the programmer in such cases, some assemblers support literals.A notable example is the MPW assembler for the Macintosh computer (see Ch. 8). A
literal is a constant preceded by an equal sign =. Using literals, the programmer
can write ADD =-7,R3 and the assembler handles this by:
Preloading the constant 7 in the rst memory location in the literal table (however, see the LITORG directive in chapter 3 for an exception). The literal table is
loaded in memory immediately following the program.
Assembling the instruction as ADD TMP,R3 where TMP is the address where the
constant was loaded
Such assemblers may also support octal (=O377 or =377B), hex (=HFF0A),
real (=1.37 or =12E-5) or other literals.
1.10.1 The literal table
To handle literals, the assembler maintains a table, the literal table, similar
to the symbol table. It has columns for the name, value, address and type of each
literal. In pass 1, when the assembler nds an instruction that uses a literal, such as
7, it stores the name (7) in the rst available entry in the literal table, together
with the value (1 . . . 110012 ) and the type (decimal). The instruction itself is treated
as any other instruction with a future symbol. At the end of pass 1, the assembler
uses the LC to assign addresses to the literals in the table. In pass 2, the table
is used, in much the same way as the symbol table, to assemble instructions using
literals. At the end of pass 2, every entry in the literal table is treated as a DC
directive and is written on the object le in the usual way. There are three points
to consider when implementing literals.
44 Basic Principles
Ch. 1
Two literals with the same name are considered identical; only one entry is generated in the literal table. On the other hand, literals with dierent names are treated
as dierent even if they have identical values, such as =12.5 and =12.50.
All literals are loaded following the end of the program. If the programmer wants
certain literals to be loaded elsewhere, the LITORG directive can be used. It is fully
described in chapter 3, but the following example claries a point that should be
mentioned here.
.
ADD =-7,R3
.
LITORG
.
SUB =-7,R4
.
The rst 7 is loaded, perhaps with other literals, at the point in the program
where the LITORG is specied. The second 7, even though identical to the rst, is
loaded separately, together with all the literals used since the LITORG, at the end of
the program.
The LITORG directive is commonly used to make sure that a literal is loaded
in memory close to the instruction using it. This may be important in case the
relative mode is used.
Sec. 1.10
Literals
45
operand becomes a word (2 bytes or 16 bits) and the instruction is now 5 bytes
long. The second byte species register F (which happens to be the PC on
the VAX) in mode 8 (autoincrement). This combination is equivalent to the
immediate mode, where the immediate operand is stored in the third byte of
the instruction. The last byte (56) is as before.
3. Again, a MOVL instruction but in a dierent context.
LC
MOVL #DATA,R6 assembled into D0 8F 00000037 56
.
.
0037 DATA .BYTE ...
.
.
Even though the operand is small (0037) and ts in six bits, the assembler
has automatically selected the immediate mode (8F) and has generated the
constant as a long word (32 bits). The reason is that the source instruction
uses a future symbol (DATA). The assembler has to determine the instruction
size in pass 1 and, since DATA is a future symbol, the assembler does not have
its value and has to assume the largest possible value. The result is a seven
byte instruction instead of the three bytes in the rst example!
Incidentally, the qoute in (00000047) indicates that the constant is relocatable.
46 Basic Principles
Ch. 1
programmer. Thus things such as TA, LB specify the type and length of symbols
and can be used throughout the program. Examples such as:
H DC LX the length of X (in words) is preloaded in location H
G DS LX array G has LX elements
AIF (TX=ABS).Z a conditional assembly directive, see chapter 4.
are possible, even though not common.
Sec. 1.12
Assembly-Time Errors
47
Even if the operands are of the right type, their values may be out of range.
In a seemingly innocent instruction such as LOD R16,#70000, either operand, or
even both, may be invalid. If the computer has 16 registers, R0-R15, then R16 is
out of range. If the computer supports 16-bit integers, then the number 70000 is
too large.
Even if the operands are valid, there may still be errors such as a bad addressing
mode. Certain instructions can only use certain modes. A specic mode can only
use addresses in a certain range. Project 14 at the end of this chapter describes
an assembler where such restrictions exist and quite a few errors are possible.
4. General errors do not pertain to any individual line and have to do with the general status of the assembler. Examples are out of memory, cannot read/write
le xxx, illegal character read from source le, table xxx overow phase
error between passes.
The last example is particularly interesting and will be described in some detail. It is issued when pass 1 makes an assumption that turns out, in pass 2,
to be wrong. This is a severe error that requires a reassembly. Phase errors require a computer with sophisticated instructions and complex memory management; they dont exist on computers with simple architectures. The Intel 80x86
microprocessorswith variable-size instructions, several oset sizes, and segmented
memory managementare a good example of computer architecture where phase
errors may easily occur.
Here are two examples of phase errors on those microprocessors. (Refs. [38, 57]
are good introductions to 8086/8088 architecture and instruction set):
An instruction in a certain code segment refers to a variable declared in a data
segment following the code segment. In pass 1, the assembler assumes that the
variable is declared in the same segment as the instruction, and is a future symbol.
The instruction is determined accordingly. In pass 2, when the time comes to
assemble the instruction, all the variables are known, and the assembler discovers
that the variable in question is far. A longer instruction is necessary, the pass-1
assumption turns out to be wrong, and pass 2 cannot assemble the instruction. This
error is illustrated below.
CODE S
CODE S
DATA S
ABC
DATA S
SEGMENT PUBLIC
..
..
MOV
..
..
ENDS
DB
..
ENDS
END
AL,ABC
SEGMENT PUBLIC
123
START
48 Basic Principles
Ch. 1
an instruction in the relative mode has a eld for the relative address (the oset).
Several possible oset sizes are possible on the 80x86, depending on the distance
between the instruction and its operand. If the operand is a future symbol, even
in the same segment as the instruction, the assembler has to guess a size for the
oset. In pass 2 the operand is known and, if it too far from the instruction, the
oset size guessed in pass 1 may turn out to be too small.
The Microsoft macro assembler (MASM), a typical modern assembler for the
80x86 microprocessors, features a list of about 100 error messages. Even an early
assembler such as IBMAP for the IBM 7090 [65] had a list of 125 error messages,
divided into four classes according to the severity of the error.
Sec. 1.13
49
N
Q
P
Z
AR 1,2
LR 3,4
SBI M,3
CR 4,5
MVI #5,Q
MR 6,7
JMP D
11. The asterisk * is used, among other things, to indicate the LC value. It can
be used in address expressions such as *+4. Since such an expression indicates an
address greater than the current address, is it an unresolved reference?
12. What feature of assembler language leads to the idea of a two-pass assembler?
13. Compare and contrast literals and the immediate mode. What are the advantages and disadvantages of each?
50 Basic Principles
Ch. 1
1.13.1 Project 11
Your task is to implement and test the assembler described below. The entire
project should be done twice, as a two-pass and as a one-pass assembler. The source
and object codes described here are extremely simple, as they ought to be for a rst
project, and are based on a hypothetical, simple, second generation computer. The
computer is supposed to have a single working register, traditionally called the
Accumulator (Acc). It has M words of storage, each N bits long. Neither M nor
N are specied, and part of your task is to come up with several sets of reasonable
values, select two sets, and use one for the two-pass and the other, for the one-pass,
assembler. This would guarantee a full understanding of the way those values aect
the design of the instruction set, the source instructions, and the object instructions.
More about M , N below.
The instruction set is both small and simple, making it easy to implement
the assembler, but hard to write programs for the computer (however, your test
programs should be short and can be meaningless, since they only test the assembler,
not the hardware).
mnem
LOD
STO
ADD
BZE
BNE
BRA
INP
OUT
CLA
HLT
OpCode
1
2
3
4
5
6
7
8
9
0
operand description
yes
no
AccMem(op)
Mem(op)Acc
AccAcc+Mem(op)
branch to Op if Acc=0
same for Acc<0
unconditional branch
Accthe next character in the input stream
next char in output streamAcc
Acc0
stop
Sec. 1.13
Label Source
X
Y
INP
STO
INP
STO
BZE
ADD
OUT
BRA
LOD
ADD
STO
HLT
50
51
X
50
Y
50
50
52
51
Object
7
2
7
2
4
3
8
6
1
3
2
0
50
51
X
50
Y
50
50
52
Test Program
Before implementing the assembler, values for M , N should be selected. One
simple, although not a very practical, choice is M = 1024 = 1k, N = 16. This implies that each operand is 10 bits long, and also makes it easy to t each instruction
in one memory word. Each instruction will now have a 6-bit OpCode, allowing for
up to 64 instructions, and a 10-bit operand which, in many cases, will be unused.
The example above is now duplicated, as table 12, with the object codes shown in
binary.
LC Label
0
1
2
3
4
5
6
7
8
X
9
10
Y
11
Source
INP
STO 50
INP
STO 51
BZE X
ADD 50
OUT
BRA Y
LOD 50
ADD 50
STO 52
HLT
Object
OpCode
Op
000111
0
000010 0000110010
000111
0
000010 0000110011
000100 0000001000
000011 0000110010
001000
0
000110 0000001010
000001 0000110010
000011 0000110010
000010 0000110100
000000
0
Table 12.
The most important point about this choice of M , N is the many unused
Op elds (in a third of the instructions in our example). This clearly points to
a very common principle of instruction set design: variable length instructions.
Instructions with an Op eld should be longer than ones with only an OpCode.
52 Basic Principles
Ch. 1
Sec. 1.13
53
54 Basic Principles
Ch. 1
108
R1,...
Table 13.
The DC generates the record 0...0108 01 since the value of label A is 108
locations from the start of the program.
Notice that now we have three types of records on the object le, namely, absolute instructions, relocatable instructions, and loader directives. They should each
be identied by two bits (see also discussion of special relocation in this chapter).
1.13.4 Project 14
This is more than an extension of previous projects. Here we describe a more
realistic assembler that can handle more registers, more directives, and addressing
modes. It still is a very simple assembler, though. A special feature of this assembler
is that it can operate as either a one-pass or a two-pass assembler. As usual, it
should read a source le with a test program, assemble it into machine code and
either load the machine code directly in memory (the one-pass version), or write it
on an object le (the two-pass one). It is suggested to write the one-pass and twopass assemblers as two separate parts of the project, sharing common procedures.
Examples of common procedures are symbol table search, OpCode table search,
and lexical scan of the source line.
The Hypothetical Processor
It has sixteen 16-bit registers, two status ags Z, N , and can handle up to 64k
addresses (a choice of M = 16). All instructions have the following format:
OpCode Register
4
Mode Operand
2
Sec. 1.13
55
and are 16-bit long. Each instruction is loaded into one memory word, implying
a choice of N = 16. This roughly corresponds to the architecture of early thirdgeneration computers. The concept of a status ag is explained in any book on
computer organization and in many books on assembler language. As mentioned
before, it is not necessary to understand status ags in order to implement the
assembler.
The source instructions are:
Op- Mnemonic
valid
Code Operands Z N modes
0
LOD R,Op x x
0123
1
STO R,Op
012
2
ADD R,Op x x
0123
3
COM R
x x
0
4
DIV R,Op x x
0123
5
JZE Op
012
6
JNE Op
012
7
JMP Op
012
8
PRT R,Op
012
9
OUT Op
012
10
HLT none
0
Description
Load register R from the loc. specied by Op
Store R in memory
Add contents of memory location to register R
Complement every bit in R
RQuotient(R/Op). Integer/integer division
Jump to address Op if Z=1
Jump if N=1
Unconditional jump
Print R characters, starting from location Op.
Print the operand as an integer
Stop the processor
All other OpCodes are not implemented yet and are reserved for future use. It
is important to understand that the assembler only assembles the instructions, and
does not execute them. Therefore the decription of the instructions in not important.
Omitting that column from the table above would not make it impossible (or even
harder) to implement the assembler. An assembler generally does not know what
the instructions do, what the addressing modes mean, and when and how the status
ags are used. These are all run-time features, handled by the hardware.
The modes:
0 - Direct, 1 - Relative. 2 - Indirect. 3 - Immediate. They are special cases
of the modes explained in appendix A. Note that certain instructions can only use
certain modes. An error should be detected (and handled as shown below) if such
an instruction tries to use an invalid mode. Also note that mode 1 can generate,
when the instruction is executed, addresses > 64k. Those are illegal addresses on
our machine, but can only be detected at run time, so you dont have to worry
about them.
The indirect & immediate modes are explicitly selected by the programmer. If
a mode is not specied, the assembler should try the direct mode (as on lines 50,
52 in the example below). Since the Op eld is six bits, a direct address must t
in six bits and is therefore limited to the range 063. If the direct mode cannot be
used (the Op is > 63), the assembler should try the relative mode (as on line 55).
If that mode cannot be used either, the assembler should issue an error, assemble
the line as all zeros, and set a ag that will prevent execution of the program.
Exercise 1.24 In what cases is the relative mode invalid?
56 Basic Principles
Ch. 1
The Directives
1. PASS n where n is either 1 or 2. This should always be the rst line and it
species the number of passes.
2. ORG n where n is a non-negative integer. This diredctive resets the LC to n.
It is executed in pass 1 and only aects the LC.
3. label EQU n where n is as above. This directive places the label in the symbol
table with a value n and a type of abs. It is executed in pass 1 and only aects the
symbol table.
4. DC m initializes the current location to m, where m is a number as abve or
a string of up to three characters.
5. END n indicates the end of the source program. Here n is a symbol and, if
present, it indicates the address of the rst executable instruction.
see chapter 3 for more information on those directives.
The Object le
Should contain the machine instructions (absolute and relative), and loader
directives. Each item should be identied by one bit as either an instruction, data
or a loader directive. There are no relocation bits. Design your own format and
document your design.
The loader directives are generated by the assembler in response to the ORG,
END directives.
An example test program
Such a program does not have to be meaningful but, in our case, it is. It
calculates the expression [(A + B) + C]/D = [(2 + 2) + 8]/2 = 2 and stores it in
register R1.
address
source
code RMOp address
source
code RMOp
49
50
51
52
53
54
55
56
57
58
59
DC 2
LOD R1,A
ADD R1,@B
COM R1
ADD R1,#1
ADD R1,C
DIV R1,D
JZE N
PRT 3,E
HLT
PRT 2,F
0
2
3
2
2
4
5
8
10
8
2
1061
1262
1000
1301
1063
1109
0059
3108
0000
3109
60
61
62
63
64
65
66
67
68
69
HLT
A CD 2
B DC G
C DC 8
D DC 2
E DC YES
DC NO
10 0000
2
49
8
2
89 ASCII
69
83
78
79
END
Notice that the program starts at address 49 but the rst executable instruction
is at address 50. The program is loaded in memory locations 4970 so it would
straddle address 63. This is done in order to illustrate both the direct and relative
modes.
Sec. 1.13
57
General Notes
1. Pass 1 is especially simple since all the instructions have the same size.
You will also nd that the intermediate le contains a copy of the source and
almost nothing else. To make this project more interesting, you should redesign the
instruction set to have a few long instructions. Perhaps all the instructions with an
Op eld should be 32 bits long.
2. Notice that the Op eld is only 6 bits wide. In a one-pass assembler, this
may make it impossible to store a pointer in this eld and you may have to resort
to storing all the unresolved references of each future symbol, in a linked list, as
described earlier in this chapter.
The shortage of a single kind of bolt would hold up the entire assembly. . .
The organization of the symbol table is the key to fast assembly. Even when
working on a small program, the assembler may use the symbol table hundreds
of times and, consequently, an ecient implementation of the table can cut the
assembly time signicantly even for short programs.
The symbol table is a dynamic structure. It starts empty and should support
two operations, insertion and search. In a two-pass assembler, insertions are done
only in the rst pass and searches, only in the second. In a one-pass assembler,
both insertions and searches occur in the single pass. The symbol table does not
have to support deletions, and this fact aects the choice of data structure for
implementing the table. A symbol table can be implemented in many dierent
ways but the following methods are almost always used, and will be discussed here:
A linear array.
A sorted array with binary search.
Buckets with linked lists.
A binary search tree.
A hash table.
Ch. 2
Sec. 2.3
61
Ch. 2
BGH
A345
CC
J12
MED
ON
TOM
QUE
ZIP
PETS
Figure 21. A Binary Search Tree
Sec. 2.4
63
Reference [15] is a good source for binary search trees and it also discusses the
average times for insertion, search, and deletion (which, in the case of a symbol
table, is unnecessary). The minimum number of steps for insertion or search is
obviously 1. The maximum number of steps depends on the height of the tree. The
tree in Fig. 21 above has a height of 7, so the next insertion will require from 1
to 7 steps. The height of a binary tree with N nodes varies between log2 N (which
is the height of a fully balanced tree), and N (the height of a skewed tree). It can
be proved that an average binary tree is closer to a balanced tree than to a skewed
tree, and this implies that the average time for insertion or search in a binary search
tree is of the order of log2 N .
Advantages: Ecient operation (as measured by the average number of steps).
Flexible size.
Disadvantages: Each step is more complex than in an array-based symbol table.
The recommendations for use are the same as for the previous method.
Ch. 2
Ideally, a hash table requires xed time for insert and search, and can be
an excellent choice for a large symbol table. There are, however, two problems
associated with this method namely, collisions and overow, that make hash tables
less than ideal.
Collisions involve the case where two entirely dierent symbol names are hashed
into identical indexes. Names such as SYMB and ZWYG6 can be hashed into the same
value, say, 54. If SYMB is encountered rst in the program, it will be inserted
into entry 54 of the hash table. When ZWYG6 is found, it will be hashed, and
the assembler should discover that entry 54 is already taken. The collision problem
cannot be avoided just by designing a better hash function. The problem stems from
the fact that the set of all possible symbols is very large, but any given program
uses a small part of it. Typically, symbol names start with a letter, and consist of
letters and digits only. If such a name is limited to six characters, then there are
26 365 ( 1.572 billion) possible names. A typical program rarely contains more
than, say, 500 names, and a hash table of size 512 (= 29 ) may be sucient. When
1.572 billion names are mapped into 512 positions, more than 3 million names will
map into each position. Thus even the best hash function will generate the same
index for many dierent names, and a good solution to the collision problem is the
key to an ecient hash table.
The simplest solution involves a linear search. All entries in the symbol table
are originally marked as vacant. When the symbol SYMB is inserted into entry 54,
that entry is marked occupied. If symbol ZWYG6 should be inserted into entry 54
and that entry is occupied, the assembler tries entries 55, 56 and so on. This implies
that, in the case of a collision, the hash table degrades to a linear table.
Another solution involves trying entry 54 + P where P and the table size are
relative primes. In either case, the assembler tries until a vacant entry is found or
until the entire table is searched and found to be all occupied.
Morris [16] presents a complete analysis of hash tables, where it is shown that
the average number of steps to insert (or search for a) symbol is 1/(1 p) where p
is the percent-full of the table. p = 0 corresponds to an empty table, p = 0.5 means
a half-full table, etc. The following table gives the average number of steps for a
few values of p.
number
of steps
0
.4
.5
.6
.7
.8
.9
.95
1
1.66
2
2.5
3.33
5
10
20
Sec. 2.5
A Hash Table
65
It is clear that when the hash table gets more than 50%60% full, performance
suers, no matter how good the hashing function is. Thus a good hash table design
makes sure that the table never gets more than 60% occupied. At that point the
table is considered overowed.
The problem of hash table overow can be handled in a number of ways. Traditionally, a new, larger table is opened and the original table is moved to the new one
by rehashing each element. The space taken by the original table is then released.
Hopgood [17] is a good analysis of this method. A better solution, though, is to use
open hashing.
2.5.2 Open hashing
An open hash table is a structure consisting of buckets, each of which is the
start of a linked list of symbols. It is very similar to the buckets with linked lists
discussed above. The principle of open hashing is to hash the name of the symbol
and use the hash index to select a bucket. This is better than using the rst
character in the name, since a good hash function can evenly distribute the names
over the buckets, even in cases where many symbols start with the same letter. Aho
et al. [18] presents an analysis of open hashing.
Ch. 2
Sec. 2.6
67
2.6.3 Project 23
Implement, test, and compare the following four hashing algorithms:
1. Split the symbol name into groups of two characters each. Add all the groups.
Divide the sum by the table size. The hash index is the remainder of the
division.
2. Split the symbol name into two groups, as equal in size as possible. Add the
two groups. Square the sum. Use the center 16-bit part of the result, and
divide it by the table size. Again, the hash index is the remainder.
3. Split the symbol name into two groups as before. Perform the Exclusive-Or
of the two groups. The hash index is a chunk of size log2 [the table size] taken
from the center of the result.
4. Convert the symbol name into groups of four bits each. Each character of the
name is split into two such groups. Convert each four-bit group into a decimal
digit according to the rule: If the group is 9, leave it alone; otherwise, mask
o the lefmost bit. Thus the character 11010110 is split into 1101, 0110 and
the rst group (whose value is 13) is converted into 0101 (=5). The result is
56.
Each character results in two decimal digits. Concatenate all the left decimal
digits to form one decimal number, and all the right decimal digits, to form another
number. The two decimal numbers should be added, the result reversed (e.g. 12345
becomes 54321), and divided by the table size. The hash index is the remainder,
after being converted back into binary.
To test and compare the four methods, prepare a list of names whose size is
60%70% of the hash table size. Apply each method to the list. Measure the time
it takes to insert all names in the hash table, and also measure quantities such as
the average number of steps for an insert, and the distribution of hash indexes.
Thomas Carlyle
An idea, in the highest sense of that word, cannot be conveyed but by a
symbol
3. Directives
3.1 Introduction
In addition to assembling instructions, the assembler oers help to the programmer in the form of directives. The directives are commands to the assembler,
directing it to perform operations other than assembling instructions. The directives are thus executed by the assembler, not assembled by it. They may aect all
the operations of the assembler. Directives may aect the object code, the symbol
table, the listing le, and the values of internal assembler parameters. Certain directives are executed in pass 1 and others, in pass 2. Many directives are executed
in both passes. Directives that have to do with macros and conditional assembly
are normally executed in a special pass, pass 0. Regardless of when a directive is
executed, it must be passed over to pass 2, to be included in the listing le.
Some directives are passed by the assembler to the loader, through the object
le. They are eventually executed by the loader. Other directives are used as
programming tools, to simplify the process of writing the program and preparing
the source le.
Simple assemblers may support only a few directives, while large, modern assemblers may support about a hundred. Perhaps the fastest way for the assembler
to identify each directive, is to include all the directives in the OpCode table. The
table should, in such a case, contain more information, identifying each item as
either a machine instruction or a directive. The table should also specify the pass
70 Directives
Ch. 3
(0,1 or 2) in which the directive should be executed, and should contain the start
address of the routine that executes the directive. If a directive is executed in both
passes, the table should include two such addresses.
Exercise 3.1 Some assemblers require each directive to start with a period ., for
easy identication. Why isnt such a convention adopted by every assembler?
Figure 31 is a generalization of gure 12, that includes directives.
pass 1
read line
from source file
generate END
record on intermediate file
1
yes
directive
?
yes
no
yes
store name
& value in
symbol table
END
?
no
label
defined
?
a
pass 1
directive?
pass 2
no
yes
no
execute it
determine size
of instruction
LC:=LC+size
generate a
record on
intermediate file
Sec. 3.1
Introduction
71
pass 2
2
read next line from
intermediate file
yes
stop
END
?
no
directive
?
yes
a
pass 2
directive
?
no
no
yes
assemble
instruction
execute it
72 Directives
Ch. 3
Sec. 3.2
73
Operation
IDENT
Operand
name,origin
The name in the operand eld becomes the name of the program. This directive is for the benet of the loader, which uses the program name to identify the
individual programs loaded, and to print a memory map. This directive is normally
not required and many assemblers do not even support it. Some assemblers use the
TITLE directive for this purpose. To execute it, the assembler writes the name on
the object le together with a special code (a loader directive) that tells the loader
what it is. chapter 7 contains examples of loader directives and memory maps. The
origin eld, if used, indicates the start address of the program. It is only used for
absolute assembly.
A few words about loader directives are in order. The relocatable object le
contains machine instructions (each with its relocation bit) and should also contain
loader directives. Both machine instructions and loader directives are written as
binary numbers on the object le and should be explicitly distinguished. The assembler does this by adding one more bit to the relocation bit. With two such bits
(identifying bits), the assembler can identify a record on the object le as one of
four dierent types. So far we have seen three types, machine instructions (absolute
and relative), and loader directives. The fourth type is machine instructions that
require special relocation. They are discussed later in this chapter, in the section
on the ENTRY, EXTRN directives.
Throughout this book we will assume that the identication bits have the
following meaning:
00an absolute machine instruction.
01a relative machine instruction.
10a machine instruction that needs special relocation.
11a loader directive.
See chapter 7 about specic loader directives.
74 Directives
Ch. 3
Operation
END address
Operand
expression
Indicates to the assembler the end of the source program. Upon reading the
END from the source le, the assembler stops reading and completes assembling the
program. Certain assemblers allow the source le to have more than one program,
each having its own END, to indicate separate assembly. In such a case, the assembler
completes one assembly and then tries to read beyond the END to see if there is
another program on the same source le.
The expression in the operand eld indicates the address of the rst executable
instruction. It is thus possible to start the source program with some data items or
with a procedure, and follow them by the main program. In such a case the rst
source line is not the rst executable instruction. The assembler does not know
where execution should start and, if it should start at any point other than the
beginning, that point should be specied in the operand eld of the END.
Example:
label
mnemonic
SUB
LOAD
.
RET
DATA
STRT CLR
.
.
END
operand
comment
A,R1
1,2,3
R2
End of procedure
Some data items
First executable instruction
STRT
3. ICTL
Label
none
Operation
ICTL
Operand
b,e,c
Sec. 3.3
75
Operation
INCLUDE
Operand
lename
When the INCLUDE directive is encountered, the assembler opens the le specied in the INCLUDE and uses it as a source le. When that le is fully read, the
assembler returns to the original source le. The new source le may itself include
an INCLUDE directive, that will direct the assembler to start reading a third source
le.
Note. A similar loader directive is mentioned in chapter 7. It it used to explicitly
include an object le in the load.
5. PUNCH
Label
optional
Operation
PUNCH
Operand
string
The string in the operand eld is punched on a card. The PUNCH directive has
no other eect on the assembly.
6. REPRO
Label
none
Operation
REPRO
Operand
address
The next source line is punched on a card. There is no eect on the assembly.
Exercise 3.3 If it does not aect the assembly, what is this directives used for?
76 Directives
Ch. 3
Operation
MACHINE
Operand
processor code
This directive is used by assemblers that serve a family of computers, such as the
CDC 6000 series, 7000 series, & Cyber series computers. The dierent computers
in such a family are upper compatible, but not identical. Some of them support
instructions and hardware features that others do not. The processor code tells
the assembler on what machine the program is supposed to run and the assembler,
based on this information, can detect errors in the source code that stem from small
incompatibilities berween dierent machines in the same family.
8. p286
Label
none
Operation
p286
Operand
none
The 80x86 is a family of microprocessors that are dierent but are upper compatible. A program intended for the 80286 microprocessor must use this directive,
so that the assembler can generate instructions specic to that machine. There are
also p186 and similar directives.
9. PPU
Label
none
Operation
PPU
Operand
string
This directive is specic to the Cyber 70/76 or CDC 7600. Those machine have
one CP (Central Processor) and several PPUs (Peripheral Processing Units). The
CP and the PPUs have dierent architectures and, as a result, dierent assembler
languages. The same assembler, however, can handle programs in either language.
The directive declares the program as a PPU, rather than as a CP, program. The
string operand has to do with the way certain PPU instructions are assembled [30].
Sec. 3.4
77
Operation
LCC
Operand
loader directive
The loader directive in the operand eld is written on the object le. The
loader directive should be coded as a number. This directive allows the advanced
user to explicitly insert loader directives in the object le.
Exercise 3.4 What are examples of loader directives?
Operation
ABS
Operand
none
Directs the assembler to generate an absolute object le, rather than a relocatable one.
12. BASE
Label
none
Operation
BASE or RADIX
Operand
number base
BASE 8
.
.
DATA 56 the constant will be interpreted as 568
DATA 80 this would be agged as an error since 8 is invalid as an octal digit
Exercise 3.5 In what pass is BASE executed, and how?
78 Directives
Ch. 3
13. CODE
Label
none
Operation
CODE
Operand
char
Computers use dierent character codes, such as the ASCII and EBCDIC. If
the assembler supports more than one code, this directive tells it what code to use
for the current assembly. The operand can be a single letter code such as A (for
ASCII), E (for EBCDIC), D (for Display, the CDC 6-bit code) or anything else.
14. QUAL
Label
none
Operation
QUAL
Operand
qualier or blank
PROC1
PROC2
15. COL
Label
none
Operation
COL
Operand
column number
A source line with a comment but without an operand eld may present a
problem to the assembler. Consider the line ABC HLT 2 TIMES. On many com-
Sec. 3.6
79
puters, the HLT instruction may be written with or without an operand, and the
assembler has to determine whether the 2 is an operand or part of the comment.
Most assemblers require a special character, such as a semicolon, to precede a comment. Some old, punched card based assemblers, consider a certain column as the
beginning of comments. The COL directive can alter that column. Thus COL 40
means that anything on column 40 or after is a comment.
Operation
BEGIN
Operand
Op1,Op2
Where Op1 is a location counter name, and Op2 is a start value for that location
counter.
Some assemblers can use several location counters to assemble one program
(see example below). The dierent location counters are assigned names, can be
initialized by the programmer to any values, and can be used in any order. The
default location counter (the one used in the absence of any BEGIN) is called blank
and is initialized to zero. This directive causes the assembler to start using the
location counter named in Op1, and initializes that location counter to the value of
Op2. The directive may appear anywhere in the program and is used to separate
the program into parts that are written in a certain order and are eventually loaded
in memory in a dierent order.
If the rst line of the program is not a BEGIN, the assembler generates a
BEGIN ,0 as the rst line. Such a program starts by using the blank location
counter, which is initialized to zero (but see also the discussion of ORG).
17. COMM
Label
name
Operation
COMM
Operand
expr
This directive declares the name to be that of an array of size expr. The
array will later be located in blank common. This is handy for compatibility with
Fortran and other languages that use COMMON. The actual storage space is allocated
by the loader, and the directive is executed by the assembler by generating a loader
directive and placing it in the object le.
80 Directives
Ch. 3
18. DS
Label
optional
Operation
DS
Operand
an expression
DS 5
DS N
DS NN
To execute the DS, the assembler evaluates the expression in the operand eld
(the array size) and increments the LC by this amount. This guarantees that the
next instruction will be loaded in the word following the reserved area.
Example:
LC
15
15
16
19
20
25
N
B
C
EQU 5
CLR R3
DS 3
ADD R2,AB
DS N
N=5
...
The DS directives should therefore be concentrated at the beginning or, preferably, at the end of the program, following all executable instructions.
Exercise 3.6 What are some ways of mixing instructions and data?
Modern computers have a word size thats a multiple of 8, so their assemblers
have directives such as BYTE (to reserve bytes), WORD (to reserve words), LONG (to
reserve longwords). Older assemblers use names such as BSS (Block Starting with
Symbol), or BES (Block Ending with Symbol). Those are all similar to DS.
Exercise 3.7 The directive B BES 5 causes ve words of storage to be reserved,
and assigns B a value that is the address of the rst word following this array. Why
isnt B assigned the address of the last word of the array, and what is the use of
BES?
Sec. 3.7
81
Exercise 3.8 The directive DS 0 seems useless. Still, it has an important use on
some computers, what is it?
19. =
Label
*
Operation
=
Operand
address expr
Operation
EVEN
Operand
none
Operation
LIMIT
Operand
none
This is executed by generating a DS 2 directive at the beginning of the program. The loader then stores the upper and lower address boundaries of the program
in those two locations. This is done so that the program could determine its own
boundaries in memory at run time.
22. ODD
Label
none
Operation
ODD
Operand
none
Operation
ORG
Operand
expression
82 Directives
Ch. 3
The ORG directive instructs the assembler to continue the assembly from the
memory location specied by the operand. The operand must be an expression
that can be immediately evaluated, and its value must be a valid address (i.e., it
cannot be negative). Thus the operand can be a number, a known symbol, or an
expression that can be evaluated by the assembler at this point. Such an operand
is called denable.
Example:
AB ORG 100
Meanscontinue the assembly from location 100 and assign 100 as the value
of symbol AB.
To execute this directive, the assembler:
Evaluates the operand (pass 1).
Resets the LC to the value of the operand (pass 1).
Stores the label, if there is one, in the symbol table, with the LC as its value
(pass 1).
Prepares a loader directive on the object le (pass 2). This tells the loader where
to load subsequent instructions.
The LC points to the current address and, by resetting it, the programmer
instructs the assembler to load the instructions that follow into a dierent memory
area.
Example:
LC
Source
0000
LOAD
.
.
ADD
ORG 170
COMP
SUB
.
.
0150
0170
0171
The assembler starts at location 0. It resets the LC to 0 and the rst block of
instructions, from the LOAD to the ADD, will be assembled and loaded into memory
locations 0000 through 0150. The ORG 170 causes the COMP instruction to be
loaded into memory location 170, followed by SUB in location 171, and so on. The
area from location 151 to location 169 remains empty and can be used to store data.
There are two problems, however:
It is hard to refer to this area since it is not labeled (but see the description of
the EQU directive for ways to label any memory location).
Sec. 3.7
83
At run time, the computer will execute the ADD instruction and will then proceed
to location 0151, trying to execute its contents as an instruction. Since location
0151 does not contain an instruction, an error will occur.
The precise error depends on the contents of location 0151. If it contains a bit
pattern that happens to be the code of an instruction, the computer will execute it
and proceed to location 0152. If, however, location 0151 contains a bit pattern that
is not the code of any instruction, an interrupt will be generated by the hardware.
Most 8-bit microprocessors, unfortunately, simply skip such invalid bit patterns,
with no interrupts generated.
If the ORG above is changed to ORG 150, the assembler will reset the LC to
150, with the result that the COMP instruction will go into location 0150 (overwriting
the ADD), the SUB, into location 0151, and so on. The assembler executes the ORG
without checking its operand, and it is therefore easy to make mistakes.
Exercise 3.9 What is a common use of ORG?
Since the assembler does not load the program into memory, it can not directly
execute the ORG. It resets the LC (in pass 1) and writes a loader directive on the
object le (in pass 2). The actual execution is done by the loader when it nds the
loader directive in the object le.
24. OVERLAY
Label
none
Operation
OVERLAY
Operand
n
Declares the following code an overlay. The overlay ends at the next OVERLAY
directive or at the end of the program. Overlays are useful when a large program
has to t in a small memory, or in a multi-user computer where many application
programs compete for limited memory. The program can be logically divided into a
main part and a number of overlays. At run time, the main part resides in memory
and calls any overlay when needed. The overlays are loaded on top of each other
(they overlay each other). Overlays are discussed in detail in chapter 7.
25. POS
Label
none
Operation
POS
Operand
expression
In a computer with a large word size, several instructions can be packed in one
word. An assembler for such a computer uses, in addition to the LC, a position
counter, SC. The SC tells the assembler where, in the current word, to assemble
the next instruction. If the next instruction is longer than the rest of the word,
the assembler lls the current word with NOP, resets the SC to 0, and increments
the LC by 1. The next instruction thus goes into the next word. This process is
called a forcing up of the next instruction, and it is discussed in chapter 1. The
84 Directives
Ch. 3
POS directive resets the SC to the value of the expression in the operand eld. The
expression should not be greater than the word size.
26. USE
Label
none
Operation
USE
Operand
LC name or *
This directive tells the assembler to start using the LC named in the operand.
This can be an existing LC or a new one. An asterisk in the operand means returning
to the previously used LC. The special name // refers to the special LC used for
the blank common block.
Example:
LC
00
00
02
56
00
00
10
12
57
USE
A LOD ...
C STO ...
.
.
ADD ...
USE XY
D DS 10
E DC 1,2
.
USE *
a new LC is initialized to 0
symbol D has value of 0 relative to block XY
More information on the use of USE and on multiple LCs can be found in
chapter 1 and in references [26, 27].
* The asterisk is not a directive in the usual sense. It is not written on a separate
source line but is rather part of the operand of instructions. It stands for the LC
value, and is very useful. A typical simple example is:
BPL *+2
ADD ...
COM ...
the BPL instruction branches to the COM instruction. This is useful in short local
communication between instructions, but requires the programmer to know the size
of each instruction. The example above would work if the instructions involved are
1 word long each. If the ADD instruction is two words long, the BPL would branch
to the middle of the ADD.
Sec. 3.8
85
Operation
SEGMENT
Operand
parameters
Used to declare the start of a segment. The end of the segment is dened by
an ENDS directive. Example:
MINE
SEGMENT PUBLIC
DB 0
B
DW 0
MINE ENDS
All segments using the parameter PUBLIC and having the same name are concatenated into one module (see reference [35] for more information on possible parameters).
28. ASSUME
Label
none
Operation
ASSUME
Operand
seg reg:name
When a program uses segments, every address must have two parts, a segment
address and an oset within the segment. The segment address must be loaded
86 Directives
Ch. 3
by the program into one of the segment registers. The oset is included in the
individual instructions. Thus when an instruction is executed that uses an address,
the hardware fetches the oset from the instruction, adds it to the contents of one
of the segment registers, and ends up with a complete address. This process is
called address mapping. The user is responsible for loading the start address of
each segment into a segment register. The user then needs to tell the assembler
what each of the segment registers contains. This is done by the ASSUME directive.
Thus ASSUME DS:MINE tells the assembler that segment register DS contains the
start address of segment MINE. The ASSUME directive does not load the address into
the registerthat must be done by an instruction at run timeit only tells the
assembler to assume that the register has been loaded.
29. GROUP
Label
optional
Operation
GROUP
Operand
list of segment names
This directs the assembler to group several segments into one contiguous module.
The assembler generates a loader directive, and the actual grouping is done by
the loader.
Operation
EQU or =
Operand
expression
Where the label is mandatory and the operand is an expression that cannot
include any future symbols. The directive is executed by assigning the operand as
the value of the symbol. The symbol is stored in the symbol table with its value
and with the proper type. This is the rst example of a symbol whose value is not
the LC. The EQU directive makes it possible to dene symbols with any values and
any type, absolute or relative.
Example:
LC
0000
AB
CD
EF
GH
IJ
EQU
EQU
ADD
EQU
EQU
0
5
1,2
AB+1
EF+1
Sec. 3.9
87
value type
AB
CD
EF
GH
IJ
0
5
0
1
1
ABS
ABS
REL
ABS
REL
The symbol types are worth noting in this example. GH is absolute since it is
dened by absolute quantities only. IJ is relative since its denition contains at least
one relative quantity. See also the discussion of address expressions in chapter 1.
The dierence between GH and IJ is in the way they are used. GH is identical
to the number 1 and the two can be used interchangeably. Thus:
ADD
ADD
R1,R2
RGH,R2
are identical instructions that add registers 1 and 2. They are assembled into
identical machine instructions and are treated by the loader identically. Symbol IJ,
however, is relative and thus stands for address 1. As a result, ADD RIJ,R2 is
wrong and would produce an assembler error message, since the assembler expects
a register number (an absolute number) at this point, but
LOAD
LOAD
R3,1
R3,IJ
is okay since the second operand of LOAD should be an address. Note, however,
that the two instructions are not identical. They go on the object le with dierent
identication bits.
Chapter 1 explains how the type of a symbol is used by the assembler to
generate relocation bits.
The main use of EQU is in assigning meaningful names to registers and locations
that are frequently used in the program. In the Apple II computer, for example,
addresses C000 and C010 have a special meaning. They are the data register and
the strobe (see reference [40] p. 79) of the keyboard. The directives:
KYBD
STRB
EQU $C000
EQU $C010
make it easy to input from the keyboard since the programmer has to memorize
the symbols KYBD ,STRB instead of the addresses C000, C010. Notice that the $ is
used above to specify a hexadecimal constant.
Exercise 3.10 A 2-pass assembler can handle future symbols and an instruction
can therefore use a future symbol as an operand. This is not always true for directives. The EQU directive, for example, cannot use a future symbol. The directive
A EQU B+1 is easy to execute if B is previously dened, but impossible if B is a
future symbol. Whats the reason for this?
88 Directives
Ch. 3
Exercise 3.11 Suggest a way for the assembler to eliminate this limitation such
that any source line could use future symbols.
31. MAX and MIN
Label
symbol
Operation
MAX or MIN
Operand
list of expressions
The label is mandatory and is assigned the value of the largest (smallest)
expression in the operand eld.
Example:
AB
CD
GH
NOW
EQU
EQU
EQU
MAX
0
5
LQ+AB
where LQ is a dened symbol
AB,CD,GH
Symbol NOW is assigned the maximum of the three operands. These directives are
similar to EQU and are used in those rare cases where the largest (or smallest) of a
group of symbols is needed.
Exercise 3.12 How is MAX executed and in what pass?
32. MICCNT
Label
symbol
Operation
MICCNT
Operand
micro name
The symbol is set to the number of characters in the value of the micro (micros
are discussed in a later section). What makes this directive interesting is that the
symbol can be redened. Thus:
FST
AB
SC
AB
MICRO 1,,*STRING*
.
.
MICCNT FST
.
.
MICRO 1,,*ALPHA*
.
.
MICCNT SC
AB is set to 6
AB is reset to 5
Sec. 3.9
89
33. SET
Label
symbol
Operation
SET
Operand
expression
The SET directive is similar to the EQU directive except that it allows a redenition of the symbol. Thus the usage:
.
.
G SET 1
.
.
G SET 2
.
.
point 1
point 2
is valid. Between points 1 and 2, the value of symbol G will be 1. From point 2 on,
the value of G will be 2. The SET directive is another example where a symbol can
be redened. The execution of the SET directive and the reason for having such a
directive are discussed in chapter 4. Certain assemblers (see Ch. 8) use = instead
of SET.
Operation
USING
Operand
name
90 Directives
Ch. 3
35. DROP
Label
none
Operation
DROP
Operand
name
TITLE A
.
.
JSR Q
a Jump SubRoutine instruction
.
.
END
TITLE B
ADD ..
.
.
LOD ..
.
.
END
Sec. 3.11
91
Operation
CSECT
Operand
a symbol
This directive is used on the IBM 360 computers to divide a program into parts,
to be assembled separately. Each part starts with a CSECT and ends with the next
CSECT of with the END diective. The PSECT directive on the VAX is similar. It is
described in Ch. 1.
37. EXTRN
Label
none
Operation
EXTRN
Operand
list of symbols
The symbols in the operand eld are declared as external symbols, symbols
that are used in the current program but are declared elsewhere. In the example
above, the declaration:
EXTRN Q or EXTRN P,Q
should be added to program A, preferably at the beginning of the program. The
assembler still does not have a value for Q but, because of the EXTRN declaration, it
treats the JSR instruction in a special way and does not consider it an error. The
programs are separately assembled and, therefore, when the assembler assembles A,
it knows nothing about program B (it does not have a value for symbol Q). When
the assembler gets to program B, it has already forgot everything about A (it has
erased the symbol table). Thus, in principle, the assembler cannot assemble the
JSR instruction. Only the loader, while loading all the programs as one executable
module, can use values of special symbols from one program in another. Thus our
JSR instruction presents a special case, a case that cannot be fully handled by the
assembler, and requires the help of the loader. It is interesting to note here that a
one pass assembler, since it does not use a separate loader, cannot handle separate
source les and thus cannot support the EXTRN, ENTRY directives.
The assembler writes the JSR instruction on the object le without the value of
Q, and with special identication bits (id ) indicating that the instruction is incomplete, and that it is missing the value of symbol Q. The loader eventually reads the
92 Directives
Ch. 3
object le, reads the JSR instruction, recognizes the id bits, and completes the instruction when it nds the value of symbol Q. This process is explained in chapter 7,
and in this section we will concentrate on the execution of the EXTRN directive.
The EXTRN species that Q is a special symbol, a symbol dened outside the
current program. The assembler stores Q in the symbol table with a special type
ext, and without a value. The instruction itself is assembled, and is written on the
object le, without the value of Q in its address eld. The assembler stores a pointer
in the empty address eld, to point to the symbol table entry for Q. At the end of
pass 2, the symbol table is erased, but the special entries[ in the symbol table] are
copied by the assembler onto the object le, for later use by the loader. The object
le thus contains the object code followed by the special symbol table. It will later
be shown that there are other items in the object le. The following diagram shows
the symbol table entry (entry 5) for symbol Q.
1.
2.
.
.
5.
6.
name
value type
..
..
..
Q
..
..
ext
..
The JSR instruction goes on the object le as the record OpCode, 5, id=10.
The id indicates (to the loader) that the 5 in the address eld is not the value of a
symbol but a pointer to the special-symbol table at the end of the object le. That
table includes the entry 5 Q ext among, perhaps, other ones.
After recognizing the id and nding the entry, the loader knows that the JSR
instruction needs the value of symbol Q. This value is dened in program B and
should, therefore, be included in the object le generated by that program.
The id is a generalization of the concept of a relocation bit, and is discussed in
this chapter, in conjunction with the IDENT directive.
The EXTRN directive is executed in both passes. In pass 1 the symbol is entered
into the symbol table as a special symbol of type ext. In pass 2, the object
instruction is generated and the id bits added to it. Also, the special symbol table
is written onto the object le.
Exercise 3.13 What if an external symbol is used in an address expression, such
as JSR Q+1 or DC Q+1?
It should be noted that Macro, the VAX assembler, treats undened symbols
as external. When an undened symbol appears in a VAX assembler program, the
assembler does not issue an error. It treats the symbol as external, and the error is
eventually issued by the linker (when it does not nd a corresponding entry point
in any of the object les loaded). This feature is not always desirable, and can be
disabled by the DISABLE GLOBAL directive.
Sec. 3.11
93
38. ENTRY
Label
none
Operation
ENTRY
Operand
list of symbols
The symbols in the operand eld are dened as entry points to the current
program. An entry point is a point in the program where it can be entered by a call
from another program. In program B above, Q (and possibly P) are entry points,
and the directive:
ENTRY Q or ENTRY P,Q
should appear at the beginning of B. The assembler executes the directive by storing
the symbols in the symbol table with a type of ent. An example follows:
1.
2.
3.
4.
5.
6.
7.
name
value type
..
..
P
..
..
Q
..
..
..
12
..
..
23
rel
abs
ent
rel
rel
ent
At the end of pass 2, the symbol table is erased, but the special entries, those with
a type of ext or ent, go on the object le. When the loader reads the object le for
B, it nds the entry for Q and uses the value of Q to complete the JSR instruction
from program A.
This directive is executed partly in pass 1 (setting the type in the symbol table)
and partly in pass 2 (writing the special symbol table on the object le).
Operation
ASCII
Operand
char string
The ASCII codes of all the characters in the string are generated and stored in
consecutive bytes in memory. If there is a label, its value is the address of the rst
byte. Example:
ASCII "HELLO THERE"
generates (in octal): 150 145 154 154 157 040 164 150 145 162 145 in 11
consecutive bytes.
94 Directives
Ch. 3
40. ASCIIZ
Label
optional
Operation
ASCIIZ
Operand
char string
Same as the ASCII directive above except that the string of bytes is followed
by a zero byte. This feature is for generating strings that C programs can use.
41. BSSZ
Label
symbol
Operation
BSSZ
Operand
expression
Similar to BSS except the block being reserved is initialized to all zeros.
Exercise 3.14 Why is BSSZ described here and not next to BSS?
42. CON
Label
symbol
Operation
CON
Operand
expr1,expr2,. . .
The expressions are evaluated and stored in consecutive words, one value per
word. This directive is similar to DATA (see below) except that expressions, rather
than constants, can be used, and each value is stored in one word.
43. DATA (or DC)
Label
Optional
Operation
DATA or DC
Operand
list of integer, real, or character constants, or symbols
The DATA or DC (Dene Code) directive preloads the constants in the operand
eld in successive memory locations. The constants can be of dierent types
(some assemblers support a large number of constant types, both numeric and
non-numeric) and can have dierent lengths.
Example: AB DC 1,-5.3,DOD$,XY The operand eld contains four constants
(notice the three separating commas) that are of dierent types. The rst is the
integer 1, the second, a real number, the third, a string of length four, and the
fourth, the value of symbol XY. The assembler generates the binary values of all
items and stores them in consecutive locations, updating the LC in the process.
The assembler executes the DC in both passes. In pass 1 it eveluates the size of each
constant and updates the LC. In pass 2 it generates the binary constants and writes
them on the object le. Many assemblers permit expressions in a DC directive, thus
DC A-1,B-C,. . . is valid but all the symbols involved should be known at the time
the DC is encountered in pass 1. Such a DC may include an external symbol and,
Sec. 3.12
95
in such a case, the assembler cannot evaluate the expression and has to generate a
modify loader directive.
If the programmer knows the sizes of the data items, they can refer to any of
the items by calculating the sizes of the preceding ones. Assuming that an integer
number occupies one word, a real, two words, and each word can contain two
characters, the total size of the rst three items in the example above is ve and the
following is true: The instruction LOD AB+1,R1 will load the real constant 5.3,
whereas STO R1,AB+4 will store register 1 in the word containing the characters
D$ (thereby erasing those characters). The instruction CMP AB+5,R2 will compare
the value of symbol XY (stored in location AB+5) with the contents of register 2.
Some assemblers use DATA instead of DC while others support separate directives for dierent data types. OCT, DEC, BCD are a few such directives, supported
mostly by old assemblers, that preload octal, decimal, and strings in storage. The
examples below are from the ASM 86 assembler (references [3335])a modern,
powerful assembler for Intel 16-bit microprocessorsbut are typical to data directives supported by many modern assemblers.
DB (Dene Byte), DW (Dene Word), DD (Dene Double), DQ (Dene Quade,
a quade is 8 consecutive bytes or 64 bits), DT (Dene Ten), ten consecutive bytes).
These directives are powerful, as the following examples illustrate:
DB
DW
DW
DB
DQ
DD
DB
12DUP(0)
?,?,?
3DUP(?)
2DUP(1,3DUP(2))
1.234E
-9.8E-34
HELLO,0DH,0AH
44. DIS
Label
symbol
Operation
DIS
Operand
n,string
A total of n characters from the string are preloaded into successive words in
memory (as many words as necessary to hold n characters). If n is larger than
the length of the string, the string is repeatedly loaded into memory until all n
characters have been loaded.
45. DEF
Label
symbol
Operation
DEF
Operand
m[,I]
Where m is an address (absolute or relative) and the optional I indicates indirect addressing. This directive is used in the HP1000 & 2100 minicomputers [79]
96 Directives
Ch. 3
Operation
DEC
Operand
d1[,d2,...,dn]
Operation
LIT
Operand
list of expressions
The expressions are evaluated and their values inserted into the literal table.
This directive is normally not necessary since literals are added to the literal table
when they are found in the source. However, if LIT is used early in the program to
load the literal table, the assembler will not load it with duplicate values. Some experienced programmers like to know what their literal table contains. They initially
preload it with all the literals they think their program is using. At the end of the
second pass, they print the literal table and, if it contains anything not inserted by
them (through the LIT), they check for a possible error.
48. LITORG
Label
none
Operation
LITORG
Operand
expression
Sec. 3.12
97
by the rst pass and is read by the second pass. It contains a copy of the source
le plus information on literals. The literal table contains the name, value, size and
other attributes of each literal used in the program.
Example:
name
value size
1000
13.6
-1
DATUM
13.60
4
8
4
5
8
Where the value eld contains the binary values of the literals. In the rst pass,
each time a literal is found in the source program, the literal table is checked for
a literal with the same name. If no such literal exists, the new literal is added
to the table, otherwise, the existing literal is used. The relative address of the
literal is then written on the intermediate le following the instruction that uses
the literal. Notice that literals with dierent names and identical values (like 13.6,
13.60) appear as dierent entries in the literal table. When a LITORG is encountered
(or, in the absence of a LITORG, at the end of the rst pass), the assembler copies
the value elds from the literal table to memory (to the memory address specied
in the LITORG or, with no LITORG, the address following the end of the program)
and erases the table. Thus, several literal tables may be generated during the rst
pass. In the second pass the intermediate le is read and, when an instruction is
encountered that uses a literal, the assembler nds the relative address of the literal
in the le, following the instruction. It uses this address to calculate the absolute
address of the literal, and assembles the instruction with that absolute address.
Literalsliterals are treated in some detail in chapter 1.
49. PACKED
Label
symbol
Operation
PACKED
Operand
list of expressions
The expressions are calculated and their values are stored in successive memory
locations in packed decimal form.
50. RECORD
Label
symbol
Operation
RECORD
Operand
eld1:size1,eld2:size2,. . .
ID RECORD YR:3,CODE:4,GEN:1
98 Directives
Ch. 3
Symbol ID is now the name of a record (just a template, not any actual area in
memory) containing three elds: YR, a 3-bit value, CODE, a number between 0 and
15, and GEN, a single bit. The total length of the record is thus 8 bits. Each size
eld is the size (in bits) of a eld in the record. To actually allocate memory to
such a record, other directives are used. For example: ACT ID 6DUP(?) ACT is 6
consecutive bytes, each a record of type ID, uninitialized. SAM ID 12DUP(5,0,1)
SAM is 12 consecutive bytes, each a record of type ID. The individual elds are
initialized to YR=5, CODE=0, GEN=1.
Records allow for easy manipulation of small elds and individual bits while
packing them tightly in either bytes or words. The following example species
default values for the individual elds.
VOL RECORD X:8=A,Y:8=B
The following source lines illustrate actual storage allocation.
BAT VOL <,Q> BAT is a record of type VOL with eld Y initialized to Q and
eld X having its default value A.
CAT VOL<> CAT is a record of type VOL (2 bytes) where the X, Y elds have the
default values.
To access a record, an instruction such as below can be used.
MOV AX,OFFSET CAT. This is an 8086 MOV instruction [37,38] whose general form
is
MOV destination,source
The MOV instruction above moves OFFSET CAT (the address of record CAT) to
the 16-bit register AX. The instruction
MOV AH,OFFSET SAM[3] moves the address of the third component of the array
SAM of records to the 8-bit register AH.
For more information on records see [34,35,37].
51. STRUC
Label
symbol
Operation
STRUC
Operand
none
STRUC
DB 3,5
DB 0
DW 0
DB 10DUP(?)
ENDS
Sec. 3.12
99
This denes a structure called DIM with 4 elds, the rst one is named LEN and is
2 bytes long. The last one has the name MISC and is ten bytes long. To actually
build such a structure in memory, source lines such as:
DAN
BAN
DIM
DIM 5DUP(?)
can be used. The rst declares DAN as a structure of type DIM, the second one
declares BAN as an array of 5 such structures. To access a eld in a structure, the
name of the structure should be followed by a .field name.
MOV DAN.LEN,AL moves the 8-bit register AL to eld LEN of structure DAN.
52. VFD
Label
symbol
Operation
VFD
Operand
l1/expr1,l2/expr2,. . .
Operation
DECMIC
Operand
expr,n
Like OCTMIC below except that the rightmost n decimal digits are used.
100 Directives
Ch. 3
54. MICRO
Label
name
Operation
MICRO
Operand
n1,n2,dstring
dstring is a delimited string (it is preceded and followed by the same character). A substring of n2 characters, starting at position n1 is extracted and is
assigned the name in the label eld. The name then becomes a micro name. If n2
is zero, the substring starts at position n1 and goes to the end of the original string.
Examples:
AN MICRO 1,,/STRING/ AN is the name of string STRING
BC MICRO 2,3,*NONESUCH* BC is the name of ONE
55. OCTMIC
Label
name
Operation
OCTMIC
Operand
expr,n
The expression (which must be numeric) is evaluated by the assembler and the
rightmost n octal digits extracted. They become the string assigned as the value of
name.
Example: Suppose B is a symbol (EQU or SET) with absolute value 1024. Then
J OCTMIC B,6 will assign the string 002000 to micro name J (since 20008=1024).
3.15.1 micro substitution
It is done by placing a micro name between the two micro delimiters (=) at
any point in any source line (except a comment line). The string is then substituted
for the micro name. Examples:
COMP =BC=,R1 is assembled as COMP ONE,R1
ADD #=J=,R2 is assembled as ADD #002000,R2.
Operation
ERR
Operand
none
This directive generates an assembler error of the type indicated by the ag.
This can be useful in certain conditional assemblies. The example below assumes
knowledge of macros and conditional assembly.
Sec. 3.16
SCD
A
MACRO P1,P2
IIF P1=0
ERR
.
.
ENDM
.
.
SCD 0,GG
101
57. ERRxx
Label
ag
Operation
ERRxx
Operand
expression
Will generate an assembler error of type flag when a condition detected during
pass 2 is true.
Example:
.
.
H DS 0
R ERRPL H-XFF00
END
Operation
EJECT
Operand
none
It causes the next line of the listing to appear at the top of the next page
following the page heading. Some assemblers use PAGE instead of EJECT.
102 Directives
Ch. 3
59. LIST
Label
none
Operation
LIST
Operand
ON or OFF
Used to control the listing of the program. LIST ON instructs the assembler
to list subsequent lines. LIST OFF instructs it to suppress listing. The LIST directive may appear anywhere in the program and is eective until the next LIST is
encountered or until the end of the program, whichever occurs rst. Some assemblers support two directives LIST, NOLIST instead of a LIST ON/OFF. It should be
emphasized that the LIST directive aects the listing le only. It does not aect
the assembly of the program. The entire program is assembled but the programmer
may decide to list only certain parts of it.
60. PDC
Label
none
Operation
PDC
Operand
none
Operation
SBTTL
Operand
a string
Species a one-line subheading for the listing le. The assembler prints the
subheading as the second line of each listing page. The rst line is the heading (see
TITLE below for other features of the subheading).
62. SPACE
Label
none
Operation
SPACE
Operand
n
Inserts n blank lines in the listing. When the number of blank lines exceeds
the number of lines left on the page, a page is ejected, a new heading is printed,
and the rest of the blank lines are allocated on the new page.
63. TITLE
Label
none
Operation
TITLE
Operand
a string
Species a one line heading for the listing le. The assembler prints the heading
at the beginning of every page. The TITLE directive can appear anywhere in the
Sec. 3.17
103
program, and each TITLE supersedes its predecessor. Each TITLE except the rst
one also causes a page eject. This directive is similar to SBTTL above and both have
the same features.
64. XREF
Label
none
Operation
XREF
Operand
string
Controls the printing of the cross-reference table. This table lists all the symbols dened in the program and for each symbol, the numbers of all source lines
referring to it. The operand is a string containing characters that specify whether
to print the table and how to list references to a symbol, by line number within the
source le, or by page number and line within the page.
65. %OUT
Label
none
Operation
%OUT
Operand
string
It is useful for displaying progress through a long assembly. When the %OUT is
encountered, the operand (a string) is displayed on the standard output device.
Operation
HERE
Operand
none
A remote code that was saved before is fetched and assembled. If the label eld
has a name, only remote code with that name is assembled. If there is no name, all
unlabeled remote code existing at this point is fetched and assembled.
67. RMT
Label
name
Operation
RMT
Operand
none
The name is optional. A pair of RMT directives with the same name (or both
without names) delimit a section of code that is saved, to be later assembled by a
HERE directive.
104 Directives
Ch. 3
Operation
DUP
Operand
rep,lcnt
rep species how many times to duplicate and assemble the sequence. lcnt
species the number of source lines in the sequence. The sequence starts on the line
following the DUP and is lcnt lines long. The lcnt parameter is optional and if it
is missing, the sequence goes up to the nearest ENDD. The name is also optional and
serves to identify the sequence duplicated. DUP sequences may be nested, in which
case the names are important and dene the inner and outer DUP ranges.
Example:
OUT
DUP 3
ADD
INN DUP 2
SUB
INN ENDD
LOD
OUT ENDD
duplicate 3 times
an inner DUP duplicated twice
Operation
ECHO
Operand
lcnt,p1=list1,p2=list2,. . .
The name and lcnt elds are as before. p1, p2,. . . are parameters that appear in the sequence to be duplicated. Each time the sequence is duplicated, each
occurence of p1 in the body of the sequence is replaced with something from list1,
each occurence of p2 is replaced with something from list2, etc.
Each listi should have the form (a1,a2,...,an) where a1 is substituted for
pi on the rst duplication, a2 is substituted for pi in the second duplication, and
so on.
Sec. 3.19
105
Operation
ENDD
Operand
none
Operation
STOPDUP
Operand
none
Operation
OPDEF
Operand
parameters
OPDEF P1,P2
ADD #2,P1
ADD P1,P2
ENDM
106 Directives
Ch. 3
Operation
PURGDEF
Operand
none
The instruction named in the label eld is removed. It can only be something
originally dened with an OPDEF.
Operation
OPSYN
Operand
mnemonic
This makes the mnemonic in the label eld synonymous with the mnemonic in
the operand eld. However, if the operand eld is blank, this directive deletes the
instruction from the OpCode table (actually, it is deactivated in the table).
Example:
Declares the new mnemonic STORE to be the same as the existing mnemonic
STA. The programmer may use this directive to change the names of mnemonics
to more familiar ones, or to ones easier to remember. Notice that STA can still be
used. This directive applies to mnemonics of instructions and of directives. Thus
EQUATE OPSYN EQU declares EQUATE as another way of saying EQU.
This directive is rarely supported by assemblers since it is hard to implement. It
requires the OpCode table to be dynamic, so that the assembler can insert the new
mnemonics. The OpCode table is usually static, which allows for a fast search. In a
dynamic OpCode table the search is slower and, since OpCode table search is done
very often, a dynamic OpCode table should be very carefully designed. The main
design guideline is: If the OPSYN directive has not been used (no new mnemonics
added to the OpCode table), the search time in the dynamic table should be the
same as that of a static table. Only the actual insertion of new mnemonics should
degrade the search time. Such an OpCode table can be designed in two ways:
1. As a hash table (chapter 2). A hash table can be designed such that originally
any mnemonic can be found in a one step search. As mnemonics are added,
the search time degrades.
Sec. 3.21
107
3.22 Summary
Directives illustrate the hidden power of the assembler. It should now be clear
to the reader that the main task of assembling instructions is simple, and consumes
only a fraction of the power of the assembler. The many directives presented here
range from very simple to very complex (the most complicated ones are covered in
the next chapter), they provide the user with many services, and constitute more
than half the volume of a typical two-pass assembler. One-pass assemblers, designed
for speed, support just a few directives and are, therefore, much simpler.
108 Directives
Ch. 3
4. Macros
Webster [42] denes the word macro (derived from the greek o) as meaning long, great, excessive or large. The word is used as a prex in many compound
technical terms, e.g., Macroeconomics, Macrograph, Macronesia. We will see that
a single macro directive can result in many source lines being generated, which justies the use of the word macro in assemblers. As mentioned in the introduction,
macros were introduced into assemblers very early in the history of computing, in
the 1950s.
4.1 Introduction
Strictly speaking, macros are directives but, since they are so commonly used
(and also not easy for the assembler to execute), most assemblers consider them features, rather then directives. The concept of a macro is not limited to assemblers, It
is useful in many applications and has been used in many software systems. References [1922] describe the use and implementation of macros in general. Knuth[36]
describes the use of macros in a large software system. Dellert [92] is an interesting
example of the use of macros in reprogramming (translating or rewriting a program
from computer A to computer B).
A macro is similar to a subroutine (or a procedure), but there are important
dierences between them. A subroutine is a section of the program that is written
once, and can be used many times by simply calling it from any point in the program.
Similarly, a macro is a section of code that the programmer writes (denes) once,
110 Macros
Ch. 4
and then can use many times. The main dierence between a subroutine and a
macro is that the former is stored in memory once (just one copy), whereas the
latter is duplicated as many times as necessary.
A subroutine call is specied by an instruction that is executed by the hardware.
The hardware saves the return address and causes a branch to the start of the
subroutine. Macros, however, are handled in a completely dierent way. A macro
call is specied by a directive that is executed by the assembler. The assembler
generates a copy of the macro and places it in the program, in place of the directive.
This process is called macro expansion, and each expansion causes another copy of
the macro to be generated and placed in the program, thus increasing the size of
the object code.
As a result, subroutines are completely handled by the hardware, at run time;
macros are completely handled by the assembler, at assembly time. The assembler
knows nothing about subroutines; the hardware knows nothing about macros. A
subroutine call instruction is assembled in the usual way and is treated by the
assembler as any other instruction. Macro denition and macro expansion, however,
are executed by the assembler, so it has to know all the features, options and
exceptions associated with them. The hardware, on the other hand, executes the
subroutine call instruction, so it has to know how to save the return address and
how to branch to the subroutine. The hardware, however, gets to execute the object
code after it has been assembled, with the macros expanded in place. By looking
at an instruction in the object code, it is impossible to tell whether it came from
the main program or from an expanded macro. The hardware thus executes the
program in total ignorance of macros.
Figure 41 illustrates the dierences between subroutines and macros:
When program A in gure 41a is executed, execution starts at label K and each
CALL N instruction causes the subroutine (section 1) to be executed. The order of
execution will thus be 2,1,3,1,4. Even though section 1 is the rst in the program,
it is not the rst to be executed since it constitutes the denition of a subroutine.
When the program is assembled, the assembler reads and assembles the source le
straight through. It does not change the positions of the dierent sections, and
does not treat section 1 (the subroutine) in any special way. The order of execution
has therefore to do only with the way the CALL instruction works. This is why
subroutines are a hardware feature.
Program B (Fig. 41b) is handled in a dierent way. The assembler reads the
MACRO, ENDM directives and thus recognizes the two instructions DIV, OUT as the
body of macro N. It then places a copy of that body wherever it nds a source line
with N in the operation eld. The output is the same program, with the macro
denition removed, and with all the expansions in place. This output (Fig. 41c) is
ready to be assembled, in the usual way, by passes 1 and 2. This is why macros are
an assembler feature and handling them is done by a special pass, pass 0, where a
new source le is generated (Fig. 41d) to be read by pass 1 as its source le.
Having a separate pass 0 simplies the design of the assembler, since it divides
the entire assembly job in a logical way between the three passes. The user, of
Sec. 4.1
K
N
K
K
Introduction
CALL
LOD
..
.
RET
CALL
IN P
..
.
SU B
CALL N
CALL N
K CALL N
ADD
3
..
BP L
K CALL N
CALL N
K CALL N
M U LT
4
..
HLT
K CALL N
EN D K
K
N MACRO
DIV
OUT
ENDM
K INP
.
.
.
SUB
N
ADD
.
.
.
BPL
N
MULT
.
.
.
111
Source le
K INP
.
.
.
Pass 0
macro denitions
& expansions
SUB
DIV
OUT
ADD
.
.
.
New source le
BPL
DIV
OUT
MULT
.
.
.
HLT
END
HLT
END
course, has to pay a price in the form of increased assembly time, but this is a
reasonable price to pay for the added power of the assembler. It is possible to
combine passes 0 and 1 into one pass, which speeds up the assembler. However,
this results in a very complex pass 1, which takes more time to write and debug,
and reduces assembler reliability.
The task of pass 0 is thus to read the source le, handle all macro denitions
and expansions, and generate a new source le that is identical to the original
le, except that it does not have the macro denitions, and it has all the macro
expansions in place. In principle, the new source le should have no mention of
macros; in practice, it needs to have some macro information which eventually is
transferred to pass 2, to be written on the listing le. This point is further discussed
below.
112 Macros
Ch. 4
MACRO
LOD A
ADD B
STO C
ENDM
Those two directives always come in pairs. The MACRO directive denes the start of
the macro denition, and should have the macro name in the label eld. The ENDM
directive species the end of the denition.
Some assemblers use dierent syntax to dene a macro. The IBM 360 assembler
uses the following syntax:
&p1
MACRO
name
.
.
MEND
&p2,&p3,. . .
instead of ENDM
where &p1, &p2 are parameters (explained later), each starting with an ampersand
&.
To expand a macro, the name of the macro is placed in the operation eld, and
no special directives are necessary.
.
.
COMP ..
NU
SUB ..
.
.
The assembler recognizes NU as the name of a macro, and expands the macro by
placing a copy of the macro denition between the COMP and SUB instructions. The
object code generated will contain the codes of the ve instructions:
COMP ..
LOD A
ADD B
STO C
SUB ..
Handling macros involves two separate phases. Handling the denition and
handling the expansions. A macro can only be dened once (see the discussion of
Sec. 4.1
Introduction
113
nested macros later for exceptions, however), but it can be expanded many times.
Handling the denition is a relatively simple process. The assembler reads the
denition from the source le and saves it in a special table, the Macro Denition
Table (MDT). The assembler does not try to check the denition for errors, to
assemble it, execute it, or do anything else with it. It just saves the denition as it
is (again, there is an exception, mentioned below, that has to do with identifying
parameters). On encountering the MACRO directive, the assembler switches from the
normal mode of operation to a special macro-denition mode in which it:
locates available space in the MDT
reads source lines and saves them in the MDT until an ENDM is read.
Upon reading ENDM from the source le, the assembler switches back to the
normal mode. If the ENDM is missing, the assembler stays in the macro denition
mode and saves source lines in the MDT until an obvious error is found, such as
another MACRO, or the END of the entire program. In such a case, the assembler
issues an error (run away denition) and aborts the assembly.
Handling a macro expansion starts when the assembler reads a source line that
is not any instruction or directive. The assembler searches the MDT for a macro
with that name and, on locating it, switches from the normal mode of operation to
a special macro-expansion mode in which it:
Reads a source line from the MDT.
Writes it on the new source le, unless it is a pass 0 directive, in which case it is
immediately executed.
Repeats the two steps until the end of the macro is located in the MDT.
The following example illustrates this process. The macro denition contains
an error and a label.
BAD MACRO
ADD #1,R4
A$D R5
wrong mnemonic
LAN CMP R3,R5
ENDM
The denition is stored in the MDT with the error (A$D) and the label. Since the
assembler copies the macro denition verbatim, it does not recognize LAN as a label
at this point. The macro may later be expanded several times, causing several
copies to be written onto the new source le. Pass 0 does not check these copies in
any way and, as a result, does not issue any error messages (note that pass 0 does
not handle labels and does not maintain the symbol table). When pass 1 reads the
new source le, it discovers the multiple denitions of LAN and issues an error on
the second and subsequent denitions. When pass 2 assembles the instructions, it
discovers the bad A$D instructions and ags each of them.
114 Macros
Ch. 4
Exercise 4.1 In such a case, how can we ever dene a macro with a label?
This does not sound like a good way to implement macros. It would seem
better to assemble the macro when it is rst encountered, i.e., when its denition is
found, and to store the assembled version in the MDT. The reason why assemblers
do not do that but rather treat macros as described above, is because of the use of
parameters.
Sec. 4.2
Macro Parameters
115
X. The process of placing the actual arguments in place of the formal parameters
when expanding a macro, is called parameter substitution.
Exercise 4.2 Consider the case of an actual argument that happens to be identical
to a formal parameter. If the macro of example 2 above is expanded as MG2 B,X,Y,
we would end up with the expansion
LOD B
ADD X
STO Y
However, B is the name of the second parameter. Would the assembler perform
double substitution, to end up with LOD X?
Example 3 is even more striking. Here the parameters are used in the operation
eld. The operands are always the same. When such a macro is expanded, the
user should specify three arguments which are valid mnemonics. The expansion
MG3 LOD,SUB,STO would generate:
LOD G
SUB H
STO I
whereas the expansion MG3 LOD,CMP,JNE would generate
LOD G
CMP H
JNE I
which is a very dierent macro. It is obvious now that such a macro cannot be
assembled when it is dened.
In example 4 the parameter is in the label eld. Each expansion of this macro
will have to specify an argument for the parameter, and that argument will become
a new label. Thus MG4 NON generates
NON
LOD G
ADD H
STO I
BON
LOD G
ADD H
STO I
Each expansion involves the creation of a new label that will be added to the
symbol table in pass 1. To avoid multiply-dened labels, each expansion should use
a dierent argument. The argument could also be null (see below), which would
generate no label. It is important to realize, however, that the label becomes known
only when the macro is expanded and not before. Macro expansions, therefore, must
be done early in the assembly process. They cannot be done in the second pass
because the symbol table must be stable during that pass. All macro expansions
are done in pass 0 (except in assemblers that combine passes 0 and 1).
116 Macros
Ch. 4
MACRO L1,L2,L3,L4,L5,L6
ADD L1,L2(2)
L2 is assumed compound and its 2nd component used
L3
BL4 DEST
CL5D L6
.
.
ENDM
Sec. 4.2
Macro Parameters
117
which illustrates the following points about handling arguments in a macro expansion:
1. There are two spaces between the ADD and the SUM on the rst line. This
is because the macro denition has two spaces between the ADD and the L1.
In the second line, though, there is only one space between the SUB and the
R1. This is because the argument in the expansion has one space in it. The
assembler expands a macro by copying the arguments, as strings, into the
original macro line without any editing (except that when the argument is
compound, its parentheses are stripped o). In the third line, the parameter
occupies three positions (L4) but the argument Z only takes one position.
When Z is substituted for L4, the source line becomes two positions shorter,
which means that the rest of the line is moved two positions to the left. The
assembler also preserves the original space between L4 and DEST. As a result,
the expanded line has one space between BZ and DEST.
2. The second line of the denition simply reads L3. The assembler replaces L3
by the corresponding argument SUB R1,L1 and, before trying to assemble
it, scans the line again, looking for more occurrences of parameters. In our
example it nds that the newly generated line has an occurrence of L1 in it.
This is immediately replaced by the argument corresponding to L1 (SUM) and
the line is then completely expanded. The process of macro expansion turns
out to be a little more complex than originally described.
3. In line three of the denition the quote () separates the character B from the
parameter L4. On expanding this line, the assembler treats the quote as a
separator, removes it, and concatenates the argument Z to the character B,
thus forming BZ. If BZ is a valid mnemonic, the line can be assembled. This is
another example of a macro line that has no meaning before being completely
expanded.
4. The argument corresponding to parameter L5 is null. The result is the string
CD with nothing between the C and the D. Again, if CD is a valid mnemonic (or
118 Macros
Ch. 4
directive), the line can eventually be assembled (or executed). Otherwise, the
assembler ags it as an error.
Note that there is a dierence between a null argument and an argument that is
blank. In the expansion C SUM,(D,T,U),(SUB/R1,L1),Z,/,SN, the fth argument
is a blank space, which ends up being inserted between the C and the D in the
expanded source line. The nal result is C/D/SN which is not the same as CD/SN. It
could even be interpreted by the assembler as C=label, D=mnemonic, SN=operand.
If a mnemonic D exists, the instruction would be assembled and C would be placed
in the symbol table, without any error messages or warnings.
Exercise 4.4 What if the last argument of an expansion is null? How can the
assembler distinguish between a missing last argument and a null one?
A little thinking shows that the precise names of the formal parameters are
not important. The parameters are only used when the macro is expanded. No
parameter names are used after pass 0 (except that the original names should appear
in the listing). As a result, the assembler replaces the parameter names with serial
numbers when the macro is placed in the MDT. This makes it easier to locate
occurences of the parameters when the macro is later expanded. Thus in one of the
examples above:
D MACRO A,B,C
LOD A
ADD B
STO C
ENDM
macro D is stored in the MDT as:
The vertical bar is used to separate source lines in the MDT. The 3 in the
second eld indicates that the macro has three parameters, and #1, #2 etc., refer
to the parameters. The bold vertical bar signals the end of the macro in the
MDT. This editing slows down the assembler when handling a macro denition but
it speeds up every expansion. Since a macro is dened once but may be expanded
many times, the advantage is clear.
It is now clear that three special characters are needed when macros are placed
in the MDT. In general, the assembler should use characters that cannot appear in
the original denition of the macro, and each assembler has a list of characters that
are invalid inside macros, or are even prohibited anywhere in the source le.
The example above is simple since each parameter is a single letter. In the
general case, a parameter may be a string of characters, called a token. The editing
process mentioned above is done by breaking up each source line into tokens. A
token is an indivisible unit of the line and is made up of a string of letters and digits,
or a single special character. Each token is compared to all parameter names and,
in case of a match, the token is replaced by its serial number. The example above
Sec. 4.2
Macro Parameters
119
may now be rewritten with the paramater names changed to more than one letter.
D
MACRO AD,BCD,ST7
LOD AD
ADD BCD
STO ST7
ENDM
Lets follow the editing of the second line. It is broken up into the two tokens
ADD and BCD, and each token is compared with all three parameter names. The rst
token ADD almost matches the rst parameter AD. The second one BCD exactly
matches the second parameter. That token is replaced by the serial number #2
of the parameter, and the entire lineincluding the serial numberis stored in the
MDT. Our new example will be stored in the MDT in exactly the same way as
before, illustrating the fact that parameter names can be changed without aecting
the meaning of the macro.
start
mode:=N
1
4
Input
mode
macro
name
yes
mode:=E
N
no
3
yes
mode:=D
MACRO
no
output
no
END
yes
pass 0
directive
Execute it
yes
stop
no
1
4
120 Macros
Ch. 4
input
N,D
output
mode
read line
from source
read line
from MDT
write lineon
new source
write line
in MDT
return
E,N
mode
return
expansion
mode
definition
mode
input
locate space
in MDT
yes
end of
macro
mode:=N
store macro
name and
parameters
no
1
scan line &
substitute
parameters
input
yes
output
Execute it
no
MEND
yes
mode:=N
pass 0
directive
no
3
output
Sec. 4.3
Operation of Pass 0
121
122 Macros
Ch. 4
...
name
name
pointer
...
where the last separator is immediately followed by the name of the next macro.
Such an MDT is easy to search by following the pointers and comparing names.
Since the pointers point backwards, the table is searched from the end to the beginning; an important feature. It guarantees that when a multiply-dened macro is
expanded, the back search will always nd the last denition of the macro. Multiplydened macros are a result of nested macro denitions(see later), or of macros that
users write to supersede system macros. In either case, it is reasonable to require
that the most recent denition always be used. The advantage of this organization
is its exibility. It is only limited by one parameter, the size of the array. The total
size of all the denitions cannot exceed this parameter. Thus we can dene a few
long macros or many short ones.
The other way to organize the MDT is to store the macros in an MDT array
with separators as described before, but without pointers. An additional array,
called the MNT, contains pairs <macro name, pointer> where the pointers point
to the start of a denition in the MDT array. The advantage of this organization is
that the MNT has xed size entries, allowing for a faster search of names. However,
the total amount of macros that can be stored in such an MDT is limited by two
parameters. The size of the MDT arraywhich limits the total size of all the
macrosand the size of the MNTwhich limits the number of macros that can
be dened. The following diagram is an example of such an MDT organization. It
shows an MDT array with 3 macros. The rst has 3 parameters, the second, 4, and
the third, 2. The MNT array has xed-size entries.
Sec. 4.4
MDT Organization
name1
name2
name3
123
MDT Array
In old assemblers this directive is executed by removing the pointer that points
to the macro, not the macro itself. Removing the macro itself and freeing the space
in the MDT is done by many new assemblers (see Ch. 8). It is done by changing
the macro denition to a null string, and storing another (smaller) macro in the
space thus freed. After dening and removing many macros, the MDT becomes
fragmented; it can be defragmented by moving macros around and changing pointers
in the MNT.
Exercise 4.6 What could be a practical example justifying the actual removal of
a macro from the MDT?
4.4.2 Order of search of the MDT
Pass 0 has to identify each source line as either a macro name, a pass 0 directive,
or something else. To achieve quick identication, the pass 0!directives should be
stored in the MDT with a special ag, identifying them as directives. This way,
only the MDT has to be searched in pass 0.
An assembler combining passes 0 and 1 has to do more searching. Each source
line has to be identied as either an instruction (its size should be determined), as
a pass 0 or pass 1 directive (to be executed), as a pass 2 directive (to be written on
the intermediate le), or as a macro name (to be expanded).
One approach is to search the OpCode table rst (it should contain all the
mnemonics and directives) and the MDT next, the idea being that most source
lines are either instructions or directives, macro expansions are relatively rare.
The alternative approach is to search the MDT rst and the OpCode table next.
This way, the programmer can dene macros which redene existing instructions or
directives. If this approach is used, the MDT must be designed to allow for quick
search.
124 Macros
Ch. 4
Sec. 4.5
125
CLEER ARG
RARG
After dening this macro, we assign CONS=5, and expand CLEER twice. The
expansion CLEER CONS generates CLRL RCONS (which is probably wrong),
whereas the expansion CLEER \CONS generates CLRL R5.
4.5.4 Attributes of macro arguments
Each argument in a macro expansion has attributes that can be used to make
decisionsinside the macro denitioneach time the macro is expanded. At the
time the macro denition is written, the arguments are unknown. They only become
known when the macro is expanded, and may have dierent attributes each time
the macro is expanded.
Exercise 4.10 Chapter 1 mentions attributes of symbols. What is the dierence
between attributes of symbols and of macro arguments?
We will cover the six attributes supported by the IBM 360 assembler and will
use, as an example, the simple macro denition:
M
MACRO P1
P1
ENDM
126 Macros
Ch. 4
Block A,B,C,D
Num
Num
Sec. 4.5
127
L:
.MACRO
.NCHR
...
.BLKW
.ENDM
Exmpl L,S
Size,S
Size
MACRO
P1 ..
BNZ G12
A
LOD ..
.
.
G12 STO ..
ENDM
Each time this macro is expanded, symbols A, G12 will be dened. As a result, the
second and subsequent expansions will cause assembler errors.
Most assemblers oer help in the form of automatically generated unique
names. They are called local labels or automatic labels. Here are two examples,
the rst one from the MPW assembler for the Macintosh computer. The two lines
with labels in the above denition can be written as:
128 Macros
Ch. 4
A
G12
and every time the macro is expanded, the assembler will append a sux of the
form 00001, 00002,. . . to any label generated. This way all labels generated are
unique. In our example they will be A00001, G1200002, A00003, . . .
The second example is from Macro, the VAX assembler. When a local label
is needed in a macro, a parameters shoudl be added preceded by a ?. Thus in:
NEG:
DONE:
.MACRO
TSTL
BLSS
MOVL
BRB
MNEGL
.ENDM
ABS A,B,?NEG,?DONE
A
NEG
A,B
DONE
A,B
MAC
MACRO P1,P2
IRP P1
ADD P1
IRP
.
.
ENDM
The sequence to be duplicated consists of the single instruction ADD. Each time it is
duplicated, one of the components of the compound parameter P1 is selected. The
expansion:
MAC (A,B,#3),H
will generate
ADD A
ADD B
ADD #3
.
.
Here is another IRP example, from Macro, the VAX assembler.
Sec. 4.5
.MACRO
.NARG
.IRP
.IIF
.ENDR
CALLS
.ENDM
129
CallProc Name,A,B,C,D
Num
Temp,<D,C,B,A>
NOT BLANK,Temp, PUSHL Temp
#<Num-1>,Name
When a program contains many long macros that are expanded many times,
the programmer may not want to see all the expansions listed in the printed output. The PRINT directive may be used to suppress listing of macro expansions
(PRINT NOGEN) or to turn on such listings (PRINT GEN). This directive does not
aect the listing of the macro denitions or of the body of the program. Those
listings are controlled by the LIST, NOLIST directives.
Exercise 4.12 Is the PRINT NOGEN itself listed?
Macro, the VAX assembler, supports the similar directives .SHOW & .NOSHOW.
Thus one can write, e.g., .NOSHOW ME to suppress listings of all macro expansions
(ME stands for Macro Expansion), or .SHOW MEB (where MEB stands for Macro
Expansion Binary) to list just lines that actually create binary code generated
during macro expansions.
4.5.10 Comment lines in macros
If a macro denition contains comment lines such as in:
MACRO A,B,C
* WATCH OUT, PARAMETER B IS SPECIAL
.
.
C R1
* THE PREVIOUS LINE CHANGES ITS MEANING
.
.
The comments should be printed, together with the denition of the macro, in the
listing le, but should they also be printed with each expansion? The most general
130 Macros
Ch. 4
answer is: It depends. Some comments refer to the lines in the body of the macro
and should be printed each time an expansion is printed (as mentioned elsewhere,
the printing of macro expansions is optional). Other comments refer to the formal
parameters of the macro, and should be printed only when the macro denition
is printed. The decision should be made by the programmer, which means that
the assembler should have two types of comment lines, the regular type, which is
indicated by an asterisk, and the special type, indicated by another character, such
as a !, for comments that should be printed only as part of a macro denition.
MACRO
COMP
JMP
ENDM
A MACRO
ADD
C
SUB
ENDM
B MACRO
LOD
A
STO
ENDM
Sec. 4.6
Nested Macros
131
mode is very similar to the normal mode. Each line is identied as either the name
of a macro, a pass 1 directive, or something else. If the line is the name of a
macro, the assembler suspends the expansion of B, fully expands the new macro,
and then returns to complete Bs expansion. If the line is a pass 0 directive, the
assembler executes it. If the line is something else, the assembler scans it, substitutes
parameters, and writes the line on the new source le. During this process the
assembler maintains a pointer that always points to the current line in the MDT.
When the expansion of B starts, the macro is located in the MDT, and a
pointer is set to point to its rst line. That line (LOD) is fetched and identied as an
instruction. It is written on the new source le, and the pointer is updated to point
to the second line (A). The second line is then fetched and is identied as a macro
name. The assembler then suspends the expansion of B and starts expanding A by
performing the following steps:
It locates macro A in the macro denition table
It sets a new pointer to the rst line of macro A.
It saves the values of the actual arguments of macro B.
From then on, macro A is expanded in the usual way until its second line (C) is
fetched. At that point the new pointer points to that line. The assembler suspends
the expansion of A and starts the expansion of macro C by performing three steps as
above. While expanding macro C, the assembler uses a third pointer and, since C is
not nested, the expansion terminates normally and the third pointer is discarded.
At that point the assembler returns to the expansion of macro A and resumes using
the second pointer (which should be incremented to point to the next waiting line
of A). When the expansion of A is completed, the assembler discards the second
pointer and switches to the rst onewhich means resuming the expansion of the
original macro B. Three typical steps in this process are shown in gure 44 below.
In part I, the second line of B has been fetched and the (rst) pointer points to
that line. The expansion of macro A has just started and the second pointer points
to the rst line of A.
In part II, the second pointer points to the second line of A. This means that
the line being processed is the second line of A. The expansion of C has just started.
In part III, the expansion of C has been completed, the third pointer discarded,
and the assembler is in the process of fetching the third line of A.
The rules for nested macro expansion therefore are:
In the macro expansion mode, when encountering the name of a macro, nd it in
the MDT, set up a new pointer to point to the rst line, save the arguments of the
current macro, and continue expanding, using the new pointer.
After fetching and expanding the last source line of a macro, discard the current
pointer and start using the previous one (and the previous set of arguments).
If there is no previous pointer, the (nested) macro expansion is over.
132 Macros
Ch. 4
II
III
Figure 44. Three typical steps in a nested macro expansion.
From this discussion it is clear that the pointers are used in a Last In First Out
(LIFO) order, and should thus be stored in a stack. This stack is called the macro
expansion stack (MES), and its size determines the number of possible pointers and
thus the maximum depth of nesting.
Implementing nested macro expansions is, therefore, done by declaring a stack
and using it while expanding a macro. All other details of the expansion remain
the same as in a normal expansion (however, see conditional assembly below).
The following is a set of rules and a ow chart (gure 45, a generalized subset
of gure 42) which illustrate the main operations of a pass 0 supporting nested
macro expansions.
1. Input line from MDT (since mode=E).
2. If it is a pass 0 directive, execute it. Goto 1.
3. If is is a macro name, start a new expansion. Goto 1.
4. If it is an end-of-macro character, stop current expansion and look back in
MES.
If MES empty, change mode to N. Goto main input.
Elsestart using previous pointer in MES, remain in E mode. Goto 1.
5. Line is something else, substitute parameters and write line on new source le.
Goto 1.
Sec. 4.6
Nested Macros
expansion
mode
input
end of
macro
empty
MES
mode:=N
yes
133
not empty
resume
using
previous
parameters
no
pass 0
directive
Execute it
no
macro
name
yes
no
scan line &
substitute
parameters
output
134 Macros
Ch. 4
MACRO
INST1
SET Q+1
NOGOOD
INST2
ENDM
Sec. 4.8
Conditional Assembly
135
N. This test is another pass 0 directive, typically called AIF (Assembler IF). Its
general form is AIF exp.symbol, where exp is a boolean expression containing
only quantities known to the assembler. The assembler evaluates the expression
and, if its value is true, the assembler goes to the line labeled .symbol.
The next version of the same macro is now:
GOOD
Q
.F
MACRO
INST1
SET Q+1
AIF Q=N.F
GOOD
INST2
ENDM
An expansion such as
N EQU 2
Q SET 0
GOOD
will generate the following:
1.
2.
3.
4.
5.
6.
7.
8.
9.
INST1
SET Q+1
AIF Q=N.F
GOOD
INST1
SET Q+1
AIF Q=N.F
INST2
INST2
Of the 9 lines expanded, lines 2, 3, 4, 6, 7 are pass 0 directives. They are executed
and do not appear in the nal program. Lines 1, 5, 8, 9 are instructions, and are
the only ones actually expanded and written onto the new source le.
The macro has been recursively expanded to a depth of 2 because of the way
symbols Q, N have been dened. It is also possible to say AIF Q=2.F, in which case
the depth of recursion will depend only on the initial value of Q.
Exercise 4.14 Is it valid to write AIF Q>N.F?
An important question that should be raised at this point is: what can N be?
Clearly it can be anything known in pass 0, such as another (non-future) SET symbol,
a constant, or an actual argument of the current macro. This, however, oers only
a limited choice and some assemblers allow N to be an absolute symbol, dened by
an EQU. An EQU, however, is a pass 1 directive, so such an assembler should look at
each EQU directive in pass 0 andif the EQU denes an absolute symbolexecute
136 Macros
Ch. 4
it, store the symbol in the temporary symbol table, and write the EQU on the new
source le for a second execution in pass 1. In pass 1, all EQU directives are executed
and all EQU symbols, absolute and relative, end up in the permanent symbol table.
Some assemblers go one more step and combine pass 0 with pass 1. This makes
for a very complex pass but, on the other hand, it means that conditional assembly
directives can use any quantities known in pass 1. They can use the values of any
symbols in the symbol table (i.e., any non-future symbols), the value of the LC,
and other things known in pass 1 such as the size of the last instruction read. The
following is an example along these lines.
.
.
P DS 10
N DS 3
.
.
Q SET P
GOOD
.
.
The next example is more practical. It is a recursive macro FACT that calculates
a factorial. Calculating a factorial is a common process and is done by computers
all the time. In our example, however, it is done at assembly time, not at run time.
FACT
S
K
MACRO N
SET S+1
SET K*S
AIF S=N.DON
FACT N
ENDM
.DON
The expansion:
S
K
SET 0
SET 1
FACT 4
will calculate 4! (=24) and store it in the temporary symbol table as the value of the
SET symbol K. The result can only be used at assembly time, since the SET symbols
are wiped out at the end of pass 0. Symbol K could be used, for example, in an
array declaration such as: FACT4 DS K which declares FACT4 as an array of length
4!. This, of course, can only be done if the assembler combines passes 0 and 1.
The FACT macro is rewritten below using the IIF directive. IIF stands for
Immediate IF. It has the form IIF condition,source line. If the condition is true,
Sec. 4.8
Conditional Assembly
137
MACRO N
SET S+1
SET K*S
IIF S=N,(FACT N)
ENDM
If X=2, then line 1 will be expanded, otherwise, lines 2 & 3 will be expanded.
Exercise 4.15 What can X be?
The IBM 360, 370 assemblers, were the rst ones to oer an extensive conditional assembly facility, and similar facilities are featured by nost modern assemblers. It is interesting to note that the MPW assembler for the Macintosh computer
supports conditional assembly directives that are almost identical to those of the old
360. Some of the conditional assembly features supported by the 360, 370 assemblers will be discussed here, with examples. Notice that those assemblers require
all macro parameters and all SET symbols to start with an ampersand &.
The AIF directive on those assemblers has the format AIF (exp)SeqSymbol.
The expression may contain SET symbols, absolute EQU symbols, constants, attributes of arguments, arithmetic operators, the six relationals (EQ NE GT LT GE
LE), the logical operators AND OR NOT, and parentheses. SeqSymbol (sequence symbol) is a symbol that starts with a period. Such a symbol is a special one, it has no
value and is not stored in any symbol table. It is used only for conditional assembly
138 Macros
Ch. 4
and thus only exists in pass 0. When the assembler executes an AIF and decides to
go to, say, symbol .F, it searches, in the MDT, for a line labeled .F, sets the current
MES pointer to point to that line, and continues the macro expansion. If a line
fetched from the MDT has such a label, the line goes on the new source le without
the label, which guarantees that only pass 0 will see the sequence symbols. In contrast, regular symbols (address symbols and absolute symbols) do not participate
in pass 0. They are dened in pass 1, and their values used in pass 2.
Examples:
AIF (&A(&I) EQ ABC).TGT where &A is a compound parameter and &I is a SET
symbol used to select one component of &A.
AIF (T&X EQ O).KL if the type attribute of argument &X is O, meaning a null
argument, then go to symbol .KL.
AIF (&J GE 8).HJ where &J could be either a parameter or a SET symbol.
AIF (&X AND &B EQ ( ).LL where &X is a B type SET symbol (see below) and
&B is either a C type SET symbol or a parameter.
The AGO Directive: The general format is AGO SeqSymbol. It directs the
assembler to the line labeled by the symbol. (This is an unconditional goto at
assembly time, not run time.)
The ANOP (Assembler No OPeration) directive. The assembler does nothing in
response to this directive, and its only use is to provide a line that can be labeled
with a sequence symbol. This directive is used in one of the examples at the end of
this chapter.
SET symbols. They can be of three types. A (arithmetic), B (boolean) or C
(character). An A type SET symbol has an integer value. B type have boolean
values of true/false or 1/0. The value of a C type SET symbol is a string.
Any SET symbol has to be declared as one of the three types and its type cannot
be changed. Thus:
LCLA &A,&B
LCLB &C,&D
LCLC &E,&F
declare the six symbols as local SET symbols of the appropriate types. A local SET
symbol is only known inside the macro in which it is declared (or inside a control
section, but those will not be discussed here). There are also three directives to
assign values to the dierent types of SET symbols.
&A SETA
&A SETA
&B SETA
&C SETB
1
&A+1
1+(B1011*XFF1-15)/&A-NSYSLIST(&A) where B1011 is a binary constant,
XFF1 is a hex constant, and N is the number attribute
(the number of components) of the second argument of the current
expansion (the second, because &A=2).
(&A LT 5)
Sec. 4.8
&D
&E
&E
&F
&F
&E
SETB
SETC
SETC
SETC
SETC
SETC
Conditional Assembly
139
1 means true
the null string
12 the string 12
34
0&E&F.5 the string 012345. The dot separates the value of &F from the 5
ABCDEF(2,3) the string BCD. Substring notation is allowed.
N2
&S
N3
MACRO
GBLA &S
SETA 1
AR &S,2
N2
ENDM
MACRO
LCLA &S
SETA 2
SR &S,2 the local symbol is used
N3
ENDM
MACRO
GBLA &S
CR &S,2 the global symbol is used
ENDM
140 Macros
Ch. 4
.F
MACRO &L,&P1,&P2,&P3
LCLA &SS
SETA 1
ST&P1 5,TEMP
L&P1 5,&P2(1)
AIF (&SS GE N&P2).F
SETA &SS+1
A&P1 5,&P2(&SS)
AGO .B
ST&P1 5,&P3
L&P1 5,TEMP
MEND
STD 5,TEMP
LD 5,A
AD 5,B
AD 5,C
STD 5,DEST
LD 5,TEMP
This macro uses the conditional assembly directives to loop and generate several
AD (Add Double) instructions. The number of instructions generated equals the
number of components (the N attribute) of the third argument.
A more sophisticated version of this macro lets the user specify, in argument
®, which register to use. If argument ® is omitted (its T attribute equals O)
the macro selects register 5.
SUM
MACRO &L,&P1,&P2,&P3,®
LCLC &CC
LCLC &RR
LCLA &AA
&AA
SETA 1
&CC
SETC &L
&RR
SETC ®
AIF (T® NE O).Q
is argument ® a null?
&RR
SETC 5
yes, use register 5
Sec. 4.8
&CC
&CC
.Q
&CC
.B
&AA
.F
.H
Conditional Assembly
ST&P1 &RR,TEMP
SETC
ANOP
L&P1 &RR,&P2(1)
AIF (&AA GE N&P2).F
SETA &AA+1
A&P1 &RR,&P2(&AA)
AGO .B
ST&P1 &RR,&P3
AIF (T® NE O).H
L&P1 &RR,TEMP
ENDM
141
142 Macros
Ch. 4
MACRO
MULT
Y MACRO
ADD
JMP
ENDM Y
DIV
ENDM X
This requires more work on the part of the programmer but, on the other hand,
makes the program easier to read.
Exercise 4.16 In the case where both macros X, Y end at the same point
X
MACRO
MACRO
ENDM Y
ENDM X
Sec. 4.9
143
Now may be a good point to mention that such nesting can be done in one
direction only. A macro expansion may cause the assembler to temporarily switch to
the macro denition mode; a macro denition, however, cannot cause the assembler
to switch to the expansion mode. When the assembler is in a macro expansion
mode, it may expand a macro with nested denition, so it has to enter the macro
denition mode from within the macro expansion mode. However, if the assembler
is in the macro denition mode and the macro being dened species the expansion
of an inner macro, the assembler does not start the nested expansion while in the
process of dening the outer macro. It therefore does not enter the macro expansion
mode from within the macro denition mode. The reason being that when a macro
is dened, its denition is stored in the MDT without any attempt to assemble,
execute, or expand anything. As a result, the assembler can only be in one of the
four following modes: normal, denition, expansion, & denition inside expansion.
They will be numbered 14 and denoted N,D,E,& DE, respectively.
In the example above, when macro X is dened, macro Y becomes part of the
body of X and is not recognized as an independent macro. When X is expanded,
though, macro Y is dened and, from that point on, Y can also be expanded. The
only reason to implement and use this feature is that macro X can be expanded
several times, each expansion of X creating a denition of Y in the MDT, and those
denitionsbecause of the use of parametersdo not have to be the same.
Each time Y is expanded, the assembler should, of course, expand the most
recent denition of Y (otherwise nested macro denition would be a completely
useless feature). Expanding the most recent denition of Y is simple. All that the
assembler has to do is to search the MDT in reverse order; start from the new macros
and continue with the older ones. This feature of backward search has already been
mentioned, in connection with macros that redene existing instructions.
The above example can be written, with the inclusion of parameters, and with
some slight changes, to make it more realistic.
X
Y
MACRO A,B,C,D
MULT A
MACRO C
C
ADD DIR
JMP B
ENDM Y
DIV C
Y D
ENDM X
144 Macros
Ch. 4
A DIV instruction.
An expansion of Y, consisting of three lines, the last two of which are ADD, JMP.
The expansion X SEC,TOR,DIC,BPL will generate:
Y
MULT SEC
MACRO C
C
ADD DIR
JMP TOR
ENDM Y
DIV DIC
Y BPL
BPL
ADD DIR
JMP TOR
rst line
macro Y gets stored
in the macro denition
table with the B parameter
changed to TOR but with
the C parameter unchanged
second line
a line which is expanded to give third line. BPL is substituted for the C parameter
fourth line
fth line
The only thing that may be a surprise in this example is the fact that macro Y
is stored in the MDT without C being substituted. In other words. Y is dened as
Y MACRO C and not as Y MACRO DIC. The rule in such a case is the same as in a
block structured language. Parameters of the outer macro are global and are known
inside the inner macro unless they are redened by that macro. Thus parameter B
is replaced by its value TOR when Y is dened, but parameter C is not replaced by
DIC.
Since we are interested in how things are done by the assembler, the implementation of this feature will be discussed in detail. In fact, we will describe in
detail two ways to implement nested macro denitions. One is the traditional way,
described in [28,61]. The other, due to Revesz [81] is newer and more elegant.
4.9.1 The traditional method
Normally, when a macro denition is entered into the MDT, each parameter
is replaced with a serial number #1, #2, . . . To support nested macro denition,
the assembler replaces each parameter, not with a single serial number, but with a
pair of numbers (denition level, serial number). To determine those pairs, a stack,
called the macro denition stack (MDS), is used.
When the assembler starts pass 0, it clears the stack and initializes a special
counter (the Dlevel counter mentioned above) to 0. Every time the assembler encounters a MACRO line, it increments the level counter by 1 and pushes the names
of the parameters of that level into the stack, each with a pair (level counter, i )
attached, where i is the serial number of the parameter. The assembler then starts
copying the denition into the MDT, comparing every token on every line with the
stack entries (starting with the most recent stack entry). If a token in one of the
macro lines matches a stack entry, the assembler considers it to be a parameter (of
the current level or any outer one). It fetches the pair (l,i ) from the stack entry
that matched, and stores #(l,i ) in the MDT instead of storing the token itself. If
Sec. 4.9
145
the token does not match any stack entry, it is considered a stand-alone token and
is copied into the MDT as part of the source line.
When an ENDM is encountered, the stack entries for the current level are popped
out and Dlevel is decremented by 1. After the last ENDM in a nested denition is
encountered, the stack is left empty and Dlevel should be 0.
The example below shows three nested denitions and the contents of the MDT.
It also shows the macro denition stack when the third, innermost, denition is
processed (the stack is at its maximum length at this point).
The following points should be noted about this example:
Lines 3,5,8,10 in the MDT show that the assembler did not treat the inner macros
Q, R as independent ones. They are just part of the body of macro P.
On line 4, the #(2,1) in the MDT means parameter 1 (A) of level 2 (Q), while the
#(1,3) means parameter 3 (C) of level 1 (P).
On line 7, #(3,3) is parameter 3 (E) of level 3 (R) and not that of level 2 (Q). The
H is not found in the stack and is therefore considered a stand-alone symbol, not a
parameter.
On line 11, the assembler is back to level 1 where none of the symbols is a parameter. The stack at this point only contains the four bottom lines, and symbols
E,F,G,H are all considered stand-alone.
#
1
source line
P
2
3
4
5
6
7
8
9
10
11
12
MACRO A,B,C,D
A,B,C,D
MACRO A,B,E,F
A,B,C,D
MACRO A,C,E,G
A,B,C,D
E,F,G,H
ENDM R
E,F,G,H
ENDM Q
E,F,G,H
ENDM P
MDT
P4
#(1,1),#(1,2),#(1,3),#(1,4)
Q MACRO #(2,1),#(2,2),#(2,3),#(2,4)
#(2,1),#(2,2),#(1,3),#(1,4)
R MACRO #(3,1),#(3,2),#(3,3),#(3,4)
#(3,1),#(2,2),#(3,2),#(1,4)
#(3,3),#(2,4),#(3,4),H
ENDM R
#(2,3),#(2,4),G,H
ENDM Q
E,F,G,H
146 Macros
Ch. 4
stack
G
E
C
A
F
E
B
A
D
C
B
A
(3,4)
(3,3)
(3,2)
(3,1)
(2,4)
(2,3)
(2,2)
(2,1)
(1,4)
(1,3)
(1,2)
(1,1)
top
bottom
Sec. 4.9
147
Dlevel:=Dlevel+1
no
Dlevel=1
?
yes
Clear MDS. Locate space in MDS for
a new macro. Store macro name in MDT
no
5
yes
MEND
Figure 46. The classical method for nested macro denitions (part I).
2. Opens up an area in the MDT for the new macro.
3. Brings in source lines to be stored in the MDT, as part of the new denition.
Each line is rst checked to see if it is a MACRO or a MEND.
4. If the current source line is MACRO, the line itself goes into the MDT and the
assembler enters the names of the parameters of the inner macro in stack P as
a new, higher level. Just the names are entered, with no serial numbers.
This, again, is an important principle of the method. It distinguishes between
the parameters of level 1 and those of the higher levels, but it does not distinguish
between the parameters of the dierent higher levels. Again the idea is that, at
macro denition time we only handle the level 1, outer macro, so why bother to
resolve parameter conicts on higher levels at this time. Such conicts will be
resolved by the same method anytime an inner macro is dened (becomes level 1).
5. If the current line is a MEND, the line itself again goes into the MDT and the
assembler removes the highest level of parameters from P. Thus after nding
148 Macros
Ch. 4
5
6
Break lines up into tokens.
Compare every token with
all param names in MDS,
from top to bottom
Dlevel:=Dlevel+1
match
yes
no
Store token
in MDT
Dlevel=0
?
no
yes
mode:=N
7
Store (l,i) in MDT
instead of token
1
Figure 46. The classical method for nested macro denitions (part II).
the last MEND, stack P should be empty again, signifying a non-macro denition
mode. The assembler should then switch back to the original mode (either 1
or 3).
6. If the source line is neither of the above, it is scanned, token by token, to determine what tokens are parameters. Each token is compared with all elements
on stack P, from top to bottom. There are three cases:
a: No match. The token is not a parameter of any macro and is not replaced
by anything.
b: The token matches a name in P that does not have a serial number attached. The token is thus a parameter of an inner macro, and can be
ignored for now. We simply leave it alone, knowing that it will be replaced by some serial number when the inner macro is eventually dened
(that will happen when some outer macro is eventually expanded).
c: The token matches a name in P that has a serial number attached. The to-
Sec. 4.9
pass 0
Initialize
Dlevel:=0
Elevel:=0
mode:=N
149
MACRO?
ENDM?
mode
D,DE
N,E
1
4
macro name?
mode
E,DE
N,D
5
Read next
line from
source file
Increment pointer
located at top of
MES. Use it to read
next line from MDT
mode
DE
6
eof
?
7
D
yes
end of
pass 0
output
Figure 47a. Reveszs method for nested macro denitions (part I).
ken is thus a parameter of the currently dened macro (the level 1 macro),
and is replaced by the serial number.
After comparing all the tokens of a source line, the line is stored in the MDT.
It is a part of the currently dened macro.
The important point is that the formal parameters are replaced by serial numbers only for level 1, i.e., only for the outermost macro denition. For all other
nested denitions, only the names of the parameters are placed on the stack, reecting the fact that only level 1 is processed in macro denition mode. That level
ends up being stored in the MDT with each of its parameters being replaced by a
serial number.
On recognizing the name of a macro, which can happen either in mode 1
(normal) or 3 (expansion), the assembler enters mode 3, where it:
150 Macros
Ch. 4
MACRO
ENDM
mode
E
N
mode:=D
Dlevel
mode:=DE
=0
Dlevel:= D,
Dlevel+1 DE
Error!
Unmatched
ENDM
=1
Place
in
current def
in MDT
Copy line
into current
definition
in MDT
Dlevel:=0
mode:=N
Pop current
level of
params
from P
Dlevel
>1
=1
Allocate space
in MDT for
new macro. Push
each param, with
#i attached,
into P
Push all
params into
stack P, but
without #i
mode:=N
>1
Dlevel:=
Dlevel+1
Figure 47b. Reveszs method for nested macro denitions (part II).
7. Loads stack A with a new level containing the actual arguments. If the new
macro has no arguments, the new level is empty, but it should always be in
stack A as a level.
8. Inserts a new pointer in the MES, pointing to the rst linein the MDTof
the macro to be expanded.
9. Starts expanding the macro by bringing in individual lines from the MDT.
Each line is scanned and all serial numbers are replaced by arguments from
stack A. The line is then checked to distinguish three cases:
Sec. 4.9
151
macro name
6
mode:=E
Elevel:=Elevel+1
7
Push a new pointer into
MES, pointing to 1st line
of macro to be expanded
Figure 47c. Reveszs method for nested macro denitions (part III).
d: The line is MACRO, the assembler enters mode 4 (denition mode within
expansion mode) and follows rules 16 above.
e: The current line is MEND, the assembler again follows the rules above and
may decide, if this is the last MEND, to return from mode 4 back to mode
3.
f: For any other line, the line is written on the new source le as explained
earlier.
On recognizing the end of the macro (no more lines to expand), the assembler:
10. Removes the highest level from stack A.
11. Deletes the top pointer from the MES.
12. Checks the MES and stack A. There are three cases:
g: Neither is empty. There is an outer level of expansion, the assembler stays
in mode 3 and resumes the expansion as above.
h: Only one is empty. This should never happen and can only result from
a bug in the assembler itself. The assembler should issue a message like
impossible error, inform a systems programmer.
i: Both are empty. This is the end of the expansion. The assembler switches
to mode 1.
152 Macros
Ch. 4
Dlevel
>0
=0
D,
DE
=0
Elevel
>0
E
N
Error!
illegal
character
read while
in normal
mode
Pop current
levelof args
from stack A
Dlevel:=Dlevel-1
>1
Dlevel
Elevel:=Elevel-1
mode
D
Elevel
mode:=N
DE
mode:=E
=0
>0
1
Figure 47d. Reveszs method for nested macro denitions (part IV).
L1 MACRO
MOV
CMP
L2 MACRO
MOV
CMP
L3 MACRO
MOV
CMP
MEND
A,B,C,D
A,B
C,D
E,F,D,C
E,C
A,X
A,E,C,G
C,E
A,G
L3
L1
MOV
CMP
L2
MOV
CMP
L3
MOV
CMP
MEND
#1#2#3#4
#1#2
#3#4
MACRO E,F,D,C
E,C
#1,X
MACRO A,E,C,G
C,E
A,G
L3
L2
MOV
CMP
L3
MOV
CMP
MEND
#1#2#3#4
#1,#4
M,X
MACRO A,E,C,G
C,#1
A,G
L3
L3 MACRO #1#2#3#4
MOV #3,W
CMP #1,#4
(end of macro)
Sec. 4.9
11
12
13
14
15
16
ADD
SUB
MEND
ADD
SUB
MEND
A,F
C,B
L2
E,F
G,B
L1
ADD
SUB
MEND
ADD
SUB
Source denition
#1,F
C,#2
L2
E,F
G,#2
(end of macro)
ADD
SUB
Denition of L1
153
M,#2
#4,N
(end of macro)
Denition of L2
Denition of L3
after
line
1
D
C
B
A
#4
#3
#2
#1
4
C
D
F
E
D
C
B
A
#4
#3
#2
#1
7
G
C
E
A
C
D
F
E
D
C
B
A
10
C
D
E
F
D
C
B
A
13
D
C
B
A
#4
#3
#2
#1
16
empty
#4
#3
#2
#1
#4
#3
#2
#1
154 Macros
Ch. 4
4.9.3 A note
The TEX typesetting system, mentioned earlier, supports a powerful macro
facility that also allows for nested macro denitions. It is worth mentioning here
because it uses an unconventional notation. To dene a macro a with two parameters, the user should say \def\a#1#2{...#1...#2...}. Each #p refers to one of
the parameters. To nest the denition of macro b inside a, a double ## notation
is used. Thus in the denition \def\a#1{..#1..\def\b##1{..##1..#1}} the
notation ##1 refers to the parameter of b, and #1, to that of macro a.
The rule is that each pair ## is reduced to one #. This way, macro denitions
can be nested to any depth.
Example:
\def\a#1{#1\def\b##1{[#1,##1] \def\x####1{(#1,##1,####1)}\x X}\b Y}
The denition of macro a consists of:
Printing as parameter in quotes.
Dening macro b.
Expanding b with the argument Y.
Similarly, the denition of macro b includes:
Printing as parameter and bs parameter in square brackets.
Dening macro x with one parameter.
Expanding x with the argument X.
Macro x simply prints all three arguments, as, bs and its own, in parentheses.
Exercise 4.17 What is the result of the expansion \a A?
The reader should convince himself that the rule above really allows for unlimited nesting of macro denitions.
Sec. 4.10
Summary of Pass 0
155
expects macro expansions and will ag any macro denition as an error. Example :
A
X
N
BEGIN EX
MACRO P,Q
.
.
ENDM
MACRO C,II
.
.
ENDM
DS 12 an array declaration
MACRO
.
.
ENDM
.
.
The denition of macro N will be agged since it occurs too late in the source.
With this separation of macro denition and expansion, pass 0 is easy to implement. In the normal mode such an assembler simply copies the source le into
the new source. Macro denitions only aect the MDT, while macro expansions
are written onto the new source le.
Such an assembler, however, cannot support nested macro denitions. They
generate new macro denitions too late in pass 0.
An assembler that supports nested macro denitions cannot impose the restriction above, and can only require the user to dene each macro before it is
expanded.
Having a separate pass 0 simplies pass 1. The ow charts in his chapter prove
that pass 0 is at least as complicated as pass 1, so keeping them separate reduces
the total size and the complexity of the assembler. Combining pass 0 and pass 1,
however, has the following advantages:
No new source le needed, saving both le space and assembly time.
Proceduressuch as input procedures or procedures for checking the type of a
symbolthat are needed in both passes, only need to be implemented once.
All pass 1 facilities (such as EQU and other directives) are available during macro
expansion. This simplies the handling of conditional assembly.
156 Macros
Ch. 4
Syntax of
macro def
Syntax of
Maximum #
Maximum #
param-names
of macros
Assembler A
Assembler B
3. Continue the table of question 2 with columns for: Types of SET symbols, Syntax
of SET symbol name, Syntax of sequence symbol name.
4. A macro named LOD can be used to redene the LOD instruction. An assembler
supporting this feature simply searches the MDT backwards. What is another way
of redening assembler instructions?
5. Review the block structure feature found in higher-level languages. What is the
main dierence between this feature and the scope of parameters in a nested macro
denition?
6. What stacks are involved in nested macro denition and expansion? When (in
what passes) are these stacks used? Is it possible to use the memory occupied by
these stacks for something else?
7. What is a pass 0 directive? Make a list of all pass 0 directives mentioned in this
chapter .
8. Compare the two methods described in this chapter for MDT implementation,
the one with the MNT and the one without it.
9. Apply the two methods described in this chapter for handling nested macro
denition to the case:
Sec. 4.11
157
MACRO
MACRO
ENDM B
MACRO
ENDM C
ENDM A
The denitions of the two macros B, C are nested in the denition of A, but are
separate (not nested inside each other).
10. What is the main dierence between the parameters of a macro and those of a
procedure?
11. How should a programmer decide whether to use a macro or a procedure in a
given situation?
4.11.1 Project 41
Modify project 31 to include macros with parameters. The macros should
also support unique label generation but should not be nested in any way. No
conditional assembly is necessary.
4.11.2 Project 42
The EQU, DC directives are very useful. In fact, they can be used, together with
the MACRO, ENDM directives, to implement an assembler. The following macro:
ADD
P1
LC
MACRO P1,P2,P3
EQU *
BYTE 2A
BYTE P2
BYTE P3
SET LC+3
ENDM
may be used to assemble an ADD instruction. The expansion ADD X,2,Y would
158 Macros
Ch. 4
result in:
X
EQU *
BYTE 2A
BYTE 2
BYTE Y
After writing such a macro for every instruction, source programs can easily be
assembled. Most instructions contain elds that are longer or shorter than one byte,
so a data-generating directive is necessary, that would be more general than BYTE.
The DC directive, for example, could be modied such that DC 17(2,6) would
generate the constant 17 = 10012 as a 6-bit eld, starting at bit position 2 of the
current byte. The result is xx010001 where the xx eld should be lled by the next
DC.
Your task: Select a simple assembler language, such as the one described in
project 11, and implement it by writing a full set of such macros.
Ch. 5
Exercise 5.1 What should be printed in the object code eld when a macro denition is listed?
Y: DC 11,2,$. The rst constant (11) should be printed. The complete list
of constants generated by the DC may be too long but, if the programmer wants to
see it, a special directive such as PDC (print dened constants) may be used.
Chapter 3 mentions several directives used to control the listing. They can
be used to print a title and a sub-title (including, perhaps, the date and time) on
every page, to suppress the listing or parts of it, to eject a page at any point in the
listing, and to control the listing of the cross-reference and of macros. In general,
such directives are executed in pass 2 and are easy to implement.
TITLE OF PROGRAM
SUBTITLE
line
00001
LC
Object
6800 ASSEMBLER
VER 1.5
Source
NAME SOLVE
PAGE 1
DEC. 10,1991
Sec. 5.1
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
A 6800 Example
0000
E1D1
E1AC
E07E
0000
0001
0002
0003
0005
0007
0008
0A
00
00
0000
0000
00
00
************************
* THIS PROGRAMS SOLVES...
* IT ALSO GENERATES THE ...
* AND, FINALLY, THE ...
************************
*
ORG 0
*
* EXTERNAL PROCEDURES DEFINITION
*
OUTCH
EQU $E1D1
OUTPUT SINGLE CHAR.
INCH
EQU $E1AC
INPUT CHAR.
PDATA1 EQU $E07E
PRINT MESSAGE
*
* INTERNAL STORAGE
*
DEFLT
BYTE 10
DEFAULT IS DECIMAL
BASE
BYTE 0
BASE=DEFAULT
ROTCNT BYTE 0
COUNT FOR ROTATE
INVAL
WORD 0
VALUE OF INPUT
TMPVAL WORD 0
TEMP. INPUT
INCNT
BYTE 0
COUNT OF INPUT CHARS.
LSTCHR BYTE 0
LAST CHAR BEFORE CR
TITLE OF PROGRAM
INITIALIZE
6800 ASSEMBLER
VER 1.5
line
LC
Object
Source
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
0009
000B
000D
0010
0013
0016
0019
96
97
7F
7F
BD
CE
A6
00
01
0003
0004
0231
A018
00
001B
001D
81
26
42
4D
001F
C0
05
00041
00042
00043
0021
0023
0025
2F
D7
08
47
07
161
VALIN
LDA
STA
CLR
CLR
JSR
LDX
LDA
A DEFLT
A BASE
INVAL
INVAL+1
MSGIN
#BUFFER
A 0,X
PAGE 2
DEC. 10,1991
START INPUT
HIGH ORDER BYTE
LOW ORDER BYTE
WAIT FOR INPUT
GET ADDRESS OF BUFF.
FIRST CHAR.
*
CMP A #B
BNE PRCHK
START OF BASE?
NO, LOOK FOR PREFIX
*
* SEE IF ATTEMPT TO CHANGE DEFAULT BASE
* MUST BE IN THE FORM:
* BASE XX, WHERE XX=2,8,10,16
*
SUB B #5
MUST BE 6 OR 7 CHARS
BLE BASERR
STA B
INX
IF LE 5, ERROR
INCNT STORE TO CHECK LATER
POINT PAST ASE
0026
0027
0028
0029
Ch. 5
08
08
08
08
INX
INX
INX
INX
TITLE OF PROGRAM
INPUT MESSAGE
6800 ASSEMBLER
VER 1.5
line
LC
Object
Source
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
002A
002C
002E
0031
0033
0035
0038
003A
A6
C6
BD
81
2E
7A
27
08
00
0A
021E
09
35
0007
1C
003B
003D
003F
97
A6
C6
01
00
0A
.
.
.
0066
0068
97
20
00
9F
006A
20
5C
00083
00084
00085
00086
00087
00088
00089
00090
FINALLY, POINT TO IT
LDA
LDA
JSR
CMP
BGT
DEC
BEQ
INX
A 0,X
B #10
VALCK
A #9
BASERR
INCNT
BVALCK
PAGE 3
DEC. 10,1991
006C 81
006E 26
0070 86
.
.
TITLE OF PROGRAM
CHECK FOR PREFIX
25
04
02
line
Object
Source
008A
008C
97
08
01
008D
.
.
7A
0007
LC
BASTOR
*
BASERR
*
PRCHK
STA A BASE
LDA A 0,X
LDA B #10
STA A DEFLT
BRA VALIN
FINALLY, REPLACE IT
READ INPUT AGAIN
BRA ERROR
CMP A #%
BNE OCPCK
LDA A #2
IS IT BINARY?
NO, CHECK IF OCTAL
LOAD 2 FOR BINARY
6800 ASSEMBLER
VER 1.5
PAGE 5
DEC. 10,1991
.
.
00108
00109
00110
00111
*
PRINIT
STA A BASE
INX
DEC INCNT
REPLACE BASE
Sec. 5.1
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
.
A06D
A071
A072
A076
A077
A078
A 6800 Example
20444543
04
20484558
04
0D0A0000
04
DECM
HEXM
CRLFM
163
FCC / DEC/
BYTE 4
FCC / HEX/
BYTE 4
BYTE $D,$A,0,0,4
*
A018
A018
BUFFER
*
A02C
ORG $A018
RMB 20
INPUT BUFFER
END
ERROR SUMMARY
0 ERROR(S) IN ASSEMBLY
BINARY SUMMARY
684 BYTES OUTPUT
684 BYTES TO RAM
0 BYTES TO ROM
TITLE OF PROGRAM
ERROR AND XREF
line
LC
Object
6800 ASSEMBLER
VER 1.5
PAGE 14
DEC. 10,1991
Source
Ch. 5
The subtitle is dierent on each of our pages. This is the eect of the SUBTTL
directive, which itself is not listed.
The object code is simple and short. It occupies between one and three bytes
per source line and can therefore be easily included in the listing line. On other
computers, the object code may be longer and may have very dierent sizes for each
source line. In such cases, the assembler may devote, say, 8 printing positions to
the object code per listing line and, if the object code is longer, print the rest on a
second, and even a third, line. This is the situation on the VAX computer (see next
example) where each source line can generate between one and three 32-bit words.
Source line 00454 produces several constants. Its listing is, therefore, long, consisting of two lines.
FIBONACCIS
30-OCT-1991 19:43:55 VAX MACRO V5.0-8
30-OCT-1991 18:38:08 SYS$USER3:[VACSC0HN]TEST.MAR;1
00000000EF
00000000EF
0000
00000000
0001 0000
0005 0002
00000000
0000 0000
0002
0002
0002
0002
B0 0002
1
2
3
4
5
6
7
8
9
10
11
one:
five:
.title Fibonaccis
.psect cons noexe,nowrt
.word 1
.word 5
.psect code exe,wrt
.entry fib,0
.macro move x,y,z
movw y,x
movw z,y
.endm move
movw one,a
Page 1
(1)
Sec. 5.2
00000002EF
A VAX Example
01
00000006EF
B0 000D 12
B4 0014 13
00000000 14
00000002
0000 15 a:
00000004
0002 16 b:
0000001A 17
00000002EF 00000006EF
B1 001A 18 loop:
06
12 0025 19
0000005CEF
17 0027 20
00000002EF 00000000EF
A1 002D 21 next:
00000004EF
0038
003D 22
%MACRO-E-UNRECSTMT, Unrecognized statement !
003D
003D 23
003D 24
00000000EF 00000002EF
B0 003D
00000002EF 00000004EF
B0 0048
0053
0053 25
00000006EF
B6 0053 26
BE AF
17 0059 27
005C 28 done:
00000006
00000008
00000004
0004
0006
0008
29
30
31
32
c:
k:
165
movw #1,b
clrw k
.psect data noexe,wrt
.blkw 1
.blkw 1
.psect code exe,wrt
cmpw k,five
bneq next
jmp done
addw3 a,b,c
output c
.show me
move a,b,c
movw b,a
movw c,b
.noshow me
incw k
jmp loop
$exit s
.psect data noexe,wrt
.blkw 1
.blkw 1
.end fib
FIBONACCIS
30-OCT-1991 19:43:55 VAX MACRO V5.0-8
Page 2
Symbol table 30-OCT-1991 18:38:08 SYS$USER3:[VACSC0HN]TEST.MAR;1 (1)
A
B
C
DONE
FIB
FIVE
K
LOOP
NEXT
ONE
SYS$EXIT
00000000
00000002
00000004
0000005C
00000000
00000002
00000006
0000001A
0000002D
00000000
********
PSECT name
Allocation
R
R
R
R
RG
R
R
R
R
R
GX
03
03
03
02
02
01
03
02
02
01
02
+----------------+
! Psect synopsis !
+----------------+
PSECT No.
Attributes
00000000
00000004
00000065
00000008
Ch. 5
( 0.)
( 4.)
(101.)
( 8.)
00
01
02
03
(0.)
(1.)
(2.)
(3.)
NOPIC
NOPIC
NOPIC
NOPIC
USR
USR
USR
USR
CON
CON
CON
CON
ABS
REL
REL
REL
LCL
LCL
LCL
LCL
NOSHR
NOSHR
NOSHR
NOSHR
FIBONACCIS
30-OCT-1991 19:43:55 VAX MACRO V5.0-8
Page 3
Cross reference 30-OCT-1991 18:38 SYS$USER3:[VACSC0HN]TEST.MAR;1 (1)
+------------------------+
! Symbol Cross Reference !
+------------------------+
SYMBOL
A
B
C
DONE
FIB
FIVE
K
LOOP
NEXT
ONE
SYS$EXIT
VALUE
00000000-R
00000002-R
00000004-R
0000005C-R
00000000-R
00000002-R
00000006-R
0000001A-R
0000002D-R
00000000-R
00000000-XR
DEFINITION
15 (1)
16 (1)
30 (1)
28 (1)
6 (1)
4 (1)
31 (1)
18 (1)
21 (1)
3 (1)
28 (1)
REFERENCES...
#-11 (1) #-21 (1) #-24 (1)
#-12 (1) #-21 (1) #-24 (1)
#-21 (1) #-24 (1)
20 (1)
#-18 (1)
#-13 (1) #-18 (1) #-26 (1)
27 (1)
#-19 (1)
#-11 (1)
FIBONACCIS
30-OCT-1991 19:43:55 VAX MACRO V5.0-8
Page 4
Cross reference 30-OCT-1991 18:38 SYS$USER3:[VACSC0HN]TEST.MAR;1 (1)
+------------------------+
! Macros Cross Reference !
+------------------------+
MACRO SIZE DEFINITION REFERENCES...
$EXIT S
1 28 (1)
28 (1)
MOVE
1 7 (1)
24 (1)
FIBONACCIS
30-OCT-1991 19:43:55 VAX MACRO V5.0-8
Page 5
Cross reference 30-OCT-1991 18:38 SYS$USER3:[VACSC0HN]TEST.MAR;1 (1)
+------------------------+
! Opcode Cross Reference !
+------------------------+
OPCODE
ADDW3
BNEQ
CALLS
CLRW
CMPW
INCW
JMP
VALUE
00A1
0012
00FB
00B4
00B1
00B6
0017
21
19
28
13
18
26
20
REFERENCES...
(1)
(1)
(1)
(1)
(1)
(1)
(1) 27 (1)
Sec. 5.2
MOVW
PUSHL
A VAX Example
00B0
00DD
167
FIBONACCIS
30-OCT-1991 19:43:55 VAX MACRO V5.0-8
Page 6
Cross reference 30-OCT-1991 18:38 SYS$USER3:[VACSC0HN]TEST.MAR;1 (1)
+----------------------------+
! Directives Cross Reference !
+----------------------------+
DIRECTIVE
.BLKW
.END
.ENDM
.ENTRY
.GLOBL
.MACRO
.NOSHOW
.PSECT
.SHOW
.TITLE
.WORD
REFERENCES...
15 (1) 16 (1) 30 (1) 31 (1)
32 (1)
10 (1) 28 (1)
6 (1)
28 (1)
7 (1)
25 (1)
14 (1) 17 (1) 2 (1) 29 (1) 5 (1)
23 (1)
1 (1)
3 (1) 4 (1)
+------------------------+
! Performance indicators !
+------------------------+
Phase
Initialization
Command processing
Pass 1
Symbol table sort
Pass 2
Symbol table output
Psect synopsis output
Cross-reference output
Assembler run totals
Page faults
CPU Time
27
00:00:00.04
459
00:00:00.07
214
00:00:00.13
0
00:00:00.00
72
00:00:00.05
1
00:00:00.01
2
00:00:00.00
4
00:00:00.02
785
00:00:00.32
Elapsed Time
00:00:00.48
00:00:00.70
00:00:01.06
00:00:00.00
00:00:00.29
00:00:00.01
00:00:00.02
00:00:00.02
00:00:02.68
Ch. 5
+--------------------------+
! Macro library statistics !
+--------------------------+
Macro library name
Macros defined
SYS$COMMON:[SYSLIB]STARLET.MLB;2
messages, on lines:
22 (1)
The error message below line 22 means that the output command was not recognized by the assembler. This command is a system macro that should have been
loaded (by a special option on the command line) from the macro library. It was
intentionally not loaded, in order to illustrate this common error.
There are 3 sections. The cons section occupies lines 34. The code section, lines
613 & 1828. The data section, lines 1516 & 3031. The Psect synopsis (part
of the extended cross-reference) shows the length and attributes of each section.
The symbol table is listed, as part of the cross-reference. All symbols are of type
R (relative), except FIB, which is relative global, and SYS$EXIT, which is global
external (note that its value is unknown).
The symbol cross-reference, as well as the macros-, opcodes-, and directives crossreference, are all generated when the extended cross-reference information is required by the user. They all contain the same type of information.
The Performance indicators table shows the time spent on each phase of the
assembly. Note that pass 1 took 0.13 sec., and was thus slower than pass 2, which
only required 0.05 sec.
Sec. 5.3
A MASM Example
169
cgr
mydat
array
array
abc
mydat
mycod
title example
group mycod, mydat
assume cs:cgr, ds:cgr
segment public
if var lt 10
db var
else
db 10
endif
df 2
ends
segment public
if1
begin:
endif
mov al,xyz
if2
begin:
mycod
mydat
xyz
mydat
endif
mov ax,2 eq 4
add ax,dx
ends
segment public
db 3
ends
end begin
Ch. 5
11/5/91 17:32:46
XYZ
The second set is the nal, pass-2, listing, containing the phase error. Note how
array becomes a single byte loaded with the constant 4, and how the df directive
reserves 6 bytes. The data segment mydat is thus 7 bytes long, stretching from
address 0 to address 6. The next part of mydat denes xyz as the byte at address
7.
11/5/91 17:32:46
Page 1-1
Sec. 5.3
A MASM Example
171
if2
begin:
a:prog.ASM(19): error A2006: Phase error between passes
endif
0000
B8 0000
mov ax,2 eq 4
0003
03 C2
add ax,dx
0005 mycod ends
0007 mydat segment public
0007
03 xyz
db
3
0008 mydat ends
end begin
Next come the segment table, symbol table, and general analysis:
11/5/91 17:32:46
Symbols-1
Length
CGR . . . . . . . . . . . . . .
MYCOD . . . . . . . . . . . .
MYDAT . . . . . . . . . . . .
GROUP
0005 PARA PUBLIC
0008 PARA PUBLIC
Symbols:
N a m e
Type
Value
Attr
ABC . . . . . . . . . . . . . .
ARRAY . . . . . . . . . . . . .
BEGIN
. .
VAR
. .
TEXT
XYZ
. .
@CPU . . . . . . . . . . . . . .
@FILENAME . . . . . . . . . . .
@VERSION . . . . . . . . . . . .
TEXT 0101h
TEXT a prog
TEXT 510
27 Source Lines
27 Total
Lines
13 Symbols
47906 + 395737 Bytes symbol space free
0 Warning Errors
1 Severe Errors
The cross reference below was generated by the special cref utility.
Ch. 5
Sec. 5.4
An MPW Example
eighttt
MC680xx Assembler - Ver 3.10 MPW Example
173
06-Nov-91 Page
1
Copyright Apple Computer, Inc. 1984-1989
Loc
F Object Code
0005A A873
0005C 4CDF 1E00
00060
Ch. 5
Sec. 5.5
175
6. Special
Assembler Types
Ch. 6
Exercise 6.1 The second denition above denes an HLA as a higher-level, machine dependent language. Is it also possible to design the logical contrast namely, a
lower-level (assembler), machine-independent language? What would be a possible
use for such a language?
6.1.1 NEAT/3
This is an HLA implemented on the NCR Century computers, a family of
computers designed primarily for data processing. NEAT/3 was designed, in 1966
68, with two goals in mind:
1. To make it easy to write short to medium size programs, without having to go
through a COBOL compiler (a slow and complex process at that time).
2. To make it easy to write an ecient COBOL compiler for the Century computers.
The NCR literature does not mention the name high-level assembler, nor
does it mention the term higher-level language. It simply refers to NEAT/3 as a
programming language, and to the translator, as the NEAT/3 compiler. However,
on examining the language, it is easy to see that it does not have the type of
powerful statements and control structures one expects to nd in a higher-level
language. The statements are simple, and resemble typical assembler instructions.
This is why they are easy to translate, and this is why the NEAT/3 translator was
easier to write than a COBOL compiler. Most of the time, a NEAT/3 statement
Sec. 6.1
High-Level Assemblers
179
is translated into one machine instruction. Only when conversion between data
types is necessary, the translator (we will call it a translator, not a compiler or an
assembler) generates more machine instructions.
The main feature that makes NEAT/3 look like a higher-level language is the
data denitions. The way data items and les are declared in NEAT/3 closely
resembles COBOL. Concepts such as working storage area, constants area, data
records divided into elds, and data types, are all borrowed from COBOL, which
makes it easy to write a COBOL compiler in NEAT/3. Even editing masks (for
data to be printed) are supported and use the same characters 9,Z,$,+, ,.
as in COBOL.
The data types are: Characters (coded in USASI, not ASCII), signed & unsigned decimal, signed & unsigned packed decimal, binary, and hex.
The following is an example of a record declared in NEAT/3:
Name
D
D
D
D
D
D
D
D
D
D
D
D
D
Code
SALESRED
STORECODE
MANAGER
DATE
DAY
MONTH
YEAR
GROCERY
PRODUCE
MEAT
DAIRY
MISC
DAILYTOTAL
R
F
F
F
F
F
F
F
F
F
F
F
F
Location
50
0
3
11
11
13
15
17
23
28
34
39
44
X
X
X
X
X
X
U
U
U
U
U
U
Code
Ch. 6
Data Type
X or blank Character
S
Generated spaces
Z
Generated zeros
U
Unsigned decimal
D
Signed decimal
B
Binary
H
Hex
P
Signed packed decimal
K
Unsigned packed decimal
E
Editing mask
Next we examine some of the NEAT/3 instructions (or, as the NCR literature
calls them, statements). This is the area were one can really justify the name highlevel assembler. Most of the instructions are simple, resemble typical assembler
instructions, and are easy to translate. The main dierence in translation is the
automatic conversion between data types, which NEAT/3 supports, but a typical
assembler does not.
Exercise 6.2 What are typical valid and invalid type conversions in languages
such as COBOL and NEAT/3?
1. Comparisons. The instruction COMP A,B compares its two operands and
sets status ags like any typical comparison in machine language. The only
dierence is that the operands can be of dierent types, and the translator
takes care of type conversion.
2. Conditional Branches. The BRE CALCTAX instruction is executed by testing
the status ags and branching to CALCTAX on equal. There are several such
branches, each translated into one machine instruction. They are the only way
to make decisions in NEAT/3, except for the simple IF described below.
3. Test and branch. The if alphabetic (IFAL) instruction tests its rst operand
and branches to the second operand (a label) if the rst operand is alphabetic
(of type character).
4. Moves. Those instructions work the same as in COBOL. In the simplest case,
a move is translated into one machine instruction but, if it also involves type
conversion and editing, it is translated into several instructions.
5. End of execution (FINISH). This instruction is translated into machine instructions that close all open les and perform a software interrupt to the
operating system.
In summary, NEAT/3 justies the name high-level assembler language, but
also the name low-level programming language. Its translator may be called a highlevel assembler but also a compiler. It seems to lie somewhere in between assembler
language and COBOL.
Sec. 6.1
High-Level Assemblers
181
6.1.2 PL360
The main justication for this language, to use the developers own words [61],
is:
. . . PL360 was designed to improve the readability of
programs which must take into account specic characteristics and limitations of a particular computer.
It represents an attempt to further the state of the
art of programming by encouraging and even forcing
the programmer to improve his style . . . Because of
its inherent simplicity, the language is particulaly well
suited for tutorial purposes . . . a tool that encourages
the programmer to write programs in a disciplined, lucid and readable style while still maintaining control
over the optimal use of specic machine characteristics . . . .
The main low-level feature of the language is its heavy dependence on the
IBM360 architecture, allowing the programmer to use specic registers and machine
instructions. Its main high-level features are block structure, procedures, typed
variables, and the use of statements rather than instructions. There is also a goto
statement, which is rarely used.
Specically, the following 9 points summarize the most important design features of the language. The rst 6 are low level features, and the rest, high level
ones.
1. Variables can be declared and their types are the types available on the machine
itself. For instance, an integer can be declared as a byte, a short integer, an
integer, or a long integer. Those types correspond to a byte, halfword, fullword,
and doubleword in the machine. This makes it easy to assemble (compile?
translate?) declarations, as each is directly translated into a DS directive.
2. Only one-dimensional arrays can be declared, again making it easy to translate
an array declaration into a DS directive. Also making it easy to generate an
index to an array. Accessing a multi-dimensional array like B[i,j] requires
the compiler to generate an index of the form i*n+j (or something similar).
This, however, is against the design philosophy of PL360, requiring simple
translation.
3. The names R0..R15, F0, F2, F4, F6 are reserved to indicate the general purpose
and the oating-point registers of the 360 computer. Also names such as F45
are reserved to indicate pairs of registers (see example below). Also, the syn
construct (synonym) allows the programmer to use a meaningful name instead
of Ri, and to make the two names equivalent.
Ch. 6
4. Expressions are simple, they use no parentheses, and are executed strictly from
left to right, with equal precedence for all operands (see example with R9 below). This also implies that no temporary storage is automatically used by the
translator when an expression is evaluated; more in the spirit of an assembler
than a compiler.
5. Machine instructions can easily be used, each is declared as a function in PL360.
6. Functions and procedures can be declared, and can have parameters. A good
programming practice, however, is to pass parameters through registers, avoiding the complexities of call by name, value, or address.
7. The language does not allow the use of absolute addresses, and a program
cannot modify itself.
8. High-level control structures such as if-then-else, case, for, or while, can be
used.
9. Compound statements (sandwiched between a begin end pair) and blocks can
be used. A PL360 variable thus has scope (it can be local or global).
A program written in PL360 supercially resembles an Algol 60 program. It
consists of statements, not instructions. It has the same control structures and block
structure. Even variable declarations are very similar. However, on a closer look,
one can easily see the common use of machine registers and of machine instructions.
Some simple examples are shown in Fig. 6.1 below:
A short integer is 16 bits (halfword)
Two arrays, 100 f-p values each.
Figure 6.1
Sec. 6.1
High-Level Assemblers
183
Assignments are executed from left to right without parentheses and with no
operator precedence. The example involving R9 above is executed as:
R9:=R8; R9:=R9 and R7; R9:= R8 shll 8; R9:=R9 or R6;
The shll keyword means shift left logical. The ++ notation means either a
logical (unsigned) integer addition or an unnormalized f.p. addition.
6.1.3 PL516
This language was designed and implemented [63] in 19691970, at the National Physical Laboratory in England, as a high-level assembler language for a
small, 16-bit machine, the Honeywell DDP516. It was inuenced by PL360 and
was an attempt to create a language that would be similar to Algol 60 and, at the
same time, would be machine dependent and would allow the direct use of machine
instructions. To quote from the introduction:
. . . It must be emphasized that the language is no substitute for Fortran or Algol 60, but rather a more convenient and exible system for writing long, machine-code
programs. The main advantage over DAP, the assembler
for the DDP-516, is that program texts are largely selfdocumenting due to their Algol-like structure . . .
The main features of PL516 are: variables (but no real variables), with scope;
type checking; expressions (but no temporary working storage); procedures (but
not more than one parameter per procedure and no call by name); compound statements; conditional statements; simple for loops; arrays (only one-dimensional); no
dynamic memory allocation; direct control of the machine registers; machine instructions can be included in the code.
The following two short examples, taken from the users manual, give the avor
of the language:
1.
for xsymbol176 do
if mcode[xsymbol]=ident then goto found
else codesymbol IRS,0;
This is a loop, searching array mcode. IRS is a machine instruction (IncRement
in Store, by 1) and, in our example, it increments memory location 0, which also
happens to be the X (index) register.
2.
accumulator conditional procedure bsis;
begin
xsymbolaccumulator;
more: when cleft optable[xsymbol]=bs then
begin
codesymbol STX,addbs;
Ch. 6
exittrue
end;
when accumulator nonzero then
begin
xinc icleft optable[xsymbol]+x;
goto more;
end
end;
This is a procedure bsis that searches an array optable to nd the type of a
basic symbol (basic symbols are tokens read by the compiler from the source le,
on paper tape). On nding a match in the array, variable addbs is set to the array
index by the machine instruction STX (store X register). The keyword accumulator
stands for the accumulator (the A register).
6.1.4 BABBAGE
This is a HLA designed specically for the GEC4000 family of minicomputers
made in England. It is the only assembler available for the 4000, which makes it
especially interesting. The most important feature of the language is the syntax of
the logical and arithmetic expressions, which makes it easy to assemble, not just the
expressions, but most of the statements in the language. BABBAGE expressions
supercially look like those of a higher-level language but are very dierent because
they use no operator precedence and no parentheses. This makes it easy to parse
and translate such an exprsssion. Examples:
1. a+RX & 7*b=>c. where RX stands for registerX and & is the logical and
operator. The expression is translated into:
LOD
ADD
AND
MULT
STA
a
X
#7
b
c
Sec. 6.1
High-Level Assemblers
185
L1
L2
LOD a
CMP b
BZ L1
LOD c
CMP d
BNZ L2
LOD #0
STA g
--
2. IF a EQ b THEN << 0=>a 1=>b >>. The symbols << , >> act as a beginend pair to delimit compound statements. In our case, the compound statement
is the two simple expressions setting operands a, b to 0,1 respectively. This is
translated into:
L1
LOD
CMP
BNZ
LOD
STA
LOD
STA
--
a
b
L1
#0
a
#1
b
One-dimensional arrays can be declared, used in expressions, and easily translated. Examples:
VECTOR[0,3] OF BYTE dv=ABCD
VECTOR[1,4] OF HALFWORD v=(8,11,32,0)
dv[i+2]*3=>v[i]. This exprssion, involving arrays dv & v, is translated into:
LODX i
ADDX #2
LOD RX,dv
MULT #3
LDX i
STA RX,v
Records can be declared and used in a way very similar to Pascal records.
Pointers can be set to combine records into large data structures.
The above description is incomplete but it shows the main characteristics of
the language namely, it combines higher-level language features with simple syntax
and heavy machine dependence. BABBAGE is therefore a HLA in the sense of both
denitions given in this section.
Ch. 6
6.2 Summary
In the 1970s, while the 360/370 computers were in wide use, PL360 had generated interest among programmers, and had enjoyed a certain amount of use. Today
(in the 1990s), however, that interest has virtually disappeared. However, there
seem to be language designers who believe in the concept of a high-level assembler
language. As a result, we may perhaps see more interest in this approach in the
future. More future programming languages may be combinations of the higherlevel and assembler languages of today, providing the benets of both types. If this
proves true, we may see the demise of conventional assembler languages in favor of
such hybrids.
Sec. 6.3
Meta Assemblers
187
Ch. 6
The rst instruction is easy to assemble. The MA only needs an OpCode table.
The second and third instructions require the value of symbols that should be in
the symbol table. SYM should be an absolute symbol, and ABC, a relative one. The
MA should rst evaluate the expression SYM+1, and then assemble the instructions
in the usual way.
Exercise 6.4 What does the following line mean, if anything: RR XR R4,R6?
FUNC INC,Q,P
EQU LC+P
ENDF Q
The ENDF species that the result of the function is Q. When such a function is used,
as, for example, in: . . . INC S,68 . . ., it generates S EQU LC+68 and returns the
value of S, which can be used on the same line.
Library Routines: A good assembler should have accesss to a library of routines,
and should be able to search the library and add routines to the program being
assembled. With a MA, the problem is that there may be several libraries, each
with a dierent format. A good MA can therefore accept a special command with
the name of a specic library and information on how to search it and read routines
from it.
Directives: Most directives are independent of the specic source language
used, making it easy to execute them by a MA. The most important exceptions are
the data generating directives. They are machine dependent since computers use
dierent types of data. The size of numbers, the character codes, the method used to
represent signed numbers, all those depend on the architecture of the computer, and
may be very dierent for dierent computers. However, computers use a relatively
small number of data types, which simplies the task of executing those directives.
Sec. 6.3
Meta Assemblers
189
6.4 Disassemblers
Practically speaking, a disassembler is the opposite of an assembler. It translates in the opposite direction, from machine code to assembler language. Because
of the nature of assembler language, a disassembler is a relatively simple program.
Since each assembler instruction generates one machine instruction, the opposite
translation can be done with relative ease.
The disassembler goes through the following steps:
It reads the next machine instruction from the source le.
It identies the OpCode. If the OpCode has xed length, this is very easy. Otherwise, the disassembler has to perform several tests starting with the rst few bits
of the machine instruction.
Once the OpCode has been identied, an OpCode table, similar to the one used
by the assembler, is searched. The table provides the mnemonic, the number of
operands, and other information.
Once the number of operands is known, the disassembler scans each operand in
the machine instruction to determine the addressing modes, registers, and addresses
used by each.
After obtaining all this information, the original assembler instruction is known
and can be printed or written on a le.
The simple steps described above are not sucient to disassemble machine
code. To end up with a correct, readable, assembler program, the disassembler has
to handle the following problems
The symbol names used in the original program cannot be gured out by the
disassembler. Thus an instruction such as JMP ABC may be disassembled to read
JMP A0001. When the disassembler nds an instruction with an address operand,
it assumes that the original instruction has used a symbol, not an absolute address,
and it generates a unique symbol name such as Adddd where dddd are digits. The
resulting disassembled program is technically identical to the original one, but may
be harder to read since meaningful symbol names are important.
A similar problem exists with regard to macros. Some instructions in the object
code come from the expansion of macros while others come from the original program. When the object code is disassembled, it is impossible to tell whether the
original program has used macros and, if yes, which ones. The disassembled source
can therefore have no macros.
The same thing is true for any feature that is completely handled by the assembler. EQU symbols, for instance, are transparent to the disassembler and cannot be
reected in the listing generated by it. Thus when something like:
Ch. 6
REG
EQU 5
CMP REG,. . .
Contents
F944
F945
F948
F94A
F94C
.
.
8A
4C
A2
A9
20
.
.
DA FD
03
A0
ED FD
Label Operation
TXA
JMP
LDX
LDA
JSR
Operand
SUB1
#$3
#$A0
SUB2
FD A2 03
A9 A0
.
.
Label Operation
Operand
SBC
LDA
$03A2,X
#$A0
Since the contents of locations F947F949 happens to be the OpCode of the SBC
instruction. The same disassembler, given address F949 as a start address will
produce -
Sec. 6.4
Disassemblers
Address
Contents
F949
F94A
03
A9 A0
.
.
Label Operation
???
LDA
191
Operand
#$A0
Since the contents of address F949 is not the OpCode of any 6502 instruction.
Example: PC DOS, the operating system of the IBM PC, has a simple debugger, called DEBUG, that can disassemble 80x86 machine code. The three examples
below show the results of disassembling code starting from address 0 of a certain
memory segment (the segment number is irrelevant and has been omitted).
0000
0002
0003
0004
0008
000B
000F
0013
0017
0019
001B
001D
001F
CD20
C0
9F
009AEEFE
1DF0F4
02BA102F
03BA10BC
02BA10FC
0C01
0301
0002
FFFF
FFFF
INT 20
DB C0
LAHF
ADD [BP+SI+FEEE],BL
SBB AX,F4F0
ADD BH,[BP+SI+2F10]
ADD DI,[BP+SI+BC10]
ADD BH,[BP+SI+FC10]
OR AL,01
ADD AX,[BX+DI]
ADD [BP+SI],AL
??? DI
??? DI
When DEBUG is directed to start disassembling from address 1, the bytes 20 C0,
that were interpreted as parts of two instructions, now constitute an ADD AL,AL
instruction. The rst few lines are dierent, but the rest is the same
0001 20C0
AND AL,AL
0003 9F
LAHF
0004 009AEEFE
ADD [BP+SI+FEEE],BL
0008 1DF0F4
SBB AX,F4F0
000B .....
A similar result is obtained when disassembling from address 2. The rst byte
is C0 and, since no instruction can start with this value, the disassembler considers
it to be data. It generates a DB C0 directive, and assumes that the next byte (9F)
starts the next instruction.
0002 C0
DB C0
0003 9F
LAHF
Ch. 6
0004 009AEEFE
ADD [BP+SI+FEEE],BL
0008 1DF0F4
SBB AX,F4F0
000B .....
As a result, it is important to start the disassembler properly. The user must
specify the location, in the source le, of the rst executable instruction. The
disassembler starts at this point and tries to interpret the contents as an OpCode.
If it is the OpCode of an instruction, the instructions size is determined, the entire
instruction is read from the source, and it is then disassembled. In the examples
above, instructions were 1, 2, or 3 bytes long. If the rst address does not contain
the OpCode of any instruction, the disassembler considers it data and generates
a directive (perhaps accompanied by a ???). It then starts afresh at the next
address. In addition, each byte read by the disassembler should also be interpreted
as a character (in ASCII or whatever code is used by the computer) and printed.
This way the user may decide whether certain disassembled items are instructions
or data.
Example:
Address
Contents
F952
F953
F954
F957
F959
F95C
A8
C8
AD
A9
B9
BD
.
.
B1 B2
AA
AF D8
BB B5
Label
Operation Operand
ASCII
TAY
INY
LDA
LDA
LDA
LDA
(
H
-12
)*
9/X
=4;
$B2B1
#$AA
$D8AF,Y
$B5BB,X
Sec. 6.5
Cross Assemblers
193
Ch. 6
Clean Up Windows
Empty Trash
Erase Disk
Restart
Shut Down
7. Loaders
196 Loaders
Ch. 7
of the operating system (because of its memory allocation task), while the assembler
is more a stand-alone program, having little to do with the rest of the OS.
Most of this chapter is devoted to linking loaders, but it starts with two short
sections describing assemble-go loaders and absolute loaders. It ends with a number
of sections devoted to special features of loaders and to special types of loaders.
Before we start, here is a comment on the word relocate. Loaders do not
relocate a program in the sense that they do not move it in memory from one
area to another. The loader may reload the same program in dierent memory
areas but, once loaded, the program normally is not relocated. There are some
exceptions where a program is relocated, at run time, to another memory area but,
in general, the term relocate is a misnomer.
Exercise 7.2 How can the user estimate the size of a new program?
The following two points show that a one pass assembly-load operation in a
large computer has serious disadvantages. As a result, it is used in practice only in
single-user computers.
Sec. 7.1
Assemble-Go Loaders
COMMON
unused
unused
data
area
197
Assembler
data
area
user program
user program
user program
198 Loaders
Ch. 7
unused
unused
user program
user program
SQRT routine
SQRT routine
unused
SQRT routine
unused
user program
JMP
user program
user program
Figure 72.
a. Program and SQRT routine loaded in memory.
b. Larger user program loaded in two parts.
c. First part of program JMPs to the second part, over the SQRT routine.
The discussion above illustrates the limitations of this type of loader. They are
the reasons why most loaders are of the relocating type. Only a few are absolute
loaders (see below) or are part of the assembler.
Sec. 7.2
Absolute Loaders
199
200 Loaders
Ch. 7
3. The next step is to read the next couple of items from the rst object le.
These are loader directives, each corresponding to a special symbol (EXTRN or
ENTRY). This information is loaded in memory in a special symbol table (SST) to
be used later for linking.
4. Step 3 is repeated for all remaining object les. After reading all the special
symbol information from all the object les, the loader scans the SST, merging
items as described below. This process converts the SST into a global external
symbol table (GEST). If no errors are discovered during this process, the GEST is
ready and the loader uses it later to perform linking.
5. The loader then reads the rest of the rst object le and loads it, relocating
instructions when necessary. All loader directives found in the le are executed.
Any item requiring special relocation is handled as soon as it is read o the le,
using information in the GEST. Some of those items may require loading routines
o libraries (see later in this chapter).
6. Step 5 is repeated for all remaining object les. They are read and loaded
in the order in which their names were read in step 1.
7. The loader generates three outputs. The main output is the loaded program.
It is loaded in memory as one executable module where one cannot tell if instructions
came from dierent object les. In a computer where virtual memory is used, the
program is physically divided into pages (or logically divided into segments) which
are loaded in dierent areas of memory. In such a case, the program does not occupy
a contiguous memory area. Nevertheless, it is considered a single module and it gets
executed as one unit. Pages and segments are described in any Operating Systems
or Systems Programming text.
The second (optional) output of the loader is a listing le with error messages,
if any, and a memory map. The memory map contains, for each program, its name,
start address, and size. The name is specied by the user in a special directive
(IDENT or TITLE) or, in the absence of such a directive, it is the name of the object
le.
The third loader output is also optional and is a single object le for the entire
program. This le includes all the individual programs after linking, so it does not
include any linking information, but includes relocation bits. It is called a load
module. Such a le can later be loaded by a relocating loader without having to
do any linking, which speeds up the loading. Note that a load module is the main
output of a linkage editor (see below).
The reason for this output is that, in a production environmentwhere programs are loaded and executed frequently, but rarely need to be reassembled (or
recompiled)fast load becomes important. In such an environment it makes sense
to use two types of loaders. The rst is a linker or a linkage editor, which performs
just linking and produces a load module. The second is a simple relocating loader
that reads and loads a load module, performing just the three tasks of memory
Sec. 7.3
Linking Loaders
201
LC
op
Source code
2nd
3rd
object
byte byte record
IDENT NOM
special symbols
start here
11111001
00100011
01001110
01001111
01001101
10100011
00000001
01001100
01000010
10100010
00000010
id
bits
11
11
11
11
11
11
11
11
11
11
11
size=25
N
O
M
Extrn length=3
index=1
L
B
Extrn length=2
index=2
202 Loaders
Ch. 7
lib.
file
commands &
names of all
object files
obj.
file
initial input
obj.
file
third output
loader
main
output
obj.
file
secondary
output
memory map &
error messages
a single executable
module in memory
program-size directive
special symbol information
object
instructions
id
bits
Figure 73.
a. The Files Associated with a Loader
b. Layout of a Relocatable Object File.
0
EXTRN LB,Y
0
ENTRY CD
0 Z
DS
5
5 ST LOD
R5,15
CD
COMP
R5 2
00
15
01011001
10000010
01000011
01000100
11
11
11
11
01000101
00001101
00000000
00001111
00010101
11
00
00
00
00
Y
Entry. length=2
C
D
Sec. 7.3
Linking Loaders
LSHFT R5,4
04
11
CALL
LB
01
13
BEQ
R4,X
17
15
CALL
LB 4
01
LOD
R5,Y
HLT
DC
6
1,$,X,LB
17
20
21
25
END
00
ST
00000100
00100000
00100000
00000001
00101100
00010001
00100000
00000001
02
00001101
00000000
00000010
00110000
00000001
00100100
00010001
00000001
11000101
eof on object le
00
00
00
10
01
00
00
10
00
00
10
00
00
00
01
10
11
203
ASCII code of $
Start exec. @5
0
0
op
code
Source
2nd
3rd
object
byte byte record
11101000
00100011
01010011
01010101
01000010
10000011
00000000
01001100
01000010
10000010
00000111
01011001
11
11
11
11
11
11
11
11
11
11
11
11
00111001
00000101
7
1
05
00111001
00000101
8
0
01000000
01000010
01100010
no loader directive generated
eof
00
01
00
00
00
11
11
IDENT SUB
LB
ENTRY LB,Y
ADD
R1,X
ADD
R1,5
4
5 X
7 Y
8
RET
DS
DC
END
2
2
id
bits
05
size=8
Ident, length=3
S
U
B
Entry. length=3
Value=0 (rel.)
L
B
Entry. length=2
Value=7 (rel.)
Y
204 Loaders
Ch. 7
1. The nal value of the LC in each program (the value printed to the left of
the END directive) is the size of the program. It becomes known at the end of pass
1, and is the rst item to go on the object le, at the start of pass 2. The loader
reads it and uses it to determine the memory area (and thus the start address) of
the program.
2. Symbol CD is declared as ENTRY in one program but is never declared as an
EXTRN in any other program. This is an unusual situation created either by an error
(the programmer has forgot to declare CD as an EXTRN in another program) or by
a change in the original design of the program (CD was supposed to be an EXTRN
in another program, plans were changed, CD has become just a regular symbol, but
the prgrammer forgot to delete the declaration ENTRY CD).
The loader should detect this and indicate a warning (unmatched entry CD).
This is not necessarily an error but may provide a hint to the programmer that
something in the program needs to be corrected.
The opposite casethat of an unmatched EXTRNis dierent. If a symbol
is declared EXTRN (and is also used by the declaring program) then it should be
declared as an ENTRY in another program. A failure to do so is an error, since the
loader would not be able to link the two programs. However, such a case may mean
that the symbol is the name of a library routine and is declared as an ENTRY in
the library. This is discussed in point 8 below and also in the section on library
routines.
3. Each line in the example object les is 8 bits long plus 2 identication
bits. They identify the line as either an absolute item (00), a relocatable item (01),
something that requires special relocation (10), or as a loader directive (11). Each
line thus becomes 8 + 2 = 10 bits long, of which only 8 bits actually get loaded in
memory. In certain computers, the operating system makes it hard, or inecient,
to output lines with sizes which are not multiples of 8. In such a case the assembler
may write all the id bits, packed four pairs to a byte, at the end of the object le.
The loader would have to read this information rst, reopen the object le, and
start loading. This is the reason why many loaders perform two passes over each
object le.
4. In this example, we limit ourselves to two id bits and thus can have only
four types of records on the object le. Specically, we can have just one type of
special relocation (identied by 10). Ideally, it would be desirable to have two types
of special relocation, absolute and relative. In such a case one could write:
ENTRY
.
AB EQU
AB
7
AB
AB
Sec. 7.3
Linking Loaders
205
206 Loaders
Ch. 7
assembly and the assembler, in response, completes pass 2 by writing the special
symbols from the symbol table on the object le (as loader directives, codes 101,
100, of variable size), followed by an end of le (eof). It is the programmers
responsibility to make sure that only one source le in the entire program has a
start address in the operand eld of the END directive. The loader expects only one
such address and issues an error messages on reading the second (and subsequent)
ones.
Exercise 7.4 What if the loader does not nd any code 110 directives?
Using the example, it is easy to see how the loader works. It reads the names
of the object les either from the command line (the keyboard command used to
invoke the loader) or from a special command le. It opens the two object les,
reads the rst directive from each, adds the two program sizes (27 + 8 = 35) and
locates an available memory area of size 35. If that area starts, say, at address 64,
then 64 becomes the rst relocation term. The loader reads the object les in the
order in which their names are specied. Assuming that it starts with le SUB, the
other relocation terms can now also be calculated. In our case, there is one more
such term, namely 64 + 8 = 72. In general, each relocation term equals the sum of
its predecessor and the length of the previous program.
At this point, the loader can print the memory map which, in our case, is:
Program Start Length
SUB
NOM
64
72
8
27
Exercise 7.5 What other information can the loader include in the memory map?
Next the loader reads the directives for the ENTRY symbols from the rst le.
It stores the special symbol information in the SST after relocating their values
(point 4 above shows that special symbols are always relative). Thus LB gets a
value of 0 + 64 = 64 and Y, a value of 7 + 64 = 71.
The loader repeats this for the next object le. It reads the special symbol
directives from that le into the SST.
The second le contains three special symbols LB, Y, CD. They are entered
into the symbol table with CDan ENTRYrelocated by 72. Its value becomes
8 + 72 = 80. Symbols LB, Y are of type EXTRN and their values are still unknown.
They are entered, each with its index, instead of a value. The SST now contains:
prog name
value
SUB
"
NOM
"
"
64
71
80
LB
Y
LB
Y
CD
index type
1
2
ent
ent
ext
ext
ent
Sec. 7.3
Linking Loaders
207
Note that the program name goes in the SST with each symbol. It is later used to
perform the linking.
After reading all the special symbol information from all the object les, the
loader proceeds to merge symbols in the SST. It scans the SST looking for symbols
of type ext. For each such symbol, the loader searches the SST for the corresponding
ent symbol. On nding it, the value of the ent symbol is stored in the entry for the
ext symbol, and the entry for the ent symbol is deleted.
This process results in the following table
prog name
value
index type
NOM
"
"
64
71
80
1
2
LB
Y
CD
ext
ext
ent
Exercise 7.6 What if there are two ent symbols in the SST, with the same name
but from dierent programs, both matching the same ext symbol ?
If the user has asked to see the GEST, this is the time for the loader to print
it. This is also the reason why the symbol names are retained in the table. We will
see that they are not used in the actual linking.
In the absence of errors, the loader is now ready to load the object les. It starts
with the rst object le, SUB, reads instructions, and loads them into consecutive
locations starting from location 64. Since there are 8 bytes in that le, the last one
will be loaded in location 71. Each byte with id bits 01 will get relocated by adding
the rst relocation term, 64, to it. The result in memory will be:
208 Loaders
Ch. 7
64
65
66
67
68
69
70
71
00111001
01000101
00111001
00000101
01000000
reserved for
array X
00000010
There are no linking problems in this program since it does not use any EXTRN
symbols. Notice that none of the records on the object le has id = 10.
On reading the eof, the loader switches to the next object le (NOM) and starts
using the next relocation term (72). This le is loaded in the same way as the
previous one, and ends up occupying locations 7296. The only problem in loading
the second le are the items with id bits of 10. Each of them contains an index
rather than the value of a symbol.
To nd the value of such a symbol, the loader searches the GEST for the name
of the program (NOM) and the specic index. On nding the entry, the value becomes
available. The rst such line in our example is CALL LB. It is read from the object
le with id = 10 which means that the corresponding byte (=00000001) is an index,
not a value. The loader searches the GEST for an entry with program=NOM and
index=1. On nding it, the loader uses the value eld, which has already been
relocated, to complete the instruction. It is loaded, in locations 8384 as the two
bytes 00100000 01000000. The rst being the OpCode (=4) and the second, the
value (=64) of symbol LB.
reserved
for
array Z
00001101
00000000
00001111
00010101
00000100
00100000
00100000
LOD
15
COMP
LSHFT
CALL
84
85
86
87
88
89
90
91
92
93
94
95
96
01000000
00101100
01011001
00100000
01000000
00001101
00000000
01000111
00110000
00000001
00100100
01011001
01000000
LB
BEQ
X=17+72=89
CALL
LB
LOD
Y
HLT
constants
$
X
LB
Sec. 7.4
209
size in bits:
directive
code
index
skip
=3 bytes long
The directive code in our example will be 011. The index is as before, and the
modication code can take the four values: replace (00), add (01), subtract (10),
logical OR (11). The address eld species the byte in the current object le to
be modied, and the skip & eld elds specify the exact eld in the byte to be
modied. The case skip=4, eld=3 species eld yyy in byte xxxxyyyz. In the
example above, the special relocation of the bytes at addresses 12 & 19 can now be
achieved by the two modify directives:
011 00001 0000 1000 00 001100
Note that the instruction itself does not need to contain the index of the external symbol any more. Also the id bits can simply be 00, and there is no need
for the special relocation id of 10. The loader loads the instruction in memory
without any modication and, when it nds the modify directive later, it modies
the instruction in memory. The modify directive only needs to appear in the same
object le and should not precede the instruction to be modied.
This loader directive adds power to the assembler language. It is now possible
to write something like DC Y-LB and the assembler would produce a constant
of all zeros and two modify directives, one to replace Y (or to add it) and the
other, to subtract LB. An address expression such as Y-LB involves two relative
quantities (that can be external) and, as explained in chapter 1, is itself absolute.
However, to make it meaningful, we always require that all external symbols used
in the expression be declared (as entry) in one program. If they are declared in two
dierent programs, then the dierence between them, even though still absolute, is
totally unpredictable and therefore useless.
The modify can be used for both linking and relocation. Normally it is a waste
to use it for relocation, since relocation only requires a bit or two per instruction.
However, on a computer where the relative mode is heavily used, relatively few
instructions need relocation, and the modify may be used instead of relocation
bits.
210 Loaders
Ch. 7
LC
12
25
62
X
Y
EXTRN M,N
JMP SUB EQU
A-B+M-N
DC
A-B+M-N
8
Figure 74. The modify Directive.
As a result, a modify needs to specify only the address of the rst byte of
the instruction and a number in the range 03 (the case eld below). A possible
format is:
directive
code
index
case
modic.
code
address
12
=3 bytes
Sec. 7.5
Linkage Editors
211
commands &
names of all
object files
obj.
file
obj.
file
load
module
Reloc.
Loader
initial input
loader
main output
secondary
output
memory map &
error messages
a single
executable
module
in memory
212 Loaders
Ch. 7
Sec. 7.7
unused
user
Loader Control
unused
user
soft.
inter.
unused
unused
unused
user
user
user
soft.
inter.
OS
OS
DL
DL
soft.
inter.
OS
DL
soft.
inter.
OS
DL
213
soft.
inter.
OS
DL
Initially.
A software interrupt instruction invokes the DL in the OS area.
The DL loads and calls procedure P.
Procedure P executes and returns to the DL.
The DL restarts the program at the point following the software interrupt.
214 Loaders
Ch. 7
Normally, when the loader detects errors, it does not start execution. It is
possible to override this and instruct the loader to execute the program in the
presence of certain errors. This may make sense if certain external symbols remain
unresolved, but the programmer knows that this particular run will not use their
values.
To change the list of object les to be loaded, the loader accepts INCLUDE and
DELETE commands. The INCLUDE command has the general form
INCLUDE le name,device name
It species an object le, perhaps a library routine, to be included in the load. The
DELETE command tells the loader that a certain object le should be deleted from
the list of object les to be loaded.
The CHANGE old name,new name command instructs the loader to change, in
the GEST, the name of an external symbol. This command is used when the user
decides to change a certain routine after the program has been assembled. Imagine
a program with a CALL COS instruction, calling a routine COS in object le (or
library le) MATH. The user has decided to use another routine COS1, located in le
NEW. The following loader commands may be necessary:
DELETE MATH
INCLUDE NEW
CHANGE COS,COS1
Other loader commands are mentioned in the following sections, in connection
with library search, overlays, and other loader features.
Exercise 7.9 Library routines are in the form of object les. Can such a routine
be an absolute object le?
When such a routine is loaded, it may call other routines, which means more
library searches but no special complications for the loader. If a library search fails,
the loader issues an error message and will not execute the program.
Sec. 7.8
Library Routines
215
Since this feature is used a lot, a quick library search is important. The library
is typically a disk where each le constitues one routine, and a library search means
searching the directory of the disk, a common OS operation. Many OSs even keep
library directories permanently in memory, thereby speeding up the search. If there
is more than one library, they should all be available at load time, ready to be
accessed by the loader. Otherwise, the loader has to stop and ask for a library to
be mounted.
The command LIBRARY name instructs the loader to load all the routines in
a certain library. It is similar to the INCLUDE command mentioned above but it
explicitly tells the loader that the le to be included is a library and should only be
searched when an undened external symbols is found.
Many libraries are stored on single les. A single le includes all the routines of
a certain library and, on nding the name of a routine in this le, the loader loads
the entire le. This, of course, has the disadvantage that memory space is taken for
routines that may never be used. However, if the program eventually needs several
routines from that le, the entire load is speeded up.
Another advantage of this feature is that a programmer can override a library
routine. If the programmer decides to use a special version of SIN, he only needs
to code that version and supply it to the loader on a separate object le, with its
name declared as an entry point. This would result in a GEST where the external
symbol is dened, and the loader would not search the library.
Since a library search is initiated by an undened external symbol, one can
easily use libraries of data, not just routines. Even directives such as Z DC Y,
where Y is declared external, would cause the loading of a library routine which,
in this case, would probably be a block of data.
If a certain test run is not going to call routine ABC, the user can speed up
the loading by issuing the command NOCALL ABC. This loader command tells
the loader not to search any library for routine ABC even if ABC is an unresolved
external symbol. The NOCALL ALL command means no automatic library search
at all. This command should be used carefully since it means that any unresolved
external references would lead to a loader error.
7.9 Overlays
Many modern computers use virtual memories that make it possible to run
programs larger than the physical memory. Either one program or several programs
can be executed even if the total size is greater than the entire memory available.
When a computer does not use virtual memory, running a large program becomes a
problem. One solution is overlays (or chaining), which will be discussed here since
its implementation involves the loader.
Overlays are based on the fact that many programs can be broken into logical
parts such that only one part is needed in memory at any time. The program is
216 Loaders
Ch. 7
divided, by the programmer, into a main part (the overlay root), that resides in
memory during the entire execution, and several overlays (links or segments) that
can be called, one at a time, by the root, loaded and executed. All the links share
the same memory area whose size should be the maximum size of the links. A link
may contain one program or several programs, linked in the usual way. At any
given time, only the root and one link are active (but see the discussion of sublinks
and tree structure below). Two features are needed to implement overlays:
A directive declaring the start of each overlay. Those directives are recognized by
the assembler which, in turn, prepares a separate object le for each overlay.
A special CALL OVERLAY instruction to load an overlay (a link) at run time.
Such an instruction calls a special loader routine, the overlay manager, resident in
memory with the main program, which loads the specic overlay from the object le
into the shared memory area. The last executable instruction in the overlay must
be a return. It should return to the calling program, which is typically the main
part, but could also be another overlay. Such a return works either by popping the
return address fron the stack, or by generating a software interrupt, that transfers
control to the overlay manager in the OS.
A typical directive declaring an overlay is OVERLAY n (or LINK n) where n is
the overlay number. Each such directive directs the assembler to nish the previous
assembly, write an object le for the current overlay, and start a new assembly for
the next overlay. The END directive terminates the last link. The result is a number
of object les, the rst of which is a regular one, containing the main program. All
the rest are special, each containing a loader directive declaring it to be an overlay
and specifying the number of the overlay.
The loader receives the names of all the object les, it loads the rst one but,
upon opening the other ones, nds that they are overlays. As a result, the other
object les are not loaded but are left open, accessible to the loader. The loader
uses the maximum size of those les as the size of the shared memory area and
loads, following the main program, a routine that can locate and load an overlay.
At run time, each CALL OVERLAY[n] (or CALL LINK) instruction, invokes that
routine which loads the overlay, on top of the previous one, into the shared area.
As far as relocating the dierent overlays, there are two possibilities: The rst one
is to relocate each overlay while it is loaded. The other possibility is to prepare
a pre-relocated (absolute) version of each overlay and load the absolute versions.
This requires more load time work but speeds up loading the overlays at run time.
Generally, an overlay is a large part of the program and is not loaded many times.
In such a case, the rst alternative, of relocating the overlay each time it is loaded,
seems a better choice.
In general, each overlay may be very large, and sub-overlays can be declared.
The result is a program organized as a tree where each branch corresponds to an
overlay, each smaller branch, to a sub-overlay, etc. Figure 77 is an example of such
a tree.
The table below assumes certain sizes for the dierent links and a start address
Sec. 7.9
Overlays
217
J
Figure 77. An Overlay Tree.
of 0 for the root A. It then shows the start addresses of each link and the total size
of the program when that link is loaded.
name
A
B
E
F
G
C
D
H
J
I
size
start
total
size
1000
500
350
700
100
200
250
100
200
250
0
1000
1500
1500
1500
1000
1000
1250
1350
1250
1000
1500
1850
2200
1600
1200
1250
1350
1550
1500
Links B, C, D all start at the same address. Also E, F, G start at address 1500
and , similarly, H, I start at 1250. The combined size of all the links is 3350, but the
maximum amount of memory needed is only 2200 locations (when link D is loaded).
In keeping with tradition, we say that the root is the lowest level in the tree.
The main rule concerning the layout of links in memory is: if link X is loaded in
memory, all other links lower than X, that connect X to the root, must also be loaded
at the same time. This rule implies that, when X calls a lower link, that link should
be active. If it is not, the loader should issue an error. When X calls a link on the
same level as itself, it is always an error. When X calls a link higher than itself, it
must be exactly one level higher, and it must be a son of X in the tree.
To implement such a tree overlay structure the programmer should dene the
structure in advance, to make it possible for the loader to check and verify every
call to a link and every return. To dene the tree, the programmer must start each
overlay with an OVERLAY command specifying the name of the overlay and the name
218 Loaders
Ch. 7
of its parent. The general format is OVERLAY name, parent. In the above example
the loader sees the following commands
id pair
OVERLAY
OVERLAY
OVERLAY
OVERLAY
OVERLAY
OVERLAY
OVERLAY
OVERLAY
OVERLAY
OVERLAY
A,-B,A
E,B
F,B
G,B
C,A
D,A
H,D
J,H
I,D
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(1,1)
(1,2)
(2,3)
(3,0)
(2,4)
that identify the tree structure. Note that this identication is not unique. Switching the declarations for overlays E, F would make no dierence for the loader although, in principle, it would declare a dierent tree. The loader assigns id numbers
to the overlays based on their place in the tree. Each id is a pair (level,serial ) where
level is one greater than the level of the parent, and the serial number is unique in
the level. The id pairs are shown in the list above. To control overlay loading, the
loader maintains one table, the overlay table (OT), containing the name, id pair,
and status of each overlay. The OT is used to decide whether an overlay call is valid
and where to load the next overlay. The OT starts with just the root active:
A 0,0 a
C 1,1 -
B 1,0 D 1,2 -
E 2,0 H 2,3 -
F 2,1 J 3,0 -
G 2,2 I 2,4 -
A status of a means active and a status of - means not loaded. Suppose that a
while later overlays D, H are loaded. The OT should be updated to:
A 0,0 a
C 1,1 -
B 1,0 D 1,2 a
E 2,0 H 2,3 a
F 2,1 J 3,0 -
G 2,2 I 2,4 -
To see how the OT is used to manage overlay loading and deleting, consider the
case where overlays A, D, H are active. From gure 77 it is obvious that the only
overlay that can be loaded at this point is J. The rule in such a case is: Only the
highest-level active overlay can issue an overlay-call instruction.
Similarly, the only overlay that can be deleted, by a RET instruction, is H,
implying that only the highest-level active overlay can issue a RET. The main step
in deleting an overlay is to change its status in the OT from a to -.
One more rule is needed, to determine what overlays can be called at any time.
To understand this rule, lets examine the case where overlays A, D are active. Again,
gure 77 shows that either H or I can be called, which suggests the rule: When
overlay X, with level xl, calls Y then:
1. the level of Y must be xl+1.
2. When scanning the OT, from left to right, starting with X, Y must be found
before reaching an entry with a level xl (or before reaching the end of the table).
Sec. 7.9
Overlays
219
Exercise 7.11 The serial numbers of the overlays in the OT do not seem to be
necessary. What are they used for?
use loader
directive
main 0
(1)
80
DATA 0
DATA
(2)
24
main 80
*
(3)
30
BETA 0
BETA
(4)
45
DATA 24
DATA
(5)
75
main 110
blank
(6)
15
GAMMA 0
GAMMA
(7)
10
We have already seen that, at load time, the dierent sections are loaded in
the order MAIN, DATA, BETA, GAMMA or 1,3,6,2,5,4,7. When the loader sees the rst
use loader directive, at the beginning of the object le, it loads the rst section
220 Loaders
Ch. 7
and reads the rest of the le, looking for more uses of the same section. It nds two
more (3 & 6) and loads them. In the second pass, it starts with section DATA (2)
and reads the rest of the le to nd and load the remaining part of that section (5).
Pass 3 loads the single BETA section (4) and pass 4, the single GAMMA section (7).
This is a simple process involving one simple data structure, a list of the use
directives. In the rst pass, while loading all the sections of the rst location
counter, the loader reads all the use directives and stores them in a list. Before
each pass, the loader nds the rst remaining item on that list and thus knows what
LC to load in that pass. Each time a section is found in the object le and loaded,
the loader deletes the corresponding item from the list. When the list is empty, the
loader is nished.
The use loader directive contains the name and current value of the LC to be
used in the following section. The loader compares that information to the current
load address, which constitutes a good check of the integrity of the object le (see
review question 77).
Sec. 7.11
Bootstrap Loaders
221
Typically, the bootstrap ROM contains a very small loader that reads a xedsize record from an I/O device (the device can be specied by the operator) into
a predetermined memory area, then jumps to the start of that area. The record
should contain a small loader (the second one in the bootstrap process) which, in
turn, can load the main loader (the third one so far). The main loader is then used
to load the rest of the OS. The advantagethe xed length record can easily be
modied if the OS is updated.
222 Loaders
Ch. 7
Example: An AU (Add Upper) instruction. Ideally, the operand of this instruction should be 3 locations away from it. The next instruction should be 4 locations
away from the operand. As a result, if the AU happened to be loaded at drum
address 0036, it could be assembled into 10 0039 0043 or, in general, into 10 xx39
yy43.
The SOAP assembler, mentioned in the introduction, for the 650 computer,
had a table (stored, of course, on the drum) containing the execution times of all
the instructions, and the ideal separation between each instruction and its operand.
It would read the source lines from punched cards, eight instructions per 80-column
card, assemble each and load it on the drum with the address of its successor. It
had a 2000-digit table, maintained on 200 drum locations, specifying those drum
addresses that have already been loaded with instructions and data. If the next
instruction had to go into, say, address 0020, and the table indicated that 20 is
already occupied, the assembler would try locations 21, 22, . . . until it found an
available location on the drum. Thus SOAP was a combined assembler-loader for
the 650, a 1+1 address computer. Below is an example of a SOAP program (actually,
a section of a larger program).
Source
Label
SY001
SY002
Mnem
operand
RAU
SRT
MPY
00
STL
SRT
MPY
00
AL0
SL0
SRT
AUP
STU
LSD
0004
0000
TY005
0008
0000
TY005
8002
0008
8001
RSLT
Object
Next
instr.
SY001
0240
SY002
0012
Loc.
0001
0007
0017
0022
0023
0033
0043
0048
0049
0034
0044
0054
0064
Op opercode and
60
30
19
00
20
30
19
00
15
16
30
10
21
0002
0004
0022
0000
0028
0008
0048
0000
0028
8002
0008
8001
0003
Next
instr.
0007
0017
0023
0240
0033
0043
0049
0012
0034
0044
0054
0064
0008
Sec. 7.12
223
The rst MPY instruction multiplies (the acc) by 240 . The constant itself is stored,
as part of a NOP instruction, in the word that follows the MPY (drum location 0022).
The same thing is true of the second MPY, which uses the constant 12.
Symbol TY005 has a value of 0028 and is dened outside this section of the program. Addresses 8001, 8002 have been explained earlier.
224 Loaders
Ch. 7
D
B
E
A
F
ENTRY A,B
EXTRN C,G
EQU
1
.
-.
--.
-.
END
address expressions
a. E-F+D
b. C-E
c. A-B
d. D+G-C
Sec. 7.13
225
7.13.1 Project 71
Write a simple assembler-loader to support a 1+1 address binary architecture
similar to that of the IBM 650. Our hypothetical computer has a 16-bit accumulator
and a drum with 4 bands, each a circular track with 16 locations numbered 015.
An instruction or a number can be loaded into each location. This, of course, is a
very small drum, much smaller than the one on the 650. However, the important
dierence between the drums is not the size but the fact that the 650 was a decimal
computer, and so its drum size (2000) was an round decimal number. Ours is a
binary computer and thus our drum size (64) is an integer power of 2.
Any location on the drum has an address of the form (b,l) and the LC should
also have two parts, a band and a location, which are 2 and 4 bits long, respectively.
You should simulate the drum in a 4 16 array of 16-bit integers. The instructions
are, of course, very simple and all have the same format and size. The format is:
size:
OpCode
4
Operand
2+4
next instr.
2+4
=16 bits
The number 16 is a good choice both for a decimal and for a binary computer.
Binary computers use 16-bit words because they equal two bytes. A decimal computer uses 4-bit digits. Therefore, each drum location on such a computer is a few
digits long, 16 bits implying 4 digits.
We start with just a few instructions but, since there is room for an instruction
set of up to 16 instructions, you might want to add more instructions of your own
design.
Mnemonic Operand Next
fetch
Instr. Note
ADD
LOD
STO
COM
BPL
HLT
4
2
2
4
3
-
5
1
1
3
3
-
1
2
Notes:
1. it does not matter what COM does. It could be compare, complement, or
anything else.
2. If the acc is 0 then go to the next instruction, else go to to the instruction
whose address is in the operand fetch eld.
The operand-fetch column indicates the distance, on the drum, between an
instruction and its operand. The next intruction column indicates the distance between the operand and the next instruction. There should also be the directives END,
DC, DS. Each instruction may have, in addition to its operand, a symbol indicating
226 Loaders
Ch. 7
LOD A,X
.
.
ADD B
The LOD instruction is assembled with the address of the ADD as its successor.
Before implementing the assembler-loader, it is necessary to understand the
way our drum operates. We will consider three cases, each to be implemented
separately.
Case 1. The drum has just one head which starts at band 0, location 0 and
advances to the next address in each clock cycle. The address following (0,15) is
(1,0) which means that, when the head reaches the end of a band, it automatically
moves to the start of the next band. In such a case, when an instruction is loaded
at address (x,13) and its execution time is 4, the ideal location for its successor
is (x+1,1). If this location is occupied, the assembler should advance and test
successive locations until it nds an empty one. We will assume, for the sake of
simplicity, that our test programs are short and can never overow the drum.
Case 2. As before, the only dierence being that address (x,15) is followed by
(x,0). We assume that the head does not automatically move from band to band
and, as a result, the program has to specify a head move. This is done simply by
specifying another band as the address of the operand or of the next instruction.
Assume that an instruction at address (x,13) takes 4 units to execute. The ideal
address of the next instruction is therefore (x,1) but, if this address is occupied,
it is better to load the next instruction in address (y,1) than in address (x,2). We
assume here that it is faster for the head to move to any band y than to wait in the
same band until the drum rotates to the next location.
Case 3. Similar to the 650 computer. Our drum now has 4 heads, one for
each band, positioned such that when one is above address (0,5) the other ones
are above addresses (1,5), (2,5), (3,5). Only one head can be read at any time.
If something should be placed in location (0,5) and that location is occupied, the
assembler should try locations (1,5), (2,5), (3,5), then (0,6), etc.
Your task is to write an assembler-loader to assemble and load several test
programs into such a drum. It is interesting to note that such an assembler has no
problem with future symbols and thus does not need two passes. The assembler has
a symbol table (located, of course, on the drum) where each label is stored with its
value (a drum address). A situation such as
LOD A,X
.
.
ADD B
Sec. 7.13
227
can be easily handled even though X is a future symbol. On reading the rst line
from the source, the assembler searches the symbol table and, if it does not nd
X, assumes that X is a future symbol. Since this is an unconditional jump, the ADD
instruction is the successor of the LOD and thus the assembler determines its address
(x,y)which is also the value of Xfrom the third column of the OpCode table.
The address of the ADD is known, even though it has not been read from the source
yet. The assembler then stores X in the symbol table, which amounts to reserving
address (x,y). Later, when the ADD is read from the source, the assembler nds X in
the symbol table. This also means that, before storing any instruction in address
(x,y), the assembler should verify that the address is not reserved for any future
instruction.
A conditional branch poses another problem. In a case like
BPL
SUB
.
ADD
the assembler uses the OpCode table to nd out where to place the SUB and ADD
instructions. The relevant entry is BPL 3 5, which means that the SUB instruction
should be placed 3 locations from the BPL, and the ADD, 5 locations from the SUB.
There is no need, of course, to actually simulate the execution of the test
programs. Rather, you should gather information on the way the program is loaded.
The most important items to be evaluated are:
The number of preoccupied drum locations encountered while assembling and
loading instructions.
The total execution time lost because of instructions placed in less than ideal
drum addresses.
The same thing for operands. Note that the DC, DS directives specify constants
and arrays as part of the test program. Those should, of course, be placed on the
drum, but where? In a case such as
LOD A
.
ADD A
.
DC
5
the assembler places A on the drum when it encounters the rst usage of A (the LOD
instruction). This means that the ADD instruction will have its operand placed unfavorably and, as a result, would execute slowly. When a DS directive is encountered,
the assembler should place the array wherever it nds room on the drum. This
implies that any instruction using the array as its operand would execute slowly.
228 Loaders
Ch. 7
8. A Survey of Some
Modern Assemblers
Four modern assemblers are reviewed in this chapter, and their main features
described. They are all two-pass, sophisticated macro assemblers, and each is part
of a complete development system including a debugger, loader and compilers. They
are:
The Microsoft Macro Assembler (MASM) for the Intel 80x86 & 80x88 microprocessors.
The Borland Turbo Assembler (TASM) for the same processors.
The VAX Macro assembler.
The MPW assembler for the Macintosh computer.
They all support many directives and advanced features. In principle they work
just like the older assemblers, but in practice they dier in a number of points, the
most notable of which are:
The way expressions are evaluated. Older assemblers normally calculate an expression such as 3 + 2 4 strictly from left to right, and will end up with 20. The
assemblers described here use the normal operator precedence, and will produce 11
as the value of this expression.
Ch. 8
Sec. 8.1
231
mode. The newer PS/2 computers can run in protected mode. There are no 80288
or 80388 processors.
The 80386 has 32-bit registers, and can be used as either a 16-bit or a 32-bit
processor. It can address up to 4 gigabyte (232 ) of memory. It supports virtual
memory, multiple processes, and additional instructions to handle its new features.
It is also faster than the 80286.
The 8087, 80287 & 80387 coprocessors can all operate on oating-point, decimal, and large integers. They can perform arithmetic operations, and compute
several common functions, such as sine & logarithm. The main dierence between
them is that the 80287 can also function in the protected mode, and the 80387, in
addition, supports several new operations.
One important feature of all members of the family is the way they address
memory. They create 16-bit addresses (except the 80386, which creates 32-bit ones),
so they can directly address only 64Kbytes of memory. A larger memory has to be
divided into several 64Kbyte segments. In such a memory, a physical address has
the form segment:oset, where the segment part comes from one of the segment
registers and the oset part comes from the instruction. Addresses now have to be
mapped, i.e., the nal (physical) address has to be computed from the two parts.
The mapping details are important to the assembly language programmer, and any
assembler for this family should support directives to handle segments. It should
be noted that segments have a lot of overlap, and are thus a source of confusion.
Ref. 57 is a good source of information on 80x86 memory segmentation.
Now back to MASM. An important MASM feature is the ability to maintain
a library of pre-assembled programs. These can easily be located by the librarian,
and loaded and linked with any new program.
8.1.2 MASM options
MASM is invoked by the MASM command, which species the names of all les
involved (source, object, listing, and cross reference) and can select many options.
Following is a list of some interesting or unusual options.
Many options are available for the command line, so the /H option helps the user
by displaying them all on the screen. The result of the command masm /h ... is:
Usage:
/a Alphabetize segments
/c Generate cross-reference
/d Generate pass 1 listing
/D<sym>[=<val>] Define symbol
/e Emulate floating point instructions and IEEE format
/I<path> Search directory for include files
/l[a] Generate listing, a-list all
/M{ lxu}Preserve case of labels: l-All, x-Globals, u-Uppercase Globals
/n Suppress symbol tables in listing
/p Check for pure code
Ch. 8
A pass-1 listing can be created by the /d option. Since pass 1 does not handle
future symbols, the listing will ag those symbols as undened, in addition to other
pass 1 errors. Chapter 5 features a sample MASM listing with the pass-1 errors
shown.
A pass 1-listing can be useful for locating phase errors. Those are errors stemming from pass 1 assumptions that turn out to be wrong in pass 2. They are
discussed in Ch. 1.
The same letter, /D (upper case), is another option, used to dene symbols and
assign them values. The symbol may then aect the results of the assembly. A typical example is the command MASM /Dwidth /Dopt=5. . . . It creates the symbols
width (with a null value) and opt (with a value of 5). The source code may include
conditional assembly directives to test the values of those symbols, and assemble
the program dierently. The rst of the two tests below:
optv
optv
ifdef
page
endif
..
..
if
db
else
db
endif
width
60,130
opt LT 10
opt
10
will check symbol width and, since it is dened (on the command line), will execute
the page 60,130 directive, limiting the height of a listing page to 60 lines and its
width, to 130 characters. The second if will execute the optv db opt directive
and skip the optv db 10. This is an typical example of late binding.
The /MU option tells MASM to convert all names read from the source le to
upper case. The /ML option means MASM is to be case sensitive (names such as
segment & Segment should be considered dierent). The /MX option directs MASM
to make only public and external names case sensitive.
Sec. 8.1
233
The /V (for verbose) and /T (for terse) options control the amount of listing
produced.
The /W option restricts the assembler to report only the more serious warnings.
Warnings are issued for ambiguous, inecient, or unclear instructions that are not
illegal and can be assembled.
8.1.3 MASM listing
The listing produced by MASM contains the source and object codes, the LC
values, and the source le line numbers. The line numbers are used by the debugger
to direct the user to oending lines. In addition, certain tables can optionally be
listed, giving information about the macros, the structures and records, and the
groups and segments used in the program. In addition to that, things such as the
symbol table, pass-1 listing, and assembly statistics, can also be listed. A special
utility, CREF, can be used to generate the cross reference information. Chapter 5
contains examples of MASMs pass 1 & pass 2 listings, and a cross-reference.
8.1.4 MASM source line format
The only thing unusual about the source line format in MASM is that only
labels of instructions need to terminate with a :. Labels of directives dont require
a :. Lines are written in free format and may start in column 1 even if there is no
label.
8.1.5 MASM directives
MASM supports more than 50 directives. Some directive names must start
with a period, but there does not seem to be any consistency. A few of the more
interesting or unusual ones are described here.
The DF (Dene Far) directive allocates 6 bytes in memory. It is normally used on
the 80386 to store a pointer.
A question mark ? is used to indicate a zero-value, and the notation (?)
indicates undened value. Thus x db ?,?,? allocates 3 bytes with zeros, while
y db 3 dup(?) allocates 3 undened bytes.
The public directive declares symbols to be entry points. In most assemblers
this directive is called entry. The comm directive declares symbols to be communal
(both an external and an entry point). Such a thing is normally done when a
variable should be shared by several modules. The variable is declared communal
(typically with other such variables) in a module which then becomes an include
le. The le is included in all the other modules. An alternative is to declare such
a variable public in one source module and external in all the other ones.
The two directives if1 and if2, evaluate to true in pass 1 and pass 2, respectively
(see example in Ch. 5).
The ifdef directive evaluates to true if its operand is a dened name (if it appears
in the symbol table). A future symbol is undened in pass 1, but there are no future
Ch. 8
symbols in pass 2. Any undened symbol in pass 2 is really undened (an error).
A similar directive, .errndef, performs the same test and also generates an error
if the symbol is undened.
The equal sign = is used for redenable symbols (many assemblers use SET for
the same purpose). Thus x=0 can be followed by x=x+1. The equ directive is
used, as usual, for symbols that should be uniquely dened. Note that on MASM,
such symbols can have character strings as well as numeric, values.
The words short, near & far are directives but, since they are used dierently
from other directives, they are called operators. An instruction using the relative
mode has an oset eld for the relative address. On the 80x86 microprocessors,
such an instruction has three versionswith osets of 1-, 2-, and 4 bytescalled
short, near and far, respectively. When such an instruction refers to a future
symbol, the assembler does not know which of the 3 versions to use, and how much
room to reserve for the instruction in pass 1. The user may help the assembler by
using one of the operators above. Thus if dest is a future symbol, the instruction
jmp near dest would be assembled with a 2-byte oset. A short means an 1-byte
oset, and a fara 4-byte one.
When no operator is specied, the assembler reserves room (in pass 1) for a
near oset. If this turns out to be too much (only 1 byte is needed), the extra byte
is padded with a nop instruction. If, on the other hand, 2 bytes turn out not to
be enough (the destination is far), a phase error is generated by pass 2, and the
program has to be reassembled.
8.1.6 MASM expressions
MASM can handle expressions with many operators. Operands must be absolute, except that the binary + operator can operate on one absolute and one
relative operand, and the binary - operator can, in addition to that, be used to
subtract two relative operands (but only if they are located in the same segment).
Indexing is denoted by square brackets, and there can be several of them in
the same expression. Thus mov ah,array[bx][di] is valid.
Expressions can be shifted by the shl & shr operators. They are not the
80x86 shift instructions (even though they have the same name), and are executed
at assembly time. A typical example is mov ax,01010001b shl 3 which moves
the binary number 01010001000 into register ax.
The not, and, or & xor logical operators can be used in expressions, and are
thus executed at assembly time. Again, they should not be confused with the 80x86
instructions (which happen to have the same names).
Relational operators are allowed. The instruction mov ax,2 eq 4 moves the
value zero (false) to ax (a value of 1 corresponds to true).
A $ is used for the LC value. Thus the following is very common:
Sec. 8.1
labelA
labelB
db
db
equ
235
and equates the value of labelB with the length of the string.
The normal is operator precedence is used, and parentheses can be employed
to modify it. There is also strong typing for operands. The seemingly innocent
instruction mov ax,sors[1] can cause a warning if symbol sors is dend as a
byte (sors db 123). This is because ax is a register, and thus of type word.
The instruction will be assembled and will move the two bytes starting at sors to
register ax. This may or may not be what the user wants, and it is an example of
a confusing instruction.
8.1.7 MASM macros
As can be expected, MASM supports an extensive macro facility. Macros can
have many parameters (as many as will t on one line), both macro expansions
and macro denitions can be nested, and an extensive set of conditional assembly
directives permits recursive macros. Macros can be redened and even deleted
(with the purge directive) from the MDT, to make room for new denitions. An
interesting point is that a macro can purge itself, but the purge command must be
the last line of the macro.
In addition to macros, powerful directives such as irp can be used to create
copies of blocks of code (repeat blocks) that depend on parameters. A common
example is:
FiveInts
label
irp
db
endm
byte
p,<0,1,2,3,4>
5 dup(p)
Ch. 8
TASM is invoked with the TASM command where the user can specify certain
options. Among them is the maximum number of passes. Thus the command
TASM \m1 test invokes the assembler, limits the number of passes to 1, and supplies the name of the input le test.asm. This makes sense if fast assembly is
important, and the user knows that there will be no complex forward references.
8.2.1 Special TASM features
Below is a summary of some of the special or unusual features of TASM.
Source le format: Source lines have a free format, a backslash \ indicates that
there will be a continuation line, and a ; precedes a comment. An interesting
aspect of the source line format is the labels. A label appearing on a line by itself
must terminate with a :. The same is true for a label that labels an instruction.
However, when a directive is labeled, no : is needed. Thus the following lines are
all valid:
StartLongLoop:
add ax,dx
l: jmp done
Token
dw
This makes it somewhat easier to write the program, but makes it harder for the
assembler to recognize labels.
No special syntax is used on the source lines for the immediate mode. The int
instruction (which causes an articial interrupt) does not use the immediate mode,
so int 21h causes an interrupt to address 2116 . The mov instruction, on the other
hand, uses the immediate mode, so mov cx,100 moves the decimal constant 100
to register cx. Because of the way memory is organized into segments, there is no
absolute mode on the 80x86 microprocessors. It is possible to reach any absolute
address by selecting the segment where the address is located.
Expressions. TASM supports expressions with nested parentheses, and logical and
relational operators. There is operator precedence, and there are many operators.
Extended call. TASM has been written by Borland International, a company
known for its Turbo compilers. As a result, the TASM call instruction has been
extended to allow easy interface with Turbo Pascal and Turbo C. A call instruction
can specify any of these languages, and arguments will be passed in a consistent
manner.
Extended push and pop. These instructions have been extended to allow for more
than one operand. Thus things such as push cx dx, pop dx cx will each handle
two registers.
Predened variables. The following names can be used to get useful information about the current assembly run. ??date, ??time, ??filename, ??version.
They are self explainable.
Sec. 8.2
237
Ch. 8
Sec. 8.2
239
A similar problem exists with other instructions. The mov instruction, e.g.,
has a 4-byte version that can hold a full address, and a 2-byte version that can
hold a small constant. When TASM reads a source line such as mov bl,abc, and
nds that abc hasnt been dened yet, it assembles the mov as a 4-byte instruction,
assuming that abc will turn out to be a normal label. If abc turns out to be a
constant (e.g., abc equ 5), then the short version of mov is created, followed by
two nop instructions.
8.2.5 Conclusions:
The programmer should develop a style that minimizes forward references. Arrays and other variables should be declared at the start of the program. Also,
subprograms should precede the main program.
While the program is in the debugging stages, the programmer should allow for
one- or two passes, and be prepared for less than optimum code generated. When
the program is deemed clean of bugs, it should be assembled one more time, allowing
for more passes. The resulting code should be optimum.
8.2.6 TASM macros
TASM supports a sophisticated macro facility, including:
Conditional assembly (there are if, else, endif and exit directives, as well as
a few more, just for this purpose).
Local labels (a label dened as local in a certain macro will be assigned a local
name of the form ??0001, ??0002, etc.).
Nested macro expansions and denitions.
8.2.7 The Ideal mode
TASM was designed as a competitor to the MASM assembler. It has a very
high degree of compatibility and can assemble almost all MASM programs without
any changes. However, to successfully compete with MASM, TASM has powerful
features that are grouped under the name ideal mode. A program can use either
the MASM mode or the ideal mode and can even switch between the two by means
of the ideal and masm directives. Here are a few examples of ideal mode features:
A cleaner syntax. An example is the MASM instruction mov ax,[bx][si]
which uses bx+si as an index. In the ideal mode, the same instruction is written
mov ax,[bx+si].
Powerful operators. The high operator is usually used to select the high-order
byte of a word or a constant. In the ideal mode, it can also select the high part of
an index. Thus saying mov al,high abc moves the high-order byte of the word
at address abc to register al, whereas mov al,[high abc] moves the high-order
part of the address abc to the same register.
Simpler directive names. The MASM directive .radix, e.g., is named radix in
the ideal mode.
Ch. 8
union
db
dw
ends
?
?
The next step is to declare an actual variable of type debit by, e.g.:
joe@debt debit <?,?>. When this is done, joe@debt becomes a word in memory shared by the two variables [email protected] (a byte) and [email protected]
(a word).
The ? symbol indicates an uninitialized memory location. Thus abc dw 10
preloads the word at address abc with the constant 10, while xyz dw ? just reserves one word at xyz. An example is the directive ghk dw 20 dup (?) which
duplicates dw (?) 20 times, and results in an array ghk of 20 uninitialized words.
The align directive increments the LC to the specied power of 2. Thus align 8
will increment the LC to the nearest multiple of 8, and will insert as many nop
instructions as necessary to pad up the empty bytes created.
Sec. 8.3
241
w. Suppress warning messages. The wb option suppresses branch warning messages only.
The source line format: Labels end with a :. Labels dened by Digital Equipment Corp. (the manufacturer of the VAX computers) start with a $. A label
can also end with a ::, which declares it external. There is also an .extrn directive that serves the same purpose. Comments start with a ;. A hyphen typed
as the last non-space character before the comment indicates that there will be a
continuation line.
Special characters used by Macro are:
An equal sign = is the equate operator (EQU on most assemblers). The notation
abc==12 assigns the value 12 to abc and also declares abc an external symbol.
The at-sign @ indicates a deferred (indirect) mode; square brackets [] indicate
the index mode.
The logical AND, OR and exclusive OR operations are indicated by a & !, and
\, respectively.
The circumex ^ indicates a unary operator, and is also used as a macro delimiter
argument.
The angle brackets <> indicate argument or expression grouping.
The percent sign % indicates a macro string operator.
Examples are:
1. The expression #^M<R1,R3,R6> means an immediate operand which is a
register mask (the unary operator M) for registers 1, 3 and 6.
2. ^C^XFF. This indicates the complement of the hexadecimal value FF (or,
more precisely, the 32-bit hex value 000000FF). The result is the 32-bit hex value
FFFFFF00.
8.3.2 Data types
This is an important concept on the VAX, and has to be constantly kept
in mind when writing an assembler program. The VAX hardware supports data
types of dierent sizes. Bytes, words (2 bytes), longwords (4 bytes), quadwords (8
bytes), and octawords (16 bytes). There are also 4 dierent formats of oating point
numbers. Most instructions have special versions to handle these types (or some
of them), and the programmer has to be careful to specify the right data type in
instructions. There are, e.g., the following instructions to move data: movb, movw,
movl, movq and movo.
Ch. 8
Sec. 8.3
243
8.3.5 Macros
The VAX Macro assembler supports an extensive macro facility. Macros can
have local labels, and can use both positional and keyword arguments. There are
extensive string-manipulation functions that can handle string arguments. Nested
macros are allowed, and the following short example is a useful application of nested
macro denition:
.macro
movl
for reg,from,to,?lab
#from,rreg
.macro
incl
cmpl
blss
.endm
.endm
endfor
rreg
rreg,#to
lab
endfor
for
lab:
It can be used to implement the high-level construct for in asembler. Macro for
generates a mov instruction, declares label lab, and them denes macro endfor.
That macro incremenst the loop counter, compares it to the nal value and, if
necessary, branches back to the same label. Note the notation ?lab which means
that lab is a local label.
8.3.6 Addressing modes
The VAX computer has 24 addressing modes, and Macro uses a special notation to select them. Table 81 is a summary of all the VAX addressing modes.
Of special interest is the assembler syntax used. It ranges from simple (the register mode and relative mode have the simplest syntax) to complex (the longword
displacement deferred mode, with the syntax @L^dis(Rn) is perhaps the most complex).
Mode Syntax
Name
Ch. 8
Eective Address
Index
base?
S^#N
b[Rn]
Rn
(Rn)
-(Rn)
(Rn)+
@(Rn)+
B^D(Rn)
@B^D(Rn)
W^D(Rn)
@W^D(Rn)
L^D(Rn)
@L^D(Rn)
Short literal
Index
Register
Register deferred
Autodecrement
Autoincrement
Autoincrement deferred
Byte displacement
Byte displacement deferred
Word displacement
Word displacement deferred
Longword displacement
Longword displacement deferred
None. Operand is N
c(b+sRn)
None. Operand is in Rn
c(Rn)
RnRn-s, EA=c(Rn)
EA=c(Rn), RnRn+s
EA=c(c(Rn)), RnRn+4
c(Rn+D)
c(c(Rn+D))
c(Rn+D)
c(c(Rn+D))
c(Rn+D)
c(c(Rn+D))
N
N
N
Y
?
?
?
Y
Y
Y
Y
Y
Y
I^#N
@#A
B^A
@B^A
W^A
@W^A
L^A
@L^A
Immediate
Absolute
Byte relative
Byte relative deferred
Word relative
Word relative deferred
Longword relative
Longword relative deferred
None. Operand is N
A
A=c(PC+D)
c(A)=c(c(PC+D))
A=c(PC+D)
c(A)=c(c(PC+D))
A=c(PC+D)
c(A)=c(c(PC+D))
Y
Y
Y
Y
Y
Y
Y
Y
Notes:
The operand is the contents of the EA, except where there is no EA
N
Absolute (literal number)
c(...)
contents of mem loc. ...
b
Index mode base address
s
operand size, dependent on operation context:
s=1,2,4,8,16 according as the operation is a byte, word,. . . octaword
?
Yes, provided that Rn is not the index register
D
Displacement. If byte or word, sign is extended to a longword
A
Memory address
Table 81. Summary of the VAX Addressing Modes
Either one or several object les can be created.
Code & data modules are generated.
Templates can be dened, that are similar to Pascal records.
Sec. 8.4
245
Ch. 8
8.4.3 Segments
There is also the concept of segments. They are dierent from segments on the
80x86 microprocessors. A 680x0 program can be divided into segments that share
the same memory area. This way a large program, exceeding the entire memory
available, can run one segment at a time. The SEG directive is used to dene
segments.
Source line format. A label must either start at position 1, or be terminated
with a :. A statement without a label must start at position 2 or later. Comments
are preceded by a ;. Fields are separated by a space or tab. Many mnemonics
must specify the data type of the operands. Valid types are:
Specier Type
Size
B
W
L
S
D
S
D
X
P
Byte
Word
Long word
Short
Double long word
Single precision
Double precision
Extended
Packed BCD
8 bits
16 bits
32 bits
8-bit signed offset (in the range of 128 . . . + 127)
64 bits. For use with 68851 only
32-bit IEEE floating-point format. For 68881 only
64-bit as above
96-bit as above
96-bit packed characters for f.p. strings.
For 68881 only
Sec. 8.4
247
The MACHINE directive must come with one of the following operands: MC68000,
MC68010, MC68020, MC68030. It identies the specic 680x0 microprocessor used to
the assembler.
The STRING directive should have one of the operands ASIS, PASCAL, C. It tells
the assembler how to prepare character strings.
The BRANCH & FORWARD directives tell the assembler what size to reserve for the
displacements of certain instructions if they refer to a future symbol. The BRANCH
directive relates to branch instructions. Its operand can be one of S, B, W, L. The
FORWARD directive relates to instructions in modes 6 and 7. Its operand can be one
of W,L.
There is an OPT directive to tell the assembler what level of optimization is desired. The operand is one of ALL, NONE, NOCLR. An example of optimization is
the SUBA An,An instruction. It clears register An, but a MOV #0,An instruction is
faster. The assembler substitutes it if optimization is required. On certain models
of 680x0, the CLR An is even better but, on other models, it does not produce
identical results. The NOCLR parameter means to optimize but not to substitute
CLR ... for MOV #0,...
References
1. Barron, D. W., Assemblers and Loaders, 3rd ed., New York, N.Y.: American
Elsevier 1968.
2. Kent, W., Assembler Language Macroprogramming, ACM Computing Surveys
1,4(Dec. 1969) 183196.
3. Presser, L., and J. R. White, Linkers and Loaders, ACM Computing Surveys
4,3(Sep. 1972) 149167.
4. Wilkes, M. V., D. J. Wheeler, and S. Gill, The Preparation of Programs for an
Electronic Digital Computer. Reading, MA.: Addison-Wesley, 1951.
5. Z80ASM Ver. 1.05 from SLR Systems, Butler, PA. 1984.
6. ASMZ80 Ver 3.6C from Relational Memory Systems, San Jose, CA. 1984.
7. MOPI Ver.2.0 from Voice Operated Computer Systems, Minneapolis, Minn. 1984.
8. Wilkes, M. V., The EDSAC, MTAC 4,(1950) p. 61. Also reprinted in Randall,
B., The Origins of Digital Computers, Springer Verlag, Berlin, 1982.
9. Melcher, W. P., SHARE Assembler UASAP 3-7. SHARE distribution 564, 1958.
10. Goldnger R., The IBM Type 705 Autocoder. Proc. East Joint Comp. Conf.,
San Francisco, 1956.
11. Knuth, D. E., Von Neumanns First Computer Program, Computing Surveys
2,4(Dec. 1970) 247260.
12. IBM 7040 & 7044 Data Processing Systems, Student Text. IBM Form No. C226732.
250 References
13. Signetics 2650 Microprocessor Manual. Sunnyvale, CA.: Signetics Corp., 1977.
14. Grishman, Ralph, Assembly Language Programming for the Control Data 6000
and the Cyber 70 Series. New York, NY.: Algorithmics Press, 1974.
15. Langsam Y., M. J. Augenstein and A. M. Tenenbaum, Data Structures for
Personal Computers, Englewood Clis, NJ.: Prentice-Hall, 1985.
16. Morris, R., Scatter Storage Techniques, Comm. ACM 11,(1)p. 38, (Jan. 1968).
17. Hopgood, F. R. A., A solution to the Table Overow Problem for Hash Tables,
Comp. Bull. 11, p. 297 (1968).
18. Aho, A. V., J. E. Hopcroft and J. D. Ullman, Data Structures and Algorithms,
Reading, Mass.: Addison-Wesley, 1983.
19. Brown, P. J., A Survey of Macro Processors, Ann. Rev. in Aut. Prog. Vol. 6.
Oxford & New York, NY.: Pergamon Press, 1966, pp. 3788.
20. Campbell-Kelly, M., An Introduction to Macros, New York, NY.: American
Elsevier, 1973.
21. Cole, A. J., Macro Processors, Cambridge: Cambridge University Press, 1976.
22. McIlroy, M. D., Macro Instruction Extensions of Compiler Languages, in
Comm. ACM 3,(4), p. 214 (1960).
23. Graham, M. L., P. Z. Ingerman, An Assembly Language for Reprogramming,
Comm. ACM 8,(12) p. 769, (1965).
24. Ferguson, D. E., The Evolution of the Meta-Assembly Program, Comm. ACM
9, p. 190 (1966).
25. Freeman, D.N., Macro Language Design for System/360, IBM Sys. J. 5, (1966)6
77.
26. IBM System/360 Operating System Assembler Language, IBM Form No. GC286514.
27. IBM System/360 OS/VS and DOS/VS Assembler Language, IBM Form No.
GC33-4010.
28. Wegner, P., Programming Languages, Information Structures, and Machine Organization. New York, NY.: McGraw-Hill 1968.
29. Patterson, D. E., Reduced Instruction Set Computers, Comm. ACM 28(1)820,
Jan. 1985.
30. CDC COMPASS Version 3 Reference Manual, #60492600.
31. SUN Microsystems Assembler Language Reference Manual, part #800-1179.
32. PDP-11 Macro-11 Language Reference Manual, Order #AA-5075B-TC.
33. Gorsline, G. W., 16-Bit Modern Microcomputers, Englewood Clis, NJ.:
Prentice-Hall 1985.
34. An Introduction to ASM 86, Intel Corp., Order #121689, 1981.
35. ASM 86 Language Reference Manual, Intel Corp., Order #121703.
36. Knuth, D. E., The TEXBook, Reading, Reading, MA.: Addison-Wesley, 1984.
37. IBM PC Macro Assembler, IBM #6172234.
References
251
38. Rector, R., G. Alexy, The 8086 Book, Berkeley, CA.: Osborne/McGraw-Hill,
1980.
39. NOVA Computer Assembler Manual, Data General Corp. #093-000017.
40. Apple II Reference Manual, Apple Corp. Product #A2L0001A.
41. Donovan, J. and S. Madnick, Operating Systems, New York, NY.: McGraw-Hill,
1974.
42. The Random House Dictionary of the English Language, New York, NY.: Random House, 1970.
43. Ralston, A. (ed.), Encyclopedia of Computer Science & Engineering, Van Nostrand, 1985.
44. Barron, D. W., Assemblers, ibid, pp. 124132.
45. Brown P. J., Macroinstructions, ibid pp. 904906.
46. Barron, D. W., Loaders, ibid pp. 874876. Linkage Editors, pp. 851852.
47. Conway, M. E., UNISAP, Symbolic Assembly Program for UNIVAC I and UNIVAC II, The Computer Center,Case Institute of Technology, Cleveland, 1958.
48. Skordalakis, E., Meta-Assemblers, IEEE Micro, 3(2)616 (April 1983).
49. CDC 3170/3300/3500 Computer Systems, META/MASTER Reference Manual,
Pub. No. 60236400, Control Data Corp 1968.
50. Yackle, B. E., An Assembler For All Microprocessors, Hewlett-Packard J.,
Oct. 1980, pp. 2830.
51. Heath, J. R. and S. M. Patel, How To Write a Universal Cross-Assembler, IEEE
Micro, 1(3)4566 (Aug. 1981).
52. Mezzalana, M., et. al., DEFASM: A Microprogram Meta-Assembler With Semantic Capability, IEEE Proc. 128E(4)133142 (1981).
53. Habib, S., and X. L. Yang, The Use of a Meta-Assembler to Design an M-code
Inrterpreter on AMD2900 chips, ACM SigMicro Newsletter, 12(4)3850, 1981.
54. Gill, C. F., and M. T. Holden, On the Evolution of an Adaptive Support System,
AIAA/ NASA/ IEEE/ ACM Comp. in Aerospace Conf., Los Angeles, 1977 (AIAA
Paper No. 77-1420).
55. Holden, M. T., The B-1 Support Software System for Development and Maintenance of Operational Flight Software, Proc. NAECON 76 Conf., 1976, pp. 250262.
56. Boehm, E. M., and T. B. Steel, The SHARE 709 System, Machine Implementation and Symbolic Programming, J. ACM 6,2,134140 (Apr. 1959).
57. Norton, Peter & John Socha, Peter Nortons Assembly Language Book for the
IBM PC, New York, NY.: Brady, 1986.
58. Mealy, G. H., A Generalized Assembly System, in Rosen S., Programming Systems and Languages, New York, NY.: McGraw-Hill, 1969, pp. 535559.
59. McCarthy, J. et. al., The Linking Segment Subprogram Language and Linking
Loader, ibid pp. 572581.
252 References
References
253
A. Addressing Modes
A.1 Introduction
One of the main tasks in designing a new computer is to design its instruction
set. Machine language is often called low level which means, among other things,
that every hardware feature should have a machine instruction to control or to use
it. The instruction set of a computer thus reects its architecture. This fact has long
been recognized and even serves as the basis for dening the concept of computer
architecture [29]. Designing the instruction set of a new computer is, therefore, an
important task involving many considerations. One important design criterion is
that instructions should be short. This is important because instructions have to
be fetched from memory before they can be executed. A long instruction may have
to be stored in two or even three words in memory, and thus takes two or three
times longer to fetch than a short instruction.
The size of an instruction is determined by the size of the individual elds that
make up the instruction. Of those elds, the operand eld is by far the largest. To
get an idea of the sizes involved, lets look at typical elds in a simple machine instruction. The OpCode is typically 78 bits long, allowing for 128256 instructions.
In modern computers, the OpCode has variable size, averaging 68 bits. A register
eld is typically 36 bits long, reecting the fact that most computers have between
8 and 64 registers. (Some computers have more registers, but an instruction on
such a computer can only access a register from a certain group.) In contrast to
Sec. A.1
Introduction
255
those short elds, the operand eld should contain an address and thus could be
quite long.
Exercise A.1 How can one minimize the size of the OpCode eld?
Up until the mid 1970s, memories were expensive, and computers supported
small memories. A typical size memory in a second generation computer was 16k
32k words, and in an early third generation one, 32k64k words. Today, however,
with much lower hardware prices, modern computers can access much larger memories. Most of the early microprocessors could address 64k words and most of todays
microprocessors can address between 1M and 32M words (normally 8- or 16-bit
words). As a result, those computers must handle long addresses. Since 1M (1
mega) is dened as 1024k = 1024 210 = 220 , an address in a 1M memory is 20 bits
long. In a 32M memory, an address is 25 bits long, since 32M = 25 220 = 225 .
The 68000 microprocessor [68] generates 24-bit addresses. The 80386 microprocessor [69] generates 32-bit addresses, and can thus physically address 4 giga bytes (its
virtual address space is 64 tera bytes, = 246 ). Computers that are on the drawing
boards right now could require much longer addresses.
Lets therefore assume a range of 2024 bits for a typical address size, which
results in the following representative instruction formats:
OpCode
6-8
Reg Operand
3-6
20-24
OpCode
6-8
Reg1
3-6
Reg2 Operand
3-6
20-24
The operand eld takes up about 6070% of the total instruction size, and is thus
the main contributor to long instructions. A good instruction design should result
in much shorter operands, and this is achieved by the use of addressing modes.
An addressing mode is a function, or a rule of calculation, used to calculate the
address. In an instruction set that uses addressing modes, an instruction normally
does not contain the full address (which we will call the eective address or EA),
but rather a short number, called a displacement or an oset, and the mode. The
mode eld is 34 bits long, and the result is the following typical format:
OpCode
6-8
Reg
3-6
Mode
3-4
Displacement
8-10
The operand (the mode and displacement elds) is now 1114 bits long. It is still
the largest eld in the instruction, making up 5055% of the instruction size, but
is considerably shorter. Many instructions do not need an address operand and,
therefore, do not use a mode either. Adding a mode eld, therefore, does not
increase the size of those instructions.
There is, of course, a trade o. If an instruction uses a mode, the EA has
to be calculated before the instruction can be executed, which takes time. This
calculation is done by the hardware, though, so it is fast.
Since the mode is a function, it is possible to write: EA = fm (displacement, . . .)
which implies that the EA is calculated by applying a function f to the displacement
Append. A
(and to other arguments), and that the function itself depends on the mode m. So
for dierent modes we have dierent functions, dierent ways of calculating the
EA. We will see that those functions depend on things like the PC, index registers,
and the contents of memory locations. The notation EA = fm (displacement, . . .)
therefore illustrates the nature of addressing modes.
Before looking at specic modes, it is important to mention the second property
of addressing modes. They serve to make the machine instructions more powerful.
We will see examples where a single instruction, using a powerful mode, can do the
work of several instructions.
We will start by describing the main addressing modes, and follow by a brief
discussion of less important ones.
The ve main addressing modes, found on all modern computers and many old
ones, are: direct, relative, immediate, index, and indirect.
A.2.1 The Direct mode
When an instruction uses a small address, an address that ts in the displacement eld, there is no need for any calculations and the EA is simply the displacement. This is the direct mode. It is a simple mode and, in the form described here,
used only by absolute assemblers. A typical example is:
LC
0
19
xx
Obj. Code
.
.
..
.
.
MUL A
Opc m dis
xxx 0 19
Since the value of A is a small number, the assembler uses the direct mode
for the MUL instruction. It should again be emphasized that the assembler does not
need to know what the MUL instruction is, how it works, or even the precise meaning
of the direct mode. It follows a simple rule that says: if the value of the symbol is <
the maximum value of the displacement eld, put the symbol in the displacement
eld and set the mode eld to 0 (or whatever the code for direct mode is). This,
however, implies that the instruction cannot be relocated by the loader. Assuming
that the loader decides to load the program starting at address 1000, it would want
to relocate the displacement eld to 1019, which may be too large. This mode is
thus not useful in a relocatable assembler but, as we will see later, certain versions
of it are.
Exercise A.2 What if A is a future symbol. Does that generate a problem for the
assembler?
Sec. A.2
Examples of Modes
257
57
Obj. Code
.
.
-.
.
Opc m dis
JMP A xxx 1 -39 =19-57-1
Note that the oset is negative. A little thinking shows that this is always the case
if the operand precedes the instruction. Thus, in this mode, the oset is a signed
number. Since the sign uses one of the oset bits, the maximum value of a signed
oset is only half that of an unsigned one. An unsigned 8-bit oset, for instance,
has the range 0 . . . 255, and a signed one, the range 128 . . . + 127. The ranges are
the same size, but the maximum values arent.
The example above also shows the absolute aspect of this mode. The oset is
essentially the distance between the operand and the instruction, and this distance
does not depend on the start address of the program. We say that this mode
generates position independent code, and an instruction using it should always have
a relocation bit of 0.
A.2.3 The Immediate mode
This mode is used when the instruction needs the value of a constant, not
the contents of a memory location. An ADD instruction is sometimes written as
ADD R3,XY and adds the contents of location XY to register 3. If, however, the
programmer wants to add 67 to register 3, the same instruction can be used in the
immediate mode by saying ADD R3,#67. The number sign # is normally used
to indicate the immediate mode. Assuming that the code of this mode is 2, the
Append. A
reg
3
m
2
disp
67
The immediate quantity (the number to be used by the instruction) should be small
enough to t in the displacement eld. This mode always generates an absolute
instruction and is dierent from all the other modes because it does not use any
EA. We say that, in this mode, the operand is in the instruction.
It has been mentioned in chapter 1 that this mode is rarely found in old computers. Assemblers on those computers had to compensate for the lack of this mode
by supporting literals. Today, however, literals are less useful since all new computers support the immediate mode. In fact, some modern computers (notably
the VAX[DEC!VAX] and 68000) have several versions of this mode, for dierent
maximum sizes of the immediate operand.
A.2.4 The Index mode
This mode has been developed to simplify loops. It uses the concept of an index
register which, on some computers, is a special register, but normally can be any
general purpose register. The EA function in this mode is: EA=disp+Index. The
source instruction has to specify this mode explicitly, for example: LOD R1,0(R5).
The displacement is 0 and R5 is used as the index register. Before the loop starts,
R5 should be set to some value, most likely the beginning address of the array.
Each time through the loop, R5 should be incremented, to point to the next array
element. The entire loop may look similar to:
LOD R5,M
Load the start address of the array
LUP LOD R1,0(R5) Load the next array element into R1
.
.
INC R5
Update the index register
CMP R5,#LEN
Compare with the array size (LEN is an absolute symbol)
BLT LUP
Branch on Less Than
.
.
LEN EQU 25
LEN is the array size
ARY DS
LEN
ARY is the array itself
M
DC
ARY
Location M contains the value of symbol ARY
Exercise A.3 What instruction can be used instead of LOD R5,M above, to load
the value of ARY.
This mode can also be used a little dierently, as in LOD R1,ARY(R5) where
ARY is the start address of the array and R5 is initialized to 0. In this case, the index
register really contains the index of the current array element used. This form can
be used if the value of ARY ts in the displacement eld.
On the Intel 8086, certain instructions can use two index registers. Thus
mov ah,ary[bx][di] is valid.
Sec. A.2
Examples of Modes
259
124
.
JMP @TO
.
.
TO DC 11387
.
Obj. Code
Opc m disp
xxx 3 124
The EA is 11387 and could, in principle, be any address. Notice that the DC
directive itself generates the constant 11387 on the object le with a relocation bit
of 0. However, A directive of the form TO: DC AB would generate the value of AB
on the object le as a relocatable quantity (assuming, of course, that AB is a relative
symbol). The value of TO is called the indirect address and, in the simplest version
of the indirect mode, it has to be small enough to t in the displacement eld. This
is one reason why several versions of this mode exist (see below).
What is this mode good for? As this is a discussion of assemblers, and not of
assembly language programming, a complete answer cannot be given here. We will
just show a typical use of this mode namely, a return from a procedure. When a
procedure is called, the return address has to be saved. Most computers save the
return address in either the stack, in one of the registers, or in the rst word of the
procedure (in which case the rst executable instruction of the procedure should
be stored in the second word). If the latter method is used, a return from the
procedure is a jump to the memory location whose address is contained in the rst
word of the proceudre. This is therefore a classical, simple example of the use of
the indirect mode. If the procedure name is P, then an instruction such as JMP @P
(where @ species the indirect mode) can easily accomplish the job.
Incidentally, if the return address is saved in the stack, a special instruction,
such as RET, is necessary to return from the procedure. Such an instruction should
jump to the memory location whose address is contained in the top of the stack, and
also remove that address from the stack. Thus a RET instruction uses a combination
of the indirect and stack modes.
Other common extensions of the indirect mode combine it with either the
relative or the index modes. The JMP instruction above could be assembled as
xxx 3 99 since the value of TO relative to the JMP instruction is 124 24 1 = 99.
This discussion assumes that the size of the JMP instruction is one word and that
mode 3 is the combination indirect-relative. A combination indirect-index can also
Append. A
be used and, in fact, the 6502 microprocessor uses two such combinations, a preindexed indirect (that can only be used with index register X), and a post-indexed
one (that can only be used with index register Y). Their denitions are:
Pre-indexed indirect: EA=Mem(disp+X)
Post-indexed indirect: EA=Mem(disp)+Y
In the former version, the indexing (disp+X) is done rst and the indirect
operation (the memory read), later. In the latter version, the indirect is done rst
and the indexing (...+Y), later. Leventhal [70] illustrates the use of those modes.
The two instructions LDA ($80,X) & LDA ($80),Y are typical examples. The
$ stands for hexadecimal and the parentheses () imply the indirect mode. It is
interesting to note that, in order to keep the instructions short, there is no separate
mode eld in the 6502, and the mode is implied in the OpCode. Thus an instruction
may have several OpCodes, one for each valid mode. The two instructions above
have OpCodes of A1, B1 respectively.
A.2.6 Multilevel or cascaded indirect
This is a rare version of the basic indirect mode. It is suitable for a computer
with, e.g., 16-bit words and 15-bit addresses (or N -bit words and N 1-bit addresses). The extra bit in such a word serves as a ag. If it is 1, another level of
indirect is required. The original instruction contains an indirect address, and the
hardware examines the contents of that address. If the ag is 1, the hardware uses
the contents as another indirect address. This process continues until the hardware
gets to a memory location where the ag is zero. The contents of that location is
the EA. The HP1000 and 2100 minicomputers [79] are good examples.
Exercise A.4 How can the programmer generate both an address and a ag in a
memory word?
A.2.7 Other addressing modes
Computers use many other modes, some simpler and others, more powerful
than the basic modes described so far. We will mention a few other modes, not
trying to provide a complete list but merely to show the possibilities.
A.2.8 Zero Page mode
This is a dierent name for the direct mode described earlier. If the displacement eld is N bits long, the (unsigned) displacement can have 2N values. Memory
may be divided into pages, each 2N words long, and page zerothe rst memory
pageis special in the sense that any instruction accessing it may be assembled in
the direct mode. Hence the name zero page mode. Good examples are the 6502
microprocessor [70] and the PDP-8 minicomputer [71].
Sec. A.2
Examples of Modes
261
Append. A
Sec. A.3
Base Registers
263
Instruction
disp
Instruction
disp
Operand
PC
Operand
memory
memory
Relative
Direct
Instruction
indirect
address
disp
instruction
Operand
Operand
memory
memory
Immediate
Indirect
Instruction
disp
+
Operand
memory
registers
Indexed
Figure A1. Common Addressing Modes (part 1).
above for the relative mode will be used to illustrate base registers.
LC
0
19
57
Obj. Code
.
.
A ..
.
JMP A
Opc dis
xxx 19
The JMP instruction is assembled with a displacement of 19, which is the value of
symbol A relative to the start of the program. The loader loads the program starting
Append. A
Instruction
Instruction
indirect
address
disp
+
indirect
address
disp
+
Operand
Operand
memory
memory
registers
registers
Indirect Post-Indexed
Indirect Pre-Indexed
Instruction
Instruction
reg
reg
operand
registers
Register
address
registers
Operand
memory
Register Indirect
base
top
Instruction
disp
disp
SP
Operand
Operand
Instruction
memory
stack
Stack Relative
start of
program
Base Relative
Sec. A.3
Base Registers
LC
0
.
.
57
JMP A
.
1234 A --
265
Obj. Code
Opc dis
xxx 1234 too large!
with address 1200 and tell the assembler to assemble the JMP instruction using R4
as the base register. The assembler would then generate a displacement of 34. A
directive is necessary for this purpose and, in the case of the IBM360, the directive
is called USING. Thus USING 1200,4 would do the job. Notice that the directive
cannot load the value 1200 in register 4.
Exercise A.6 Why not?
The USING directive is just a promise. Its like promising the assembler at run
time, register 4 will contain 1200. The actual loading of R4 must be done by an
instruction executed at run time. The user has, of course, to write that instruction
in the program. Struble [78] is a good reference for 360 programming and for base
registers.
Append. A
Sec. A.5
Review Questions
267
B. Hexadecimal
Numbers
Sec. B
Hexadecimal Numbers
269
Divide the bits into groups of four, going from right to left. For our example we
get 001 1111 (the leftmost group may be shorter than 4).
Convert each group into one hex digit. Our example becomes 1F.
Note that a few zeros may have to be added to the leftmost group. Conversion
in the opposite direction is as easy.
Exercise B.1 Convert 70F16 to binary.
The hex numbering system is popular because of the easy conversion and because most computers have a word length divisible by eight, implying that the
contents of a word can be written as an even number of hex digits.
Append. B
C. Answers to Exercises
I.1: Suppressing the listing le makes sense if a listing exists from a previous assembly. Also, if a printer is not immediately available, the user may want to save
the listing le and to print it later.
1.1: In many higher-level languages a semicolon is used to indicate the end of a
statement, so it seems a good choice to indicate the end of an instruction. Also, we
will see that many other characters already have special meaning to the assembler.
1.2: The F indicates a oating-point operation, so this is a oating-point add.
1.3: The number of registers should be of the form 2k . This implies that a register
number is k bits long, and ts in a k-bit eld in the instruction. A choice of
20 registers would mean a 5-bit register numberand thus a 5-bit eld in many
instructionsbut that eld would not be fully utilized since with 5 bits it is possible
to indicate 32 registers.
1.4: To indicate an index register and a base register. Appendix A contains more
information on indexing and base addressing.
1.5: An addressing mode should be used to assemble the instruction. Addressing
modes are discussed in appendix A.
1.6: The symbol is simply undened, an error situation discussed, among other
errors, at the end of chapter 1.
Append. C
1.7: Certain characters, such as , allow for a natural division of the name
into easy-to-read components. Other characters, such as $,= make labels more
descriptive. Examples: NO OF FONTS is more readable than NoOfFonts. REG=DATA
is more descriptive than RegEqData.
1.8:
a.
type
address=0..4096; an address in a 4k memory
node=record
info: address;
next: ^node
end;
list=^node; the type list is a pointer to the beginning of such a list
b.
const
lim=500; max =1000; some suitable constants
type
list=o..lim; type list is the index of the rst list element in array house
var
house: array[1..lim] of 0..max; lists of pointers are housed here
house2: array[1..lim] of 0..lim; pointers pointing to
the next element inside each list, are housed here.
1.9: Using logical operations and, perhaps, shifts to mask the rest of the instruction.
1.10: Using either the POS or the DS 0 directives. Both are described in chapter
3.
1.11: A branch instruction. Any instruction following a branch must be forced into
position 0 of the next word, even if it is not labeled. The reason is that the only
way to execute such an instruction is to branch to it, and to make it possible to
branch to it, it must be located in position 0.
1.12: Yes, it is valid, its value is 11 and its type, absolute. It simply represents
a negative distance.
1.13: External symbols are described in chapter 3, and the way they are handled,
by the loader, in chapter 7. In the above example, the assembler calculates as much
of the expression as it can (A-B) and generates two modify loader directives (see
chapter 7), one to add the value of K and the other, to subtract L, both executed at
load time.
1.14: Address 24, because of the phrasing above . . .with the name 1 and a value
17 . . .
1.15: JMP * means jump to the current instruction. Such an instruction jumps
to itself and thus causes an innite loop. In the early microprocessors, this was a
Append. C
Answers to Exercises
273
common way to end the program, since many of those microprocessors did not have
a HLT instruction.
JMP *-* means jump to location 0. However, since the LC symbol * is relative,
the expression *-* is rel rel and is therefore absolute. The instruction would
jump to location 0 absolute, not to the rst location in the program.
Note. Knuth [84] mentions another reason for those instructions. See exercise 2
in his section 1.3.2.
1.16: Because the USE is supposed to have the name, not the value, of a LC, in its
operand eld.
1.17: Yes. The only problem is that the loader needs to be told where to start
the execution of the entire program. This, however, is specied in the END directive
(see chapter 3) and is a standard feature, used even where no multiple LCs are
supported.
1.18: It is identical to :
JMP TMP
.
.
TMP DC *
The computer will branch to the location labeled TMP but that location contains an
address, not an instruction. The likely result will be an interrupt (invalid OpCode).
1.19:
LC
0
1
3
4
6
8
10
11
13
15
17
19
Label
X
Y
Source
INP
STO 50
INP
STO 51
BZE X
ADD 50
OUT
BRA Y
LOD 50
ADD 50
STO 52
HLT
Object Code
OpCode
Op
00000111
00000010 00110010
00000111
00000010 00110011
00000100 00001101
00000011 00110010
00001000
00000110 00010001
00000001 00110010
00000011 00110010
00000010 00110100
00000000
1.20: Add either addressing modes or more registers. With 4 unused bits we can
have 2 bits specify one of 4 addressing modes, and the other 2 bits, one of 4 generalpurpose registers. Alternatively, we can use 3 bits to specify 8 modes, and the fourth
bit (if one is available) to select one of two index registers. These are special purpose
registers, used to hold an address, not an operand. The arithmetic operations would
still be performed in the Acc.
Append. C
1.21: Invalid mnemonic, invalid label (not a single letter), multiply-dened label, and invalid operand (syntactically wrong, such as STO :, undened symbol,
operand greater than maximum address).
1.22: It makes it easier for the assembler to distinguish a symbol from a constant
in the operand eld. In an instruction such as ADD R1,1A, the assembler has to
scan the entire name of the symbol 1A to verify that it is a symbol. The restriction
to a letter allows the assembler to scan an instruction such as ADD R1,A1 and,
immediately after scanning the rst character of the symbol name, decide that the
instruction uses a symbol. This simplies the lexical analysis phase of the assembler.
1.23: There is no point in writing 5000 unnecessary records on the object le. The
assembler places a special code (a loader directiveloader directives) in the object
le, instructing the loader to reserve as many locations as necessary.
1.24: In the relative mode, the Op eld is essentially the distance between the
instruction and its operand. If that distance does not t in six bits, the relative
mode cannot be used. In other words, this mode can only be used if the instruction
is not too far away from its operand. See appendix A for more details.
2.1: It depends on the characters allowed. The ASCII codes of the characters <,
=, >, ?, @ immediately precede the code of A. Similarly, the codes of [, \,
] immediately follow Z in the ASCII sequence. If those codes are used, then it is
still easy to use buckets. Given the rst character of a symbol name, we only need
to subtract from it the ASCII code of the rst of the allowed characters, say <, to
get the bucket number. If other characters are allowed, then buckets may not be a
good data structure for the symbol table.
3.1: It adds more work to the programmer and doesnt speed up the identication
by much. Even if the assembler identies a source line as a directive by the period, it
still needs to search some table to nd the start address of the routine that executes
it.
3.2: The relations e b, c > e, 0 < b, e, c 80 should hold.
3.3: To generate a backup le of the source on punched cards.
3.4: See chapter 7.
3.5: In both passes. It is executed by selecting one of four built-in conversion
routines. That routine is used until the next BASE is found.
3.6: a. Place a JMP or SKIP instruction to skip over the data.
b. Use another LC to eventually relocate the data block elsewhere.
c. Fill up the data area with instructions at run time.
3.7: a. To make BSS and BES really dierent. Compare this dierence to the
dierence between the auto-increment and the auto-decrement modes discussed in
appendix A.
Append. C
Answers to Exercises
275
b. The BES directive was useful on old computers with subtractive index registers
where most loops went backwards.
3.8: To produce a forcing upper of the next source line. This is common on computers with a large word size, such as the CDC Cyber and the Cray computers.
The concept of forcing upper is discussed in chapter 1. Also, the IBM 360 uses
halfwords, fullwords, and doublewords in memory. The directive DS 0D (reserve 0
double words) on those computers is used to force the LC to the start of the next
doubleword.
3.9: The most common use for ORG is to specify a start address for the program
in a computer without an operating system. On such a machine, the user may
select a start address and may want to load dierent programs starting at dierent
addresses. In such a case, the rst source line is an ORG and is the only ORG in the
program.
3.10: The reason is that instructions are assembled in pass 2, where all the symbols
are already in the symbol table; certain directives, however, are executed in pass 1,
where future symbols have not been found yet. Thus pass 1 directives cannot use
future symbols.
3.11: The simplest way is to add another pass. The directive A EQU B+1 can be
handled in three passes. In the rst pass, label A cannot be dened, since label B
is not yet in the symbol table. However, later in the same pass, B is found and is
stored in the symbol table. In the second pass label A can be dened and, in the
third pass, the program can be assembled. This, of course, is not a general solution,
since it is possible to nest future symbols very deep. Imagine something like:
A
B
C
EQU B
EQU C
EQU D
-
Such a program requires four passes just to collect all the symbol denitions, followed by another pass to assemble instructions. Generally one could design a percolative assembler that would perform as many passes as necessary, until no more
future symbols remain. This may be a nice theoretical concept but its practical
value is nil. Cases such as A EQU B, where B is a future symbol, are not important
and can be considered invalid.
3.12: It is executed in pass 1 since it aects the symbol table. It is executed by
evaluating and comparing the expressions in the operand eld. This is another
example of a powerful directive.
Append. C
3.13: The modify loader directive, discussed in chapter 7, can be generated by the
assembler to instruct the loader to modify any item loaded, in any way, instead of
just storing in it the value of an external symbol.
3.14: Because the BSSZ generates Data. The BSS (or DS) directive only reserves
storage, so it is one of the Block Control directives.
4.1: Either make the label a parameter, use local labels (see chapter 1), or use
automatic label generation (see Ch. 4).
4.2: Generally not. In the above example it makes no sense for the assembler to
substitute X for B since it does not know if this is what the programmer wants. However, there are two cases where double substitution (or even multiple substitution)
makes sense. The rst is where an argument is the name of a macro. The second
is the case of an assembler where parameters are identied syntactically, with an
& or some other character. The rst case is called nested macro expansion and is
discussed later in this chapter. The second case occurs in the IBM360 where parameters must start with an & and are therefore easy to identify. The 360 assembler
performs multiple substitution until no & are left in the source line.
4.3: It depends on the MDT organization and on the size of the data structures
used for binding. Typically, the maximum number of parameters ranges between a
few tens and a few hundreds.
4.4: If the last argument is null, it should be preceded by a comma. A missing last
comma indicates a missing argument.
4.5: Add another pass (pass 1?) to collect all macro denitions and store them
in the MDT. Pass 0 would now be concerned only with macro expansions.
4.6: Nested macro denition. In such a case, each expansion of certain macros
causes another macro denition to be stored in the MDT, and space in the MDT
may be exhausted very rapidly.
4.7: All three parameters are bound to the same argument and are therefore substituted by the same string.
4.8: To retain the last binding and use default values. Thus P2 is rst bound to
MAN and then to DAN. Since P3 is not bound to any argument, it should get a default
value.
4.9: The macro processor would continue scanning the source, reading and assigning more and more text to the second argument, until one of the following happens:
1. It nds a period-space combination somewhere in the source. This would terminate the scan and would bound the second parameter to a (long) argument.
2. It gets to the end of the source and realizes that something went wrong.
3. It nds a character (rather, a token) in a context thats invalid inside a macro
argument. In the case of TEX, such a token could be the start of a new paragraph.
Append. C
Answers to Exercises
277
In cases 2, 3 the macro processor would issue a run away argument error message,
and either terminate the macro expansion or give the user a chance to correct the
source le interactively.
4.10: The dierence is in scope. Attributes of macro arguments exist only while
the macro is expanded. Attributes of a symbol exist while the symbol is stored in
the symbol table.
4.11: The programmer probably meant a default value of null for parameter M2.
However, depending on the syntax rules of the assembler, this may also be considered an error.
4.12: It should be, since it alerts the reader to the fact that some of the listing is
suppressed.
4.13: Because it allows the implementation of recursive macros.
4.14: Yes, many assemblers support simple expressions in the AIF directive.
4.15: A constant, a SET symbol, or a macro argument.
4.16: No, one ENDM X is enough. It signals to the assembler the end of macro X
and all its inner denitions.
4.17: The string A[AY](AYX).
5.1: Nothing, since the assembler does not process the macro lines at denition
time.
5.2: .show me. Note that me stands for Macro Expansion. Therefore, there are
no directives such as .show you or .noshow you. (Both .show and .noshow are
described in Ch. 4.)
5.3: It nds the new denition of begin, and discovers that it is dierent from
the one already in the symbol table.
6.1: Yes. See Coulouris, Ref. [91].
6.2: Refer to the codes in the table; some valid conversions are BD, KB, DP, DK, KD.
Invalid ones are XD, HE, EB.
Also see any COBOL text, e.g. [90], for a complete list of COBOL type conversions.
6.3: If something like A[5,3] is needed, the programmer may either declare three
arrays A1[5], A2[5], A3[5], or one large array A[15]. The operation A[3,2]:=0;
can then be written either as A2[3]:=0; or A[i]:=0; where i:=3+5*(2-1);.
6.4: X is a parameter that should be in the symbol table. Its value should be a
string, such as A or S. The parameter is then replaced by its value and the result
Append. C
Append. C
Answers to Exercises
279
7.10: Undened External Symbol. The loader cannot tell if a library routine is
really missing or if the name is that of a bad external symbol.
7.11: It is possible to generalize the concept of overlay load and delete, such that
any overlay can call any other one, and there is no RETurn. Lets assume that A and
D are active and D calls B. In such a case, D and all its active descendants are deleted
automatically and B is loaded. Even more, if D calls E, then D and its descendants
are deleted as before, and both overlays B and E are loaded.
7.12: There are two ways.
1. The bootstrap ROM is a separate memory, and is copied into main memory as
the rst step in a bootstrap.
2. The boostrap ROM can be switched in and out of the address space. Lets
assume that the bootstrap ROM occupies addresses xxx-yyy in the address space.
It is possible, although probably not worth it, to have a small RAM with the same
address range, and a multiplexor that can switch either the RAM or the ROM into
the address space. When the computer is reset, the multiplexor should switch in
the ROM, ready for the next bootstrap. When the bootstrap loader is executed,
its last instruction should be to switch back the RAM, so address range xxx-yyy
could be used by application programs.
7.13: As explained in chapter 7, the assembler should maintain a table (perhaps a
packed array) with 64 boolean values, each for a drum location, identifying its state
as either free or occupied.
8.1: It directs MASM to create a pass-1 listing in addition to, not instead of, the
usual, pass-2, listing.
A.1: By designing variable-length OpCodes. Most texts on computer organization
explain the method, which is based on assigning short OpCodes to long instructions,
and long OpCodes, to short instructions. Another, intriguing, possibility is to assign
the short OpCodes to commonly used instructions. Such a method is used on the
B1700 computers and is discussed by Organick [93]. An interesting theoretical
possibility is to use the Human method [94] to determine the shortest possible
OpCodes based on the frequency of use of each instruction.
A.2: Yes. The instruction is only assembled in pass 2 and, at that time, the values
of all symbols are in the symbol table. However, in pass 1 the assembler has to
determine the size of each instruction, and that size may depend on the mode. If
the mode cannot be determined because of a future symbol, the assembler selects
the mode with the largest size.
A.3: A good choice is LOD R5,#ARY. This instruction uses the immediate mode.
A.4: A computer using cascaded indirect should have a special directive for this
purpose.
A.5: Many stack operations use the one or two elements on top of the stack as
their operands. Such an operation removes the operands from the stack and pushes
Append. C
the result into the stack. If the programmer wants to use the same operands in
the future, they should be left in the stack. An easy way to accomplish this is to
generate their copies before excecuting a stack instruction. Example:
1.
2.
3.
LOD 1
PUSH
LOD 1
PUSH
ADD
The reader should try to gure out the successive states of the stack at the 3 labeled
instructions above.
A.6: Because it is a directive, executed at assembly time. Assembler directives
cannot load registers at run time.
B.1: 70F16 = 0111 0000 11112 .
Index
Page numbers in boldface indicate the most denitive source of information about an item.
* directive, 84
= directive, 81
2650 microprocessor, 15, 210
6502 microprocessor, 190, 260, 266
disassembler for, 194
6800 microprocessor, 160, 266
68000 microprocessor, 44, 81, 255, 266
680x0 microprocessors, xii, 243, 258
68851 paged memory management unit,
243
68881 oating-point coprocessor, 243
80386 microprocessor, 255
8080 microprocessor, 266
8086 microprocessor, 237
80x86 microprocessors, xii, 2, 47, 48, 76,
85, 98, 169, 187, 191, 229, 230, 235,
246, 258
absolute mode, 236
disassembler for, 192
memory addressing, 42, 231
80x87 coprocessors, 230, 231
282
accumulator, 261
autodecrement, 262
autoincrement, 262
base relative, 262
cascaded indirect, 260, 279
current page direct, 261
direct, 256, 260, 261
immediate, 256, 257
implicit, 261, 267
implied, 261
index, 256, 258, 259
indirect, 256, 259, 260
indirect index, 259
indirect relative, 259
multilevel indirect, 260
post-index indirect, 260
pre-index indirect, 260
register, 262
register indirect, 262
relative, 256, 257, 259, 261
stack, 259, 261
stack relative, 261
zero page, 260, 261
AGO directive, 138
Aho, A., 65
AIF directive, 137, 277
Algol 60, 182, 183, 194
anonymous software designer at NCR, 10
ANOP directive, 138
APL, 4
Apple II computer, 87, 192
architecture of computers, 2, 254
ASCII, 78, 94, 179, 189, 192
ASCII directive, 93
ASCIIZ directive, 94
ASM 86 assembler, 95, 97
ASM 86 meta assembler, 187
assemble-go, 2, 3, 199, 224
assemble-go loaders, 196
assembler
denition of, 1
comment, 160
cooperation with loader, 29
cross, 11, 186, 193
directives, 3, 69
errors, 46, 159
fatal, 22, 46
general, 47
label, 46
operand, 46
Index
operation, 46
phase errors, 47
warnings, 46
expressions, 35
absolute, 35
operator precedence, 35
relative, 35
high-level, xiii, 177, 178, 180, 183186
high-level language, 177, 180
IBM360, 137
inputs & outputs of, 2
instruction format, 49
its size, 5
language, denition of, 1, 178
meta, xiii, 8, 11, 186, 189, 193
mode, 121
macro denition, 113, 121, 141143,
148, 149
macro expansion, 113, 121, 130, 142,
143
MPW, 137
one and a half passes, 30, 32
one pass, xii, 3, 11, 13, 19, 23, 24, 30,
31, 36, 38, 59, 195199
linked lists, 25
listing incomplete instructions, 25
operation of, 26
origin of name, 2
pass 0 & 1 combined, 111
percolative, 275
primitive, xi
procedures in, 40
punched card based, 74, 79
reason for studying, 2
resident, 11
source le
blank lines, 14
source instructions, 13
source line
comments, 13
elds, 13
format, 52
long, 75
special types, 177
two passes, xii, 4, 11, 13, 19, 20, 22, 23,
30, 36, 59, 195
limitations of, 238
types of, 31, 36
universal, 186
Index
assembler errors, 46, 237, 238
fatal, 22, 46
general, 47
label, 46
operand, 46
operation, 46
pass 1 errors, 232
phase errors, 47, 48, 232
undened symbol, 234
warnings, 46, 233
assembler expressions
operator precedence in, 229
assembler language, 4
assembling instructions, 18
associating macro parameters with arguments, 124
ASSUME directive, 85
attributes of macro arguments, 125
Autocoder, see IBM702705 assembler
autodecrement memory locations, 262
autodecrement mode, 262
autoincrement memory locations, 262
autoincrement mode, 45, 262
automatic jump-sizing in TASM, 237, 238
automatic label generation, 127
BABBAGE language, xiii, 178, 184, 185,
194
arrays, 185
expressions, 184
records, 185
backward search of MDT, 143, 156
bad symbols, 21
BASE directive, 77
base register, 89, 199, 262, 264, 265, 267
base relative mode, 262
BASIC, 4
BEGIN directive, 79
Bendix
G15, 221
binary numbers, 268
binary search, 60, 66
binary tree in an array, 66
binder (linker loader), 195, 224
binding, 156, 212
binding time, 212
bit operations, 31
block structure, 156, 178, 181
block structured language, 144
BNF denition, 270
283
bootstrap loader, xiii, 12, 195, 220, 224
bootstrap process, 220
bootstrap ROM, 279
Borland International, 236
Borland turbo assembler, see TASM
Bostonians (anagram), 158
BRANCH directive, 247
BSSZ directive, 94
Burroughs, 195
B1700, 279
BYTE directive, 80
C programming language, 240
Calingaert, P., 175
Cambridge University, 7
cascaded indirect mode, 96, 260, 279
CDC
6000, 76
6600, 266
7600, 76
Cyber, 15, 33, 76, 187, 275
display code, 78
early computers, 187
chained MDT, 122
chaining overlays, 215
CHANGE loader command, 214
COBOL, 178180, 277
records in, 194
CODE directive, 78
CodeView debugger, 230
COL directive, 78
Coleridge, S. T., 67
collector (linker loader), 195
COM les on the IBM PC, 32
COMM directive, 79, 108
comment, in an instruction, 14
COMMON directive, 196
Compass assembler, 187
complete binary tree, 66
compound macro arguments, 116
computer architecture, 2, 254
CON directive, 94
conditional assembly, xii, 8, 69, 121, 126,
133, 134, 137, 140, 155, 243
constant types, 94
continuation lines, 75
control section, 138, 197
Conway, M. E., 8, 37
cooperation between assembler and loader,
29
core memories, 220
284
Coulouris, G. F., 277
Coward, N., 158
CP/M, xii
Cray, 275
computers, 33
CREF, 160
cross assembler, 11, 186, 193
forward references, 193
object le, 193
source computer, 193
target computer, 193
cross reference information, 159
cross reference table, 159
CSECT directive, 91
current page direct mode, 261
DATA (or DC) directive, 94
data structures for OpCode table, 49
data types, 177, 178
DB directive, 191
DC directive, 94, 157, 160, 227, 259
DEBUG, (IBM PC debugger), 191
DEC, 241
PDP-11, 39, 81, 262, 265, 267
PDP-8, 260, 261
VAX, 2, 44, 91, 125, 126, 128, 164, 168,
241, 262, 266, 267
addressing modes, 243, 244
literal mode, 44
DEC directive, 96
decimal computer, 221
DECMIC directive, 99
DEF directive, 95
denition level counter, 141
DELETE loader command, 214
delimited string, 100
delimiting macro parameters, 125
Dellert, G. T., 109
DF directive on MASM, 233
DG Nova minicomputer, 262
direct mode, 256, 260, 261
directives, xii, 3, 8, 69, 72
(?), 233
*, 84
=, 81, 234
?, 233, 240
ABS, 77
AGO, 138
AIF, 99, 135, 137, 277
ALIGN, 198, 240
Index
ANOP, 138
ASCII, 93
ASCIIZ, 94
ASSUME, 85
BASE, 77
BCD, 95
BEGIN, 79
BES, 80
BRANCH, 247
BSS, 80
BSSZ, 94
BYTE, 80
CODE, 78
COL, 78
COMM, 79, 108
COMM (on MASM), 233
COMMENT, 14
COMMON, 196
CON, 94
CSECT, 42, 91
DATA (or DC), 94
DB, 95, 191
DC, 43, 45, 53, 94, 157, 160, 227, 259
DD, 95
DEBUG, 242
DEC, 95, 96
DECMIC, 99
DEF, 95
DEFAULT, 242
DF (on MASM), 233
DIS, 95
DISABLE, 92, 242
DQ, 95
DROP, 89, 90
DS, 37, 39, 45, 53, 80, 160, 227, 272
DT, 95
DUP, 99, 104, 108
DW, 95
ECHO, 104
EJECT, 101
ENABLE, 242
END, 74, 91, 113, 196, 199, 204, 205, 216,
273
ENDbold, 74
ENDC, 137
ENDD, 105, 108
ENDF, 245
ENDM, 110, 112, 113, 157, 245
ENDP, 245
ENDR, 245
Index
ENDS, 85
ENTRY, 32, 42, 73, 90, 93, 108, 195,
200, 201, 204, 205, 233, 246
EQU, 36, 82, 86, 134, 135, 155, 157, 160,
189, 234
ERR, 100
ERRNDEF, 234
ERRxx, 101
EVEN, 81
EXIT, 137
EXPORT, 245, 246
EXTRN, 32, 42, 73, 90, 92, 108, 195,
200, 201, 204, 205
FORWARD, 247
FUNC, 245
GROUP, 86
HEAD, 108
HERE, 103
ICTL, 74
IDENT, 73, 200, 205
IF-ELSE-ENDIF, 127, 137
IF1, 137, 169
IF2, 137, 169
IFDEF, 233
IFF, 137
IFT, 137
IIF, 129, 136
IMPORT, 245, 246
INCLUDE, 75
IRP, 108, 128, 235
LABEL, 235
LCC, 77, 108
LIMIT, 81
LIST, 102, 108, 129
listing le, 3
LIT, 96
LITORG, 43, 44, 96, 97
loader, 32, 54, 73, 77, 83, 86
modify, 95, 272, 276
LONG, 80
MACHINE, 76, 247
MACRO, 109, 110, 112, 113, 147, 157
MAIN, 245
MAX, 88
MAX and MIN, 88
MDELETE, 242
MEND, 147, 148
MICCNT, 88, 134
MICRO, 100
MIN, 88
285
NARG, 126
NCHR, 127
NOLIST, 102, 129
NOSHOW, 129, 277
OCT, 95
OCTMIC, 100
ODD, 81
OPDEF, 105, 108
OPSYN, 106108
OPT, 247
ORG, 81, 160, 213
%OUT, 103
OVERLAY, 83
p286, 76
PACKED, 97
pass 0, 113, 121, 123, 131, 134, 135, 156
pass 1, 275
PDC, 102, 160
POS, 83, 272
PPU, 76
PRINT, 129
PRINT ON/OFF, 172
PROC, 245
PSECT, 42, 91, 242
PUBLIC (on MASM), 233
PUNCH, 75
PURGDEF, 106, 108
PURGE, 235
QUAL, 78
RECORD, 97, 245
REMOVE, 123
REPRO, 75
RMT, 103
SBTTL, 102, 108
SEG, 246
SEGMENT, 85
SET, 89, 99, 134, 234
SHOW, 129, 168, 277
SPACE, 102
STOPDUP, 99, 105, 108
STRING, 247
STRUC, 98, 108
SUBTTL, 164
TITLE, 73, 102, 108, 200
UNION, 240
USE, 3941, 84, 219
USING, 89, 265
VFD, 99
WORD, 80
XREF, 103
286
directives in Macro, 242
directives in a meta assembler, 188
DIS directive, 95
DISABLE directive, 92
disassembled object code, 189
disassembled source code, 189
disassembler, xiii, 11, 189, 190, 192, 194
6502, 194
disks, 193
displacement, 255, 260262, 265
Dlevel counter, 144, 146
double substitution, 115
double-operand instruction, 13
DROP directive, 89, 90
drum memory, 7, 221
DS directive, 39, 80, 160, 227
DUP directive, 104, 108
dynamic linking, 201, 212, 224
dynamic loader (DL), 212
dynamic loading, xiii, 212
dynamic OpCode table, 108
EA, 255260, 262
EBCDIC, 78, 189
Ecclesiastes, 6
ECHO directive, 104
editing a macro, 118
EDSAC, 7
assembler, 7
memory, 7
eective address, see EA
EJECT directive, 101
Elevel counter, 146
Ellizey, R. S., 108
END directive, 74, 91, 113, 196, 199, 204,
205, 216
END loader directive, 205
end of le, 206
ENDC directive, 137
ENDD directive, 105, 108
ENDF directive, 245
ENDM directive, 110, 112, 113, 157, 245
ENDP directive, 245
ENDR directive, 245
ENTRY directive, 42, 90, 93, 108, 195,
200, 201, 204, 205, 246
ENTRY loader directive, 205
entry point, 42
forgotten declaration of, 207
Index
EQU directive, 86, 134, 135, 155, 157, 160,
189
EQU symbol, 137
ERR directive, 100
ERRxx directive, 101
EVEN directive, 81
EXE les on the IBM PC, 32
executable image, 211
executable module, 91, 200
EXIT directive, 137
EXPORT directive, 245, 246
external linker, 187
external symbols, 36, 42, 209, 215, 224,
278, 279
EXTRN directive, 42, 90, 91, 92, 108, 195,
200, 201, 204, 205
EXTRN loader directive, 205
factorial, 136
Ferguson, D. E., 8, 187
rst executable instruction, 74, 199
rst source line, 74
rst-generation computers, 221
oating point hardware, 8
oating point numbers, 241
forcing upper, 33, 83, 197, 275
Ford, H., 57
formal parameters stack, 146
format of an assembler instruction, 49
Fortran, xi, 79, 80
COMMON statement, 41, 79, 139
FORWARD directive, 247
FUNC directive, 245
function in a meta assembler, 188
future symbol problem, 18, 19
future symbols, 18, 19, 24, 31, 36, 38, 43,
45, 159, 226, 234, 275, 278, 279
GAS, see IBM7090 assembler
GEC 4000 computer, xiii, 178, 184
general loader, 195
GEST, 200, 207, 208, 211, 214, 215, 278
giga (230 ), 255
global external symbol table, see GEST
global macro parameters, 144
Goldnger, R., 8
Grishman, R., xiii
GROUP directive, 86
Harrington, A., 228, 247
hash function
properties of, 63
Index
hash table, 106
closed, 63
inserting, 63
collisions, 64
solutions, 64
hashing, 63
open, 63, 65
buckets, 65
principle of, 65
overow, 65
rehashing, 65
searching, 63
hashing algorithms, 67
HEAD directive, 108
HERE directive, 103
hexadecimal numbers, 268
high-level assembler, xiii, 12, 177
high-level assembler language, 177, 180
denition, 177
higher-level languages, xi, xii, 4, 156, 178,
271
historical development of assem. and loaders, 10
Honeywell
DDP516, 178, 183
Hopgood, F. R. A., 65
HP
1000, 95, 260
2100, 95, 260
Human method, 279
IBM, 195, 199
650 assembler (SOAP), 7, 222
7000 assembler (SCAT), 8
702705 assembler (Autocoder), 8
702705 computers, 8
704 assembler (UASAP-1), 8
704-709-7090, 8
7040 assembler (IBMAP), 14
7090 assembler (GAS), 8
7090 assembler (IBMAP), 48
7090/7094, 31, 32
360, 16, 42, 89, 91, 96, 172, 178, 181, 186,
262, 265, 266, 275, 276
assembler, 112, 125, 137, 243
macros, 140
370, 89
650, 221, 225, 226
7040, xi
7090, 266
287
AT, 230
PC, 32, 137, 169, 191, 230
PS/2, 231, 235
users organization, 8
XT, 230
IBMAP, see IBM7040 assembler
ICTL directive, 74
id bits on the object le, see identication
bits
ideal mode of TASM, 239
IDENT directive, 73, 200, 205
IDENT loader directive, 205
identication bits, 73, 87, 91, 204
IF-ELSE-ENDIF directive, 127, 137
IF1 directive, 137, 169
IF2 directive, 137, 169
IFF directive, 137
IFT directive, 137
IIF directive, 129, 136
immediate mode, 4345, 246, 256, 257
implicit mode, 261, 267
implied mode, 261
IMPORT directive, 245, 246
INCLUDE directive, 75
INCLUDE loader command, 214, 215
including a source le, 75
index deferred mode (on the PDP-11), 20
index mode, 256, 258, 259, 262
on the PDP-11, 20
index register, 8, 258, 262
indexing, 260
indirect address, 259, 260
indirect addressing, 96
indirect mode, 256, 259, 260
indirect-index mode, 259
indirect-relative mode, 259
Initial Orders, see EDSAC assembler
instruction
absolute, 29, 54
padding with NOP, 33
redene, 105
relative, 54
relocatable, 29, 32
relocating, 31
type, 30
variable size, 51, 53
instruction set
design of, 33, 254
Intel, 47, 85, 95, 229, 230, 235, 258, 266
intermediate le, 20, 96, 97, 116, 160, 175
288
interpreter, 4
interpretive languages, 4
interrupt, 224
invalid OpCode, 83
invalid labels, 21
invalid OpCode interrupt, 83
IRP directive, 108, 128
Johnson, S., 267
Joyce, J., 158
Knuth, D. E., 109, 273, 280
label, 13, 46
invalid, 21
local, 36, 37, 38, 237
name convention, 52
Lamb, V. S., 193
language
higher-level, see higher-level languages
machine, 1
machine-independent, 2
machine-oriented, 1
late binding, 212, 232
LC, 3, 17, 18, 20, 24, 25, 32, 33, 38, 39,
41, 4345, 49, 50, 53, 116, 121, 136,
159, 198, 204, 220, 224, 225, 233, 234,
238, 240, 257, 273
//, 41, 84
directives aecting it, 71
multiple, 3943, 49, 84, 85
name, 79
updating in pass 1, 20
LCC directive, 77, 108
Leventhal, L., 260
lexicographic order, 61
LIBRARY loader command, 215
library routine, 199201, 204, 207, 214,
215, 224, 278, 279
in a meta assembler, 188
library search, 214, 215
LIFO, 132, 261
LIMIT directive, 81
linear array, 61
linkage editor, 12, 195, 198, 200, 211, 212,
224
linked list, 61, 65, 159
linker, 195, 200, 224
external, 187
Index
linking, 4, 201, 212
dynamic, 201, 212, 224
run time, 201
linking editor, 211
linking loader, 12, 187, 195, 199, 224
links in overlays, 216
LIST directive, 102, 108, 129
listing le, xiii, 3, 21, 69, 159, 160, 175,
245
cross reference, 3
directives, 3
listing of macros, 116
listing of pass-1, 101, 232, 279
LIT directive, 96
literal table, 43, 44, 96, 97
literals, 43, 44, 97, 258
LITORG directive, 96, 97
load module, 199, 200, 211, 212
loader, xii, xiii, 29, 31, 195, 256
absolute, 12, 195, 196, 198, 199, 224
assemble-go, 196
bootstrap, xiii, 12, 195, 220, 224
command le, 213
commands, 213
control, 213
cooperation with assembler, 29
dynamic, 212
error messages, 200, 278, 279
errors, 214
general, 195
linker, 195
linking, 12, 187, 195, 199, 224
listing le, 200
outputs, 200
passes of, 31, 40, 204
relocating, 12, 195, 200, 211, 224
tasks, 195
loader commands
CHANGE, 214
DELETE, 214
INCLUDE, 214, 215
LIBRARY, 215
NOCALL, 215
Index
loader directives, 3, 30, 32, 54, 73, 77, 79,
82, 83, 86, 200, 201, 213, 274
END, 205
ENTRY, 205
EXTRN, 205
IDENT, 205
identifying, 204
MODIFY, 95, 205, 209, 210, 224
overlay, 216
program size, 199, 205
size of, 205
special symbols, 205, 206
start execution at, 205
types, 205
USE, 219, 220, 224
loading, 4
dynamic, 212
local labels, xii, 8, 36, 37, 38, 52, 127, 239,
245
in Macro, 242
in TASM, 237
location counter, see LC
LONG directive, 80
MACHINE directive, 76, 247
machine instruction, 3, 22, 26, 30, 36
displacement eld, 26
mode eld, 26
machine language, 1, 190, 254
machine-independent languages, 2
machine-oriented language, 1
Macintosh computer, 43, 127, 128, 137,
172, 194, 229
toolbox routines, 245
Macintosh Programmers Workshop, see
MPW
macro, xii, 69, 109, 189
actual arguments, 114
arguments
count attribute, 126
integer attribute, 126
length attribute, 126
number attribute, 126
scaling attribute, 126
type attribute, 126
associating parameters with arguments,
124
attributes of arguments, 125
binding arguments, 114
compound arguments, 116
289
denition, 109, 110, 111, 116, 155, 160
nested, 122, 130, 141, 143, 144, 146,
154, 155, 276
runaway, 113
editing, 118
expansion, 110, 111, 113, 115, 121, 155
listing, 160
nested, 130, 132, 146, 276
recursive, 134
factorial, 136
global parameters, 144
in TASM, 239
listing, 116, 160
local labels, 127
multiply-dened, 122
nested, 121, 130, 243
on IBM 360, 140
on MPW assembler, 243
parameters, 114
delimiting, 125
double substitution, 115
keyword, 243
multiple substitution, 276
positional, 243
recursive, 133, 134
removing from MDT, 123
serial number replacing parameters, 144
system, 122, 123
tokens, 118
macro assembler, 11
macro denition mode, 113, 121, 141143,
148, 149
macro denition stack, see MDS
macro denition table, see MDT
macro denition time, 147
MACRO directive, 110, 112, 113, 147, 157
macro expansion mode, 113, 121, 142, 143
macro expansion stack, see MES
macro name table, see MNT
Macro (VAX assembler), 2, 42, 81, 92,
125, 126, 128, 129, 164, 229, 240
addressing modes of, 243
data types in, 241
directives, 242
local labels in, 242
macros in, 243
source line format in, 241
special characters in, 241
Macroeconomics, 109
Macrograph, 109
290
Macronesia, 109
magnetic drum, 221
magnetic tapes, 193
MAIN directive, 245
MakeIndex, xiii
MASM, 2, 3, 48, 101, 137, 160, 169, 198,
229, 230, 231, 232, 234, 237, 239, 279
assembler options, 231
directives, 233
expressions, 234
listing, 233
macros, 235
source line format, 233
MAX and MIN directive, 88
MAX directive, 88
McIlroy, M. D., 8
MDS, 144
MDT, 5, 113, 114, 116, 118, 119, 121, 122,
123, 130132, 138, 142147, 149, 150,
153, 155, 156, 235, 276
backward search, 122, 143, 156, 230
chained, 122
compacting it, 230
deleting from, 230, 242
ecient search, 122
mega (220 ), 255
memory allocation, 195
memory map, 73, 200, 206
memory page, 261
memory protection, 205
MEND directive, 147, 148
MES, 132, 133, 138, 146, 150, 151
meta assembler, xiii, 8, 11, 186, 189, 193
adaptive, 186
ASM-86, 187
directives in, 188
function in, 188
general, 186
general adaptive, 186
generative, 186
generative restricted, 186
library routines, 188
MOPI, 187
procedure in, 188
restricted, 186
SLEUTH, 187
UTMOST, 187
MICCNT directive, 88, 134
micro, 88, 100
micro assembler, 12
Index
MICRO directive, 100
micro substitution, 100
microinstructions, 12
Microsoft, 48, 101
Microsoft macro assembler, see MASM
MIN directive, 88
mnemonic, 13, 20, 46, 189
MNT, 122, 123, 156
mode
autoincrement, 45
immediate, 4345
indirect, 96
cascaded, 96
register, 44
relative, 48
short immediate, 44
MODIFY loader directive, 205, 209, 210,
224
modules in MPW assembler, 245
MOPI meta assembler, 187
Morley, C., xiii
Morris, R., 64
MPW, 243
MPW assembler, 43, 128, 137, 172, 229,
243
diagnostic output window, 245
directives, 246
expressions, 246
listing le, 245
literals, 246
local labels, 127, 245
macros, 243
modules, 245
postx notation, 245
strings, 245
multilevel indirect mode, 260
multiple location counters, xii, 39, 4043,
49, 84, 85, 219
multiple substitution, 276
multiplexor, 279
multiply-dened labels, 21
multiply-dened macro, 122
n+1 address computers, 221
NARG directive, 126
National Physical Laboratory, xiii, 183,
221
NCHR directive, 127
NCR, 178
century, 178
Index
NEAT/3, 10, 178180
compiler, 178
constants area, 179
data records, 179
data types, 179
editing masks, 179
instructions, 180
working storage area, 179
nested expansion pointers stack, 146
nested macro denition, 122, 130, 141,
143, 144, 146, 154, 155, 276
nested macro expansion, 130, 132, 146,
276
nested macros, 121, 130
new source le, 121, 131, 136, 138, 151,
153, 154, 160
NOCALL loader command, 215
NOLIST directive, 102, 129
NOSHOW directive, 129, 277
null string, 123
Nutt, Roy, 8
Oak Ridge National Labs, xiii
object code, 30, 69
disassembled, 189
listing, 164
object le, 3, 17, 21, 30, 43, 54, 200, 220,
245
absolute, 19, 23, 26, 28, 30, 77, 195, 198,
199, 211, 212, 214
id bits, 73, 87
integrity of, 220, 224
relocatable, 19, 23, 28, 2932, 73, 195,
199, 201
object module, 199
OCTMIC directive, 100
ODD directive, 81
oset, 48, 85, 255, 257
one pass assembler, see assembler, one pass
one pass assembly-load, 196
one pass linking loader, xiii
one-and-a-half pass assembler, 8, 30, 32
OpCode, 16
variable size, 279
OpCode table, 16, 20, 69, 106108, 123,
189, 227
data structures for, 49
dynamic, 108
OPDEF directive, 105, 108
operating system, 122, 195, 204
291
operator precedence, 235
OPSYN directive, 106108
OPT directive, 247
ORG directive, 81, 160, 213
Organick, E., 279
OT, 218, 224
%OUT directive, 103
overlay, xiii, 215, 216, 218, 224, 278, 279
call instruction, 218
chaining, 215
links, 216
root, 216
sublinks, 216
tree, 217
OVERLAY directive, 83
overlay loader directive, 216
overlay table, see OT
p286 directive, 76
PACKED directive, 97
page zero, 260
pages, 200, 265, 278
parameters in macros, 114
parameters in subroutines, 114
Pascal, 98, 185, 244
record, 108
pass -1, 276
pass 0, 35, 110, 115, 116, 121, 122, 123,
127, 130, 132, 134136, 138, 144, 155,
160, 243, 276
directives, 113, 121, 123, 131, 132, 134,
135, 156
summary of, 154
pass 1, 20, 36, 47, 115, 116, 121, 134, 136,
138, 154, 155, 160, 168, 204, 205, 219,
234, 243, 245, 275, 278, 279
directives, 275
errors, 232
instruction size, 20
listing, 3, 101, 232, 279
local labels in, 37
operation of, 20
pass 2, 20, 36, 47, 116, 134, 138, 159, 160,
168, 169, 175, 204206, 219, 278, 279
PC, 45, 256, 257, 261
PC DOS, 191
PDC directive, 102, 160
percolative assembler, 275
peripheral processor (PPU), 76
phase errors, 47, 48, 169, 170, 232
292
PL360, 178, 181, 183, 186, 194
compound statements, 182
expressions, 182
functions and procedures, 182
goto statement, 181
machine instructions, 182
programs, 182
scope of variables, 182
variables, 182
PL516, xiii, 178, 183
POS directive, 83
position counter, 33, 83
position independent code, 199, 257
post-index indirect mode, 260
postx notation in MPW assembler, 245
PPU directive, 76
pre-index indirect mode, 260
PRINT directive, 129
PRINT ON/OFF directive, 172
problem-oriented languages, 2
PROC directive, 245
procedure, 109, 157, 181, 259
external, 4
in a meta assembler, 188
in assembler language, 40
program name, 200, 207
PSECT directive, 42, 91
pseudo instructions, see directives
PUNCH directive, 75
punched card, 74, 79, 222
punched paper tape, 193
PURGDEF directive, 106, 108
QUAL directive, 78
reason for studying assemblers, 2
RECORD directive, 97, 245
record in COBOL, 194
recursive macro, 133, 134, 136, 277
redene machine instructions, 105
register indirect mode, 262
register mode, 44, 262
relationals, 137
relative mode, 48, 209, 256, 257, 259, 261
displacement size in Macro, 242
relative symbol, 36
relocatable constants, 54
relocatable instructions, 54, 211
relocatable object le, 8, 12, 29, 30, 73,
199, 201
Index
relocatable symbol, 53
relocating instructions, 30
relocating loader, 12, 195, 200, 201, 211,
212, 224
relocation, 4, 201, 265
relocation bit, 30, 36, 53, 73, 87, 92, 200,
257, 259
used as id, 32
relocation term, 206, 208, 211
REMOVE directive, 123
removing a macro from the MDT, 123,
230, 235, 242
Renwick, W., 7
repeat-duplicate (IRP), 128
REPRO directive, 75
reprogramming, 109
resident assembler, 11
return address, 259
Revesz, G., 144, 146
RMT directive, 103
routine library, 2
run away argument, 277
run away denition, 113
run time linking, 201
SBTTL directive, 102, 108
SCAT, see IBM7000 assembler
scope of parameters, 156
scope of variables, 178
sections of a program, 40
SEG directive, 246
segment, 85
address, 85
grouping, 86
registers, 86
SEGMENT directive, 85
segments, 200, 216, 265, 278
on the 80x86, 47
semiconductor memories, 220
separate assembly, 74, 90, 197
sequence symbols, 121, 137, 138, 156
serial number replacing macro parameters,
144
SET directive, 89, 134
SET symbol, 121, 134, 135, 137, 138, 156,
243
SHARE, the IBM users organization, 8
short immediate mode, 44
SHOW directive, 129, 168, 277
Signetics, 15, 210
size of an instruction, 20
Index
Skordalakis, E., 187
SLEUTH meta assembler, 187
SOAP, see IBM650 assembler
software interrupt, 212, 216
source code, disassembled, 189
source le, 3, 13, 75, 111, 142, 199
backup, 274
source line, 3, 13
format of, 52
SPACE directive, 102
special entries, 92
special relocation, 32, 54, 73, 200, 204
special symbol information, 206, 207
special symbol table, see SST
special symbols, 205
SST, 92, 93, 200, 206, 207
stack, 259
stack mode, 259, 261
stack operations, 279
stack pointer, 261, 267
stack relative mode, 261
start address in END directive, 205
STOPDUP directive, 105, 108
STRING directive, 247
strings, 99
Struble, G. W., 265
STRUC directive, 98, 108
students, of the author, xii
sub-overlays, 216
sub-title in listing le, 160
sublinks in overlays, 216
subroutine, 109
call, 110
parameters, 114
substring, 100
subtitle in listing le, 164
subtractive index registers, 275
SUBTTL directive, 164
Swanson, E., 253
symbol
absolute, 36, 53, 138
attributes, 45, 159
EQU, 137
external, 36, 42, 91, 209, 215, 224, 278,
279
future, 18, 24, 31, 36, 38, 43, 45, 226,
234, 278
in an instruction, 13
length attribute, 45
relative, 36, 53
293
sequence, 137
SET, 121, 134, 135, 137, 138, 156, 243
special, 205
type, 87
value of, 159
symbol denition, 18
symbol table, xii, 2, 5, 17, 18, 21, 24, 25,
32, 38, 43, 45, 59, 69, 93, 113, 116, 121,
136, 137, 159, 199, 205, 226, 227, 275,
277
binary search tree, 59, 62
inserting, 62
global, 245
hash table, 59, 63
linear array, 59, 60
inserting, 60
searching, 60
linked lists, 59, 61
inserting, 61
searching, 61
local, 245
macro, 245
organization of, 59
sorted, 59, 60
special entries, 92
temporary, 121, 134, 136
system macros, 122, 123
TASM, 198, 229, 235, 236, 237, 239
automatic jump-sizing in, 237
compatibility with MASM, 239
directives, 240
expressions in, 236
extended push and pop, 236
extended subroutine call in, 236
forward references in, 238
ideal mode of, 239
local labels in, 237
macros in, 239
predened variables of, 236
source lines format, 236
unusual features of, 236
teletype terminals, 193
template, 97
temporary symbol table, 134, 136
tera (240 ), 255
ternary numbers, 270
TEX, 125
nested macro denitions, 154
TITLE directive, 73, 102, 108, 200
title in listing le, 160
294 Index
tokens (in macros), 118
Tolliver, J., xiii
tree data structure, 194
tree overlay, 217
tree structure, 216, 218
turbo assembler, see TASM
turbo C, 236
turbo debugger, 235
turbo Pascal, 236
two pass assembler, see assembler, two
passes
twos complement, 267
type of a symbol, 87
typed variables, 181
UASAP-1, see IBM704 assembler
unconditional jump, 267
UNISAP, see UNIVAC assembler
United Aircraft Corp., 8
UNIVAC, 187, 195
1107, 187
I & II assembler (UNISAP), 8, 32, 37
UNIVAC III, 187
universal assembler, 186
unresolved references, 13, 18, 49, 57
USASI, 179
USE directive, 84, 219