Language of the Machine Recursive functions

Similar documents
Decoding example Binary=>Decimal=>Assembly=>C? Start at program at address 4,194,304 (2 22 )

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

MIPS Functions and Instruction Formats

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Lecture 7: Procedures and Program Execution Preview

UCB CS61C : Machine Structures

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Lecture 7: Procedures

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CS61C : Machine Structures

Anne Bracy CS 3410 Computer Science Cornell University

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

COE608: Computer Organization and Architecture

Procedures and Stacks

CS 316: Procedure Calls/Pipelining

CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

ECE232: Hardware Organization and Design

MIPS Functions and the Runtime Stack

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

CS61C : Machine Structures

ECE 15B Computer Organization Spring 2010

Anne Bracy CS 3410 Computer Science Cornell University

More C functions and Big Picture [MIPSc Notes]

MIPS Procedure Calls. Lecture 6 CS301

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

Procedure Call and Return Procedure call

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Implementing Procedure Calls

CA Compiler Construction

Bonus slides. Garcia, Spring 2014 UCB. CS61C L11 Introduction to MIPS : Procedures II & Logical Ops (18)

CSE Lecture In Class Example Handout

The Single Cycle Processor

Anne Bracy CS 3410 Computer Science Cornell University

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS61C : Machine Structures

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Compiling Code, Procedures and Stacks

Control Instructions

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Stacks and Procedures

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Compilers and computer architecture: A realistic compiler to MIPS

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

CS 110 Computer Architecture MIPS Instruction Formats

Programming Languages

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

CS 61c: Great Ideas in Computer Architecture

Lecture 5: Procedure Calls

Lecture 5. Announcements: Today: Finish up functions in MIPS

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Quiz for Chapter 2 Instructions: Language of the Computer3.10

Course Administration

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Lecture 5: Procedure Calls

UCB CS61C : Machine Structures

Instruction Set Architectures (4)

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Fibonacci Numbers. An Exercise in Assembly Language Programming. Andreas Klappenecker. September 7, 2004

G Programming Languages - Fall 2012

Run-time Environment

Assembly Programming (III)

Chap. 8 :: Subroutines and Control Abstraction

SPIM Procedure Calls

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

Functions in MIPS. Functions in MIPS 1

Review of Activation Frames. FP of caller Y X Return value A B C

And in Review. Register Conventions (2/4) saved. Register Conventions (1/4) Register Conventions (4/4) Register Conventions (3/4) volatile

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

MIPS function continued

EE 361 University of Hawaii Fall

Faux Midterm 1: Review Session

Lec 10: Assembler. Announcements

ú There are CONVENTIONS when calling procedures! Stack main ()! { a(0);! }! void a (int m)! frame" Garcia, Spring 2013 UCB printf() (y==?

Instructions: Assembly Language

Code Generation. Lecture 12

CSE Lecture In Class Example Handout

CIT Week13 Lecture

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Stacks and Procedures

MIPS Assembly Recursion: Fibonacci, Binary Search, Mergesort

Transcription:

EECS 322 Computer Architecture Language of the Machine Recursive functions Instructor: Francis G. Wolff wolff@eecs.cwru.edu Case Western Reserve University This presentation uses powerpoint animation: please viewshow CWRU EECS 322 1

Review: Function calling Follow calling conventions & nobody gets hurt. Function Call Bookkeeping: Caller: Arguments $a0, $a1, $a2, $a3 Return address $ra Call function jal label # $ra=pc+4;pc=label Callee: Not restored $t0 - $t9 Restore caller s $s0 - $s7, $sp, $fp Return value $v0, $v1 Return jr $ra # pc = $ra CWRU EECS 322 2

Review: Program memory layout Address $fp frame pointer $sp stack pointer Stack Memory Management: R:read only W:write only X:execute only RW Space for saved procedure information: return address, dynamic variables, >4 arguments Heap RW Explicitly created space, e.g., malloc(); C pointers $gp global pointer Static Const RW R (.data) Global static vars (.bss initialized to 0) const variables (strings) Address 0 Code X Program: machine instructions (.text) CWRU EECS 322 3

Basic Structure of a Function Prologue entry_label: addi $sp,$sp,-framesize sw $ra,framesize-4($sp)# save $ra save other regs Body. ra Epilogue restore other regs lw $ra, framesize-4($sp)#restore $ra addi $sp,$sp, framesize jr $ra CWRU EECS 322 4

Recursive functions: Fibonacci Numbers How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the 2nd month on becomes productive. Leonardo Pisano aka Fibonacci (1202, Pisa, Italy) The Fibonacci numbers are defined as follows: F(n) = F(n 1) + F(n 2), F(0) and F(1) are defined to be 1 Re-writing this in C we have: int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2)); } CWRU EECS 322 5

