Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

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

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

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

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Implementing Procedure Calls

CSE Lecture In Class Example Handout

CS64 Week 5 Lecture 1. Kyle Dewey

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

Functions in MIPS. Functions in MIPS 1

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

ECE331: Hardware Organization and Design

Procedures and Stacks

MIPS Procedure Calls. Lecture 6 CS301

COMP2611: Computer Organization MIPS function and recursion

Today. Putting it all together

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

The Activation Record (AR)

CSE Lecture In Class Example Handout

Code Generation. Lecture 12

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

CS 316: Procedure Calls/Pipelining

ECE232: Hardware Organization and Design

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

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Functions and Procedures

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

CS61C : Machine Structures

Instruction Set Architectures (4)

MIPS Assembly (Functions)

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

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

MIPS Functions and Instruction Formats

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

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

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

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

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Course Administration

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Lecture 5: Procedure Calls

MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

CS 61c: Great Ideas in Computer Architecture

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

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

ECE260: Fundamentals of Computer Engineering

Code Generation. Lecture 19

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

MIPS Functions and the Runtime Stack

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

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

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

Welcome to Computer Organization and Design Logic

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

Control Instructions

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

SPIM Procedure Calls

Compilers and computer architecture: A realistic compiler to MIPS

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

CSC258: Computer Organization. Functions and the Compiler Tool Chain

2B 52 AB CA 3E A1 +29 A B C. CS120 Fall 2018 Final Prep and super secret quiz 9

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

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

CS 61c: Great Ideas in Computer Architecture

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

CS61C : Machine Structures

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

CS 341L Fall 2009 Lab 3

Homework 9: Stack Frame

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

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

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

MIPS Assembly (FuncDons)

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

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

MIPS Assembly Recursion: Fibonacci, Binary Search, Mergesort

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

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

MIPS Assembly (FuncDons)

Compiling Code, Procedures and Stacks

Common Problems on Homework

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

Lecture 5: Procedure Calls

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

CS 110 Computer Architecture MIPS Instruction Formats

MIPS function continued

Computer Systems and Networks

Lectures 3-4: MIPS instructions

CS61C : Machine Structures

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

CS153: Compilers Lecture 8: Compiling Calls

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

Lecture 7: Procedures

Code Generation. Lecture 30

Transcription:

Using the MIPS Calling Convention Recursive Functions in Assembly CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

Administrative Lab #5 this week due on Friday Grades will be up on GauchoSpace today by noon! If you want to review your exams, see your TAs: LAST NAMES A thru Q See Bay-Yuan (Th. 12:30 2:30 pm) LAST NAMES R thru Z See Harmeet (Th. 9:30 11:30 am) Mid-quarter evaluations for T.As Links on the last slide and will put up on Piazza too Optional to do, but very appreciated by us all! Remember: NO CLASSES ON MONDAY! University holiday! Remember to thank a veteran! 11/7/18 Matni, CS64, Fa18 2

Any Questions From Last Lecture? 11/7/18 Matni, CS64, Fa18 3

5 Minute Pop Quiz! Consider this C/C++ code: void third() {} void second() {third();} void first() {second();} int main() {first();} And consider this supposedly equivalent MIPS code è è a) Are there any errors in it? (i.e. will it run?) b) Does it follow the MIPS C.C.? EXPLAIN YOUR ANSWER third: jr $ra second: move $t0, $ra jal third jr $t0 first: move $t1, $ra jal second jr $t1 main: jal first li $v0, 10 syscall 11/7/18 Matni, CS64, Fa18 4

Lecture Outline Recapping MIPS Calling Convention Function calling function example Recursive function example 11/7/18 Matni, CS64, Fa18 5

The MIPS Convention In Its Essence Remember: Preserved vs Unpreserved Regs Preserved: $s0 - $s7, and $sp, $ra Unpreserved: $t0 - $t9, $a0 - $a3, and $v0 - $v1 Values held in Preserved Regs immediately before a function call MUST be the same immediately after the function returns. Values held in Unpreserved Regs must always be assumed to change after a function call is performed. $a0 - $a3 are for passing arguments into a function $v0 - $v1 are for passing values from a function 11/7/18 Matni, CS64, Fa18 6

How the Stack Works Upon reset, $sp points to the bottom of the stack the largest address for the stack (0x7FFF FFFC, see MIPS RefCard) As you move $sp, it goes from high to low address The top of the stack is the stack limit (0x1000 8000, see MIPS RefCard) 11/7/18 Matni, CS64, Fa18 7

How the Stack Works When you want to store some N registers into the stack, the convention says you must: A. Make room in the stack (i.e. move $sp 4xN places) B. Then store words accordingly 11/7/18 Matni, CS64, Fa18 8

How the Stack Works Example: You want to store $s0, $s1, and $s2: addiu $sp, $sp, -12 # cuz 3 x 4 = 12 sw $s0, 8($sp) sw $s1, 4($sp) sw $s2, 0($sp) $sp Bottom of stack $s2 $s1 $s0 11/7/18 Matni, CS64, Fa18 9

