Computer Organization and Design Assembly & Compilation

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
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.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Organization CS224
Computer Architecture CSCE 350
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.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
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.
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.
MIPS Assembler Programming
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.
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.
C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology.
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,
Lecture 4: MIPS Instruction Set
Computer Organization and Design Addressing Modes Montek Singh Sep 30, 2015 Lecture 7.
L07 – Assemblers and Compilers 1 Comp 411 Assemblers and Compilers When I find my code in tons of trouble, Friends and colleagues come to me, Speaking.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Computer Organization and Design Assember & Compiler Montek Singh Mon, Feb 21, 2011 Lecture 7.
The Assembly Process Computer Organization and Assembly Language: Module 10.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
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.
Lecture 3 Translation.
Computer Architecture & Operations I
Lecture 6: Assembly Programs
The University of Adelaide, School of Computer Science
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Topic 2e High-Level languages and Systems Software
Lecture 4: MIPS Instruction Set
Computer Organization and Design Addressing Modes
Assemblers and Compilers
Addressing in Jumps jump j Label go to Label op address 2 address
MIPS Instructions.
Operands and Addressing Modes
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
MIPS coding.
Lecture 6: Assembly Programs
MIPS Coding Continued.
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Addressing Modes Don Porter.
Computer Organization and Design Addressing Modes
Program Assembly.
Operands and Addressing Modes
Conditional Branching (beq)
Presentation transcript:

Computer Organization and Design Assembly & Compilation Montek Singh Mon, Oct 8, 2012 Lecture 9

Path from Programs to Bits Traditional Compilation High-level, portable (architecture independent) program description C or C++ program “Library Routines” A collection of precompiled object code modules Linker Compiler Architecture dependent mnemonic program description with symbolic memory references Machine language with all memory references resolved Assembly Code “Executable” Assembler Loader Machine language with symbolic memory references Program and data bits loaded into memory “Object Code” “Memory”

How an Assembler Works Three major components of assembly Allocating and initializing data storage Conversion of mnemonics to binary instructions Resolving addresses .data array: .space 40 total: .word 0 .text .globl main main: la $t1,array move $t2,$0 move $t3,$0 beq $0,$0,test loop: sll $t0,$t3,2 add $t0,$t1,$t0 sw $t3,($t0) add $t2,$t2,$t3 addi $t3,$t3,1 test: slti $t0,$t3,10 bne $t0,$0,loop sw $t2,total j $ra Suppose the starts of .data and .text are not specified lui $9, arrayhi ori $9,$9,arraylo 0x3c09???? 0x3529????

Resolving Addresses: 1st Pass “Old-style” 2-pass assembler approach In first pass, data/instr are encoded and assigned offsets within their segment, while the symbol table is constructed. Unresolved address references are set to 0 Segment offset Code Instruction 0 4 0x3c090000 0x35290000 la $t1,array 8 12 0x00005021 0x00005821 move $t2,$ move $t3,$0 16 0x10000000 beq $0,$0,test 20 0x000b4080 loop: sll $t0,$t3,2 24 28 32 36 0x01284020 0xad0b0000 0x014b5020 0x216b0001 add $t0,$t1,$t0 sw $t0,($t0) add $t0,$t1,$t0 addi $t3,$t3,1 40 0x2968000a test: slti $t0,$t3,10 44 0x15000000 bne $t0,$0,loop 48 52 0x3c010000 0xac2a0000 sw $t2,total 56 0x03e00008 j $ra Pass 1 Symbol table after Pass 1 Symbol Segment Location pointer offset array data total 40 main text loop 20 test

Resolving Addresses: 2nd Pass “Old-style” 2-pass assembler approach In the second pass, the appropriate fields of those instructions that reference memory are filled in with the correct values if possible. Pass 2 Segment offset Code Instruction 0 4 0x3c091001 0x35290000 la $t1,array 8 12 0x00005021 0x00005821 move $t2,$ move $t3,$0 16 0x10000005 beq $0,$0,test 20 0x000b4080 loop: sll $t0,$t3,2 24 28 32 36 0x01284020 0xad0b0000 0x014b5020 0x216b0001 add $t0,$t1,$t0 sw $t0,($t0) add $t0,$t1,$t0 addi $t3,$t3,1 40 0x2968000a test: slti $t0,$t3,10 44 0x1500fff9 bne $t0,$0,loop 48 52 0x3c011001 0xac2a0028 sw $t2,total 56 0x03e00008 j $ra Symbol table after Pass 1 Symbol Segment Location pointer offset array data total 40 main text loop 20 test

Modern Way: Single-Pass Assemblers Modern assemblers essentially single-pass keep more information in symbol table to resolve addresses in a single pass there is a second step, called “back filling” known addresses (back references) are immediately resolved unknown addresses (forward references) are “back-filled” once resolved for each symbol, a “reference list” is maintained to keep track of which instructions need to be back-filled SYMBOL SEGMENT Location pointer offset Resolved? Reference list array data y null total 40 main text loop 16 test ? n

The Role of a Linker Some aspects of address resolution cannot be handled by the assembler alone References to data or routines in other object modules The layout of all segments in memory Support for REUSABLE code modules Support for RELOCATABLE code modules This final step of resolution is the job of a LINKER Source file Assembler Object file Executable File Source file Assembler Object file Linker Source file Assembler Object file Libraries

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 lining is called DYNAMIC linking (e.x. .dll).

