1. Assembly and Linking: From
Instructions to Execution
Assembly and linking are crucial steps in the compilation process,
transforming instructions into a program's memory image. Loading
then places the program in memory for execution. This presentation
explores the techniques required for assembly and linking, providing
insights into the complete compilation and loading process.
by ARTHI Thenraj
2. The Role of Assemblers and Linkers
Assembler
Translates symbolic assembly language into bit-level
representations of instructions, known as object code. It
handles instruction formats and partially translates labels
into addresses.
Linker
Completes the translation of labels into addresses and
produces an executable binary file. It combines multiple
object files into a single executable program.
Compilers often generate human-readable assembly language, freeing them from instruction format and address details.
The linker ensures the executable is ready for loading into memory.
3. Assembler Functionality: Translating Assembly to
Object Code
1 Opcode Translation
Translates assembly code opcodes
into their binary equivalents.
2 Instruction Formatting
Formats the bits in each instruction
according to the architecture's
specifications.
3 Label Translation
Translates labels into memory
addresses, enabling symbolic
addressing.
The assembler translates assembly code into object code, handling opcodes, instruction formats, and labels. Labels are a crucial
abstraction, allowing programmers to avoid specifying exact memory locations.
4. Two-Pass Assembly Process
First Pass: Symbol Table Creation
Scans the assembly code to determine the address of
each label, storing the name and address in a symbol
table.
Second Pass: Instruction Assembly
Assembles the instructions using the label values
computed in the first pass, substituting label names with
their corresponding addresses.
The assembly process involves two passes. The first pass identifies label
addresses, and the second pass assembles instructions using these
addresses. The program location counter (PLC) tracks the current
memory location during the first pass.
5. Program Location Counter
(PLC) and Origin
Program Location Counter (PLC)
Keeps track of the current location in memory during the assembly process.
It is incremented as the assembler scans through the code.
Origin (ORG)
A pseudo-op that specifies the starting address of the program. It sets the
PLC's value to the specified address, determining where the program begins
in memory.
The PLC assigns memory locations to labels during assembly, while the ORG
statement sets the program's starting address. Assemblers allow multiple ORG
statements to spread instructions and data across memory.
6. Linking: Stitching Together Program Pieces
Modularity
Breaking a large program into
smaller files helps delineate
program modularity.
1
Library Routines
If the program uses library routines,
those will already be preassembled.
2
Linker
A linker allows a program to be
stitched together out of several
smaller pieces.
3
The linker combines smaller program pieces, operating on object files and modifying assembled code to link files. Breaking
programs into smaller files enhances modularity and allows the use of preassembled library routines.
7. Entry Points and External References
1
Entry Point
The location in a file where a label is defined.
2
External Reference
The location in a file where a label is used but not defined.
Labels can be defined and used in the same file, or defined in one file and used in others. Entry points define labels, while
external references use them. The linker resolves these references.
8. Loader's Role: Resolving
External References
Resolve
The main job of the loader is to
resolve external references based
on available entry points.
Symbol Table
As a result of the need to know
how definitions and references
connect, the assembler passes to
the linker not only the object file
but also the symbol table.
The loader resolves external references using entry points, requiring the
assembler to pass the object file and symbol table to the linker. This
ensures that all references are correctly linked.
9. Linker's Two-Phase Process
1
Phase 1: Address Determination
Determines the starting address of each object file based on the load order specified by
the user or a load map file.
2
Phase 2: Symbol Table Merging and Address
Editing
Merges all symbol tables from the object files into a single
table and edits the object files to change relative
addresses into absolute addresses.
The linker operates in two phases. First, it determines the starting address of each object file. Second, it merges symbol
tables and edits object files to convert relative addresses to absolute addresses.
10. Key Takeaways and Next
Steps
Assembly and Linking
Essential steps in
transforming code into
executable programs.
Assemblers and
Linkers
Play distinct roles in
translating and combining
code.
Understanding the Process
Provides insights into software compilation and execution.
Assembly and linking are vital for creating executable programs.
Assemblers and linkers have distinct roles in translating and combining
code. Understanding this process provides valuable insights into
software compilation and execution.