Assembler Linker Loader
Assembler Linker Loader
Assembler Linker Loader
• Object file header describes the size and position of the other pieces of
the file
• Text segment contains the machine instructions
• Data segment contains binary representation of data in assembly file
• Relocation info identifies instructions and data that depend on absolute
addresses
• Symbol table associates addresses with external labels and lists
unresolved references
• Debugging info
Assembler directive
• Assembler directives are pseudo instructions
– They provide instructions to the assembler itself
– They are not translated into machine operation codes
• The SIC assembler language has the following assembler directives.
– START Specify name and staring address for the program
– END Indicate the end of the source program and (optionally) specify
the first executable instruction in the program
– BYTE Generate character or hexadecimal constant, occupying as many
bytes as needed to represent the constant
– WORD Generate one-word integer constant
– RESB Reserve the indicated number of bytes for a data area
– RESW Reserve the indicated number of words for a data area
– End of record : a null char (00)
– End of file : a zero-length record
Example of a SIC assembler
language program
Forward
Reference
Example of a SIC assembler
language program (cont’d)
Lines beginning with “.” contain comments only.
Translation of source program to
object code
Require to accomplish the following functions
• Convert mnemonic operation codes to their machine language equivalents
– e.g. translate STL to 14
– process assembler directives
• Convert symbolic operands to their equivalent machine addresses
– e.g. translate RETADR to 1033
– handle forward references
• two passes
– the first pass scans the source program for label definitions and assigns
addresses
– the second performs most of the actual translation.
• Build the machine instructions in the proper format
• Convert the data constants specified in the source program into their internal machine
representation
– e.g. translate EOF to 454F46
• Write the object program and the assembly listing
– Object program format
Program with object codes
STL
RETADR
Intermediate
file
OPTAB
LOCCTR SYMTAB
OPTAB
• In most cases, OPTAB is a static table.
• OPTAB must contain the mnemonic operation
code and its machine language equivalent
• In more complex assemblers, OPTAB also
contains information about instruction format
and length.
• OPTAB is usually organized as a hash table,
with mnemonic operation code as the key.
SYMTAB
• A symbol is basically a name and an address.
• Symbol table holds information needed to locate and
relocate a program’s symbolic definitions and references.
• This table may also contain information, such as type or
length, about the data area or instruction labeled.
• The symbol table contains an array of symbol entries.
• SYMTAB is usually organized as a hash table for efficiency
of insertion and retrieval.
– the label is the key of SYMTAB.
• non-random key
LOCCTR
• LOCCTR is a variable.
• LOCCTR is initialized to the beginning address
specified in the START statement.
• After each source statement is processed, the
length of the assembled instruction or data area
to be generated is added to LOCCTR.
• When a label is reached, the current value of
LOCCTR gives the address to be associated with
that label.
Intermediate file
• Pass 1 usually generates an intermediate file that
contains
– each source statement together with its assigned address,
error indicators, etc.
• This file is used as the input to Pass 2.
• This file retains some results of operations performed
during Pass 1
– the scanned operand field for symbols and addressing flags
– pointers into OPTAB and SYMTAB for each operation code
and symbol used.
Algorithm for Pass 1
Algorithm for Pass 1 (cont’d)
Algorithm for Pass 2
Algorithm for Pass 2 (Cont’d)
Linker
• It takes one or more object files or libraries as input and combines
them to produce a single (usually executable) file.
• In doing so, it resolves references to external symbols, assigns final
addresses to procedures/functions and variables, and revises code
and data to reflect new addresses (a process called relocation).
• Tool that merges the object files produced by separate compilation or
assembly and creates an executable file
• Three tasks
– Searches the program to find library routines used by program, e.g. printf(),
math routines,…
– Determines the memory locations that code from each module will occupy
and relocates its instructions by adjusting absolute references
– Resolves references among files
Process for producing an
executable file
Executable
Object file Linker
Source file Assembler File