[PDF] [PDF] Functions in MIPS - Washington

— 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



Previous PDF Next PDF





[PDF] Functions in MIPS - Washington

— 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



[PDF] Pointers: Parameter Passing and Return Passing and Return

Returning multiple values from a function ▫ Return statement can return only one value ▫ What if we want to return more than one value? ▫ Use pointers 7



[PDF] Functions

Can take in special inputs (arguments) ▷ Can produce an answer value (return value) ▷ Similar to the idea of a function in mathematics With functions, there 



[PDF] First-Class Functions in an Imperative World - Luaorg

(They can be passed as arguments, but cannot be returned nor assigned, to avoid escapes ) Modula-2 has both functions as first-class values and lexical scoping, 



[PDF] Functions That Return Results, Decision Structures - Rose-Hulman

Functions can take multiple parameters □ def distance (p1, p2): # p1, p2 are points xdist = abs(p1 getX()- p2 getX()) ydist = abs(p1 getY()- p2 getY()) return 



[PDF] Functions That Return Results, Decision Structures - Rose-Hulman

Python's can help increase your understanding of what really goes on in a program □ Many other programming languages (notably Java, C++, and C#) derive 



[PDF] Assembly Language: Function Calls - Princeton University

0 call instruction pushes return addr (old RIP) onto stack, then jumps RIP ( instruction pointer) register points to next instruction to be executed Note: Can't really 

[PDF] functions in mathematics

[PDF] functions lecture notes

[PDF] functions of flour in baking

[PDF] functions of ingredients worksheet

[PDF] functions of management pdf notes

[PDF] functions of mobile computing

[PDF] functions of propaganda

[PDF] functions of proteins

[PDF] functions of the nervous system

[PDF] functions of the respiratory system

[PDF] functions of the skin

[PDF] functions of theatre in society

[PDF] functions pdf

[PDF] functions pdf notes

[PDF] functions problems and solutions pdf

1

Lecture 5

ƒAnnouncements:

ƒToday:

³Finish up functions in MIPS

2

Control flow in C

ƒInvoking a function changes the

control flow of a program twice.

1.Calling the function

2.Returningfrom the function

ƒIn this example the mainfunction

calls facttwice, and fact returns twice³but to differentlocations 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 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; 3

Function control flow MIPS

ƒMIPS uses the jump-and-link instruction jalto call functions. ³The jal saves the return address (the address of the nextinstruction) 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 ƒIHP·V QRR MGG POH ÓMO MQG ÓU LQVPUXŃPLRQV POMP MUH QHŃHVVMU\ IRU RXU factorial example. 4

Data flow in C

ƒFunctions accept argumentsand

produce return values.

ƒThe blueparts of the program

show the actual and formal arguments of the fact function.

ƒThe purpleparts of the code deal

with returning and using a result. int main() t1 =fact(8); t2 =fact(3); t3 = t1 + t2; intfact(int n) int i, f = 1; for (i = n; i > 1; i--) f = f * i; return f; 5

Data flow in MIPS

ƒMIPS uses the following conventions for function arguments and results. argument registers $a0-$a3before calling the function with jal. $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. ƒIMPHU RH·OO PMON MNRXP OMQGOLQJ MGGLPLRQMO MUJXPHQPV RU UHPXUQ YMOXHVB 6 ƒAssembly language is untyped³there is no distinction between integers, characters, pointers or other kinds of values. ƒIt is up to youPR ´P\SH ŃOHŃNµ \RXU SURJUMPVB HQ SMUPLŃXOMU PMNH VXUH your function arguments and return values are used consistently. ƒFor example, what happens if somebody passes the addressof an integer (instead of the integer itself) to the fact function?

A note about types

7

The big problem so far

ƒThere is a big problem here!

³The main code uses $t1to store the result of fact(8).

³But $t1is also used within the fact function!

ƒThe subsequent call to fact(3) will overwrite the value of fact(8) that was stored in $t1. 8 A:... 3XP %·V MUJV LQ M0-$a3 jalB# $ra = A2

A2:...

B:... 3XP F·V MUJV LQ M0-$a3, HUMVLQJ %·V MUJVA jalC# $ra = B2

B2:...

jr$ra# Where does # this go??? C:... jr$ra

Nested functions

ƒA similar situation happens when

you call a function that then calls another function. ƒIHP·V VM\ $ ŃMOOV % ROLŃO ŃMOOV FB quotesdbs_dbs21.pdfusesText_27