Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology.

Similar presentations


Presentation on theme: "C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology."— Presentation transcript:

1 C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. 20/2/2009 CA&O Lecture 7 by Engr. Umbreen Sabir 1

2 P ATH FROM P ROGRAMS TO B ITS 20/2/2009 2 CA&O Lecture 7 by Engr. Umbreen Sabir

3 C OMPILERS 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/2009 3 CA&O Lecture 7 by Engr. Umbreen Sabir

4 H OW AN A SSEMBLER W ORKS Three major components of assembly 1) Allocating and initialing data storage 2) Conversion of mnemonics to binary instructions 3) 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/2009 4 CA&O Lecture 7 by Engr. Umbreen Sabir

5 H OW AN A SSEMBLER W ORKS C ONT. 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/2009 5 CA&O Lecture 7 by Engr. Umbreen Sabir

6 O BJECT F ILE C OMPONENTS 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/2009 6 CA&O Lecture 7 by Engr. Umbreen Sabir

7 E XAMPLE 20/2/2009 7 CA&O Lecture 7 by Engr. Umbreen Sabir

8 R ESOLVING A DDRESSES - 1 ST P ASS “O LD - STYLE ” 2- PASS ASSEMBLER APPROACH 20/2/2009 8 CA&O Lecture 7 by Engr. Umbreen Sabir

9 R ESOLVING A DDRESSES – 2 ND P ASS 20/2/2009 9 CA&O Lecture 7 by Engr. Umbreen Sabir

10 M ODERN W AY – 1-P ASS A SSEMBLERS 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/2009 10 CA&O Lecture 7 by Engr. Umbreen Sabir

11 T HE R OLE OF A L INKER Some aspects of address resolution cannot be handled by the assembler alone. 1) References to data or routines in other object modules 2)The layout of all segments in memory 3) Support for REUSABLE code modules 4) Support for RELOCATABLE code modules This final step of resolution is the job of a LINKER 20/2/2009 11 CA&O Lecture 7 by Engr. Umbreen Sabir

12 L INKER 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/2009 12 CA&O Lecture 7 by Engr. Umbreen Sabir

13 L OADER 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/2009 13 CA&O Lecture 7 by Engr. Umbreen Sabir

14 S TATIC AND D YNAMIC L IBRARIES 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/2009 14 CA&O Lecture 7 by Engr. Umbreen Sabir

15 D YNAMICALLY L INKED L IBRARIES 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/2009 15 CA&O Lecture 7 by Engr. Umbreen Sabir

16 D YNAMICALLY L INKED L IBRARIES 20/2/2009 16 CA&O Lecture 7 by Engr. Umbreen Sabir

17 D YNAMICALLY L INKED L IBRARIES 20/2/2009 17 CA&O Lecture 7 by Engr. Umbreen Sabir

18 D YNAMICALLY L INKED L IBRARIES 20/2/2009 18 CA&O Lecture 7 by Engr. Umbreen Sabir

19 M ODERN L ANGUAGES 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/2009 19 CA&O Lecture 7 by Engr. Umbreen Sabir

20 M ODERN L ANGUAGES Intermediate “object code language” 20/2/2009 20 CA&O Lecture 7 by Engr. Umbreen Sabir

21 M ODERN L ANGUAGES Intermediate “object code language” 20/2/2009 21 CA&O Lecture 7 by Engr. Umbreen Sabir

22 C OMPILER O PTIMIZATIONS 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/2009 22 CA&O Lecture 7 by Engr. Umbreen Sabir

23 C OMPILER O PTIMIZATIONS 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/2009 23 CA&O Lecture 7 by Engr. Umbreen Sabir

24 C OMPILER O PTIMIZATIONS 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/2009 24 CA&O Lecture 7 by Engr. Umbreen Sabir

25 C OMPILER O PTIMIZATIONS 20/2/2009 25 CA&O Lecture 7 by Engr. Umbreen Sabir

26 C OMPILER O PTIMIZATIONS Example “C” Code: 20/2/2009 26 CA&O Lecture 7 by Engr. Umbreen Sabir

27 U NOPTIMIZED A SSEMBLY O UTPUT With debug flags set: 20/2/2009 27 CA&O Lecture 7 by Engr. Umbreen Sabir

28 R EGISTER A LLOCATION Assign local variables to registers 20/2/2009 28 CA&O Lecture 7 by Engr. Umbreen Sabir

29 L OOP -I NVARIANT C ODE M OTION Assign globals to temp registers and moves assignments outside of loop 20/2/2009 29 CA&O Lecture 7 by Engr. Umbreen Sabir

30 R EMOVE U NNECESSARY T ESTS 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/2009 30 CA&O Lecture 7 by Engr. Umbreen Sabir

31 R EMOVE U NNECESSARY S TORES All we care about it the value of total after the loop, and simplify loop 20/2/2009 31 CA&O Lecture 7 by Engr. Umbreen Sabir

32 U NROLLING L OOP Two copies of the inner loop reduce the branching overhead 20/2/2009 32 CA&O Lecture 7 by Engr. Umbreen Sabir


Download ppt "C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology."

Similar presentations


Ads by Google