An Illustrative Example int subtwo(int a, int b) { int sub = a - b; return sub; } int dosomething(int x, int y) { int a = subtwo(x, y); int b = subtwo(y, x); return a + b; } subtwo doesn t call anything What should I map a and b to? $a0 and $a1 Can I map sub to $t0? Ok, b/c I don t care about $t* (not the best tactic, tho ) Eventually, I have to have sub be $v0 dosomething DOES call a function What should I map x and y to? Since we want to preserve them across the call to subtwo, we should map them to $s0 and $s1 What should I map a and b to? a+b has to eventually be $v0. I should make at least a be a preserved reg ($s2). Since I get b back from a call and there s no other call after it, I can likely get away with not using a preserved reg for b. Matni, CS64, Fa18 10

subtwo: sub $v0, $a0, $a1 jr $ra dosomething: # preserve for the sake # of whatever called # dosomething addiu $sp, $sp, -16 sw $s0, 0($sp) sw $s1, 4($sp) sw $s2, 8($sp) sw $ra, 12($sp) move $s0, $a0 move $s1, $a1 jal subtwo move $a0, $s1 move $a1, $s0 jal subtwo add $v0, $v0, $s2 # pop back the preserved # so that they re ready # for whatever called # dosomething lw $ra, 12($sp) lw $s2, 8($sp) lw $s1, 4($sp) lw $s0, 0($sp) addiu $sp, $sp, 16 jr $ra int subtwo(int a, int b) { int sub = a - b; return sub; } int dosomething(int x, int y) { int a = subtwo(x, y); int b = subtwo(y, x); return a + b; } move $s2, $v0 11/7/18 Matni, CS64, Fa18 11

subtwo: sub $v0, $a0, $a1 jr $ra dosomething: addiu $sp, $sp, -16 sw $s0, 0($sp) sw $s1, 4($sp) sw $s2, 8($sp) sw $ra, 12($sp) move $s0, $a0 move $s1, $a1 jal subtwo move $s2, $v0 stack Orig. $s0 Orig. $s1 $ra move $a0, $s1 move $a1, $s0 jal subtwo add $v0, $v0, $s2 lw $ra, 12($sp) lw $s2, 8($sp) lw $s1, 4($sp) lw $s0, 0($sp) addiu $sp, $sp, 16 jr $ra int subtwo(int a, int b) { int sub = a - b; return sub; } int dosomething(int x, int y) { int a = subtwo(x, y); int b = subtwo(y, x); return a + b; } Arguments Preserved Unpreserved $a0 $a1 int a int b $s0 $s1 int a int b $t0 - $t9 $s2 a b Orig. $s2 Orig. $ra Result Value $v0 a b

subtwo: sub $v0, $a0, $a1 jr $ra dosomething: addiu $sp, $sp, -16 sw $s0, 0($sp) sw $s1, 4($sp) sw $s2, 8($sp) sw $ra, 12($sp) move $s0, $a0 move $s1, $a1 jal subtwo move $s2, $v0 stack Orig. $s0 Orig. $s1 $ra move $a0, $s1 move $a1, $s0 jal subtwo add $v0, $v0, $s2 lw $ra, 12($sp) lw $s2, 8($sp) lw $s1, 4($sp) lw $s0, 0($sp) addiu $sp, $sp, 16 jr $ra int subtwo(int a, int b) { int sub = a - b; return sub; } int dosomething(int x, int y) { int a = subtwo(x, y); int b = subtwo(y, x); return a + b; } Arguments Preserved Unpreserved $a0 $a1 int b int a $s0 $s1 int a int b $t0 - $t9 $s2 a b Orig. $s2 Orig. $ra Result Value $v0 b a 0

subtwo: sub $v0, $a0, $a1 jr $ra dosomething: addiu $sp, $sp, -16 sw $s0, 0($sp) sw $s1, 4($sp) sw $s2, 8($sp) sw $ra, 12($sp) move $s0, $a0 move $s1, $a1 jal subtwo move $s2, $v0 stack Orig. $s0 Orig. $s1 $ra move $a0, $s1 move $a1, $s0 jal subtwo add $v0, $v0, $s2 lw $ra, 12($sp) lw $s2, 8($sp) lw $s1, 4($sp) lw $s0, 0($sp) addiu $sp, $sp, 16 jr $ra Original caller $ra int subtwo(int a, int b) { int sub = a - b; return sub; } int dosomething(int x, int y) { int a = subtwo(x, y); int b = subtwo(y, x); return a + b; } Arguments Preserved Unpreserved $a0 $a1 int b int a $s0 $s1 orig. orig. $t0 $s2 orig. Orig. $s2 Orig. $ra Result Value $v0 0

