CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (1)Garcia, Fall 2004 © UCBLecturer PSOE Dan Garciawww.cs.berkeley.edu/~ddgarciainst.eecs.berkeley.edu/~cs61cCS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II, Logical and Shift Ops 2004-09-27Gotta love Sept/Oct! ⇒Pennant races heating up!SF, As trying to make the post-season.Yankees trying to hold off Boston.Ichiro soon to beat 84yr hit record!espn.com/mlb/CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (2)Garcia, Fall 2004 © UCBReview• Functions called with jal, return with jr $ra.• The stack is your friend: Use it to saveanything you need. Just be sure to leave it theway you found it.• Instructions we know so farArithmetic: add, addi, sub, addu, addiu, subuMemory: lw, swDecision: beq, bne, slt, slti, sltu, sltiuUnconditional Branches (Jumps): j, jal, jr• Registers we know so far• All of them!• There are CONVENTIONS when calling procedures!CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (3)Garcia, Fall 2004 © UCBRegister Conventions (1/4)• CalleR: the calling function• CalleE: the function being called• When callee returns from executing,the caller needs to know whichregisters may have changed and whichare guaranteed to be unchanged.• Register Conventions: A set ofgenerally accepted rules as to whichregisters will be unchanged after aprocedure call (jal) and which may bechanged.CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (4)Garcia, Fall 2004 © UCBRegister Conventions (2/4) - saved• $0: No Change. Always 0.• $s0-$s7: Restore if you change. Veryimportant, that’s why they’re calledsaved registers. If the callee changesthese in any way, it must restore theoriginal values before returning.• $sp: Restore if you change. The stackpointer must point to the same placebefore and after the jal call, or elsethe caller won’t be able to restorevalues from the stack.• HINT -- All saved registers start with S!CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (5)Garcia, Fall 2004 © UCBRegister Conventions (3/4) - volatile• $ra: Can Change. The jal call itselfwill change this register. Caller needsto save on stack if nested call.• $v0-$v1: Can Change. These willcontain the new returned values.• $a0-$a3: Can change. These arevolatile argument registers. Callerneeds to save if they’ll need them afterthe call.• $t0-$t9: Can change. That’s whythey’re called temporary: anyprocedure may change them at anytime. Caller needs to save if they’llneed them afterwards.CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (6)Garcia, Fall 2004 © UCBRegister Conventions (4/4)• What do these conventions mean?• If function R calls function E, thenfunction R must save any temporaryregisters that it may be using onto thestack before making a jal call.• Function E must save any S (saved)registers it intends to use before garblingup their values• Remember: Caller/callee need to saveonly temporary/saved registers they areusing, not all registers.CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (7)Garcia, Fall 2004 © UCBParents leaving for weekend analogy (1/5)• Parents (main) leaving for weekend• They (caller) give keys to the houseto kid (callee) with the rules(calling conventions):• You can trash the temporary room(s), likethe den and basement (registers) ifyou want, we don’t care about it• BUT you’d better leave the rooms(registers) that we want to save for theguests untouched. “these rooms betterlook the same when we return!”• Who hasn’t heard this in their life?CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (8)Garcia, Fall 2004 © UCBParents leaving for weekend analogy (2/5)• Kid now “owns” rooms (registers)• Kid wants to use the saved rooms fora wild, wild party (computation)• What does kid (callee) do?• Kid takes what was in these rooms andputs them in the garage (memory)• Kid throws the party, trashes everything(except garage, who goes there?)• Kid restores the rooms the parentswanted saved after the party by replacingthe items from the garage (memory) backinto those saved roomsCS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (9)Garcia, Fall 2004 © UCBParents leaving for weekend analogy (3/5)• Same scenario, except before parentsreturn and kid replaces saved rooms…• Kid (callee) has left valuable stuff(data) all over.• Kid’s friend (another callee) wants thehouse for a party when the kid is away• Kid knows that friend might trash theplace destroying valuable stuff!• Kid remembers rule parents taught andnow becomes the “heavy” (caller),instructing friend (callee) on good rules(conventions) of house.CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (10)Garcia, Fall 2004 © UCBParents leaving for weekend analogy (4/5)• If kid had data in temporary rooms(which were going to be trashed),there are three options:• Move items directly to garage (memory)• Move items to saved rooms whosecontents have already been moved to thegarage (memory)• Optimize lifestyle (code) so that theamount you’ve got to shlep stuff backand forth from garage (memory) isminimized• Otherwise: “Dude, where’s my data?!”CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (11)Garcia, Fall 2004 © UCBParents leaving for weekend analogy (5/5)• Friend now “owns” rooms (registers)• Friend wants to use the saved roomsfor a wild, wild party (computation)• What does friend (callee) do?• Friend takes what was in these rooms andputs them in the garage (memory)• Friend throws the party, trasheseverything (except garage)• Friend restores the rooms the kid wantedsaved after the party by replacing theitems from the garage (memory) back intothose saved roomsCS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (12)Garcia, Fall 2004 © UCB Administrivia• Project 1 due Friday @ 23:59CS 61C L12 Introduction to MIPS: Procedures II, logical & shift ops (13)Garcia, Fall 2004 © UCBBitwise Operations• Up until now, we’ve done arithmetic (add,sub,addi ), memory access (lw and sw),and branches and jumps.• All of these instructions view contents ofregister as a single quantity (such as asigned or unsigned integer)• New Perspective: View register as 32 rawbits rather than as a single
View Full Document