Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs.

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
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.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
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.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Spring 2008, Jan. 14 ELEC / Lecture 2 1 ELEC / Computer Architecture and Design Spring 2007 Introduction Vishwani D. Agrawal.
Intro to Computer Architecture
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.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
RISC Concepts, MIPS ISA and the Mini–MIPS project
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
Fall 2015, Aug 17 ELEC / Lecture 1 1 ELEC / Computer Architecture and Design Fall 2015 Introduction Vishwani D. Agrawal.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
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 Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
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.
Spring 2016, Jan 13 ELEC / Lecture 1 1 ELEC / Computer Architecture and Design Spring 2016 Introduction Vishwani D. Agrawal.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
ELEC / Computer Architecture and Design Fall 2014 Introduction
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs (Chapter 2) Vishwani D. Agrawal James J. Danaher Professor Department of Electrical and Computer Engineering Auburn University, Auburn, AL

Spr 2015, Feb 9... ELEC / Lecture 4 2 Software in a Computer Application software Programs user writes and runs Hardware Systems software Operating system Compiler assembler User

Spr 2015, Feb 9... ELEC / Lecture 4 3 Software Application software, a program in C: swap (int v[ ], int k) {int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } MIPS binary machine code: CompilerAssembler Application software Hardware Systems software See pages MIPS compiler output, assembly language program: swap; muli$2,$5, 4 add$2,$4, $2 lw$15, 0 ($2) lw$16, 4 ($2) sw$16, 0 ($2) sw$15, 4 ($2) jr$31

Spr 2015, Feb 9... ELEC / Lecture 4 4 Memory and Registers Word 0 Word 1 Word 2 Word n n. Word n+1 Register 0 Register 1 Register 2 Register 3 Register 4 Register 31 Register 5 Memory byte addr. jump addr. zero......

Spr 2015, Feb 9... ELEC / Lecture 4 5 Policy of Register Usage (Conventions) Register 1 ($at) reserved for assembler, for operating system 2004 © Morgan Kaufman Publishers

Spr 2015, Feb 9... ELEC / Lecture 4 6 Translating a Program to Executable Code C Program (Data) Compiler (Sys. Prog.) Assembly Language Code (Data) Assembler (Sys. Prog.) Machine Language Object Code (Data) Subroutine Library Machine Language Object Code (Data) Linker (Sys. Prog.) Machine Language Executable Code (Data) Loader (Sys. Prog.) Program in Computer Memory

Spr 2015, Feb 9... ELEC / Lecture 4 7 Compiler System program Inputs: Programming language code, e.g., a C program Instruction set (including pseudoinstructions) Memory and register organization Output: Assembly language code Compiler’s function Specific to ISA and machine organization Assigns variables to registers Translates C into assembly language Saves and restores registers $s0 through $s7 when compiling a subroutine (or procedure) that uses them

Spr 2015, Feb 9... ELEC / Lecture 4 8 Compiler is a Multi-Pass Program Programming language specific, machine-independent processing High-level optimization (loop transformation, procedure integration) Global optimization (register allocation, code optimization) Code generator C or Java Code MIPS Code Decreasing program dependence Increasing machine dependence Intermediate representation

Spr 2015, Feb 9... ELEC / Lecture 4 9 Compiler Optimizes Code Minimize number of machine instructions Example: x[i] = x[i] + 4, memory address for x[i] is generated only once, saved in a register, and used by lw and sw – Common subexpression elimination. Local optimization within a block of code. Global optimization across blocks. Global register allocation. Strength reduction. Example: replace integer multiply by 2 k with shift left. Unnecessary instructions. A value not used in the later part of program may not be stored in memory (eliminate sw).

Spr 2015, Feb 9... ELEC / Lecture 4 10 MIPS Compiler Example C code: f = (g+h) – (i+j) Compiler assigns variables f, g, h, i and j to registers $s0, $s1, $s2, $s3 and $s4 Uses two temporary registers, $t0 and $t1, to produce the following MIPS assembly code: add $t0, $s1, $s2# reg $t0 contains g+h add $t1, $s3, $s4# reg $t1 contains i+j sub $s0, $t0, $t1# reg $s0 = $t0 – $t1 = (g+h) – (i+j)

Spr 2015, Feb 9... ELEC / Lecture 4 11 Registers $sp and $gp 0 pc $gp $sp Stack Dynamic data heap Static data Machine code (text) Reserved Temp. registers saved before procedure call $s0-$s7 saved in stack by procedure Global variables in program

Spr 2015, Feb 9... ELEC / Lecture 4 12 Register $fp (Frame Pointer) 0 pc $gp $fp Dynamic data heap Static data Machine code (text) Reserved $sp Procedure frame 0 pc $gp $fp Dynamic data heap Static data Machine code (text) Reserved $sp Old procedure frame Procedure frame

Spr 2015, Feb 9... ELEC / Lecture 4 13 Compiling a Procedure Call Consider a program that uses register $t0 and calls a procedure proc Assembly code of program with procedure call:.. addi$sp, $sp, - 4 # adjust stack pointer sw$t0, 0($sp)# save $t0 jalproc# call proc lw$t0, 0($sp)# restore $t0 addi$sp, $sp, 4# pop 1 word off stack..

Spr 2015, Feb 9... ELEC / Lecture 4 14 Compiling a Procedure Consider a procedure proc that uses register $s0 Assembly code: proc : addi$sp, $sp, - 4 # adjust stack pointer sw$s0, 0($sp)# save $s0. Assembly code of proc. lw$s0, 0($sp)# restore $s0 addi$sp, $sp, 4# pop 1 word off stack jr$ra# return

Spr 2015, Feb 9... ELEC / Lecture 4 15 Dragon Books on Compilers Addison-Wesley 1986Pearson Education 2006

Beyond Textbooks: Papers Available at Course Website M. Hall, D. Padua and K. Pingali, “Compiler Research: The Next 50 Years,” CACM, vol. 52, no. 2, pp , Feb J. Larus and G. Hunt, “The Singularity System,” CACM, vol. 53, no. 8, pp , Aug S. V. Adve and H.-J. Boehm, “Memory Models: A Case for Rethinking Parallel Languages and Hardware,” CACM, vol. 53, no. 8, pp , Aug G. L. Steele Jr., “An Interview with Frances E. Allen,” CACM, vol. 54, no. 1, pp , Jan Spr 2015, Feb 9... ELEC / Lecture 4 16

Spr 2015, Feb 9... ELEC / Lecture 4 17 Assembler System program Input: Assembly language code Output: Machine language code (binary or object code) contains: –Machine instructions –Data –Symbol table Converts pseudoinstructions into core instructions using register $at Converts assembly code into machine code Creates a symbol table – labels and locations in code

Spr 2015, Feb 9... ELEC / Lecture 4 18 MIPS Assembler Example Use register convention: –Registers are $s0 through $s7 –Registers 8-15 are $t0 through $t7 Machine code: three 32-bit words from assembly code of slide 10 add add sub

Spr 2015, Feb 9... ELEC / Lecture 4 19 Assembler and Linker Assembler Main Proc A Proc B Object file Linker Program Library Executable File Assembly code Machine code (binary)

Spr 2015, Feb 9... ELEC / Lecture 4 20 Symbols Program names and labels are symbols. In executable code, a symbol is a memory address. In object code, a symbol’s final value (address) has not been determined: Internal symbols (statement labels) have relative addresses. External symbols (called subroutine names, library procedures) are unknown (unresolved). See example on page 143 of textbook (page 110 in old version).

Spr 2015, Feb 9... ELEC / Lecture 4 21 Linker System program. Inputs: Program and procedure libraries, all in machine code. Output: Executable machine code. Linker functions: –Links the machine code of procedures from a library. –Sets memory addresses of data variables for procedures. –Sets procedure addresses in the calling program code.

Spr 2015, Feb 9... ELEC / Lecture 4 22 Loader System program Inputs: Machine code and data from disc Output: Set up program and data in memory Loader functions: –Read executable code and data from disc to computer memory –Initialize registers and set stack pointer to first free location –Transfer control to a start-up routine that calls the main routine of the program

Spr 2015, Feb 9... ELEC / Lecture 4 23 Recursive and Nested Programs The following convention is understood and used by all calling (caller) and called (callee) programs. PreservedNot preserved Saved reg. $s0 - $s7Temp. reg. $t0 - $t9 Stack pointer reg. $spArgument reg. $a0 - $a3 Return addr. Reg. $raReturn value reg. $v0 - $v1 Stack above the stack pointerStack below the stack pointer

