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 link hello.o -o hello.out lnk.cmd Libraries: lib add.o sub.o mul.o div.o -o hello.a

(OBJ -> EXE)

(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 Compiler hello.asm Assembly-Source Assembler hello.obj Machine-Object Code Linker hello.exe .o, .obj + library.obj(s) ( .lst,) [+.cmd] .s , .asm Librarian Machine-Object Code files .a, .lib .c, .cpp, .h (preprocessor, code gen, optimize)

Machine-Executable .out, .exe Code ( .lst, .map, .xref) Loader Relocated Memory Image . Execute (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 item: .word 10 .text .globl main main: lw $t0, item li $v0, 10 syscall .end # data segment # define/name a variable and initial value # code segment # must be global; # your code goes here

# exit to kernel

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 Text segment contains the machine language code for routines in the source file. The routines may be unexecutable because of unresolved references. 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. 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. 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 macros 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) Program code after macro replacement by assembler move $a0, $t0 li $v0, 1 syscall

3)

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) 4) Helps minimize the number of actual machine instructions needed in H/W. 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. Resolves references among modules Searches the program libraries to find library routines used by the program and adds them in Writes out the results as an executable module, i.e. a.out. using the object file format structure. Common Object File Format, COFF: is an industry standard for object and Executable files.

2) 3)

1)

2)

System Software: Assemblers, Linkers, and Loaders


Loaders:

Steps for loading executable modules: 1) 2) 3) 4) 5) Reads the executable files header Allocates space from OS Copies segments into memory, with relocation if required. Copies arguments onto stack Jumps to a start up routine, entry_point, that copies

MIPS standard segment memory assignments; Segment Address vect = 0x0000 0000 - 0x003F FFFF .text = 0x0040 0000 .data = 0x1000 0000 - 0x1001 0000 $gp = 0x1000 8000 .stack = 0x xxxx xxxx - 0x7FFF FFFF $sp = 0x7FFF FFFF .ktext = 0x8000 0080 .kdata = 0x9000 0000 - 0x9001 0000 Heap Management, brk/malloc. Data => Heap => Stack

1) 2) 3) 4) 5) 6) 7)

System Software:
Organization of a simple 2-pass assembler
Pass 1: loct = 0 locd = 0 Read Line y .directive? y label? Update locx Pass 2: loct = 0 locd = 0 Update label table (label, locx) Process Directive .end

loct = 0 locd = 0 Read Line y .directive? .word init locd Process Directive .space locd .global ref/def .end copy images to object file

update object file image n 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