Prologue: Fibonacci Numbers Now, let s translate this to MIPS! Reserve 3 words on the stack: $ra, $s0, $a0 The function will use one $s register, $s0 Write the Prologue: fib: addi $sp, $sp, -12 # Space for three words sw $ra, 8($sp) # Save the return address sw $s0, 4($sp) # Save $s0 CWRU EECS 322 6

Epilogue: Fibonacci Numbers Now write the Epilogue: fin: lw $s0, 4($sp) # Restore caller s $s0 lw $ra, 8($sp) # Restore return address addi $sp, $sp, 12 # Pop the stack frame _jr $ra # Return to caller CWRU EECS 322 7

Body: Fibonacci Numbers Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return fib(n - 1) + fib(n - 2); } addi $v0,$zero,1 # $v0 = 1; return $v0 beq $a0,$zero,fin # if (n == 0) goto fin addi $t0,$zero,1 # $t0 = 1; beq $a0,$t0,fin # if (n == $t0)goto fin # Contiued on next slide... CWRU EECS 322 8

return: Fibonacci Numbers Almost there, but be careful, this part is tricky! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } sw $a0,0($sp) # Need $a0 after jal addi $a0,$a0, -1 # $a0 = n - 1 jal fib # fib($a0) add $s0,$v0,$zero # $s0 = fib(n-1) lw $a0,0($sp) # Restore original $a0 = n addi $a0,$a0, -2 # $a0 = n 2 jal fib # fib($a0) add $v0,$s0,$v0 # fib(n-1) + fib(n-2) CWRU EECS 322 9

return: $s1 improvement? Can we replace the sw $a0,0($sp) with add $s1,$a0,$zero in order to avoid using the stack? add $s1,$a0,$zero # was sw $a0,0($sp) addi $a0,$a0, -1 # $a0 = n - 1 jal fib # fib($a0) add $s0,$v0,$zero # $s0 = fib(n-1) # was lw $a0,0($sp) addi $a0,$s1, -2 # $a0 = n 2 jal fib # fib($a0) add $v0,$s0,$v0 # fib(n-1) + fib(n-2) CWRU EECS 322 10

return: $s1 improvement... Can we replace the sw $a0,0($sp) with add $s1,$a0,$zero in order to avoid using the stack? We did save one instruction so far, a plus! By convention, all $s registers must be preserved for the caller. Thus, modifying $s1 will confuse the caller. and therefore we would have to add another lw and sw for $s1 in the prologue and epilog. The saving of instruction, requires 2 new instructions; resulting in a net gain of minus one! CWRU EECS 322 11

return: $t1 improvement? Can we replace the sw $a0,0($sp) with add $t1,$a0,$zero in order to avoid using the stack? We did save one instruction so far, a plus! By convention, all $t registers are not preserved for the caller. and therefore we would have to add another lw and sw for $t1 to the stack. CWRU EECS 322 12

Here s the complete code: Fibonacci Numbers fib: addi $sp, $sp, -12 sw sw $ra, 8($sp) sw sw $s0, 4($sp) addi $v0, $zero, 1 beq $a0, $zero, fin addi $t0, $zero, 1 beq $a0, $t0, fin sw sw $a0, 0($sp) addi $a0, $a0, -1-1 jal fib add $s0, $v0, $zero lw lw $a0, 0($sp) addi $a0, $a0, -2-2 jal fib add $v0, $v0, $s0 fin: # epilog lw lw $s0, 4($sp) lw lw $ra, 8($sp) addi $sp, $sp, 12 12 jr jr $ra CWRU EECS 322 13

Time Complexity: recursive The Fibonacci numbers are defined as follows: F(n) = F(n 1) + F(n 2), F(0) and F(1) are defined to be 1 Let T(n) be the number of steps required to calculate F(n). Then we can set up a recurrence relation for T(n) Note that call=return=compare=arithmetic=1 time unit Step 1: initial call F(n) =1 In F: 1. If n<=1 ret 1 =2 2. else ret F(n-1)+F(n-2) =6+T(n-1)+T(n-2) The approximate solution is T(n) > O( 1.5 n ) exponential growth! want to avoid that! CWRU EECS 322 14

Time Complexity: non-recursive non-recursive version t1=1; t2=2; for(i=2; i > n; i++) { t3=t2+t1; t1=t2; t2=t3; } return t3; T(n) = 7(n-1)+4 = 7n - 3 = O(7n - 3) = O(n) O(n) Linear in Time! better than the recursive version! This shows that the algorithm applied to the target architecture has the greatest impact on performance issues. C/C++ compilers are not the answer to everything. Thinking out the problem beforehand is. CWRU EECS 322 15

