The University of Adelaide, School of Computer Science

Slides:



Advertisements
Similar presentations
C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface 5 th Edition Chapter 2 Instructions: Language of the Computer.
Advertisements

The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 9: MIPS Instruction Set
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Organization CS224
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
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.
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.
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
1 Lecture 5: MIPS Examples Today’s topics:  the compilation process  full example – sort in C Reminder: 2 nd assignment will be posted later today.
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.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
MIPS coding. SPIM Some links can be found such as:
C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology.
© 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.
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.
Chapter 2 — Instructions: Language of the Computer — 1 Branch Addressing Branch instructions specify – Opcode, two registers, target address Most branch.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 14, 15 Addressing Mode.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
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.
C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface Chapter 2 Instructions: Language of the Computer.
Lecture 3 Translation.
Computer Architecture & Operations I
The University of Adelaide, School of Computer Science
Assemblers, linkers, loaders
Computer Architecture & Operations I
Lecture 6: Assembly Programs
MIPS ISA-II: Procedure Calls & Program Assembly
The University of Adelaide, School of Computer Science
Linking & Loading.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
Program Execution in Linux
Procedures (Functions)
The University of Adelaide, School of Computer Science
Topic 2e High-Level languages and Systems Software
ARM Assembly Programming
Assemblers and Compilers
The University of Adelaide, School of Computer Science
Lecture 7: Examples, MARS, Arithmetic
Computer Organization and Design Assembly & Compilation
Computer Architecture
Lecture 6: Assembly Programs
Program Execution in Linux
The University of Adelaide, School of Computer Science
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
Program Assembly.
Topic 2b ISA Support for High-Level Languages
The University of Adelaide, School of Computer Science
Presentation transcript:

The University of Adelaide, School of Computer Science 5 July 2018 Chapter 2 Instructions: Language of the Computer Chapter 2 — Instructions: Language of the Computer

Linking Object Modules The University of Adelaide, School of Computer Science 5 July 2018 Linking Object Modules A linker produces an executable image 1. Merges segments 2. Resolve labels (determine their addresses) 3. Patch location-dependent and external refs Could leave location dependencies for fixing by a relocating loader But with virtual memory, no need to do this Program can be loaded into absolute location in virtual memory space 2 Chapter 2 — Instructions: Language of the Computer

