computer architecture & organization
DESCRIPTION
Computer Architecture & Organization. Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. Path from Programs to Bits. C OMPILERS. - PowerPoint PPT PresentationTRANSCRIPT
COMPUTER ARCHITECTURE & ORGANIZATION
Assemblers and Compilers
Engr. Umbreen Sabir
Computer Engineering Department,
University of Engg. & Technology Taxila.
20
/2/2
00
9C
A&
O Lecture 7 by E
ngr. Um
breen Sabir
1
COMPILERS
The compiler transforms the C program into an assembly language program, a symbolic form of what the machine understands.
Higher level language programs take many fewer lines of code than assembly language.
So, programmer productivity is higher.
20
/2/2
00
9
3
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
HOW AN ASSEMBLER WORKS Three major components of assembly1) Allocating and initialing data storage2) Conversion of mnemonics to binary instructions3) Resolving addresses Pseudoinstructions:
Assembler can also treat common variations of machine level instructions as if they were instructions in their own right. Such instructions are called Pseudoinstructions. e.g.
Move $t0, $t1 #register $t0 gets register $t1. Assembler converts this assembly language instruction
into the machine language equivalent of the following instruction:
add $t0, $zero, $t1 It converts blt into two instructions slt and bne. MIPS assembler also allows 32-bit constants to be loaded
into a register despite the 16-bit limit of immediate instructions.
20
/2/2
00
9
4
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
HOW AN ASSEMBLER WORKS CONT.
The assembler turn the assembly language program into an object file, which is a combination of machine language instructions, data and information needed to place instructions in memory.
Assembler keeps track of labels used in branches and data transfer instructions in a symbol table.
20
/2/2
00
9
5
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
OBJECT FILE COMPONENTS
Object file typically contains six distinct pieces.
1. Object File Header: describes size and position of other pieces.
2. Text Segment: Contains machine code.3. Data segment: static or dynamic data.4. Relocation information: identifies instructions
and data words that depend on absolute addresses when the program is loaded in memory.
5. Symbol Table: contains the remaining labels such as external references.
6. Debugging Information
20
/2/2
00
9
6
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
RESOLVING ADDRESSES- 1ST PASS“OLD-STYLE” 2-PASS ASSEMBLER APPROACH
20
/2/2
00
9
8
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
MODERN WAY – 1-PASS ASSEMBLERS
Modern assemblers keep more information in their symbol table which allows them to resolve addresses in a single pass.
Known addresses (backward references) are immediately resolved.
Unknown addresses (forward references) are “back-filled” once they are resolved.
20
/2/2
00
9
10
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
THE ROLE OF A LINKER Some aspects of address resolution cannot
be handled by the assembler alone.1) References to data or routines in other
object modules2)The layout of all segments in memory3) Support for REUSABLE code modules4) Support for RELOCATABLE code modules This final step of resolution is the job of a
LINKER
20
/2/2
00
9
11
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
LINKER Also called link editor. A system program that combines independently
assembled machine language programs and resolves all undefined labels into an executable file.
Three steps of linker Place code and data modules symbolically in
memory. Determine the addresses of data and instruction
labels. Place both the internal and external references.
The linker uses the relocation information and symbol table in each object module to resolve all undefined labels.
The linker produces an executable file that can be run on any computer. This file has same format as object file, except that it contains no unresolved references.
20
/2/2
00
9
12
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
LOADER A system program that places an object
program in main memory so that it is ready to execute.
It follows following steps Reads the exe file header to determine size of the
text and data segments. Creates an address space large enough for text
and data. Copies the instructions and data from the exe to
memory. Copies the parameters to the main program onto
the stack. Initializes the machine registers and sets the stack
pointer to the first free location. Jumps to a start-up routine that copies the
parameters into the argument registers and calls the main routine of the program. Terminates program by exit system call.
20
/2/2
00
9
13
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
STATIC AND DYNAMIC LIBRARIES LIBRARIES are commonly used routines stored
as a concatenation of “Object files”. A global symbol table is maintained for the entire library with entry points for each routine.
When routines in LIBRARIES are referenced by assembly modules, the routine’s entry points are resolved by the LINKER, and the appropriate code is added to the executable. This sort of linking is called STATIC linking.
Many programs use common libraries. It is wasteful of both memory and disk space to include the same code in multiple executables. The modern alternative to STATIC linking is to allow the LOADER and THE PROGRAM ITSELF to resolve the addresses of libraries routines. This form of linking is called DYNAMIC linking (e.x. .dll).
20
/2/2
00
9
14
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
DYNAMICALLY LINKED LIBRARIES
Library routines are not linked and loaded until the program is run.
Program and library routines keep extra information on the location of nonlocal procedures and their names.
In Initial version, loader ran a dynamic linker to linker, using extra information in file to find appropriate libraries but it still linked all routines of the library.
Then came lazy procedure linker version of DLLs, where each routine is linked after it is called.
20
/2/2
00
9
15
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
MODERN LANGUAGES
Intermediate “object code language” Rather than compiled to assembly language,
java is compiled to instructions that are easy to interpret, the Java Byte Code,
A software interpreter, Java Virtual Machine (JVM) can execute java bytecodes.
An interpreter is a program that simulates an instruction set architecture. E.g. MIPS simulator is an interpreter.
Upside of interpreter is portability. Downside of interpreter is low performance. To preserve portability and improve performance,
Just In Time (JIT) compilers were introduced, which translate while the program is running.
20
/2/2
00
9
19
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
MODERN LANGUAGES
Intermediate “object code language”
20
/2/2
00
9
20
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
MODERN LANGUAGES
Intermediate “object code language”
20
/2/2
00
9
21
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
COMPILER OPTIMIZATIONS
High Level Optimization Transformations that are done at something
close to the source level. Local & Global Optimization
Local Optimization Works within a single basic block.
Global Optimization Works across multiple basic blocks.
Global Register Allocation Allocates variables to registers for regions of the code.
20
/2/2
00
9
22
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
COMPILER OPTIMIZATIONS Common Subexpression Elimination:
Finds multiple instances of the same expression and replaces the second one by a reference to the first.
Strength Reduction: Replaces complex operations by simpler ones.
Constant Propagation/Constant Folding: Find constants in code and propagates them,
collapsing constant values whenever possible. Copy Propagations:
Propagates values that are simple copies, eliminating the need to reload values.
Dead Store Elimination: Finds stores to values that are not used again
and eliminates the stores.
20
/2/2
00
9
23
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
COMPILER OPTIMIZATIONS
Global Code Optimization Code motion
Finds code that is loop invariant: a particular piece of code computes the same value on every loop iteration and may be computed once outside the loop.
Induction variable elimination Combination of transformations that reduce overhead
on indexing arrays, essentially replacing array indexing with pointer access.
20
/2/2
00
9
24
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
UNOPTIMIZED ASSEMBLY OUTPUT With debug flags set:
20
/2/2
00
9
27
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
REGISTER ALLOCATION
Assign local variables to registers
20
/2/2
00
9
28
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
LOOP-INVARIANT CODE MOTION Assign globals to temp registers and moves
assignments outside of loop 20
/2/2
00
9
29
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
REMOVE UNNECESSARY TESTS Since “i” is initially set to “0”, we already
know it is less than “10”, so why test it the first time through?
20
/2/2
00
9
30
CA
&O
Lecture 7 by Engr. U
mbreen S
abir
REMOVE UNNECESSARY STORES All we care about it the value of total after
the loop, and simplify loop 20
/2/2
00
9
31
CA
&O
Lecture 7 by Engr. U
mbreen S
abir