14557_passes of an assembler

Upload: rahul-gupta

Post on 06-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 14557_passes of an Assembler

    1/13

    044264 Software Systems Fall 2006/7 Lect. 8 1

    Lecture 8 - Assembler passes,Linking and Loading

    2044264 Software Systems Fall 2006/7 Lect. 8

    Course Flow

    Lecture 1: Intro Lecture 2: Representing code and data Lecture 3: Addressing modes Lecture 4: Procedures Lecture 5: Data Structures Lecture 6: More Procedures & Structures Lecture 7: Interrupts Lecture 8: Assembling, Linking and Loading Lecture 9: Virtual Memory

    Lecture 10: File Systems

  • 8/3/2019 14557_passes of an Assembler

    2/13

    3044264 Software Systems Fall 2006/7 Lect. 8

    Steps in program developmentSource

    program

    Compiler orassembler

    Link the object

    modules

    Errors?

    Errors?

    Run executable

    Correct the

    source program

    no

    no

    CurrentFocus

    4044264 Software Systems Fall 2006/7 Lect. 8

    How does a program becomeexecutable?

    High-level program (C, Pascal, C++)

    Object code

    [.c, .pas, .cpp]

    Assembly program [.asm]

    [.obj]

    Compiler

    Assembler

    Compiler

    Executable program [.exe]

    Linker [.lib]Library

    Application (algorithm)

    Programming

    Loader

    [.map]Map

  • 8/3/2019 14557_passes of an Assembler

    3/13

    5044264 Software Systems Fall 2006/7 Lect. 8

    SW program life cycle#include #include #define PRIMES 100long primes=0,value,divisor,prime[PRIMES];void main(void){printf("\nPrime Numbers:\n");for (value=2; primes

  • 8/3/2019 14557_passes of an Assembler

    4/13

    7044264 Software Systems Fall 2006/7 Lect. 8

    Assembler pass objectives

    Pass one: build a symbol table containing thevalues of all labels and symbols defined in theprogram.

    Pass two: Use the symbol table to translatethe program

    8044264 Software Systems Fall 2006/7 Lect. 8

    Assembler pass one

    Begin pass 1 Init LC=0 Read next line from

    source program

    Analyze line for

    syntaxErrors?

    Determine

    statement type

    End

    statement?Label?

    Symbol

    definition?

    Storage

    allocation?Instruction?

    Comment?Assembler

    Directive?

    Count errors

    yes

    no

    Pass 2

    yes

    nonono

    no

    no

    Label

    value=LC

    yes

    Compute

    symbol value

    specification

    yes

    Store name and value

    in symbol table

    Compute

    size of

    storage

    yes

    no

    Compute

    size of

    instruction

    yes

    Increment LC by the

    computed size

    yes

    Perform directive function

    yes

    no Invalid statement

    What is LC?

  • 8/3/2019 14557_passes of an Assembler

    5/13

    9044264 Software Systems Fall 2006/7 Lect. 8

    Assembler pass two

    Begin pass 2 Init LC=0 Read next line from

    source and insert in

    proper field oflisting line

    Determine statement

    typeLabel or

    symbol?

    End

    statement?

    Assembler

    Directive?

    Storage

    Allocation?Instruction?

    Any more

    Characters?Write to listing

    file

    yes

    yes

    Exit

    yes

    nonono

    Perform directive

    function

    yes

    Translate opcode

    to binary

    no

    yes

    no

    yes

    Output data to

    object file

    Invalid statement

    Evaluate operand

    specifiers and

    convert address

    to binary

    Write to object

    file

    Write binary to

    listing fileUpdate LC by

    instruction size

    Increment LC by

    the size of data

    no

    10044264 Software Systems Fall 2006/7 Lect. 8

    The job of the linker

    Resolve symbol references between

    different object files. Creates an executable file out of the

    object modules..asm .asm .asm .asm

    assembler

    .obj .obj .obj .obj

    linker

    .exe

  • 8/3/2019 14557_passes of an Assembler

    6/13

    11044264 Software Systems Fall 2006/7 Lect. 8

    The job of the loader

    Allocates space in memory for theprogram.

    Determines the start address of theprogram.

    Has to update all absolute addressing

    modes respectively before running theprogram.

    12044264 Software Systems Fall 2006/7 Lect. 8

    Information required for thelinker/loader

    The assembler needs to provide the followinginformation: The size of each object module. A list of all the symbols defined in a given module

    that can be used by other modules. A list of all symbols used by the given module that

    are defined in other modules. A list of all the places in the code that need to be

    fixed when the program is being linked/loaded. A machine code

    May also provide debugging info: Line#s, etc.

  • 8/3/2019 14557_passes of an Assembler

    7/13

    13044264 Software Systems Fall 2006/7 Lect. 8

    3 New Assembler Directives

    The new Assembler Directives are notsupported by sim: .cset defines the start address of a module.

    Relative to this address all other addresses withinthe module will be calculated.

    .extern specifies using a symbol defined inanother module.

    .entry allows other modules to use a local symbol

    14044264 Software Systems Fall 2006/7 Lect. 8

    Tables generated by theassembler

    External Symbol Table EST

    For resolving linkage Relocation and Linkage Table RLT

    For address fixes

  • 8/3/2019 14557_passes of an Assembler

    8/13

    15044264 Software Systems Fall 2006/7 Lect. 8

    External symbol table

    Each entry consists of 3 fields: Symbol symbol name.

    Type 3 symbol types: LD local definition (symbols which appear with .entry)

    ER external symbols (symbols which appear with.extern).

    SD Symbols which appear with .cset.

    Relative location the distance of the symbolfrom .cset. Always 0 for SD symbols.

    16044264 Software Systems Fall 2006/7 Lect. 8

    Relocation and linkage table

    A list of all the relative addresses that needto be fixed (position dependent) at link orload time.

    Each entry consists of 3 fields: Relative location the distance of the address

    that has to be fixed relative to .cset . Flag indication whether we need to add/subtract

    from address. Symbol the value that needs to be added to /

    subtracted from the address.

  • 8/3/2019 14557_passes of an Assembler

    9/13

    17044264 Software Systems Fall 2006/7 Lect. 8

    An example:

    .cset A

    .extern proced

    .entry arg

    Main: Mov $Main, ..

    Calls $1, proced

    Arg: .long 0

    5

    20

    40

    EST

    Symbol Type Rel. Loc

    A SD 0

    proced ER -

    Arg LD 40

    RLT

    Symbol Flag Rel. Loc

    A + 5

    A - 20proced + 20

    Module A

    18044264 Software Systems Fall 2006/7 Lect. 8

    An example:

    .cset B

    .extern arg

    .entry proced

    proced: Arg,

    Movb d(r0), r2

    d: .long 0

    6

    16

    75

    EST

    Symbol Type Rel. LocB SD 0

    Arg ER -

    proced LD 0

    RLT

    Symbol Flag Rel. Loc

    B - 6

    Arg + 6

    B + 16

    Module B

  • 8/3/2019 14557_passes of an Assembler

    10/13

    19044264 Software Systems Fall 2006/7 Lect. 8

    An example:

    EST

    Symbol Type Rel. Loc

    B SD 0 50

    Arg ER -

    proced LD 0 50

    RLT

    Symbol Flag Rel. Loc

    B - 6 56

    Arg + 6 56

    B + 16 66

    Memory map table

    A

    B

    old

    0

    43

    0

    78

    new

    0

    43

    50

    128

    Unified ESTEST

    Symbol Type Rel. Loc

    A SD 0

    proced ER -

    Arg LD 40

    EST

    Symbol Type Rel. Loc

    A SD 0

    Arg LD 40

    B SD 50

    proced LD 50

    Unified RLTRLT

    Symbol Flag Rel. LocA + 5

    A - 20

    proced + 20

    RLT

    Symbol Flag Rel. Loc

    A + 5

    A - 20

    proced + 20

    B - 56

    Arg + 56

    B + 66

    20044264 Software Systems Fall 2006/7 Lect. 8

    The linking process

    Link modules A, B and C: Linker prepare memory map according to:

    Order of linking (provided by the user). The length of each module (provided by the assembler).

    Unify the external symbol table from the differentobjects: Compute all SDs

    Check that each ER has a corresponding LD (otherwise this is anerror).

    Delete all ERs Update all relative location fields (if necessary).

    A

    B

    C

  • 8/3/2019 14557_passes of an Assembler

    11/13

    21044264 Software Systems Fall 2006/7 Lect. 8

    The linking process

    Unifying relocation and linkage tables: Fix the relative location field in table according to theunified external symbol table.

    Fixing in the code: All places with 2 entries in the RLT are fixed accordingly and the

    entries are deleted from the RLT (relative reference).

    All places with 1 entry in the RLT are fixed in the code according to theRLT and are remained in the RLT.

    The fixed code and the new tables are passed to the

    loader. Loading : fix the tables according to the start

    address where the program will be loaded in memory.

    22044264 Software Systems Fall 2006/7 Lect. 8

    Position dependent symbols

    A position dependent symbol appears whenthere is : An absolute reference to a symbol in either

    displacement (symbol(rn)), displacement deferred (*symbol(rn)), literal ($symbol), immediate ($symbol) or absolute (*$symbol) addressing modes.

    A relative reference to a symbol in either

    Relative (symbol). Relative deferred (*symbol).

    since the relative PC is used as the base register

  • 8/3/2019 14557_passes of an Assembler

    12/13

    23044264 Software Systems Fall 2006/7 Lect. 8

    Relocation and linkage tableconstruction rules

    Operand has a label

    Label is defined in current .csetLabel is defined in current .extern

    RLT:LC, +, current cset

    Opcode:Value of symbol

    RLT:none

    Opcode:Relative distance to

    address

    RLT:LC, +, name ofexternal symbol

    Opcode:

    Value of symbol istemporarily 0.

    RLT:LC, -, current csetLC,+, name ofexternal symbol

    Opcode:Current relativedistance.

    AbsoluteRelativeAbsoluteRelative

    24044264 Software Systems Fall 2006/7 Lect. 8

    Relocation and linkage tableconstruction rules

    Operand is an explicit value

    RLT:None.

    Opcode:Value of operand.

    E.g.Movl $4, R4

    RLT:LC, -, current cset

    Opcode:Relative distance totarget addressassuming startaddress is 0

    E.g.Movl 40, R10

    AbsoluteRelative

  • 8/3/2019 14557_passes of an Assembler

    13/13

    25044264 Software Systems Fall 2006/7 Lect. 8

    Related issues

    Symbolic debugging info Object info (symbol overload allowed)