Lessons Learned We passed arguments into the functions using $a* We used $s* to work out calculations in registers that we wanted to preserve, so we made sure to save them in the call stack These var values DO need to live beyond a call In the end, the original values were returned back We could use $t* to work out some calcs. in regs that we did not need to preserve These values DO NOT need to live beyond a function call We used $v* as regs. to return the value of the function 11/7/18 Matni, CS64, Fa18

Another Example Using Recursion 11/7/18 Matni, CS64, Fa18 16

Recursive Functions This same setup handles nested function calls and recursion i.e. By saving $ra methodically on the stack Example: recursive_fibonacci.asm 11/7/18 Matni, CS64, Fa18 17

recursive_fibonacci.asm Recall the Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, etc fib(n) = fib(n 1) + fib(n 2) In C/C++, we might write the recursive function as: int fib(int n) { if (n == 0) Base cases } return (0); else if (n == 1) return (1); else return (fib(n-1) + fib(n-2)); 11/7/18 Matni, CS64, Fa18 18

recursive_fibonacci.asm We ll need at least 3 registers to keep track of: The (single) input to the call, i.e. var n The output (or partial output) to the call The value of $ra (since this is a recursive function) We ll use $s* registers b/c we need to preserve these vars/regs. beyond the function call If we make $s0 = n and $s1 = fib(n 1) Then we need to save $s0, $s1 and $ra on the stack in the fibonnaci function So that we do not corrupt/lose what s already in these regs 11/7/18 Matni, CS64, Fa18 19

recursive_fibonacci.asm So, we start off in the main: portion n is our argument into the function, so it s in $a0 We ll put our number (example: 7) in $a0 and then call the function fibonacci i.e. li $a0, 7 jal fibonacci 11/7/18 Matni, CS64, Fa18 20

recursive_fibonacci.asm Inside the function fibonacci First: Check for the base cases Is n ($a0) equal to 0 or 1? Branch accordingly $s0 $s1 Orig. s0 Orig. s1 $a0 n $ra Orig. ra Next: Do the recursion --- but first! We need to plan for 3 words in the stack $sp = $sp 12 Push 3 words in (i.e. 12 bytes) The order by which you put them in does not strictly matter, but it makes more organized sense to push $s0, then $s1, then $ra $sp stack Orig. $ra Orig. $s1 Orig. $s0 11/7/18 Matni, CS64, Fa18 21

recursive_fibonacci.asm Next: calculate fib(n 1) Call recursively & copy output ($v0) in $s1 Next: calculate fib(n 2) $s0 $s1 n fib(n-1) $a0 n $v0 fib(n-1) $ra Orig. New ra stack $sp Orig. $ra Orig. $s1 Orig. $s0 Matni, CS64, Fa18 22

recursive_fibonacci.asm Next: calculate fib(n 1) Call recursively & copy output ($v0) in $s1 Next: calculate fib(n 2) Call recursively & add $s1 to the output ($v0) $s0 $s1 n Orig. fib(n-1) s1 $a0 n $v0 fib(n-1) + fib(n-1) fib(n-2) fib(n-2) $ra New ra stack $sp Orig. $ra Orig. $s1 Orig. $s0 Matni, CS64, Fa18 23

recursive_fibonacci.asm Next: calculate fib(n 1) Call recursively & copy output ($v0) in $s1 Next: calculate fib(n 2) Call recursively & add $s1 to the output ($v0) Next: restore registers Pop the 3 words back to $s0, $s1, and $ra Next: return to caller (i.e. main) Issue a jr $ra instruction Note how when we leave the function and go back to the callee (main), we did not disturb what was in the registers previously And now we have our output where it should $s0 $s1 Orig. n s0 Orig. fib(n-1) s1 $a0 n $sp $v0 fib(n-1) + fib(n-2) $ra Orig. New ra stack Orig. $ra Orig. $s1 Orig. $s0 be, in $v0 Matni, CS64, Fa18 24

A Closer Look at the Code Open recursive_fibonacci.asm 11/7/18 Matni, CS64, Fa18 25

Tail Recursion Check out the demo file tail_recursive_factorial.asm at home What s special about the tail recursive functions (see example)? Where the recursive call is the very last thing in the function. With the right optimization, it can use a constant stack space (no need to keep saving $ra over and over it s more efficient) int TRFac(int n, int accum) { if (n == 0) return accum; else return TRFac(n 1, n * accum); } For example, if you said: TRFac(4, 1) Then the program would return: TRFac(3, 4), then return TRFac(2, 12), then return TRFac(1, 24), then return TRFac(0, 24), then, since n = 0, It would return 24 11/7/18 Matni, CS64, Fa18 26

Your To-Dos Again, MAKE SURE you ve read the MIPS Calling Convention PDF from our class website Go over the fibonnaci.asm and tail_recursive_factorial.asm programs Next week: Intro to Digital Logic (on Wed., cuz we don t have class on Mon.!!!!) 11/7/18 Matni, CS64, Fa18 27

11/7/18 Matni, CS64, Fa18 28