A Simple Two-Pass Assembler
A Simple Two-Pass Assembler
Main Functions
• Translate mnemonic operation codes to
their machine language equivalents
• Assign machine addresses to symbolic
labels used by the programmers
Call
subroutine
code
Comment line
Indexing mode
Hexadecimal number
Subroutine entry point
OPTAB SYMTAB
SYMTAB
( 54 )16 op(6)
1 1 1 1 0n0(i003
x b) 16p e disp(12)
(54) 111010 0036-1051= -101B 16
• displacement= BUFFER - B = 0036 - 0033 = 3
– NOBASE is used to inform the assembler that the contents of the base register no
longer be relied upon for addressing
Immediate Address Translation
• Immediate addressing
– 55 0020 LDA #3 010003
op(6) n i xbp e disp(12)
( 00 )16 010000 ( 003 ) 16
Chap 2
Program relocation
• Assembler does not know the actual location
where the program is loaded.
• But it can identify for the loader those parts of
the object program that need modification.
• The object program having this modification
information is the relocatable program.
Chap 2
Program Relocation
• Example Fig. 2.1(the program for which
we wrote object program)
– Absolute program, starting address 1000
e.g. 55 101B LDA THREE 00102D
– Relocate the program to 2000
e.g. 55 101B LDA THREE 00202D
– Each Absolute address should be modified
• Example Fig. 2.5:
– Except for absolute address, the rest of the
instructions need not be modified
• not a memory address (immediate
addressing)
• PC-relative, Base-relative
– The only parts of the program that require
modification at load time are those that
specify direct addresses
Program relocation
• Assembler does two functions:
1. Inserts the address of labeled
instruction(RDREC) relative to the start of the
program.
2. It will produce the command for the loader,
instructing it to add the beginning address of
the program to the address field in the
instruction (JSUB) to be modified at load time.
Example
Relocatable Program
One-pass assemblers
Multi-pass assemblers
Two-pass assembler with overlay
structure
Two-Pass Assembler with Overlay
Structure
• For small memory
– pass 1 and pass 2 are never required at the same
time
– three segments
• root: driver program and shared tables and subroutines
• pass 1
• pass 2
– tree structure
– overlay program
One-Pass Assemblers
• Main problem
– forward references
• data items
• labels on instructions
• Solution
– data items: require all such areas be defined
before they are referenced
– labels on instructions: no good solution
One-Pass Assemblers
• Main Problem
– forward reference
• data items
• labels on instructions
• Two types of one-pass assembler
– load-and-go
• produces object code directly in memory for immediate
execution
– the other
• produces usual kind of object code for later execution
Load-and-go Assembler
• Characteristics
– Useful for program development and testing
– Avoids the overhead of writing the object program out
and reading it back
– Both one-pass and two-pass assemblers can be designed
as load-and-go.
– However one-pass also avoids the over head of an
additional pass over the source program
– For a load-and-go assembler, the actual address must be
known at assembly time, we can use an absolute program
Forward Reference in One-pass Assembler
• For any symbol that has not yet been defined
1. omit the address translation
2. insert the symbol into SYMTAB, and mark this symbol
undefined
3. the address that refers to the undefined symbol is added to
a list of forward references associated with the symbol table
entry
4. when the definition for a symbol is encountered, the proper
address for the symbol is then inserted into any instructions
previous generated according to the forward reference list
Load-and-go Assembler (Cont.)
• At the end of the program
– any SYMTAB entries that are still marked with *
indicate undefined symbols
– search SYMTAB for the symbol named in the END
statement and jump to this location to begin
execution
• The actual starting address must be specified at
assembly time
• Example
– Figure 2.18, 2.19
Producing Object Code
• When external working-storage devices are not available or
too slow (for the intermediate file between the two passes
• Solution:
– When definition of a symbol is encountered, the assembler
must generate another Tex record with the correct operand
address
– The loader is used to complete forward references that could
not be handled by the assembler
– The object program records must be kept in their original order
when they are presented to the loader
• Example: Figure 2.20
Multi-Pass Assemblers
• Restriction on EQU and ORG
– no forward reference, since symbols’ value can’t
be defined during the first pass
• Example
– Use link list to keep track of whose value depend
on an undefined symbol
• Example shown in Figure below
Multi-Pass Assemblers
Multi-Pass Assemblers
Multi-Pass Assemblers
Multi-Pass Assemblers
• Basic Loader functions
1)Design of an absolute loader
2) A Simple bootstrap loader
Design of an Absolute Loader
• Absolute Program
– Advantage
• Simple and efficient
– Disadvantage
• the need for programmer to specify the actual address
• difficult to use subroutine libraries
• Program Logic
Fig. 3.2 Algorithm for an absolute loader
Begin
read Header record
verify program name and length
read first Text record
while record type is not ‘E’ do
begin
{if object code is in character form, convert into internal representation}
move object code to specified location in memory
read next object program record
end
jump to address specified in End record
end
A Simple Bootstrap Loader
• Bootstrap Loader
– When a computer is first turned on or restarted, a
special type of absolute loader, called bootstrap
loader is executed
– This bootstrap loads the first program to be run by
the computer -- usually an operating system
• Example (SIC bootstrap loader)
– The bootstrap itself begins at address 0
– It loads the OS starting address 0x80
– No header record or control information, the
object code is consecutive bytes of memory