32
Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions JR and JAL Stack pointer register $SP and stack conventions

Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Lecture 6: Procedure Call (Part I)•Standard conventions for procedure call

•MIPS support for procedure call•Register conventions

•JR and JAL

•Stack pointer register $SP and stack conventions

Page 2: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Function call conventions in Cmain() {int i,j,k,m;...i = add(j,k); m = add(i,i);

}

/* function add */

int add (int addin1, int addin2){int sum; sum = addin1 + addin2; }return sum;}

Calling procedure: pass parameters to function by putting them in an agreed upon place

Called procedure: get parameters, use them, and return results by putting them in agreed upon place.

Page 3: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Function call conventions in MIPS# One caller: j in $s1,k in $s2,I in $s3...JUMP ADDFUNC

----

# Another caller: j in $s7,k in $s8,I in $t3...JUMP ADDFUNC

----

# function ADDFUNC

ADDFUNC: ADD $t1,$t2,$t3

ADD $t4,$t1,$t1

JUMP ???

Calling procedure:Where to put the parameters ???

Called procedure: where to get the parameters, what registers to use for computation, how to get back to caller??

Page 4: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Function Call Conventions in Assembly

•Registers play a major role in keeping track of information for function calls.

•MIPS register conventions:•Return address $ra

•Arguments $a0, $a1, $a2, $a3

•Return value $v0, $v1

•Local variables $s0, $s1, … , $s7

•The stack is also used; more later.

Page 5: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Instruction Support for Functions (1/5) ... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;}

address1000 add $a0,$s0,$zero # x = a1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 #$ra=10161012 j sum #jump to sum1016 ...

2000 sum: add $v0,$a0,$a12004 jr $ra # new instruction

C

MIPS

Page 6: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Instruction Support for Functions (2/5) ... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;}

2000 sum: add $v0,$a0,$a12004 jr $ra # new instruction

C

MIPS

•Question: Why use jr instead of using j?

•Answer: sum might be called by many functions, so we can’t return to a fixed location/address. The calling proc to sum must be able to specify “return address”.

Page 7: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Instruction Support for Functions (3/5)• Single instruction to jump and save

return address: jump and link (jal)

• Earlier approach:

1008 addi $ra,$zero,1016 #$ra=10161012 j sum #goto sum

• Faster approach:

1008 jal sum # $ra=address of next # instruction # $ra=1012;goto sum Why have a new instruction (jal)?

• Make the common case fast, function calls are very common.

• Also, you don’t need to know the memory address of individual instructions with jal.

Page 8: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Instruction Support for Functions (4/5)•Syntax for jal (jump and link) is same as for j (jump):

jal label

• jal should really be called laj for “link and jump”:•Step 1 (link): Save address of next instruction into $ra - Why next instruction? Why not current one?

•Step 2 (jump): Jump to the given label

Page 9: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Instruction Support for Functions (5/5)•Syntax for jr (jump register):

jr register

•Instead of providing a label to jump to, the jr instruction provides a register which contains an address to jump to.

•Only useful if we know exact address to jump to.

•Use this MIPS convention for function call and return:•jal stores return address in register ($ra)•jr $ra jumps back to that address

Page 10: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

########################################################### MY FIRST SPIM PROCEDURE## Simple procedure example: not more than 4 arguments, only ## 1 return value, no calls from within the procedure, and ## no local variables!########################################################### Write SPIM/MIPS code for this procedure using register# conventions we just learned## int foo (int ain, int bin) {# int n= 2+ain+bin;# return n;# }#########################################################

Page 11: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

# load parameters for test call to foo(4,6) li $a0,4 # set up first parameter li $a1,6 # set up second parameter

# call foo jal foo # call function

# on return from foo, the result is in $v0, save it and print it move $a0,$v0 # move result into argument li $v0,1 # syscall code for print integer syscall

# end program li $v0,10 syscall # terminate execution

Solution: calling program

Page 12: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

## Procedure foofoo:# get arguments mov $t0,$a0 # get first argument mov $t1,$a1 # get second argument

# perform body of the procedure addi $t0,$t0,2 #compute 2+first argument add $t2,$t0,$t1 #compute 2+first arg+second arg

