System Software: Assemblers, Linkers, and Loaders: Program Development

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

System Software:

Assemblers, Linkers, and Loaders


 
Program Development
  
·   Compiler:
a)   Code generator: creates symbolic assembly or machine code.
b)   Programming productivity is improved.
 
·    Assembler:
a) Translates symbolic language source into machine code. (There is a
one-to-one relationship between symbols/labels and machine
opcodes/addresses.)
b) Higher code optimization and mission critical code timing.
 
·    Interpreter:
a) Executes a source language program.
b) Poor performance (3x-20x) but replaces non-existent hardware.
c) Processes a source language program and executes the equivalent
machine code. Some compile to an intermediate format, p-codes ,
then execute a p-code engine
 
System Software:
Assemblers, Linkers, and Loaders
 
Program Development (cont)

·    Linker/Link Editor:


a)  Combines object files and libraries to form an executable file.
1)      absolute
2)      relocatable
b)  Merges target h/w configuration with relocation information(control file).
c)  Combines all text(data) segments from different modules in one text(data)
segment

·    Loader/Debug Loader:


a)  Places/relocates an executable file into memory and starts its execution.
b)  Supports debugger/IDE environment when symbolic information is include
 
System Software:
Assemblers, Linkers, and Loaders
 
Program Development (cont )
  
·      Compiler: Converts HLL source into assembly language source/equivalent
intermediate file or target language. i.e. name.c to name.s
Compiler Phases: pre-processor, code gen, optimization

Examples:
cc hello.c hello.o (C -> ASM/OBJ)
 
·      Assembler: Translates symbolic language source into machine language
binary format and creates an object file, i.e. name.s to name.o/obj
Uses a one-pass or two pass strategy. Forward references are a
problem for one pass assemblers.
A group of common object modules in a single file, i.e. name.lib

Examples:
asm hello.s hello.o (ASM -> OBJ)
 
System Software:
Assemblers, Linkers, and Loaders
 
Program Development (cont )
  
·   Linker/Link Editor: Combines object files and libraries to form an executable
file, i.e. name.o to name.out, (a.out). (link control file = .cmd)

Examples:
link hello.o a.out (OBJ -> EXE)
link hello.o -o hello.out lnk.cmd
  Libraries:
lib add.o sub.o mul.o div.o -o hello.a (OBJ(s) -> LIB)
 
·    Loader/Debug Loader: Places/relocates an executable file into memory
and starts its execution.
Examples:
load hello.out (EXE->MEM)
System Software:
Source Files to Program Execution
 
hello.cpp HLL-Source .c, .cpp, .h (preprocessor, code gen, optimize)

 
Compiler
 
hello.asm Assembly-Source .s , .asm
 
Assembler Librarian
 
hello.obj Machine-Object .o, .obj + library.obj(s) Machine-Object .a, .lib

Code ( .lst,) Code files


 
  Linker [+.cmd]
 
hello.exe Machine-Executable .out, .exe
Code ( .lst, .map, .xref)
 
Loader .
 
Relocated Memory Execute
Image (entry-point)
System Software:
Assemblers, Linkers, and Loaders
 
Assembler-Compiler: Software Structures/Files
 
·  Program Memory Organization.
Systems based on MIPS processors typically divide memory into 3 parts.
 
1)   Text segments: are used to hold executable program code
 
2)   Data segments: are used to hold static data types/structures
a)  Static = constants and program variables, structures, etc
allocated at link time.
b)  Dynamic = holds data allocated at run time, i.e. malloc
 
3)   Stack segment: is used to hold the program stack and dynamic data
used during program execution, i.e. automatic and local variables, heap
 
System Software:
Assemblers, Linkers, and Loaders
 
Assembler-Compiler: Program Organization (cont)
4)      Example: Required MIPS instructions for a program

.data # data segment


item: .word 10 # define/name a variable and initial value

.text # code segment


.globl main # must be global;
main: # your code goes here
lw $t0, item
… 
li $v0, 10 # exit to kernel
syscall
.end
System Software:
Assemblers, Linkers, and Loaders
 
 ·     Object Files and Program Linkage
 
COFF, Common Object File Format:
 
o     Object file header (table of contents/directory)
Describes the size and position of the other pieces of the file
 
o     Text segment contains the machine language code for routines in the source
file. The routines may be unexecutable because of unresolved references.
 
o     Data segment (static, Block Storage Space uninitialized, stack dynamic),
contains a binary representation of the data in the source file. The data may
be incomplete because of unresolved references to labels in other files.
 
System Software:
Assemblers, Linkers, and Loaders
 
  
COFF, Common Object File Format,:
 
o      Relocation information (address and type of each instruction which must be
modified) Identifies instructions and data words that depend on absolute
addresses. These references must change if portions of the program are
moved in memory.
 