Linking example Link the two object files below. Show updated addresses of the first few instructions of the completed executable file. Object file header Name Procedure A Text size 100hex Data size 20hex Text segment Address Instruction LDUR X0, [X27,#0] 4 BL 0 … Data segment (X) Relocation information Instruction type Dependency LDUR X BL B Symbol table Label - Procedure A needs to find the address for the variable labeled X to put in the load instruction and to find the address of procedure B to place in the BL instruction. 3

Linking example Procedure B needs the address of the variable labeled Y for the store instruction and the address of procedure A for its BL instruction. Object file header Name Procedure B Text size 200hex Data size 30hex Text segment Address Instruction STUR X1, [X27,#0] 4 BL 0 … Data segment (Y) Relocation information Instruction type Dependency STUR Y BL A Symbol table Label - 4

Linking example From Figure 2.14 on page 108, Text segment starts at address 0000 0000 0040 0000hex and data segment at 0000 0000 1000 0000hex. Executable file header Text size 300hex Data Size 50hex Text segment Address Instruction 0000 0000 0040 0000hex LDUR X0, [X27,#0hex] 0000 0000 0040 0004hex BL 000 00FChex … 0000 0000 0040 0100hex STUR X1, [X27,#20hex] 0000 0000 0040 0104hex BL 3FF FEFChex Data segment 0000 0000 1000 0000hex (X) 0000 0000 1000 0020hex (Y) 5

The University of Adelaide, School of Computer Science 5 July 2018 Loading a Program Load executable file on disk into memory 1. Read header to determine segment sizes 2. Create virtual address space 3. Copy text and initialized data into memory Or set page table entries so they can be faulted in 4. Set up arguments on stack 5. Initialize registers (including SP, FP) 6. Jump to startup routine Copies arguments to X0, … and calls main When main returns, do exit syscall 6 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 Dynamic Linking Only link/load library procedure when it is called Requires procedure code to be relocatable Avoids image bloat caused by static linking of all (transitively) referenced libraries Automatically picks up new library versions Initial version of DLLs linked all routines of the library instead of those that are called during the running of the program. Lazy procedure linkage version of DLLs, links each routine only after it is called 7 Chapter 2 — Instructions: Language of the Computer

Lazy Procedure Linkage The University of Adelaide, School of Computer Science 5 July 2018 Lazy Procedure Linkage Indirection table Stub: Loads routine ID, Jump to linker/loader Linker/loader code Dynamically mapped code 8 Chapter 2 — Instructions: Language of the Computer

Lazy Procedure Linkage DLLs require additional space for the information needed for dynamic linking, but do not require that whole libraries be copied or linked. They pay a good deal of overhead the first time a routine is called, but only a single indirect branch thereafter. Return from the library pays no extra overhead. 9

Starting Java Applications The University of Adelaide, School of Computer Science 5 July 2018 Starting Java Applications Simple portable instruction set for the JVM Compiles bytecodes of “hot” methods into native code for host machine Interprets bytecodes 10 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 C Sort Example Illustrates use of assembly instructions for a C bubble sort function Swap procedure (leaf) void swap(long long int v[], long long int k) { long long int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } v in X0, k in X1, temp in X9 §2.13 A C Sort Example to Put It All Together 11 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 The Procedure Swap swap: LSL X10,X1,#3 // X10 = k * 8 ADD X10,X0,X10 // X10 = address of v[k] LDUR X9,[X10,#0] // X9 = v[k] LDUR X11,[X10,#8] // X11 = v[k+1] STUR X11,[X10,#0] // v[k] = X11 (v[k+1]) STUR X9,[X10,#8] // v[k+1] = X9 (v[k]) BR LR // return to calling routine 12 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 The Sort Procedure in C Non-leaf (calls swap) void sort (long long int v[], size_t n) { size_t i, j; for (i = 0; i < n; i += 1) { for (j = i – 1; j >= 0 && v[j] > v[j + 1]; j -= 1) { swap(v,j); } v in X0, n in X1, i in X19, j in X20 13 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 The Outer Loop Skeleton of outer loop: for (i = 0; i <n; i += 1) { MOV X19,XZR // i = 0 for1tst: CMP X19, X1 // compare X19 to X1 (i to n) B.GE exit1 // go to exit1 if X19 ≥ X1 (i≥n) (body of outer for-loop) ADDI X19,X19,#1 // i += 1 B for1tst // branch to test of outer loop exit1: 14 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 The Inner Loop Skeleton of inner loop: for (j = i − 1; j >= 0 && v[j] > v[j + 1]; j − = 1) { SUBI X20, X19, #1 // j = i - 1 for2tst: CMP X20,XZR // compare X20 to 0 (j to 0) B.LT exit2 // go to exit2 if X20 < 0 (j < 0) LSL X10, X20, #3 // reg X10 = j * 8 ADD X11, X0, X10 // reg X11 = v + (j * 8) LDUR X12, [X11,#0] // reg X12 = v[j] LDUR X13, [X11,#8] // reg X13 = v[j + 1] CMP X12, X13 // compare X12 to X13 B.LE exit2 // go to exit2 if X12 ≤ X13 MOV X0, X21 // first swap parameter is v MOV X1, X20 // second swap parameter is j BL swap // call swap SUBI X20, X20, #1 // j –= 1 B for2tst // branch to test of inner loop exit2: 15 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 5 July 2018 Preserving Registers Preserve saved registers: SUBI SP,SP,#40 // make room on stack for 5 regs STUR LR,[SP,#32] // save LR on stack STUR X22,[SP,#24] // save X22 on stack STUR X21,[SP,#16] // save X21 on stack STUR X20,[SP,#8] // save X20 on stack STUR X19,[SP,#0] // save X19 on stack MOV X21, X0 // copy parameter X0 into X21 MOV X22, X1 // copy parameter X1 into X22 Restore saved registers: exit1: LDUR X19, [SP,#0] // restore X19 from stack LDUR X20, [SP,#8] // restore X20 from stack LDUR X21,[SP,#16] // restore X21 from stack LDUR X22,[SP,#24] // restore X22 from stack LDUR X30,[SP,#32] // restore LR from stack SUBI SP,SP,#40 // restore stack pointer 16 Chapter 2 — Instructions: Language of the Computer

The Full Procedure Sort 17

Effect of Compiler Optimization 18

Effect of Compiler Optimization The University of Adelaide, School of Computer Science 5 July 2018 Effect of Compiler Optimization Compiled with gcc for Pentium 4 under Linux 19 Chapter 2 — Instructions: Language of the Computer

Effect of Language and Algorithm 20