300 likes | 469 Views
MIPS Calling Convention. Chapter 2.7 Appendix A.6. Procedure Calls. Main. Procedure. Call Procedure. Call Procedure. Procedure Calls. Procedure must _____ _______ from any call Procedure uses _____ that main was using We need a convention to. MIPS-specific info. Page 140, Figure 3.13.
E N D
MIPS Calling Convention Chapter 2.7 Appendix A.6
Procedure Calls Main Procedure Call Procedure Call Procedure
Procedure Calls • Procedure must _____ _______ from any call • Procedure uses _____ that main was using • We need a convention to
MIPS-specific info Page 140, Figure 3.13
MIPS-specific info – who cares? • Preserved – Value is same after call • Caller • Procedure • Not preserved – No guarantees • Caller • Procedure
Steps for caller • Store away any temporary registers we want • Pass function parameters to procedure • Transfer control to procedure • (then procedure executes) • Get return value • Restore any temp regs we saved away
Steps for procedure • Allocate stack space • Store preserved regs we may use • Perform task • Place result in proper location for caller • Restore preserved regs we may have used • Transfer control back to caller
Write the caller & procedure code for the following function: Assume: g,h are in $s2,$s3. We want return value in $s0. Caller wants to preserve $t0 across function call. int MyFunc(int g, int h) { return (g + h);} Caller: Callee:
How do we know how much space until we know how many regs to store? How do we know what we’ll use until we write task code? Steps for procedure • Allocate stack space • Store preserved regs we may use • Perform task • Place result in proper location for caller • Restore preserved regs we may have used • Transfer control back to caller
Definitions • Leaf function • Makes no function calls • Non-leaf function • Contains a function call
Allocating stack space Stack space for this function $sp $sp Minimum allocation: 24 bytes $sp Only allocate once per function!!!! $sp contains address of bottom of stack. What operation on $sp allocates space?
How much stack space? • Minimum allocation 24 bytes • $ra , $fp even if unused. • 4 words for $a0-$a3 in case we need it • Preserved registers that we destroy (i.e. $s0) • Local variables declared in our function • Any other outgoing arguments (non-leaf) • Total bytes must be divisible by 8 (aligned for floating-point numbers)
What actually goes in stack Extra Arguments $sp before call $fp during call $ra $fp P*4 bytes for preserved regs ($s0-$s7) preserved registers (and $a0-$a3) padding local data L*4 bytes for local data Extra outgoing arguments A*4 bytes for outgoing args $sp during call
Assume it needs 2 saved registers Example Local 256-byte array int foo(int arg1, int arg2) { int myarray[64]; myarray[3] = 5; … bar(a1, a2, a3, a4, a5); … return (myarray[3]); } Non-leaf function, 5 outgoing args
Caller’s Stack addi $sp, $sp, - (1+64+1+2+6)*4 sw $ra, 73*4($sp) sw $fp, 72*4($sp) sw $s1, 67*4($sp) sw $s0, 66*4($sp) addi $t0, $zero,5 # $t0 = 5 sw $t0, (1+3)*4 ($sp)# myarray[3] = 5 … lw $ra, 73*4($sp) lw $fp, 72*4($sp) lw $s1, 67*4($sp) lw $s0, 66*4($sp) addi $sp, $sp, (1+64+1+2+6)*4 jr $ra $ra $sp before call $fp $a0 $a1 $a2 $a3 $s1 $s0 padding myarray $sp during call outgoing arg 5
Recursive call: SumToN int SumToN(int N) { if (N < 2) return 1; else return (SumToN(N-1) + N); } Both the caller and the callee!!!
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp MIPS Stack SumToN(2)
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp MIPS Stack SumToN(2)
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp MIPS Stack SumToN(2) $ra $fp
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp MIPS Stack SumToN(2) $ra $fp $a0 (2)
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp $sp MIPS Stack SumToN(1) $ra $fp $a0 (2) $ra $fp
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp $sp MIPS Stack SumToN(1) $ra $fp $a0 (2) $ra $fp $v0 = 1
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp MIPS Stack SumToN(1) $ra $fp $a0 (2) $ra $fp $v0 = 1
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp $sp MIPS Stack SumToN(2) $ra $fp $a0 (2) $ra $fp $v0 = 3
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp MIPS Stack SumToN(2) $ra $fp $a0 (2) $ra $fp $v0 = 3
SumToN: addi $sp, $sp, -24 sw $ra, 20 ($sp) sw $fp, 16 ($sp) addi $fp, $sp, 20 slti $t0, $a0, 2 beq $t0, $0, result addi $v0, $zero,1 j end result: sw $a0, 0 ($sp) addi $a0, $a0, -1 jal SumToN lw $a0, 0 ($sp) add $v0, $v0, $a0 end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra $sp MIPS Stack SumToN(2) $ra $fp What about the garbage in the stack? $a0 (2) $ra $fp $v0 = 3
MIPS instructions • Rule: Destination register always comes first • Exception: • Rule: Any instruction involving constants has “i” at the end of instruction(add vs addi) • Exception: