{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L12 - CS61C Machine Structures Lecture 12 MIPS Procedures...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61C L12 MIPS Procedures II / Logical (1) Wawrzynek Fall 2007 © UCB 9/24/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS61C – Machine Structures Lecture 12 - MIPS Procedures II & Logical Ops CS 61C L12 MIPS Procedures II / Logical (2) Wawrzynek Fall 2007 © UCB Review ° Functions called with jal , return with jr $ra . ° The stack is your friend: Use it to save anything you need. Just be sure to leave it the way you found it. ° Instructions we know so far Arithmetic: add, addi, sub, addu, addiu, subu Memory: lw, sw Decision: beq, bne, slt, slti, sltu, sltiu Unconditional Branches (Jumps): j, jal, jr ° Registers we know so far All of them! There are CONVENTIONS when calling procedures!
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS 61C L12 MIPS Procedures II / Logical (3) Wawrzynek Fall 2007 © UCB Register Conventions (1/4) ° Calle R : the calling function ° Calle E : the function being called ° When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged. ° Register Conventions : A set rules as to which registers will be unchanged after a procedure call ( jal ) and which may be changed . Note: This is software policy, no hardware support. CS 61C L12 MIPS Procedures II / Logical (4) Wawrzynek Fall 2007 © UCB Register Conventions (2/4) - saved ° $0 : No Change . Always 0. ° $s0 - $s7 : Restore if you change . Very important, that s why they re called saved registers. If the callee changes these in any way, it must restore the original values before returning. ° $sp : Restore if you change . The stack pointer must point to the same place before and after the jal call, or else the caller won t be able to restore values from the stack. ° HINT -- All saved registers start with S !
Image of page 2
CS 61C L12 MIPS Procedures II / Logical (5) Wawrzynek Fall 2007 © UCB Register Conventions (3/4) - volatile ° $ra : Can Change . The jal call itself will change this register. Caller needs to save on stack if nested call. ° $v0 - $v1 : Can Change . These will contain the new returned values. ° $a0 - $a3 : Can change . These are volatile argument registers. Caller needs to save if they are needed after the call. ° $t0 - $t9 : Can change . That s why they re called temporary: any procedure may change them at any time. Caller needs to save if they ll need them afterwards. CS 61C L12 MIPS Procedures II / Logical (6) Wawrzynek Fall 2007 © UCB Register Conventions (4/4) ° What do these conventions mean? If function R calls function E, then function R must save any volatile registers that it may be using onto the stack before making a jal call. Function E must save any S (saved) registers it intends to use before garbling up their values Remember: Caller /callee need to save only temporary/saved registers they are using , not all registers.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS 61C L12 MIPS Procedures II / Logical (7) Wawrzynek Fall 2007 © UCB Example: Fibonacci Numbers 1/8 ° 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 ° In scheme, this could be written: (define (Fib n) (cond ((= n 0) 1) ((= n 1) 1) (else (+(Fib (- n 1)) (Fib (- n 2)))))
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern