CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.

Slides:



Advertisements
Similar presentations
Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.
Advertisements

Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
CS61C L11 Linker © UC Regents 1 CS61C - Machine Structures Lecture 11 - Starting a Program October 4, 2000 David Patterson
8.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
Compiling, Assembling, Loading, Linking (CALL) I (1) Fall 2005 Lecture 09: Program Translation.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Instructor: Justin Hsia 7/08/2013Summer Lecture #81 CS 61C: Great Ideas in Computer Architecture Running a Program.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L18 Running a Program I (1) Garcia, Spring 2007 © UCB Gaze-controlled UI!  Researchers at Stanford have designed a system that allows a user to.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 19 – Running a Program II (Compiling, Assembling, Linking, Loading) Researchers.
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
CS 61C L13 CALL (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #13: CALL Andy Carle.
CS61C L18 Running a Program I (1) Garcia, Fall 2006 © UCB Security hole finder  Google’s just-released new /* Code Search */, which allows you to specify.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L18 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) I (1) Garcia © UCB Lecturer PSOE Dan Garcia
CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
MIPS Assembler Programming
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 19 – Running a Program II (Compiling, Assembling, Linking, Loading) I’m.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 16 – Running a Program (Compiling, Assembling, Linking, Loading) Highlights:
CS61C L13 CALL I (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #13 – Compiling,
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
Cs 61C L10 Start.1 Patterson Spring 99 ©UCB CS61C Starting a Program Lecture 10 February 19, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CSCI 136 Lab 1 Outline Go through the CD attached to the textbook. Homework Hints 135 Review.
Computer Architecture and Design – ECEN 350 Part 4 [Some slides adapted from M. Irwin, D. Paterson and others]
CS61C L13 Compiling, Linking, and Loading (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 – Running.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS coding. SPIM Some links can be found such as:
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 4.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
CS 61C L3.2.2 Floating Point 2 (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Floating Point II & Linking Kurt Meinz.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 16 – Running a Program (Compiling, Assembling, Linking, Loading) Research shows laptops.
8.
CENG 311 Starting a Program. Review (1/2) °IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with +/- infinity, Not-a-Number (Nan),
The Assembly Process Computer Organization and Assembly Language: Module 10.
CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading) 1 Instructors: Nick Weaver & Vladimir.
CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) 1 Instructors: Nicholas Weaver & Vladimir Stojanovic
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 110 Computer Architecture Lecture 7: Running a Program - CALL (Compiling, Assembling, Linking, and Loading) Instructor: Sören Schwertfeger
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 – Loaders.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
1 Computer Architecture & Assembly Language Spring 2001 Dr. Richard Spillman Lecture 10 –Assembly V.
Lecture 3 Translation.
faculty “re-imagine” ugrad education
Assemblers, linkers, loaders
Computer Architecture & Operations I
IT 251 Computer Organization and Architecture
System Programming and administration
May 2006 Saeid Nooshabadi ELEC2041 Microprocessors and Interfacing Lectures 25: Compiler, Assembler, Linker and Loader – II
Instructor: Justin Hsia
Topic 2e High-Level languages and Systems Software
There is one handout today at the front and back of the room!
CALL & Pthread.
Assemblers and Compilers
Green Subscription Based PC Announced
Computer Organization and Design Assembly & Compilation
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
Presentation transcript:

CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB 9

C program: foo.c Compiler Assembly program: foo.s Assembler Linker Executable(mach lang pgm): a.out Loader Memory Object(mach lang module): foo.o lib.o Where Are We Now?

Input: Object Code files (e.g., foo.o,libc.o for MIPS) Output: Executable Code (e.g., a.out for MIPS) Combines several object (.o) files into a single executable (“linking”) Enable Separate Compilation of files –Changes to one file do not require recompilation of whole program Windows NT source is > 40 M lines of code! –Old name “Link Editor” from editing the “links” in jump and link instructions Linker

.o file 1 text 1 data 1 info 1.o file 2 text 2 data 2 info 2 Linker a.out Relocated text 1 Relocated text 2 Relocated data 1 Relocated data 2 Linker

Step 1: Take text segment from each.o file and put them together Step 2: Take data segment from each.o file, put them together, and concatenate this onto end of text segments Step 3: Resolve References –Go through Relocation Table and handle each entry –That is, fill in all absolute addresses Linker

Linker assumes first word of first text segment is at address 0x (More on this later when we study “virtual memory”) Linker knows: –length of each text and data segment –ordering of text and data segments Linker calculates: –absolute address of each label to be jumped to (internal or external) and each piece of data being referenced Resolving References

To resolve references: –Based on list in each relocation table search for reference (label) in all “user” symbol tables –if not found, search library files (for example, for printf ) –once absolute address is determined, fill in the machine code appropriately Output of linker: executable file containing text and data (plus header) Resolving References

Static vs. Dynamically linked libraries What we’ve described is the traditional way: “statically- linked” approach –The library is now part of the executable, so if the library updates we don’t get the fix (have to recompile if we have source) –It includes the entire library even if not all of it will be used –Executable is self-contained An alternative is dynamically linked libraries (DLL), common on Windows & UNIX platforms –1 st run overhead for dynamic linker-loader –Having executable isn’t enough anymore!

C program: foo.c Compiler Assembly program: foo.s Assembler Linker Executable(mach lang pgm): a.out Loader Memory Object(mach lang module): foo.o lib.o Where Are We Now?

Input: Executable Code (e.g., a.out for MIPS) Output: (program is run) Executable files are stored on disk When one is run, loader’s job is to load it into memory and start it running In reality, loader is the operating system (OS) –loading is one of the OS tasks Loader

Example: C  Asm  Obj  Exe  Run #include int main (int argc, char *argv[]) { int i, sum = 0; for (i = 0; i <= 100; i++) sum = sum + i * i; printf ("The sum from is %d\n", sum); } prog.c printf lives in libc

Compilation: MIPS.text.align2.globlmain main: subu $sp,$sp,32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6,$t6 lw$t8, 24($sp) addu $t9,$t8,$t7 sw$t9, 24($sp) addu $t0, $t6, 1 sw$t0, 28($sp) ble$t0,100, loop la$a0, str lw$a1, 24($sp) jal printf move $v0, $0 lw$ra, 20($sp) addiu $sp,$sp,32 jr $ra.data.align0 str:.asciiz"The sum from is %d\n" Where are 7 pseudo- instructions?

Compilation: MIPS.text.align2.globlmain main: subu $sp,$sp,32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6,$t6 lw$t8, 24($sp) addu $t9,$t8,$t7 sw$t9, 24($sp) addu $t0, $t6, 1 sw$t0, 28($sp) ble$t0,100, loop la$a0, str lw$a1, 24($sp) jal printf move $v0, $0 lw$ra, 20($sp) addiu $sp,$sp,32 jr $ra.data.align0 str:.asciiz"The sum from is %d\n" 7 pseudo- instructions underlined

Assembly step 1 00 addiu $29,$29, sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, loop 40 lui$4, l.str 44 ori$4,$4,r.str 48 lw$5,24($29) 4c jalprintf 50 add$2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Remove pseudoinstructions, assign addresses

Assembly step 2 Create relocation table and symbol table Symbol Table –Label Address (in module) Type main:0x global text loop:0x local text str:0x local data Relocation Information –AddressInstr. TypeDependency 0x luil.str 0x orir.str 0x cjalprintf

Assembly step 3 00 addiu $29,$29, sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, lui$4, l.str 44 ori$4,$4,r.str 48 lw $5,24($29) 4c jal printf 50 add $2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Resolve local PC-relative labels

Assembly step 4 Generate object (.o) file –Output binary representation for text segment (instructions) data segment (data) symbol and relocation tables –Using dummy “placeholders” or “guesses” for unresolved absolute and external references

Text segment in object file 0x x x x00000c x x x x00001c x x x x00002c x x x x00003c x x x x00004c x x x x00005c

Link step 1: combine prog.o, libc.o Merge text/data segments Create absolute memory addresses Modify & merge symbol and relocation tables Symbol Table –Label Address (in module) main:0x loop:0x str:0x printf:0x000003b0 Relocation Information –AddressInstr. TypeDependency 0x luil.str 0x orir.str 0x cjalprintf

Link step 2 00 addiu $29,$29, sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, lui$4, ori$4,$4, lw $5,24($29) 4c jal add $2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Edit Addresses in relocation table (in binary)

Link step 3 Output executable of merged modules –Single text (instruction) segment –Single data segment –Header detailing size of each segment Note –The preceding example was a much simplified version of how ELF and other standard formats work, meant only to demonstrate the basic principles

Decimal Numbers: Base 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Example: 3271 = (3x10 3 ) + (2x10 2 ) + (7x10 1 ) + (1x10 0 )

Number Base B  B symbols per digit: –Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 –Base 2 (Binary): 0, 1 Number representation: –d 31 d d 1 d 0 is a 32 digit number –value = d 31  B 31 + d 30  B d 1  B 1 + d 0  B 0 Binary: 0,1 (in binary digits called “bits”) –0b11010 = 1      2 0 = = 26 –Here 5 digit binary # turns into a 2 digit decimal # –Can we find a base that converts to binary easily? #s often written 0b… Numbers: positional notation

Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F –Normal digits + 6 more from the alphabet –In C, written as 0x… (e.g., 0xFAB5) Conversion: Binary  Hex –1 hex digit represents 16 decimal values –4 binary digits represent 16 decimal values  1 hex digit replaces 4 binary digits Example: – (binary) = 0x_____ ? Hexadecimal Numbers: Base 16

Examples: (binary) = 0xAC (binary) = (binary) = 0x17 0x3F9 = (binary) How do we convert between hex and Decimal? A B C D E F1111 MEMORIZE! Decimal vs. Hexadecimal vs. Binary

What to do with representations of numbers? Just what we do with numbers! –Add them –Subtract them –Multiply them –Divide them –Compare them Example: = 17 –…so simple to add in binary that we can build circuits to do it! –subtraction just as you would in decimal –Comparison: How do you tell if X > Y ?

Characters? –26 letters  5 bits (2 5 = 32) –upper/lower case + punctuation  7 bits (in 8) (“ASCII”) –standard code to cover all the world’s languages  8,16,32 bits (“Unicode”) Logical values? –0  False, 1  True colors ? Ex: locations / addresses? commands? MEMORIZE: N bits  at most 2 N things Red (00)Green (01)Blue (11) BIG IDEA: Bits can represent anything!!