Spr 2015, Feb 9... ELEC / Lecture 4 24 When Calee becomes a Caller Saving and restoring of saved and temporary registers is done same as described before. May reuse argument registers ($a0 - $a3); they are saved and restored as necessary. Must reuse $ra; its content is saved in memory and restored on return.

Spr 2015, Feb 9... ELEC / Lecture 4 25 Example: Program→Callee A→Calee B Main programProcedure A Procedure B. addi $a0, $zero, 3 jal A. addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) addi $a0, $zero, 7 jal B lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8. jr $ra. jr $ra

Spr 2015, Feb 9... ELEC / Lecture 4 26 Example: A Recursive Procedure Intfact (n) { if (n < 1) return (1); elsereturn (n * fact (n-1)); } This procedure returns factorial of integer n, i.e., n! = n × (n-1) × (n-2) ×... × 2 × 1 = n × (n-1)! Boundary case, 0! = 1

Spr 2015, Feb 9... ELEC / Lecture 4 27 Flowchart of fact n < 1 v(n) = 1 n ≥ 0 v(n) = n * v(n-1) Yes No Return v(n)

Spr 2015, Feb 9... ELEC / Lecture 4 28 Example: Compute 4! n = 4 v(4) = 4 * v(3) v(3) = 3 * v(2) v(2) = 2 * v(1) v(1) = 1 * v(0) v(0) = 1 Main program calls fact, $a0 = 4 fact calls fact, $a0 = 3 fact calls fact, $a0 = 2 fact calls fact, $a0 = 1 fact calls fact, $a0 = 0 Returns $v0 = $v0 * $a0 = 24 Returns $v0 = $v0 * $a0 = 6 Returns $v0 = $v0 * $a0 = 2 Returns $v0 = $v0 * $a0 = 1 Returns $v0 = 1

Spr 2015, Feb 9... ELEC / Lecture 4 29 What to Save? Recursive procedure will call itself. So, –will reuse return address register $ra –will change argument register $a0 These two registers must be saved.

Spr 2015, Feb 9... ELEC / Lecture 4 30 Saved Registers for 4! Example Initial $sp $sp fact calls fact, $a0 = 3 fact calls fact, $a0 = 2 fact calls fact, $a0 = 1 fact calls fact, $a0 = 0 Return address within main program Argument register, $a0 = 4 Return address within fact Argument register, $a0 = 3 Return address within fact Argument register, $a0 = 2 Return address within fact Argument register, $a0 = 1 } } } }

Spr 2015, Feb 9... ELEC / Lecture 4 31 Assembly Code for fact fact: addi$sp, $sp, -8# adjust stack for two items sw$ra, 4($sp)# save return address of caller sw$a0, 0($sp)# save caller supplied argument n slti$t0, $a0, 1# $t0 = 1, if n ≤ 1, i.e., n = 0 beq$t0, $zero, L1# go to L1, if n ≥ 1 addi$v0, $zero, 1# return $v0 = 1 to caller addi$sp, $sp, 8# no need to restore registers, since none was changed, but must restore stack pointer jr$ra# return to caller instruction after jal

Spr 2015, Feb 9... ELEC / Lecture 4 32 Assembly Code Continued L1:addi $a0, $a0, -1# set $a0 to n-1 jal fact# call fact with argument n-1 lw $a0, 0($sp)# on return from fact, restore n lw $ra, 4($sp)# restore return address addi $sp, $sp, 8# adjust stack pointer mul $v0, $a0, $v0# n! = n × (n-1)! jr $ra# return to caller

Spr 2015, Feb 9... ELEC / Lecture 4 33 Execution of fact for n ≥ 1 CallCaller#a0$raReturned $v0 Sequence 1Main Program nPC+4n×(n-1)! 2fact n-1L1+8(n-1)×(n-2)! 3fact n-2L1+8(n-2)×(n-3)! fact. n-2fact 3L1+83×2 = 6 n-1fact 2L1+82×1 = 2 nfact 1L1+81×1 = 1 n+1fact 0L1+81

Spr 2015, Feb 9... ELEC / Lecture 4 34 Summary A user’s program is processed by several system programs: –Compiler –Assembler –Linker –Loader –Start-up routine: begins program execution and at the end of the program issues an exit system call.