ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,

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 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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
Lecture 8: MIPS Instruction Set
MIPS Assembly Language
Assembly Language II CPSC 321 Andreas Klappenecker.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
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 )
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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.
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
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.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
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 Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
CSCI206 - Computer Organization & Programming
MIPS Assembly Language Programming
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
Addressing in Jumps jump j Label go to Label op address 2 address
Solutions Chapter 2.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
MIPS function continued
Computer Architecture Procedure Calls
MIPS function continued
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
MIPS function continued
Presentation transcript:

ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,

Fibonacci numbers F(n) = F(n-1)+F(n-2) F(1) = 1 F(2) = 1 n = … F(n) = … /* Recursive function in c */ int fib(int n) { If (n==1) return 1; If (n==2) return 1; return fib(n-1)+fib(n-2); } ECE 15B Spring 2011

Procedures Prolog - spill all register to stack used by procedure expect for $t0-$t9 and the one used for returning values - advance stack pointer ($sp) first then write to stack Body code of the procedure Epilog - restore all used registers - adjust stack pointer at the end ($sp) ECE 15B Spring 2011

/* Recursive function in c */ int fib(int n) { If (n==1) return 1; If (n==2) return 1; return fib(n-1)+fib(n-2); } # Recursive function in MIPS assembler #prolog FIB: addi $sp, $sp, -12 sw $a0, 0($sp) sw $s0, 4($sp) sw $ra, 8($ra) # body addi $v0, $zero, 1 addi $t0, $zero, 1 beq $a0, $t0, EPILOG#check for n==1 addi $t0, $zero, 2 beq $a0, $t0, EPILOG#check for n==2 addi $a0, $a0, -1 jal FIB#calculate fib(n-1) addi $s0, $zero, $v0# save fib(n-1) to $s0 addi $a0, $a0, -1 #calculate fib(n-2) jal FIB addi $v0, $v0, $s0 #epilog EPILOG: lw $a0, 0($sp) lw $s0, 4,($sp) lw $ra, 8,($sp) addi $sp, $sp, 12 jr $ra /* Recursive function in c */ int fib(int n) { v0 = 1; If (n==1) goto EXIT; If (n==2) goto EXIT; n = n – 1; s0 = fib(n); n = n – 1; v0 = fib(n); v0 = v0 +s0; EXIT: return v0; }

ECE 15B Spring 2011 /* Recursive function in c */ int fib(int n) { If (n==1) return 1; If (n==2) return 1; return fib(n-1)+fib(n-2); } # Recursive function in MIPS assembler #prolog FIB: addi $sp, $sp, -12 sw $a0, 0($sp) sw $s0, 4($sp) sw $ra, 8($ra) # body addi $v0, $zero, 1 addi $t0, $zero, 1 beq $a0, $t0, EPILOG#check for n==1 addi $t0, $zero, 2 beq $a0, $t0, EPILOG#check for n==2 addi $a0, $a0, -1 jal FIB#calculate fib(n-1) addi $s0, $zero, $v0# save fib(n-1) to $s0 addi $a0, $a0, -1 #calculate fib(n-2) jal FIB addi $v0, $v0, $s0 #epilog EPILOG: lw $a0, 0($sp) lw $s0, 4,($sp) lw $ra, 8,($sp) addi $sp, $sp, 12 jr $ra /* Recursive function in c */ int fib(int n) { v0 = 1; If (n==1) goto EXIT; If (n==2) goto EXIT; n = n – 1; s0 = fib(n); n = n – 1; v0 = fib(n); v0 = v0 +s0; EXIT: return v0; }

Now let’s see changes in the memory (stack) and register file as we execute recursive function with n = 3 First let’s review datapath and where code is stored ECE 15B Spring 2011

Simple datapath review ECE 15B Spring 2011

Simple datapath review ECE 15B Spring 2011 stack instructions

Where is the code stored? ECE 15B Spring 2011 stack Code (????) static data dynamic data (heap) RF[$sp] 0 ANS: In main memory