o     Symbol table (contains labels and locations used to complete external
references, ref/def tables) associates addresses with external labels in
the source file and lists unresolved references.
 
o     Debug info (matches HLL source line with machine instruction, complete
symbol table) Contains a concise description of the way in which the
program was compiled, so a debugger can find which instruction address
corresponds to lines in a source file and print the data structures in a
readable form.
System Software:
Assemblers, Linkers, and Loaders
 
 
·    Macros
 
1)   Are a pattern-matching and replacement facility that provides a simple
mechanism to name a frequently used sequence of code
 
2)   Like subroutines permit a programmer to create and name a new abstraction
for a common operation.
 
3)   A macro call is replaced by the macro’s body definition when the program
is assembled
 
·   General Macro model/template

.macro macro_name ( $p1, $p2, .. $pn)


# your code goes here using macro macro_arguments
.end_macro
Reminder: SPIM does not currently support a user macro capability 
System Software:
Assemblers, Linkers, and Loaders
 
  ·    Macros Example
 
1) Definition of a Macro
.text
.macro print_int( $p1)
move $a0, $p1
li $v0, 1
syscall
.end_macro

2)      Program source code with macro call:



print_int($t0)

3)      Program code after macro replacement by assembler

move $a0, $t0
li $v0, 1
syscall
System Software:
Assemblers, Linkers, and Loaders
 
  
·     Assembly Language Features and Limitations

1)  Where and why assembly used:


a)     Interfaces: input/output, i.e. device-drivers
b)     Performance, size, optimization, i.e. OS/kernel critical
c)     Dedicated uses, limited resources(pwr, RAM, cost), i.e. embedded
computers
d)     Normally used close to hardware architecture level.
 
2)  Why not used
a)      Slower development cycle. Takes more lines of code for same task.
Has a bigger expansion factor for lines of code.
b)     Lack of structure, programming oversight, built in checking
c)       More time to understand and difficult to maintain
d)     If not WELL documented, special program tricks are not obvious.
            e) Difficult to test and debug because there are no enforceable
            programming guidelines
            f) Dedicated to one machine. Must be rewritten for each new processor
 
System Software:
Assemblers, Linkers, and Loaders
 
 Assembler-Compiler: Advantages/Limitations (cont)
·     RISC and Pseudo Instructions

1)   Assembler create/treat common variations of machine language instructions


as if they were a single instructions in their own right.

2)   Cost/penalty for pseudo-instructions is to reserve one of the general purpose


register, $at, for use by assembler.

3)    Helps minimize the number of actual machine instructions needed in H/W.

4)    Requires assemblers/compilers to be very machine H/W aware.


 
System Software:
Assemblers, Linkers, and Loaders
 
Linkers, Link Editors
 
Steps to merge object modules into one executable file.
 
1)       Determines the memory locations that each text and data module will
occupy and relocates each by adjusting memory references.
 
2)       Resolves references among modules
 
    3) Searches the program libraries to find library routines used by the program
     and adds them in
             
1)       Writes out the results as an executable module, i.e. a.out. using the object
file format structure.
 
2)       Common Object File Format, COFF: is an industry standard for object and
Executable files.
System Software:
Assemblers, Linkers, and Loaders
 
 Loaders:
 
·   Steps for loading executable modules:
 
1)       Reads the executable file’s header
2)       Allocates space from OS
3)       Copies segments into memory, with relocation if required.
4)       Copies arguments onto stack
5)       Jumps to a start up routine, entry_point, that copies
 
·    MIPS standard segment memory assignments;
 
Segment Address
1)       vect = 0x0000 0000 - 0x003F FFFF
2)       .text = 0x0040 0000
3)       .data = 0x1000 0000 - 0x1001 0000 $gp = 0x1000 8000
4)       .stack = 0x xxxx xxxx - 0x7FFF FFFF $sp = 0x7FFF FFFF
5)       .ktext = 0x8000 0080
6)       .kdata = 0x9000 0000 - 0x9001 0000
7)       Heap Management, brk/malloc. Data => Heap => Stack
System Software:
 
 Organization of a simple 2-pass assembler

  Pass 1:
  loct = 0
locd = 0
 
Read Line
 
  y
.directive? Process Directive .end
 

label? Update label table (label, locx)
 
Update locx

Pass 2:
loct = 0
locd = 0
loct = 0
locd = 0
 
Read Line

.directive? Process Directive
 
.word .space .global .end
init locd ref/def copy images to
locd object file
 
update object file image

instruction? error
 
build instruction
opcode to instruction operation fields
registers to instruction operand fields
immediate to instruction imm field
label to instruction address field
 
update relocation table
update object file image.

  Update loct
 

You might also like