# set up return value in register $v0 move $v0,$t2

# return jr $ra #return

Solution: procedure foo

Page 13: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Summary: Rules for Simple Procedures•Called with a jal instruction, returns with a jr $ra

•Accepts up to 4 arguments in $a0, $a1, $a2 and $a3

•Return value is always in $v0 (and if necessary in $v1)

•Must follow register conventions (even in functions that only you will call)!

Page 14: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Nested Procedures (1/2)

int sumSquare(int x, int y) {return mult(x,x)+ y;

}

•A procedure called sumSquare, now sumSquare is calling mult.

•Problem: there’s a return address in $ra that sumSquare wants to jump but it will be overwritten by the call to mult.

•Need to save sumSquare return address before call to mult.

Page 15: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Nested Procedures (2/2)

•In general, may need to save some other info in addition to $ra.

•When a C program is run, there are 3 important memory areas allocated:•Static: Variables declared once per program, cease to exist only after execution completes. e.g., C globals

•Heap: Variables declared dynamically

•Stack: Space to be used by procedure during execution; this is where we can save register values

Page 16: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

C Memory Allocation

0

fff fffAddress

Code Program

StaticVariables declaredonce per program

HeapExplicitly created space, e.g., malloc(); C pointers

StackSpace for saved procedure information$sp

stackpointer

Page 17: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Using the Stack (1/2)

•C stack grows downward in memory

•register $sp always points to the last used space in the stack.

•To use stack, we decrement this pointer by the amount of space we need and then fill it with info.

•So, how do we compile this?int sumSquare(int x, int y) {

return mult(x,x)+ y;}

Page 18: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Using the Stack (2/2)•Hand-compile

sumSquare: addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret addr sw $a1, 0($sp) # save y

add $a1,$a0,$zero # mult(x,x) jal mult # call mult

lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # mult()+y lw $ra, 4($sp) # get ret addr addi $sp,$sp,8 # update sp jr $ra

mult: ...

int sumSquare(int x, int y) {return mult(x,x)+ y; }

“push”

“pop”

Page 19: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Register Conventions (1/4)

•Caller: the calling function

•Callee: the function being called

•When callee returns from executing, the caller needs to know: •which registers may have changed, and

•which registers are guaranteed to be unchanged.

•Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call (jal) and which registers may be changed.

Page 20: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Register Conventions (2/4) - saved•$0: No Change. Always 0.

•$s0-$s7: Save and 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: Save and 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!

Page 21: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

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’ll need them 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.

Page 22: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Register Conventions (4/4)

•What do these conventions mean?•If function R calls function E, then function R/caller must save any t (temporary) registers that it may be using onto the stack before making a jal call.

•Function E/callee 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.

Page 23: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Beta:

Save Saved Registers

Restore Saved Registers

Return Results

Send Parameters

Receive Results

Call Procedure

Save Unsaved Registers

$t’s, $v’s, $a’s, $ra

Restore Unsaved Regs.

$t’s, $v’s, $a’s, $ra

Return

…Retrieve Parameters

Alpha:

Save Saved Registers

Restore Saved Registers

Return Results

Send Parameters

Receive Results

Call Procedure

Retrieve Parameters

Save Unsaved Registers

$t’s, $v’s, $a’s, $ra

Restore Unsaved Regs.

$t’s, $v’s, $a’s, $ra

Return

Structure of a

Procedure

Page 24: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

MIPS Registers The constant 0 $0 $zeroReserved for Assembler $1 $atReturn Values $2-$3 $v0-$v1Arguments $4-$7 $a0-$a3Temporary $8-$15 $t0-$t7Saved $16-$23 $s0-$s7More Temporary $24-$25 $t8-$t9Used by Kernel $26-27 $k0-$k1Global Pointer $28 $gpStack Pointer $29 $spFrame Pointer $30 $fpReturn Address $31 $ra

Use names for registers -- code is clearer!

Page 25: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Other Registers

•$at: may be used by the assembler at any time; unsafe to use

•$k0-$k1: may be used by the OS at any time; unsafe to use

•$gp, $fp: don’t worry about them