Modern Languages Intermediate “object code language” Java program High-level, portable (architecture independent) program description Java program Compiler PORTABLE mnemonic program description with symbolic memory references JVM bytecode “Library Routines” An application that EMULATES a virtual machine. Can be written for any Instruction Set Architecture. In the end, machine language instructions must be executed for each JVM bytecode Interpreter

Modern Languages Intermediate “object code language” Java program High-level, portable (architecture independent) program description Java program Compiler PORTABLE mnemonic program description with symbolic memory references JVM bytecode “Library Routines” While interpreting on the first pass it keeps a copy of the machine language instructions used. Future references access machine language code, avoiding further interpretation JIT Compiler Memory

Self-Study Example A simple C program to: The following slides show: Initialize an array with the values 0, 1, 2… Add the array elements together The following slides show: The C code A straightforward (non-optimized) compiled assembly version Several optimized versions that: Use registers wherever possible, instead of memory locations Remove unnecessary branch tests Remove unnecessary stores Unroll the loop (i.e., replicate the loop body so there are fewer branch instructions overall)

Compiler Optimizations Example “C” Code: array and total are global variables (in .data) i is local (on stack) int array[10]; int total; int main( ) { int i; total = 0; for (i = 0; i < 10; i++) { a[i] = i; total = total + i; }

Unoptimized Assembly Output Compile C  assembly without optimizations .globl main .text main: addi $sp,$sp,-8 # allocates space for ra and i sw $0,total # total = 0 sw $ra,4($sp) # save a copy of the return address sw $0,0($sp) # save i on stack, and initialize 0 lw $8,0($sp) # copy i into register $t0 j L.3 # jump to test L.2: # for(...) { sll $24,$8,2 # make i a word offset sw $8,array($24) # array[i] = i lw $24,total # total = total + i addu $24,$24,$8 sw $24,total addi $8,$8,1 # i = i + 1 L.3: sw $8,0($sp) # update i in memory slti $1,$8,10 # is i < 10? bne $1,$0,L.2 #} loops while i < 10 lw $ra,4($sp) # restore the return address addi $sp,$sp,8 jr $ra

Register Allocation Optimization: put variables in registers so “i” is not repeatedly read/written from/to memory .globl main .text main: addi $sp,$sp,-4 #allocates space only for ra sw $ra,0($sp) # save a copy of the return address sw $0,total #total = 0 add $8,$0,$0 #i = 0 j L.3 #goto test L.2: #for(...) { sll $24,$8,2 # make i a word offset sw $8,array($24) # array[i] = i lw $24,total # total = total + i addu $24,$24,$8 sw $24,total addi $8,$8,1 # i = i + 1 L.3: slti $1,$8,10 # is i < 10? bne $1,$0,L.2 #} loops while i < 10 lw $ra,0($sp) # restore the return address addi $sp,$sp,4 jr $ra

Loop-Invariant Code Motion Assign even globals to registers .globl main .text main: addu $sp,$sp,-4 #allocates space for ra sw $ra,0($sp) # save a copy of the return address sw $0,total #total = 0 add $9,$0,$0 #temp for total add $8,$0,$0 #i = 0 j L.3 #goto test L.2: #for(...) { sll $24,$8,2 # make i a word offset sw $8,array($24) # array[i] = i addu $9,$9,$8 sw $9,total addi $8,$8,1 # i = i + 1 L.3: slti $1,$8,10 # is i < 10? bne $1,$0,L.2 #} loops while i < 10 lw $ra,0($sp) # restore the return address addi $sp,$sp,4 jr $ra

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? .globl main .text main: addi $sp,$sp,-4 #allocates space for ra sw $0,total #total = 0 add $9,$0,$0 #temp for total add $8,$0,$0 #i = 0 L.2: #for(...) { sll $24,$8,2 # make i a word offset sw $8,array($24) # array[i] = i addu $9,$9,$8 sw $9,total addi $8,$8,1 # i = i + 1 slti $24,$8,10 # loads const 10 bne $24,$0,L.2 #} loops while i < 10 addi $sp,$sp,4 j $ra

Remove Unnecessary Stores All we care about is the value of total after the loop, and simplify loop .globl main .text main: addi $sp,$sp,-4 #allocates space for ra and i sw $0,total #total = 0 add $9,$0,$0 #temp for total add $8,$0,$0 #i = 0 L.2: sll $24,$8,2 #for(...) { sw $8,array($24) # array[i] = i addu $9,$9,$8 addi $8,$8,1 # i = i + 1 slti $24,$8,10 # loads const 10 bne $24,$0,L.2 #} loops while i < 10 sw $9,total addi $sp,$sp,4 j $ra

Unrolling Loop Two copies of the inner loop reduce the branching overhead .globl main .text main: addi $sp,$sp,-4 #allocates space for ra and i sw $0,total #total = 0 move $9,$0 #temp for total move $8,$0 #i = 0 L.2: sll $24,$8,2 #for(...) { sw $8,array($24) # array[i] = i addu $9,$9,$8 addi $8,$8,1 # i = i + 1 sll $24,$8,2 # slti $24,$8,10 # loads const 10 bne $24,$0,L.2 #} loops while i < 10 sw $9,total addi $sp,$sp,4 j $ra