Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures

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.
Lecture 9: MIPS Instruction Set
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.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
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 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.
Ch. 8 Functions.
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
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Assembly Language
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Assembly Language II CPSC 321 Andreas Klappenecker.
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
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.”
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
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 )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
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
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.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
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.
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
Function Calls in MIPS To call a function: jal func
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
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
MIPS functions.
Program and memory layout
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures Csci136 Computer Architecture II Lab#4 - Stack and Nested Procedures - Project#1: due on Feb 13, 2005 - Quiz#1 Feb.9, 2005

Stack and Nested Procedures Why? -may be modified by others… Must be saved before you go! Return address Local parameters Others How? -save to stacks! Before you go… addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other regs if need be Before you return… restore other regs if need be lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra

Example: Fibonacci Numbers 1/7 Definition: F(n) = F(n – 1) + F(n – 2), F(0) and F(1) are defined to be 1. C Programming: int fib(int n) { if(n == 0) { return 1; }   if(n == 1) { return 1; }   return (fib(n - 1) + fib(n - 2)); }

Example: Fibonacci Numbers 2/7 Now, let’s translate this to MIPS! You will need space for three words on the stack Why three? Return addr($ra) n($a0) temp sum($s0) Write the Prologue: # Space for three words # Save the return address # Save $s0 addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) fib: ___________________

Example: Fibonacci Numbers 3/7 Now write the Epilogue: lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 # Restore $s0 # Restore return address # Pop the stack frame # Return to caller fin: ___________________ jr $ra_____________

Example: Fibonacci Numbers 4/7 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)); } # $v0 = 1 # # $t0 = 1 addi $v0, $zero, 1_ _______, _____,_fin addi $t0, $zero, 1_ _______,______,_fin Continued on next slide. . . beq $a0 $zero beq $a0 $t0 ___________________ __if (n == 0). . . ______ ____________ __if (n == 1). . .

Example: Fibonacci Numbers 5/7 Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { … return (fib(n - 1) + fib(n - 2)); } # $a0 = n - 1 # addi $a0, $a0, -1__ sw____, ___________ ___________________ lw____,____________ addi $a0, ___,_____ $a0 0($sp) jal fib $a0, -1 Continued on next slide. . . ___________________ __Need $a0 after jal _ fib(n – 1) ______ __Restore $a0______ __$a0 = n – 2_________

Example: Fibonacci Numbers 6/7 Remember that $v0 is caller saved! int fib(int n) { . . . return (fib(n - 1) + fib(n - 2)); } # Place fib(n – 1) # somewhere it won’t get # clobbered # add $s0,_ ___,______ ___________________ add $v0, $v0, $s0__ To the epilogue and beyond. . . _______ $v0 $zero jal fib ____________________ __fib(n – 2) __________ __$v0 = fib(n-1) + fib(n-2)

Example: Fibonacci Numbers 7/7 Here’s the complete code for reference fib: addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) addi $v0, $zero, 1 beq $a0, $zero, fin addi $t0, $zero, 1 beq $a0, $t0, fin addi $a0, $a0, -1 sw $a0, 0($sp) jal fib lw $a0, 0($sp) addi $a0, $a0, -1 add $s0, $v0, $zero jal fib add $v0, $v0, $s0 fin: lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra

Exercise Show the content of the stack when computing Fib(2). Assume the top of the stack is at address 0x7FFF and initially the stack is empty. fib(2)=fib(1)+fib(0) fib(1)=fib(0)=1

Project#1: MergeSort Procedure Mergesort (input A[1:n], i,j; output B[1:n]) begin Datatype C[1:n]; If i=j then B[i] = A[i]; Return; endif Mergesort (A,i,(i+j)/2;C); /* sorts the first half*/ Mergesort (A,(i+j)/2 +1,j;C); /* sorts the second half*/ Merge(C,i,j;B); /*merges the two sorted halves * *into a single sorted list */ end

Project#1: MergeSort Example:

Project#1: MergeSort

Quiz#1 Give the MIPS code to load a large constant number 0xABCD9876 into register $s0. Why not lw? Compile the C code A[5]=A[4]+100 into MIPS code, where A is an integer array. MIPS Addressing mode Immediate addressing Register addressing Base/Displacement addressing PC-relative addressing

Quiz#1 Specify the types of the following MIPS instructions. Describe the changes to PC after each instruction is executed. Lw $s5, 8($sp) Jr $31 Beq $1, $2, 100 J, Jr, Jal …

Quiz#1 if(b == 0) { return 0; } if (even(b)) return fmult(2*a,b/2); Fast multiplication: int fmult(int a,b) { if(b == 0) { return 0; } if (even(b)) return fmult(2*a,b/2); if (odd(b)) return (a+fmult(a, b-1)); } Use stack in your programming! Nested Procedures