1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.

Slides:



Advertisements
Similar presentations
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Advertisements

Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
CS61C L11 Linker © UC Regents 1 CS61C - Machine Structures Lecture 11 - Starting a Program October 4, 2000 David Patterson
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 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.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
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.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
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
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
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.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
COMP3221 lec25-linker.II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 25: Compiler, Assembler, Linker and Loader – II
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Computer Architecture and Design – ECEN 350 Part 4 [Some slides adapted from M. Irwin, D. Paterson and others]
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 3204 Operating Systems Godmar Back Lecture 11.
Introduction CS 104: Applied C++ What is Programming? For some given problem: __________ a solution for it -- identify, organize & store the problem's.
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
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.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
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),
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) 1 Instructors: Nicholas Weaver & Vladimir Stojanovic
Linking I Topics Assembly and symbol resolution Static linking Systems I.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
1 Computer Architecture & Assembly Language Spring 2001 Dr. Richard Spillman Lecture 10 –Assembly V.
Lecture 3 Translation.
Reading Condition Codes (Cont.)
Instruction Set Architecture
Assemblers, linkers, loaders
Computer Architecture & Operations I
Computer Architecture and Assembly Language
The University of Adelaide, School of Computer Science
Homework Reading Machine Projects Labs PAL, pp ,
May 2006 Saeid Nooshabadi ELEC2041 Microprocessors and Interfacing Lectures 25: Compiler, Assembler, Linker and Loader – II
CS 5204 Operating Systems Linking and Loading Godmar Back.
May 2006 Saeid Nooshabadi ELEC2041 Microprocessors and Interfacing Lectures 24: Compiler, Assembler, Linker and Loader – I
Machine-Level Programming 1 Introduction
There is one handout today at the front and back of the room!
asum.ys A Y86 Programming Example
Computer Architecture and Assembly Language
Machine-Level Programming 4 Procedures
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Green Subscription Based PC Announced
CS 4284 Systems Capstone Linking and Loading Godmar Back.
Assembly Language Programming II: C Compiler Calling Sequences
Computer Organization and Design Assembly & Compilation
Machine-Level Programming: Introduction
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
Computer Architecture and System Programming Laboratory
Presentation transcript:

1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization, ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 21 April 2004 L. Yohanes Stefanus Bobby Nazief bahan kuliah:

2 Steps to Starting a Program C program: foo.c Assembly program: foo.s Executable(mach lang pgm): foo.exe Compiler Assembler Linker Loader Memory Object(mach lang module): foo.o lib.o

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

4 Compiler °Input: High-Level Language Code (e.g., C, Java) °Output: Assembly Language Code (e.g., Intel x86) °Note: Output may contain directives & pseudoinstructions

5 Example: C  Asm  Obj  Exe  Run segment.text LC0: db "The sum from is %d",0xa,0 _main: push ebp mov ebp,esp sub esp,24 mov dword [ebp-8],0 mov dword [ebp-4],0 L3: cmp dword [ebp-4],100 jle L6 jmp L4 L6: mov eax,[ebp-4] imul eax,[ebp-4] add [ebp-8],eax L5: inc dword [ebp-4] jmp L3 L4: add esp,-8 mov eax,[ebp-8] push eax push dword LC0 call _printf add esp,16 L2: mov esp,ebp pop ebp ret

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

7 Assembler °Reads and Uses Directives °Replace Pseudoinstructions °Produce Machine Language °Creates Object File

8 Producing Machine Language °Simple Case Arithmetic, Logical, Shifts, and so on. All necessary info is within the instruction already. °What about Branches? PC-Relative So once pseudoinstructions are replaced by real ones, we know by how many instructions to branch. °What about jumps? Some require absolute address. °What about references to data? These will require the full 32-bit address of the data. °Addresses can’t be determined yet, so we create two tables…

9 Symbol Table °List of “items” in this file that may be used by other files. °What are they? Labels: function calling Data: anything in the.data section; variables which may be accessed across files °First Pass: record label-address pairs °Second Pass: produce machine code Result: can jump to a later label without first declaring it

10 Relocation Table °List of “items” for which this file needs the address. °What are they? Any label jumped to: jmp or call -internal -external (including lib files) Any piece of data

11 Object File Format °object file header: size and position of the other pieces of the object file °text segment: the machine code °data segment: binary representation of the data in the source file °relocation information: identifies lines of code that need to be “handled” °symbol table: list of this file’s labels and data that can be referenced °debugging information

12 Example: C  Asm  Obj  Exe  Run segment.text 0x0: db "The sum from is %d",0xa,0 0x1d: push ebp mov ebp,esp sub esp,24 mov dword [ebp-8],0 mov dword [ebp-4],0 0x34: cmp dword [ebp-4],100 jle 0x05 (0x42) jmp 0x (0x54) 0x42: mov eax,[ebp-4] imul eax,[ebp-4] add [ebp-8],eax 0x4c: inc dword [ebp-4] jmp 0xffffffe0 (0x34) 0x54: add esp,-8 mov eax,[ebp-8] push eax push 0x0 call 0x0 add esp,16 0x6e: mov esp,ebp pop ebp ret

13 Symbol Table Entries °Symbol Table Label Address LC0:0x main:0x d L3:0x L6:0x L5:0x c L4:0x L2:0x e °Relocation Information OffsetTypeValue 0x fdir32.text (LC0: offset 0 of.text segment) 0x DISP32_printf

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