Instruction memory ECE 15B Spring 2011 stack code static data dynamic data (heap) IM: Physically different memory Logically mapped to main memory

Direct mapped cache implementation of instruction memory ECE 15B Spring 2011 Main Main memory Instruction memory At any point in time IM has a copy of a portion of main memory Separate memory (tag) to store which location is currently mapped Tag (28 bit) If upper portion of PC (28 bit) matches tag then IM has right values (hit), otherwise stop execution and load right portion

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v00 $a03 $t00 $s00 $sp0xFFFF0000 $ra0x0108 indexvalue 0xFFFF00000x xFFFF00FC 0xFFFF00F8 0xFFFF00F4 RF (right after execution of initial jal FIB at 0x0104) MM (initial values in stack)

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v01 $a02 $t02 $s00 $sp0xFFFF00F4 $ra0x102C indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 RF (after execution jal FIB at 0x1028) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v01 $a02 $t02 $s00 $sp0xFFFF00E8 $ra0x102C indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x102C 0xFFFF00EC0 0xFFFF00E82 RF (after execution beq at 0x1020) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v01 $a02 $t02 $s00 $sp0xFFFF00F4 $ra0x102C indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x102C 0xFFFF00EC0 0xFFFF00E82 RF (after execution jr at 0x104C) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v01 $a01 $t02 $s01 $sp0xFFFF00F4 $ra0x1038 indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x102C 0xFFFF00EC0 0xFFFF00E82 RF (after execution jal at 0x1034) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v01 $a01 $t01 $s01 $sp0xFFFF00E8 $ra0x1038 indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x1038 0xFFFF00EC1 0xFFFF00E81 RF (after execution beq at 0x1018) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v01 $a01 $t01 $s01 $sp0xFFFF00F4 $ra0x1038 indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x1038 0xFFFF00EC1 0xFFFF00E81 RF (after execution jr at 0x104C) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v02 $a03 $t01 $s00 $sp0xFFFF00F4 $ra0x0108 indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x1038 0xFFFF00EC1 0xFFFF00E81 RF (after execution jr at 0x104C) MM - stack

Recursive function execution: step by step ECE 15B Spring x0100addi $a0, $zero, 3 0x0104 jal FIB 0x0108 next instruction XXXXXXXXXXXXXXXXXXXXXX FIB: 0x1000addi $sp, $sp, -12 0x1004sw $a0, 0($sp) 0x1008sw $s0, 4($sp) 0x100Csw $ra, 8($ra) 0x1010addi $v0, $zero, 1 0x1014addi $t0, $zero, 1 0x1018beq $a0, $t0, EPILOG 0x101Caddi $t0, $zero, 2 0x1020beq $a0, $t0, EPILOG 0x1024addi $a0, $a0, -1 0x1028jal FIB 0x102Caddi $s0, $zero, $v0 0x1030addi $a0, $a0, -1 0x1034 jal FIB 0x1038addi $v0, $v0, $s0 EPILOG: 0x103Clw $a0, 0($sp) 0x1040lw $s0, 4,($sp) 0x1044lw $ra, 8,($sp) 0x1048addi $sp, $sp, 12 0x104Cjr $ra indexvalue $v0 2 $a03 $t01 $s00 $sp0xFFFF00F4 $ra0x0108 indexvalue 0xFFFF01000x xFFFF00FC0x0108 0xFFFF00F80 0xFFFF00F43 0xFFFF00F00x1038 0xFFFF00EC1 0xFFFF00E81 RF (at the end of program) MM - stack

Is code optimal? * No need to spill registers when n = 1 and n = 2 * F(n-2) is calculated independently of F(n-1). Better version could be (which is linear in time with n): int fib(int a, int b, int n) { if (n==2) return a; else return fib(a+b, a, n‐1); } …. and use fib(1,1,n) * Even better to use for-loop or do-while ? ECE 15B Spring 2011