Unformatted text preview:

CS 232MP#1: Functions in MIPSControl flow in CControl flow in MIPSData flow in CData flow in MIPSA note about typesThe big problem so farNested functionsSpilling registersWho saves the registers?The caller could save the registers……or the callee could save the registers……or they could work togetherRegister spilling exampleHow to fix factorialWhere are the registers saved?Function calls and stacksStacks and function callsThe MIPS stackPushing elementsAccessing and popping elementsSummaryFebruary 3, 2003 Functions in MIPS 1CS 232It is important that students bring a certain ragamuffin, barefoot irreverence to their studies; they are not here to worship what is known, but to question it. - J. Bronowski Question authority; but, raise your hand first. - A. Dershowitz Pick up the handout on your way in!!January 14, 2019 ©2003-2006 Craig Zilles (adapted from slides by Howard Huang)2MP#1: Functions in MIPSWe’ll talk about the 3 steps in handling function calls:1. The program’s flow of control must be changed.2. Arguments and return values are passed back and forth.3. Local variables can be allocated and destroyed.And how they are handled in MIPS:—New instructions for calling functions.—Conventions for sharing registers between functions.—Use of a stack.January 14, 2019 Functions in MIPS 3Control flow in CInvoking a function changes the control flow of a program twice.1. Calling the function2. Returning from the functionIn this example the main function calls fact twice, and fact returns twice—but to different locations in main.Each time fact is called, the CPU has to remember the appropriate return address.Notice that main itself is also a function! It is, in effect, called by the operating system when you run the program.int main(){...t1 = fact(8);t2 = fact(3);t3 = t1 + t2;...}int fact(int n){int i, f = 1;for (i = n; i > 1; i--)f = f * i;return f;}January 14, 2019 Functions in MIPS 4Control flow in MIPSMIPS uses the jump-and-link instruction jal to call functions.—The jal saves the return address (the address of the next instruction) in the dedicated register $ra, before jumping to the function.—jal is the only MIPS instruction that can access the value of the program counter, so it can store the return address PC+4 in $ra.jal FactTo transfer control back to the caller, the function just has to jump to the address that was stored in $ra.jr $raLet’s now add the jal and jr instructions that are necessary for our factorial example.January 14, 2019 Functions in MIPS 5Data flow in CFunctions accept arguments and produce return values.The blue parts of the program show the actual and formal arguments of the fact function.The purple parts of the code deal with returning and using a result.int main(){...t1 = fact(8);t2 = fact(3);t3 = t1 + t2;...}int fact(int n){int i, f = 1;for (i = n; i > 1; i--)f = f * i;return f;}January 14, 2019 Functions in MIPS 6Data flow in MIPSMIPS uses the following conventions for function arguments and results.—Up to four function arguments can be “passed” by placing them in argument registers $a0-$a3 before calling the function with jal.—A function can “return” up to two values by placing them in registers $v0-$v1, before returning via jr.These conventions are not enforced by the hardware or assembler, but programmers agree to them so functions written by different people can interface with each other.Later we’ll talk about handling additional arguments or return values.January 14, 2019 Functions in MIPS 7Assembly language is untyped—there is no distinction between integers, characters, pointers or other kinds of values. It is up to you to “type check” your programs. In particular, make sure your function arguments and return values are used consistently.For example, what happens if somebody passes the address of an integer (instead of the integer itself) to the fact function?A note about typesJanuary 14, 2019 Functions in MIPS 8The big problem so farThere is a big problem here!—The main code uses $t1 to store the result of fact(8).—But $t1 is also used within the fact function!The subsequent call to fact(3) will overwrite the value of fact(8) that was stored in $t1.January 14, 2019 Functions in MIPS 9A: ...# Put B’s args in $a0-$a3jal B # $ra = A2A2: ...B: ...# Put C’s args in $a0-$a3,# erasing B’s args!jal C # $ra = B2B2: ...jr $ra # Where does# this go???C: ...jr $raNested functionsA similar situation happens when you call a function that then calls another function.Let’s say A calls B, which calls C.—The arguments for the call to C would be placed in $a0-$a3, thus overwriting the original arguments for B.—Similarly, jal C overwrites the return address that was saved in $ra by the earlier jal B.January 14, 2019 Functions in MIPS 10Spilling registersThe CPU has a limited number of registers for use by all functions, and it’s possible that several functions will need the same registers.We can keep important registers from being overwritten by a function call, by saving them before the function executes, and restoring them after the function completes.But there are two important questions.—Who is responsible for saving registers—the caller or the callee?—Where exactly are the register contents saved?January 14, 2019 Functions in MIPS 11Who saves the registers?Who is responsible for saving important registers across function calls?—The caller knows which registers are important to it and should be saved.—The callee knows exactly which registers it will use and potentially overwrite.However, in the typical “black box” programming approach, the caller and callee do not know anything about each other’s implementation.—Different functions may be written by different people or companies.—A function should be able to interface with any client, and different implementations of the same function should be substitutable.So how can two functions cooperate and share registers when they don’t know anything about each other?January 14, 2019 Functions in MIPS 12The caller could save the registers…One possibility is for the caller to save any important registers that it needs before making a function call, and to restore them after.But the caller does not know what registers are actually written by the function, so it may save more registers than necessary.In the example on the


View Full Document

U of I CS 232 - Functions in MIPS

Documents in this Course
Goal

Goal

2 pages

Exam 1

Exam 1

5 pages

Exam 1

Exam 1

6 pages

Exam 2

Exam 2

6 pages

Exam 1

Exam 1

5 pages

Load more
Download Functions in MIPS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions in MIPS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions in MIPS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?