DOC PREVIEW
U of I CS 232 - Functions in MIPS

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MP#2: 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 togetherAlternate way of saying thisRegister spilling exampleFixing infinite loop in ‘main’Where are the registers saved?Function calls and stacksStacks and function callsThe MIPS stackPushing elementsAccessing and popping elementsSummary1MP#2: 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.2Control 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 differ ent 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 a0){int t1, v0 = 1;for(t1 = a0;t1 > 1;t1--)v0 = v0 * t1;return v0;}3Control 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 $ra4Data 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 a0){int t1, v0 = 1;for(t1 = a0;t1 > 1;t1--)v0 = v0 * t1;return v0;}5Data 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.6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 types7The 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.8A: ...# 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 o verwriting the original arguments for B.—Similarly, jal C overwrites the return address that was saved in $ra by the earlier jal B.9Spilling 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?10Who saves the registers?Who is responsible for saving important registers across function calls?1. The caller knows which registers are important to it and should be saved.2. 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?11The 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 right, frodo wants to preserve $a0, $a1, $s0 and $s1 from gollum, but gollum may not even use those registers.frodo: li $a0, 3li $a1, 1li $s0, 4li $s1, 1# Save registers# $a0, $a1, $s0, $s1jal gollum# Restore registers# $a0, $a1, $s0, $s1add $v0, $a0, $a1add $v1, $s0, $s1jr $ra12…or the callee could save the registers…Another possibility is if the callee saves and restores any registers it might overwrite.For instance, a gollum function that uses registers $a0, $a2, $s0 and $s2 could save the original values first, and restore them before returning.But the callee does not know what registers are important to the caller, so again it may save more registers than necessary.gollum:# Save registers# $a0 $a2 $s0 $s2li $a0, 2li $a2, 7li $s0, 1li $s2, 8...# Restore registers# $a0 $a2 $s0


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?