Document
Document
Immediate Operands
Allowed:
ALPHA RESW 1
BETA EQU ALPHA
Not Allowed:
BETA EQU ALPHA
ALPHA RESW 1
ORG ALPHA
BYTE1 RESB 1
BYTE2 RESB 1
BYTE3 RESB 1
ORG
ALPHA RESB 1
⚫ The assembler would not know what value to assign to the location
counter in response to the first ORG statement. As a result, the
symbols BYTE1, BYTE2, BYTE3 could not be assigned addresses
during pass 1
3.1.3 Expressions
⚫ The assemblers allow “the use of expressions as operand”
⚫ The assembler evaluates the expressions and produces a single
operand address or value.
⚫ Expressions consist of Operators+,-,*,/ (division is usually
defined to produce an integer result)
⚫ Individual terms in the expressions may be
o Constants
o User-defined symbols
o Special terms, e.g., *, the current value of LOCCTR
Example:
MAXLEN EQU BUFEND-BUFFER
Relocation Problem in Expressions
⚫ Values of terms can be
o Absolute (independent of program location)
constants
o Relative (to the beginning of the program)
Labels on instructions and data areas
References to the location counter value
⚫ Expressions can be
Absolute
Contains only absolute terms.
MAXLEN EQU 1000
Absolute expressions may contain relative terms in pairs with opposite
signs for each pair.
MAXLEN EQU BUFEND-BUFFER
None of the relative terms may enter into a multiplication or division
operation
Relative
All the relative terms except one can be paired as described in
“absolute”.
The remaining unpaired relative term must have a positive sign.
STAB EQU OPTAB + (BUFEND – BUFFER)
None of the relative terms may enter into a multiplication or division
operation
⚫ Expressions that do not meet the conditions of either “absolute” or
“relative” expressions should be flagged as errors.
o BUFEND + BUFFER
o 100 – BUFFER
⚫ A relative term or expression represents some value that may be written
as s+r where s is the starting address of the program and r is the value
of the term or expression relative to the starting address
⚫ When relative terms are paired with opposite signs, the dependency on
the program starting address is canceled out. The result will be an
absolute value
MAXLEN EQU BUFEND-BUFFER
⚫ BUFEND and BUFFER are relative terms representing an address
within the program. But the expression represents an absolute value that
is the difference between the two addresses which is the length of the
buffer area in bytes.
Handling Relative Symbols in SYMTAB
⚫ To determine the type of an expression, we must keep track of the
types of all symbols defined in the program.
⚫ We need a “flag” in the SYMTAB for indication.
3.1.4 Program blocks
❑ In the previous examples the source program as a whole were
handled by the assembler as a single entity resulting in a single
block of object program
31
Example: pp. 81, Figure 2.12
(default) block
004D 0 USE
004D 0 WRREC CLEAR X B410
004F 0 LDT LENGTH 772017
0052 0 WLOOP TD =X’05’ E3201B
0055 0 JEQ WLOOP 332FFA
0058 0 LDCH BUFFER,X 53A016
005B 0 WD =X’05’ DF2012
005E 0 TIXR T B850
0060 0 JLT WLOOP 3B2FEF
k
0063 0 RSUB
LTOR 4F0000
0007
0007 11 * GUSE CDATA 454F4
CDATA bloc
000 1 * =C’EOF 6
A FIRS 05
=X’05 T
’
END
32
■ Pass 1
■ A separate location counter for each program block
■ Save and restore LOCCTR when switching between
blocks
■ At the beginning of a block, LOCCTR is set to 0.
SYMTAB
label name block num addr. Flag
LENGTH 1 0003
…. …. …. ….
37
■ It is not necessary to physically rearrange the generated code in
the object program
■ The assembler just simply insert the proper load address in each
Text record.
■ The loader will load these codes into correct place
Advantages
⚫ The separation of the program into blocks has considerably reduced the
addressing problems.
⚫ Large buffer area is moved to the end of the object program hence no
need to use extended format instructions.
⚫ Base register is no longer needed.
⚫ Problem of placement of literals solved. An LTORG statement is
included in the CDATA block to be sure that the literals are placed
ahead of any large data areas.
■ Case 3
■ On line 107, BUFEND and BUFFER are defined in the same control
section and the expression can be calculated immediately.
107 1000 MAXLEN EQU BUFEND-BUFFER
50
⚫ The assembler must remember(via entries in SYMTAB) in which
control section a symbol is defined.
⚫ Any attempt to refer to a symbol in another control section must be
flagged as an error unless the symbol is identified(using
EXTREF)as an external reference.
⚫ The assembler must allow the same symbol to be used in different
control sections.
Eg: MAXLEN
⚫ The assembler must include information in the object program that
will cause the loader to insert the proper values where they required.
⚫ 2 new record types are used for this in the object program.
■ Extended restriction
■ If relative terms are used, both the terms in each pair of an
expression must be within the same control section
■ Legal: BUFEND-BUFFER
■ Illegal: RDREC-COPY
■ How to enforce this restriction
■ When an expression involves external references, the assembler
cannot determine whether or not the expression is legal.
■ The assembler evaluates all of the terms it can, combines these to
form an initial expression value, and generates Modification
records.
■ The loader checks the expression for errors and finishes the
evaluation.
61
Assembler design options
■ One-pass assemblers
■ Multi-pass assemblers
One-Pass Assemblers
⚫ They are used when it is necessary or desirable to avoid a second pass
over the source program.
Problem
⚫ Trying to assemble a program in one pass involves forward references.
Remedy
⚫ Define all the forward reference areas before they are referenced .i.e.
placing all such data areas in the start of the source program.
5
Object Code in Memory and
SYMTAB Figure 2.19(a), pp.95
After scanning line 40 of the program in Fig. 2.18
7
Object Code in Memory and
SYMTAB Figure 2.19(b), pp.96
After scanning line 160 of the program in Fig. 2.18
8
Second case:
⚫ One pass assemblers that produce object programs follow a slightly
different procedure from the previous procedure.
⚫ If the operand contains an undefined symbol, use 0 as the address and
write the Text record to the object program.
⚫ Forward references are entered into lists as in the load-and-go
assembler.
⚫ When the definition of a symbol is encountered, the assembler
generates another Text record with the correct operand address of each
entry in the reference list.
⚫ When loaded, the incorrect address 0 will be updated by the latter Text
record containing the symbol definition.
Object code generated by
one-pass assembler Figure
2.18, pp.97
10
Multi-Pass Assemblers
⚫ For a two pass assembler, forward references in symbol definition
are not allowed:
ALPHA EQU BETA
BETA EQU DELTA
DELTA RESW 1
⚫ The symbol BETA cannot be assigned a value when it is
encountered during Pass1 because DELTA has not yet been
defined.
⚫ Hence ALPHA cannot be evaluated during Pass 2.
⚫ Any assembler that makes only two sequential passes over the
source program cannot resolve such a sequence of definitions.
⚫ Prohibiting forward references in symbol definition is not a serious
inconvenience.
⚫ Forward references tend to create difficulty for a person reading the
program.
⚫ The general solution for forward references is a multi-pass
assembler that can make as many passes as are needed to process
the definitions of symbols.
⚫ It is not necessary for such an assembler to make more than 2 passes
over the entire program.
⚫ The portions of the program that involve forward references in
symbol definition are saved during Pass 1.
⚫ Additional passes through these stored definitions are made as the
assembly progresses.
⚫ This process is followed by a normal Pass 2.
Implementation
⚫ For a forward reference in symbol definition, we store in the
SYMTAB:
o The symbol name
o The defining expression
o The number of undefined symbols in the defining expression
⚫ The undefined symbol (marked with a flag *) associated with a list
of symbols depend on this undefined symbol.
⚫ When a symbol is defined, we can recursively evaluate the symbol
expressions depending on the newly defined symbol.
Multi-pass assembler example
Figure 2.21, pp. 99-101
# of undefined symbols in the
defining expression
The defining expression
Depending list
Undefined symbol
13
Multi-pass assembler example
Figure 2.21, pp. 99-101
14
Multi-pass assembler example
Figure 2.21, pp. 99-101
15