Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIPS Calling Convention Chapter 2.7 Appendix A.6.

Similar presentations


Presentation on theme: "MIPS Calling Convention Chapter 2.7 Appendix A.6."— Presentation transcript:

1 MIPS Calling Convention Chapter 2.7 Appendix A.6

2 Procedure Calls Main Procedure Call Procedure

3 Procedure Calls Procedure must _____ _______ from any call Procedure uses _____ that main was using We need a convention to –

4 NameReg NumberUsagePreserved across call? $zero0The constant 0Yes $v0-$v12-3Function resultsNo $a0-$a34-7Function ArgumentsNo $t0-$t78-15TemporariesNo $s0-$s716-23SavedYes $t8-$t924-25More temporariesNo $gp28Global pointerYes $sp29Stack pointerYes $fp30Frame pointerYes $ra31Return addressYes Page 140, Figure 3.13 MIPS-specific info

5 MIPS-specific info – who cares? Preserved – Value is same after call –Caller –Procedure Not preserved – No guarantees –Caller –Procedure

6

7 Steps for caller 1.Store away any temporary registers we want 2.Pass function parameters to procedure 3.Transfer control to procedure 4.(then procedure executes) 5.Get return value 6.Restore any temp regs we saved away

8 Steps for procedure 1.Allocate stack space 2.Store preserved regs we may use 3.Perform task 4.Place result in proper location for caller 5.Restore preserved regs we may have used 6.Transfer control back to caller

9 Write the caller & procedure code for the following function: Caller:Callee: 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);}

10 Steps for procedure 1.Allocate stack space 2.Store preserved regs we may use 3.Perform task 4.Place result in proper location for caller 5.Restore preserved regs we may have used 6.Transfer control back to caller How do we know what we’ll use until we write task code? How do we know how much space until we know how many regs to store?

11

12 Definitions Leaf function –Makes no function calls Non-leaf function –Contains a function call

13 Allocating stack space $sp Stack space for this function Only allocate once per function!!!! $sp contains address of bottom of stack. What operation on $sp allocates space? Minimum allocation: 24 bytes

14 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 )

15 What actually goes in stack $ra Extra Arguments Extra outgoing arguments $sp before call $sp during call padding local data L*4 bytes for local data P*4 bytes for preserved regs ($s0-$s7) A*4 bytes for outgoing args $fp preserved registers (and $a0-$a3) $fp during call

16 Example int foo(int arg1, int arg2) { int myarray[64]; myarray[3] = 5; … bar(a1, a2, a3, a4, a5); … return (myarray[3]); } Local 256- byte array Non-leaf function, 5 outgoing args Assume it needs 2 saved registers

17 Caller’s Stack addi$sp, $sp, - ( )*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, ( )*4 jr $ra $a3 myarray $sp before call $sp during call $a2 $s1 $s0 padding outgoing arg 5 $ra $fp $a0 $a1

18

19 Recursive call: SumToN int SumToN(int N) { if (N < 2) return 1; else return (SumToN(N-1) + N); } Both the caller and the callee!!!

20 MIPS Stack SumToN(2) $sp 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

21 MIPS Stack SumToN(2) $sp 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

22 MIPS Stack SumToN(2) $sp $fp $sp $ra 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

23 MIPS Stack SumToN(2) $sp $a0 (2) $fp $sp $ra 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

24 MIPS Stack SumToN(1) $sp $a0 (2) $fp $sp $ra 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 $fp $sp $ra

25 MIPS Stack SumToN(1) $sp $a0 (2) $fp $sp $ra 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 $fp $sp $ra $v0 = 1

26 MIPS Stack SumToN(1) $sp $a0 (2) $fp $sp $ra 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 $fp $ra $v0 = 1

27 MIPS Stack SumToN(2) $sp $a0 (2) $fp $sp $ra 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 $fp $ra $v0 = 3

28 MIPS Stack SumToN(2) $a0 (2) $fp $sp $ra 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 $fp $ra $v0 = 3

29 MIPS Stack SumToN(2) $a0 (2) $fp $sp $ra 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 $fp $ra $v0 = 3 What about the garbage in the stack?

30 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:


Download ppt "MIPS Calling Convention Chapter 2.7 Appendix A.6."

Similar presentations


Ads by Google