Вы находитесь на странице: 1из 29

Assembler, Linker, Loader

Andrew Tanenbaum
Structured Computer Organization
(c) 2006 Pearson Education, Inc.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Running Programs
Steps that the computer goes through to run a
program:
Memory

Machine language
program
(executable file)

Input Data

Data entered
during execution

CPU

Computed results

Program Output

Program Execution
Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed

Program Errors
a) Syntax Errors:

Errors in grammar of the language

a) Runtime error:

When there are no syntax errors, but the program


cant complete execution

Divide by zero
Invalid input data

a) Logical errors:

The program completes execution, but delivers


incorrect results
Incorrect usage of parentheses

Computer Languages

Machine Language

Uses binary code


Machine-dependent
Not portable

Assembly Language
Uses mnemonics
Machine-dependent
Not usually portable

High-Level Language (HLL)

Uses English-like language


Machine independent
Portable (but must be compiled for different platforms)
Examples: Pascal, C, C++, Java, Fortran, . . .

Machine Language
a)

The representation of a computer program which is


actually read and understood by the computer.

a)

A program in machine code consists of a sequence of


machine instructions.

Instructions:

Machine instructions are in binary code


Instructions specify operations and memory cells involved in
the operation

Example:

Operation

Address

0010

0000 0000 0100

0100

0000 0000 0101

0011

0000 0000 0110

Assembly Language
a)
b)
c)
d)
e)
f)

A symbolic representation of the machine language of


a specific processor.
Is converted to machine code by an assembler.
Usually, each line of assembly code produces one
machine instruction (One-to-one correspondence).
Programming in assembly language is slow and errorprone but is more efficient in terms of hardware
performance.
Mnemonic representation of the instructions and data
Example:
Load
Add
Store

Price
Tax
Cost

High-level language
A programming language which uses statements
consisting of English-like keywords such as "FOR",
"PRINT" or IF, ... etc.
Each statement corresponds to several machine
language instructions (one-to-many correspondence).
Much easier to program than in assembly language.
Data are referenced using descriptive names
Operations can be described using familiar symbols
Example:
Cost := Price + Tax

Format of an Assembly Language Statement

Computation of N = I + J. (a) Pentium 4.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Compilers & Programs


Compiler
A program that converts another program from some
source language (or high-level programming language /
HLL) to machine language (object code).
Some compilers output assembly language which is
then converted to machine language by a separate
assembler.
Is distinguished from an assembler by the fact that each
input statement, in general, correspond to more than
one machine instruction.

Compilation
Source
Program
Input
a)
b)
c)
d)

Compiler
Target Program

Target
Program
Output

Compiler translates source into target (a machine


language program)
Compiler goes away at execution time
Compiler is itself a machine language program,
presumably created by compiling some other highlevel program
Machine language, when written in a format
understood by the OS is object code

Compilation into Assembly Language


Source
Program

Assembly
Language

Compiler

Assembly
Language

Assembler

Machine
Language

Compilers & Programs


Source program
The form in which a computer program, written in
some formal programming language, is written by
the programmer.
Can be compiled automatically into object code or
machine code or executed by an interpreter.
Source programs in C-language have extension .c

Compilers & Programs


Object program
Output from the compiler
Equivalent machine language translation of the source
program
Files usually have extension .obj or '.o'

Executable program
Output from linker/loader
Machine language program linked with necessary libraries
& other files
Files usually have extension .exe or '.out'

Linking and Loading

Generation of an executable binary program from a collection of


independently translated source procedures requires using a linker.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

What is a Linker?
a) A program that pulls other programs together
so that they can run.
b) Most programs are very large and consist of
several modules.
c) Even small programs use existing code
provided by the programming environment
called libraries.
d) The linker pulls everything together, makes
sure that references to other parts of the
program (code) are resolved.

Tasks Performed by the Linker (1)

Each module has its own address space, starting at 0.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Tasks Performed by the Linker (2)

Each module has its own address space, starting at 0.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Tasks Performed by the Linker (3)

Each module has its own address space, starting at 0.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Tasks Performed by the Linker (4)

Each module has its own address space, starting at 0.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Tasks Performed by the Linker (5)

The object modules after being positioned in the


binary image but before being relocated and linked.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Tasks Performed by the Linker (6)

The same object modules after linking and after relocation


has been performed. Together they form an executable
binary program, ready to run

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Structure of an Object Module

The internal structure of an object module produced by a translator.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Binding Time and Dynamic Relocation

The relocated binary program of moved up 300


addresses. Many instructions now refer to an incorrect
memory address.
So use table to link dynamically.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Dynamic Linking in MULTICS (1)

Before EARTH is called.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Dynamic Linking in MULTICS (2)

After EARTH has been called and linked.

Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education

Basic Loader Functions


A Loader is a system program that
performs the loading function
It brings object program into memory
and starts its execution

Role of Loader

Source
Program

Assembler

Object
Program

Loader

Object
program
ready for
execution

Memory

Role of Loader and Linker


Memory

Source
Program

Assembler

Object
Program

Linker

Executable
Code
Loader

Object
program
ready for
execution