Professional Documents
Culture Documents
SP2.3 Assembler-Machine-Independent Assembler Features
SP2.3 Assembler-Machine-Independent Assembler Features
Tzong-Jye Liu
Department of Information Engineering and Computer Science
Feng Chia University
tjliu@fcu.edu.tw
Outline
n Literals
n Symbol Defining Statement
n Expressions
n Program Blocks
n Control Sections and Program Linking
System Programming 2
Literals
n Consider the following example
:
LDA FIVE
:
FIVE WORD 5
:
n It is convenient to write the value of a constant operand as a
part of instruction
:
LDA =X’05’
:
System Programming 3
Literals
System Programming 4
Literals vs. Immediate Operands
n Literals
n The assembler generates the specified value as a
constant at some other memory location
45 001A ENDFIL LDA =C’EOF’ 032010
n Immediate Operands
n The operand value is assembled as part of the machine
instruction
55 0020 LDA #3 010003
Literal Pools
n Normally literals are placed into a pool at the
end of the program
n see Fig. 2.10 (END statement)
n In some cases, it is desirable to place literals
into a pool at some other location in the object
program
n Assembler directive LTORG
n When the assembler encounters a LTORG
statement, it creates a literal pool
(containing all literal operands used since
previous LTORG)
n Reason: keep the literal operand close to the instruction
n Otherwise PC-relative addressing may not be
allowed
System Programming 6
Duplicate literals
n The same literal used more than once in the program
n Only one copy of the specified value needs to be stored
n For example, =X’05’ in Figure 2.10 (pp. 68)
n How to recognize the duplicate literals
n Compare the character strings defining them
n Easier to implement, but has potential problem
(see next)
n Ex: =X’05’
n Compare the generated data value
n Better, but will increase the complexity of the
assembler
n Ex: =C’EOF’ and =X’454F46’
System Programming 7
n LITTAB
n Content
n Literal name
n Operand value and length
n Address
n LITTAB is often organized as a hash table, using the
literal name or value as the key
System Programming 9
Implementation of Literal
n Pass 1
n Build LITTAB with literal name, operand value and
length, leaving the address unassigned
n When LTORG or END statement is encountered, assign
an address to each literal not yet assigned an address
n The location counter is updated to reflect
the number of bytes occupied by each
literal
n Pass 2
n Search LITTAB for each literal operand encountered
n Generate data values using BYTE or WORD statements
n Generate modification record for literals that represent
an address in the program
System Programming 10
Example: (pp. 67, Figure 2.9)
SYMTAB & LITTAB
SYMTAB LITTAB
System Programming 11
Symbol-Defining Statements
n Assembler directive EQU
n Allows the programmer to define symbols and specify their values
Ex: symbol EQU value
n To improve the program readability, avoid using the magic
numbers, make it easier to find and change constant values
n Replace
+LDT #4096
n with
MAXLEN EQU 4096
+LDT #MAXLEN
n Define mnemonic names for registers
n A EQU 0
n X EQU 1
n Expression
n MAXLEN EQU BUFEND-BUFFER
System Programming 12
Symbol-Defining Statements
n Assembler directive ORG
n Allow the assembler to reset the PC to values
Ex: ORG value
n When ORG is encountered, the assembler resets its
LOCCTR to the specified value
n ORG will affect the values of all labels defined until the
next ORG
n If the previous value of LOCCTR can be automatically
remembered, we can return to the normal use of
LOCCTR by simply write
ORG
System Programming 13
Forward-Reference Problem
System Programming 16
Expression
n The assemblers allow “the use of expressions as
operand”
n The assembler calculates the expressions and products a single
operand address or value
n Expressions consist of
n Operator
n +,-,*,/ (division is usually defined to produce an
integer result)
n Individual terms
n Constants
n User-defined symbols
n Special terms, e.g., *, the current value of LOCCTR
n Examples
n MAXLEN EQU BUFEND-BUFFER
n STAB RESB (6+3+2)*MAXENTRIES
System Programming 17
System Programming 19
System Programming 20
Program Blocks
n Allow the generated machine instructions and
data to appear in the object program in a
different order
n Separating blocks for storing code, data, stack, and
larger data block
n Program blocks v.s. Control sections
n Program blocks
n Segments of code that are rearranged
within a single object program unit
n Control sections
n Segments of code that are translated into
independent object program units
System Programming 21
Program Blocks
System Programming 22
Program Blocks
n Assembler rearrange these segments to gather
together the pieces of each block and assign address
n Separate the program into blocks in a particular order
n Large buffer area is moved to the end of the object program
n Program readability is better if data areas are placed in the source
program close to the statements that reference them.
n Example: pp, 81, Figure 2.12
n Three blocks are used
n default: executable instructions
CDATA n CDATA: all data areas that are less in length
n CBLKS: all data areas that consists of larger
CBLKS
blocks of memory
System Programming 23
CDATA block
CBLKS block
System Programming 24
Example: pp. 81, Figure 2.12
(default) block
CDATA block
System Programming 25
(default) block
CDATA block
System Programming 26
Rearrange Codes into Program Blocks
n Pass 1
n A separate location counter for each program block
n Save and restore LOCCTR when switch between blocks
n At the beginning of a block, LOCCTR is set to 0
n Assign each label an address relative to the start of the block
n Store the block name or number in the SYMTAB along with the
assigned relative address of the label
n Indicate the block length as the latest value of LOCCTR for each block
at the end of Pass1
n Assign to each block a starting address in the object program by
concatenating the program blocks in a particular order
Block name Block number Address Length
(default) 0 0000 0066
CDATA 1 0066 000B
CBLKS 2 0071 1000
System Programming 27
n Pass 2
n Calculate the address for each symbol relative to the
start of the object program by adding
n The location of the symbol relative to the
start of its block
n The starting address of this block
System Programming 28
Example of Address Calculation
20 0006 0 LDA LENGTH 032060
n The value of the operand (LENGTH)
n Address 0003 relative to Block 1 (CDATA)
n Address 0003+0066=0069 relative to program
n When this instruction is executed
n PC = 0000 (starting addr. Of default block) + 0009
n disp = 0069 – 0009 = 0060
n op nixbpe disp
000000 110010 060 => 032060
SYMTAB
label name block num addr. Flag
LENGTH 1 0003
…. …. …. ….
System Programming 29
0027
70 0024 Default(2) Default(2)
95 0000
Not present CDATA(1)
in object 004D
100 0003 CDATA(2) Default(3)
program 105 0000
CBLKS(1) Default(3)
125 0027 0066
Default(2) CDATA(1)
006C
CDATA(3) CDATA(2)
006D
180 0047 CDATA(3)
185 0006 0071
CDATA(2) CBLKS(1)
210 004D
Default(3)
245 0063
253 0007
CDATA(3)
1071
System Programming 30
Object Program
Default(1)
Default(2)
CDATA(2)
Default(3)
CDATA(3)
System Programming 31
System Programming 32
External Definition and Reference
n Instructions in one control section may need to refer to
instructions or data located in another section
n External definition
EXTDEF name [, name]
n EXTDEF names symbols that are defined in this control section
and may be used by other sections
n Ex: EXTDEF BUFFER, BUFEND, LENGTH
n External reference
EXTREF name [,name]
n EXTREF names symbols that are used in this control section and
are defined elsewhere
n Ex: EXTREF RDREC, WRREC
n To reference an external symbol, extended format instruction is
needed (why?)
System Programming 33
System Programming 34
Example: pp. 86, Figure 2.15
Implicitly defined as an external symbol
second control section
System Programming 35
System Programming 36
External Reference Handling
n Case 1 (P.87)
15 0003 CLOOP +JSUB RDREC 4B100000
n The operand RDREC is an external reference.
n The assembler
n has no idea where RDREC is
n inserts an address of zero
n can only use extended format to provide
enough room (that is, relative addressing
for external reference is invalid)
n The assembler generates information for each external
reference that will allow the loader to perform the
required linking
System Programming 37
n Case 3
n 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
System Programming 38
Object Code of Figure 2.15
Case 1
System Programming 39
Case 2
System Programming 40
Object Code of Figure 2.15
System Programming 41
System Programming 42
Records for Object Program
n Modification record
n Col. 1 M
n Col. 2-7 Starting address of the field to be modified (hexadecimal)
n Col. 8-9 Length of the field to be modified, in half-bytes (hexadecimal)
n Col.11-16 External symbol whose value is to be added to or subtracted
from the indicated field
n Control section name is automatically an external symbol, i.e.
it is available for use in Modification records.
System Programming 43
System Programming 44
Object Program of Figure 2.15
RDREC
BUFEND - BUFFER
WRREC
System Programming 45
Expressions in
Multiple Control Sections
n Extended restriction
n Both terms in each pair of an expression must be within the same
control section
n Legal: BUFEND-BUFFER
n Illegal: RDREC-COPY
n How to enforce this restriction
n When an expression involves external references, the assembler
cannot determine whether or not the expression is legal.
n The assembler evaluates all of the terms it can, combines these to
form an initial expression value, and generates Modification
records.
n The loader checks the expression for errors and finishes the
evaluation.
System Programming 46