Closed form F(n) = 1/ 5 ( Φ n Φ ^ n ) where the golden ratio Φ = (1+ 5) / 2 and Φ ^ = 1 Φ Even from the time of the Greeks, the golden proportion has been used in architecture. The most famous is the Parthenon build circa 430 BC. CWRU EECS 322 16

Signatures and Silicon Art Just as the great architects, place their hidden Φ signature, so too do computer designers. The Pentium Killer Macintosh G3 chips were code-named "Arthur" as in Camelot, and the sword represents Excalibur. Motorola/IBM PowerPC 750 MIPS R10000 Processor CWRU EECS 322 17

Argument Passing greater than 4 C code fragment g=f(a, b, c, d, e); MIPS assembler addi $sp, $sp, -4 sw $s4, 0($sp) # push e add $a3, $s3, $0 # register push d add $a3, $s2, $0 # register push c add $a1, $s1, $0 # register push b add $a0, $s0, $0 # register push a jal f # $ra = pc + 4 add $s5, $v0, $0 # g=return value CWRU EECS 322 18

Argument Passing Options 2 common choices Call by Value : pass a copy of the item to the function/procedure Call by Reference : pass a pointer to the item to the function/procedure Single word variables passed by value Passing an array? e.g., a[100] Pascal--call by value--copies 100 words of a[] onto the stack: inefficient C--call by reference--passes a pointer (1 word) to the array a[] in a register CWRU EECS 322 19

Memory Allocation int *sumarray(int x[ ], int y[ ]) adds two arrays and puts sum in a third array 3 versions of array function that Dynamic allocation (stack memory) Static allocation (global memory) Heap allocation (malloc, free) Purpose of example is to show interaction of C statements, pointers, and memory allocation CWRU EECS 322 20

Dynamic Allocation Caller provides temporary work space int f(int x[100], y[100], ) { Stack c[100] int c[100]; sumarray(x, y, c);... C calling convention means above the same as sumarray(&x[0], &y[0], &c[0]); Heap Static Code CWRU EECS 322 21

Optimized Compiled Code void sumarray(int a[],int b[],int c[]) { int i; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; } addi $t0,$a0,400 # beyond end of a[] Loop: beq $a0,$t0,exit # if (i==sizeof(int)* 100) lw $t1, 0($a0) # $t1=a[i] lw $t2, 0($a1) # $t2=b[i] add $t1,$t1,$t2 # $t1=a[i] + b[i] sw $t1, 0($a2) # c[i]=a[i] + b[i] addi $a0,$a0,4 # $a0++ addi $a1,$a1,4 # $a1++ addi $a2,$a2,4 # $a2++ j Loop Exit: jr $ra CWRU EECS 322 22

Static allocation (scope: private to function only) Static declaration int *sumarray(int a[],int b[]) { int i; static int c[100]; } for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; Compiler allocates once for function, space is reused by function On re-entry will still have old data Can not be seen by outside functions Stack Heap Static c[100] Code CWRU EECS 322 23

Alternate Static allocation (scope: public to everyone) Static declaration int c[100]; int *sumarray(int a[],int b[]) { int i; static int c[100]; } for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; The variable scope of c is very public and is accessible to everyone outside the function Stack Heap Static c[100] Code CWRU EECS 322 24

Heap allocation Solution: allocate c[] on heap int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); } for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; Not reused unless freed Can lead to memory leaks Java, Scheme have garbage collectors to reclaim free space Stack Heap c[100] Static Code CWRU EECS 322 25

Lifetime of storage & scope automatic (stack allocated) typical local variables of a function created upon call, released upon return scope is the function heap allocated created upon malloc, released upon free referenced via pointers external / static exist for entire program CWRU EECS 322 26

What about Structures? Scalars passed by value (i.e. int, float, char) Arrays passed by reference (pointers) Structures by value ( struct { } ) Pointers by value Can think of C passing everything by value, just that arrays are simply a notation for pointers CWRU EECS 322 27

Register Names as Numbers (page A-23) Register Names Register No. $zero $0 $at (reserved for assembler) $1 (Return) Value registers ($v0,$v1) $2 - $3 Argument registers ($a0-$a3) $4 - $7 Temporary registers ($t0-$t7) $8 - $15 Saved registers ($s0-$s7) $16 - $22 Temporary registers ($t8-$t9) $23 - $24 $k0,$k1 (reserved for OS kernel) $26, $27 Global Pointer ($gp) $28 Stack Pointer ($sp) $29 Frame Pointer ($fp), or $t10 $30 Return Address ($ra) $31 CWRU EECS 322 28