15 Link Editor/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

16 Four Types of Addresses °PC-Relative Addressing ( beq, bne ): never relocate °Absolute Address ( jmp, call ): always relocate °External Reference (usually call ): always relocate °Data Reference: always relocate

17 Resolving References °Linker assumes first word of first text segment is at address 0x °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 °To resolve references: search for reference (data or label) in all 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)

18 Example: C  Asm  Obj  Exe  Run segment.text 0x15c0: db "The sum from is %d",0xa,0 0x15dd: push ebp mov ebp,esp sub esp,24 mov dword [ebp-8],0 mov dword [ebp-4],0 0x15f4: cmp dword [ebp-4],100 jle 0x05 (0x1602) jmp 0x12 (0x1614) 0x1602: mov eax,[ebp-4] imul eax,[ebp-4] add [ebp-8],eax 0x160c: inc dword [ebp-4] jmp 0xe0 (0x15f4) 0x1614: add esp,-8 mov eax,[ebp-8] push eax push 0x000015c0 call 0x (0x2da0)* add esp,16 0x162e: mov esp,ebp pop ebp ret *0x x1778 = 0x2da0

19 Peta Memori.EXE C B BB04 Obj lainnya (..., _printf,...) Obj lainnya Foo.o.text.data

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

21 Loader (1/3) °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

22 Loader (2/3) °So what does a loader do? °Reads executable file’s header to determine size of text and data segments °Creates new address space for program large enough to hold text and data segments, along with a stack segment °Copies instructions and data from executable file into the new address space (this may be anywhere in memory)

23 Loader (3/3) °Copies arguments passed to the program onto the stack °Initializes machine registers Most registers cleared, but stack pointer assigned address of 1st free stack location °Jumps to start-up routine that copies program’s arguments from stack to registers and sets the PC If main routine returns, start-up routine terminates program with the exit system call

24 Example: C  Asm  Obj  Exe  Run 0x000015c0:0x x206d75730x6d6f72660x2e x000015d0:0x e0x x0a xe x000015e0:0x0018ec810x45c700000x000000f80xfc45c700 0x000015f0:0x x64fc7d810x7e x0012e905 0x :0x458b00000x45af0ffc0xf84501fc0xe9fc45ff 0x :0xffffffe00xfff8c4810x458bffff0xc06850f8 0x :0xe x x0010c4810xec x :0x0000c35d 0x000015c0: f 6d e T h e s u m f r o m dd: 55push ebp de:89e5 mov ebp,esp e0: 81ec sub esp,0x e6: c745f mov [ebp-8], ed: c745fc mov [ebp-4], f4: 817dfc cmp [ebp-4],0x fb: 7e05 jle 0x fd: e jmp 0x1614

25.ASM,.O, &.EXE (FORMAT COFF)

26 Example: C  Asm  Obj  Exe  Run.text LC0:.ascii "The sum from is %d\12\0" main: pushl %ebp movl %esp,%ebp subl $24,%esp movl $0,-8(%ebp) movl $0,-4(%ebp) L3: cmpl $100,-4(%ebp) jle L6 jmp L4 L6: movl -4(%ebp),%eax imull -4(%ebp),%eax addl %eax,-8(%ebp) L5: incl -4(%ebp) jmp L3 L4: addl $-8,%esp movl -8(%ebp),%eax pushl %eax pushl LC0 call _printf addl $16,%esp L2: movl %ebp,%esp popl %ebp ret

27 Example: C  Asm  Obj  Exe  Run.text 0x0:.ascii "The sum from is %d\12\0" 0x20: pushl %ebp movl %esp,%ebp subl $24,%esp movl $0,-8(%ebp) movl $0,-4(%ebp) 0x34: cmpl $100,-4(%ebp) jle 6 (0x40) jmp 0x14 (0x50) 0x40: movl -4(%ebp),%eax imull -4(%ebp),%eax addl %eax,-8(%ebp) 0x4a: incl -4(%ebp) jmp -0x1b (0x34) 0x50: addl $-8,%esp movl -8(%ebp),%eax pushl %eax pushl 0x0 call 0x0 (undefined) addl $16,%esp 0x64: movl %ebp,%esp popl %ebp ret

28 Symbol Table Entries °Symbol Table Label Address LC0:0x L2:0x L3:0x L4:0x L5:0x a L6:0x main:0x °Relocation Information AddressInstr. TypeDependency 0x ccallprintf

29 Example: C  Asm  Obj  Exe  Run.text 0x15c0:.ascii "The sum from is %d\12\0" 0x15e0: pushl %ebp movl %esp,%ebp subl $24,%esp movl $0,-8(%ebp) movl $0,-4(%ebp) 0x15f4: cmpl $100,-4(%ebp) jle 6 (0x1600) jmp 0x14 (0x1610) 0x1600: movl -4(%ebp),%eax imull -4(%ebp),%eax addl %eax,-8(%ebp) 0x160a: incl -4(%ebp) jmp -0x1b (0x15f4) 0x1610: addl $-8,%esp movl -8(%ebp),%eax pushl %eax pushl 0x15c0 call 0x2d90 addl $16,%esp 0x1624: movl %ebp,%esp popl %ebp ret