•Note: Feel free to read up on $gp and $fp in Appendix A, but you can write perfectly good MIPS code without them.

Page 26: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

Quickie Quiz (True or False?)

When translating this to MIPS…

A. We COULD copy $a0 to $a1 (not store $a0 or $a1 on the stack) to preserve n across recursive calls.

B. We MUST save $a0 on the stack since it gets changed.

C. We MUST save $ra on the stack since we need to know where to return to…

int fact(int n){ if(n == 0) return 1; else return(n*fact(n-1));}

Tail Recursion

F

F

F

Page 27: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

What does r have to push on the stack before “jal e”?

1: Nothing 2: 1 of ($s0,$sp,$v0,$t0,$a0,$ra) 3: 2 of ($s0,$sp,$v0,$t0,$a0,$ra) 4: 3 of ($s0,$sp,$v0,$t0,$a0,$ra) 5: 4 of ($s0,$sp,$v0,$t0,$a0,$ra) 6: 5 of ($s0,$sp,$v0,$t0,$a0,$ra)

r: ... # R/W $s0,$v0,$t0,$a0,$sp,$ra,mem ... ### PUSH REGISTER(S) TO STACK? jal e # Call e ... # R/W $s0,$v0,$t0,$a0,$sp,$ra,mem jr $ra # Return to caller of r e: ... # R/W $s0,$v0,$t0,$a0,$sp,$ra,mem jr $ra # Return to r

Volatile! -- need to pushSaved

e can’t return changed, no need to push

e can return changed

Quickie Quiz

Page 28: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

#------------- Function fibo ---------------------------------

## int fibo(int n) {## if (n <= 2)## return 1;## else return(fib(n-2) + fib(n-1));## }

## fibo is a recursive procedure to find a given Fibbonacci numberfibo: sub $sp, $sp, 12 # push stack frame sw $ra, 0($sp) # save return address sw $s0, 4($sp) # and s registers used here sw $s1, 8($sp)

bgt $a0, 2, f0 # check for input greater than 1 li $v0, 1 # if not, just return a 1 b fret

Page 29: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

# input OK, set up recursive callsf0: move $s0, $a0 # copy a0 to s0 so it's saved across calls

sub $a0, $s0, 2 # set param to n-2 jal fibo # and make recursive call move $s1, $v0 # save fib(n-2)

sub $a0, $s0, 1 # param = n-1 jal fibo # compute fib(n-1) add $v0, $v0, $s1 # add fib(n-2)

# return value from this call is already in $v0 as needed

fret: lw $ra, 0($sp) # restore return address, lw $s0, 4($sp) # s registers, lw $s1, 8($sp) add $sp, $sp, 12 # pop stack frame, jr $ra # and return....

Page 30: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

# RECURSIVE PROCEDURE TO COMPUTE FACTORIALS

FACT: #save the registers that FACT uses sub $sp,$sp,8 #modify stack top sw $s0,0($sp) #save registers sw $ra,4($sp)

move $s0,$a0 #get parameter out of a0

#For n=1, return 1

bne $s0,1,MORE li $v0,1 #return a 1 j RET

Page 31: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

#For n>1, return n*FACT(n-1)MORE:## move arguments in for call sub $a0,$s0,1 #compute n-1

## call FACT on n-1 jal FACT #recursive call

## use returned value and set up return value for this call mul $v0,$v0,$s0

RET: lw $s0,0($sp) #restore registers lw $ra,4($sp) add $sp,$sp,8 #reposition stack top

jr $ra #return

Page 32: Procedures I Fall 2007 Lecture 6: Procedure Call (Part I) Standard conventions for procedure call MIPS support for procedure call Register conventions

Procedures I Fall 2007

“And in Conclusion…”•Functions called with jal, return with jr $ra.

•Use stack to save anything you need. Just be sure to leave it the way you found it.

•Instructions we know so farArithmetic: add, addi, sub, addu, addiu, subu

Memory: lw, sw, lh, sh, lb, sb

Decision: beq, bne, (blt, ble, bgt, bge)

slt, slti, sltu, sltiu

Unconditional Branches (Jumps): j, jal, jr

•Registers we know so far•All of them!