Module III
Assembler Design Options
2
RDREC
3
WRREC
4
215 1062 WLOOP TD =X’05’
E32011
Specifies a 1-byte literal with the hexadecimal value 05
The difference between literal operands and
immediate operands
=, #
Immediate addressing, the operand value is
assembled as part of the machine instruction, no
memory reference.
With a literal, the assembler generates the specified
value as a constant at some other memory location.
The address of this generated constant is used as the
TA for the machine instruction, using PC-relative or
base-relative addressing with memory reference.
Literal pools
At the end of the program.
Assembler directive LTORG, it creates a literal pool
that contains all of the literal operands used since
the previous LTORG
Symbol-Defining Statements
Allow the programmer to define symbols and specify
their values.
Assembler directive EQU.
Improved readability in place of numeric values.
+LDT #4096
MAXLEN EQU BUFEND-BUFFER (4096)
+LDT #MAXLEN
Use EQU in defining mnemonic names for registers.
Registers A, X, L can be used by numbers 0, 1, 2.
RMO 0, 1
RMO A, X
21
Symbol-Defining Statements
The standard names reflect the usage of the
registers.
BASE EQU R1
COUNT EQU R2
INDEX EQU R3
Assembler directive ORG
Use to indirectly assign values to symbols.
ORG value
The assembler resets its LOCCTR to the
specified value.
ORG can be useful in label definition.
22
Symbol-Defining Statements
SYMBOL is 6-byte, VALUE is 3-byte, and FLAGS is 2-byte.
24
Use LDA VALUE, X to fetch the VALUE field form the
table entry indicated by the contents of register X.
Symbol-Defining Statements
All terms used to specify the value of the new symbol
--- must have been defined previously in the program.
...
BETA EQU ALPHA
ALPHA RESW 1
...
Need 2 passes
27
Symbol-Defining Statements
All symbols used to specify new location counter
value must have been previously defined.
ORG ALPHA
BYTE1 RESB 1
BYTE2 RESB 1
BYTE3 RESB 1
ORG
ALPHA RESW 1
Forward reference
ALPHA EQU BETA
BETA EQU DELTA
DELTA RESW 1
Need 3 passes
61
Expressions
Allow arithmetic expressions formed
Using the operators +, -, , /.
Division is usually defined to produce an integer result.
Expression may be constants, user-defined symbols, or
special terms.
106 1036 BUFEND EQU *
Gives BUFEND a value that is the address of the next
byte after the buffer area.
Absolute expressions or relative expressions
A relative term or expression represents some value
(S+r), S: starting address, r: the relative value.
30
Expressions
107 1000 MAXLEN EQU BUFEND-
BUFFER
Both BUFEND and BUFFER are relative terms.
The expression represents absolute value: the difference
between the two addresses.
Loc =1000 (Hex)
The value that is associated with the symbol that
appears in the source statement.
BUFEND+BUFFER, 100-BUFFER, 3*BUFFER
represent neither absolute values nor locations.
Symbol tables entries
31
Program Blocks
The source program logically contained main,
subroutines, data areas.
In a single block of object code.
More flexible (Different blocks)
Generate machine instructions (codes) and data in a different
order from the corresponding source statements.
Program blocks
Refer to segments of code that are rearranged within a
single object program unit.
Control sections
Refer to segments of code that are translated into
independent object program units.
32
Main
33
RDREC
34
WRREC
35
Program Blocks
Three blocks,
Default (USE), CDATA (USE CDATA), CBLKS (USE
CBLKS).
Assembler directive USE
Indicates which portions of the source program blocks.
At the beginning of the program, statements are
assumed to be part of the default block.
Lines 92, 103, 123, 183, 208, 252.
Each program block may contain several separate segments.
The assembler will rearrange these segments to gather
together the pieces of each block.
36
37
38
39
Program Blocks
Pass 1,
The block number is started from 0.
A separate location counter for each program block.
The location counter for a block is initialized to 0 when
the block is first begun.
Assign each block a starting address in the object
program (location 0).
Labels, block name or block number, relative addr.
Working table is generated
Block name Block number Address End Length
default 0 0000 0065 0066 (0~0065)
CDATA 1 0066 0070 000B (0~000A)
CBLKS 2 0071 1070 1000 (0~0FFF)
40
41
Control Sections & Program Linking
Control section
Handling of programs that consist of multiple control
sections.
Each control section is a part of the program.
Can be assembled, loaded and relocated independently.
Different control sections are most often used for
subroutines or other logical subdivisions of a program.
The programmer can assemble, load, and manipulate
each of these control sections separately.
More Flexibility then the previous.
Linking control sections together.
42
Control Sections & Program Linking
External references (external symbol references)
Instructions in one control section might need to refer to
instructions or data located in another section.
See Figure, multiple control sections.
Three sections, main COPY, RDREC, WRREC.
Assembler directive CSECT.
Assembler directives EXTDEF and EXTREF for
external symbols.
The order of symbols is not significant.
COPY START 0
EXTDEF BUFFER, BUFEND, LENGTH
EXTREF RDREC, WRREC (symbol name)
43
44
45
46
Control Sections & Program Linking
the generated object code.
15 0003 CLOOP +JSUB RDREC 4B100000
160 0017 +STCH BUFFER,X 57900000
The LOC of all control section is started form 0
RDREC is an external reference.
The assembler has no idea where the control section
containing RDREC will be loaded, so it cannot assemble
the address.
The proper address to be inserted at load time.
Must use extended format instruction for external
reference (M records are needed).
190 0028 MAXLEN WORD BUFEND-
BUFFER
An expression involving two external references. 47
48
49
50
Assembler Design Options
Most assemblers
Processing the source program into two passes.
The internal tables and subroutines that are used
only during Pass 1.
The SYMTAB, LITTAB, and OPTAB are
used by both passes.
The main problems to assemble a program in one
pass involves forward references.
54
One-Pass Assemblers
Eliminate forward references
Data items are defined before they are referenced.
But, forward references to labels on instructions cannot be
eliminated as easily.
Prohibit forward references to labels.
Two types of one-pass assembler.
One type produces object code directly in memory for
immediate execution.
The other type produces the usual kind of object
program for later execution.
55
56
57
58
One-Pass Assemblers
Load-and-go one-pass assembler
The assembler avoids the overhead of writing the object
program out and reading it back in.
The object program is produced in memory, the
handling of forward references becomes less difficult.
Figure, shows the SYMTAB after scanning line 40 of
the program.
Since RDREC was not yet defined, the instruction was
assembled with no value assigned as the operand
address (denote by - - - -).
59
One-Pass Assemblers
Load-and-go one-pass assembler
RDREC was then entered into SYMTAB as an undefined
symbol, the address of the operand field of the instruction
(2013) was inserted.
Figure, when the symbol ENDFIL was defined (line 45),
the assembler placed its value in the SYMTAB entry; it
then inserted this value into the instruction operand field
(201C).
At the end of the program, all symbols must be defined
without any * in SYMTAB.
For a load-and-go assembler, the actual address must be
known at assembly time.
62
One-Pass Assemblers
Another one-pass assembler by generating OP
Generate another Text record with correct operand address.
When the program is loaded, this address will be inserted
into the instruction by the action of the loader.
Figure, the operand addresses for the instructions on lines 15,
30, and 35 have been generated as 0000.
When the definition of ENDFIL is encountered on line 45,
the third Text record is generated, the value 2024 is to be
loaded at location 201C.
The loader completes forward references.
64
Multi-Pass Assemblers
Use EQU, any symbol used on the RHS be defined previously
in the source.
LOC Pass1 2 3
1000 LDA #0 1000 1000 1000
1003 ALPHA EQU BETA ???? ???? 1003
1003 BETA EQU DELTA ???? 1003 1003
1003 DELTA RESW 1 1003 1003 1003
Need 3 passes!
multi-pass assembler
65
Multi-Pass Assemblers
66
Multi-Pass Assemblers
67
Multi-Pass Assemblers
68
Multi-Pass Assemblers
69
Multi-Pass Assemblers
70
Implementation Examples
Microsoft MASM Assembler
• SEGMENT
– a collection segments, each segment is defined as
belonging to a particular class, CODE, DATA, CONST,
STACK
– registers: CS (code), SS (stack), DS (data), ES, FS, GS
– similar to program blocks in SIC
• ASSUME
– e.g. ASSUME ES:DATASEG2
– e.g. MOVE AX, DATASEG2
MOVE ES,AX
– similar to BASE in SIC
Microsoft MASM Assembler
• JUMP with forward reference
– near jump: 2 or 3 bytes
– far jump: 5 bytes
– e.g. JMP TARGET
– Warning: JMP FAR PTR TARGET
– Warning: JMP SHORT TARGET
– Pass 1: reserves 3 bytes for jump instruction
– phase error
• PUBLIC, EXTRN
– similar to EXTDEF, EXTREF in SIC
Sun Sparc Assembler
• Sections
– .TEXT, .DATA, .RODATA, .BSS
• Symbols
– global vs. weak
– similar to the combination of EXTDEF and
EXTREF in SIC
• Delayed branches
– delayed slots
– annulled branch instruction
Sun Sparc Assembler
LOOP: . LOOP: .
.
ADD %L2, %L3, %L4 .
CMP %L0, 10 .
BLE LOOP
.
ADD %L2, %L3, %L4
CMP %L0, 10
BLE LOOP
NOP
.
.
CMP %L0, 10
BLE LOOP
ADD %L2, %L3, %L4
.
Sun Sparc Assembler
LOOP: ADD %L2, %L3, %L4
.
.
CMP %L0, 10
BLE,A LOOP
. LOOP: .
.
.
CMP %L0, 10
BLE,A LOOP
ADD %L2, %L3, %L4
AIX Assembler
AIX Assembler for PowerPC
• Similar to System/370
• Base relative addressing
– save instruction space, no absolute address
– base register table:
• general purpose registers can be used as base register
– easy for program relocation
• only data whose values are to be actual address
needs to be modified
– e.g. USING LENGTH, 1
– USING BUFFER, 4
– Similar to BASE in SIC
– DROP
AIX Assembler for PowerPC
• Alignment
– instruction (2)
– data: halfword operand (2), fullword operand (4)
– Slack bytes
• .CSECT
– control sections: RO(read-only data), RW(read-
write data), PR(executable instructions),
BS(uninitialized read/write data)
– dummy section