30
MIPS Calling Convention Chapter 2.7 Appendix A.6

MIPS Calling Convention Chapter 2.7 Appendix A.6

Embed Size (px)

Citation preview

Page 1: MIPS Calling Convention Chapter 2.7 Appendix A.6

MIPS Calling Convention

Chapter 2.7

Appendix A.6

Page 2: MIPS Calling Convention Chapter 2.7 Appendix A.6

Procedure CallsMain

Procedure

Call Procedure

Call Procedure

Page 3: MIPS Calling Convention Chapter 2.7 Appendix A.6

Procedure Calls

• Procedure must _____ _______ from any call

• Procedure uses _____ that main was using

• We need a convention to– – –

Page 4: MIPS Calling Convention Chapter 2.7 Appendix A.6

Name Reg Number Usage Preserved across call?

$zero 0 The constant 0 Yes

$v0-$v1 2-3 Function results No

$a0-$a3 4-7 Function Arguments No

$t0-$t7 8-15 Temporaries No

$s0-$s7 16-23 Saved Yes

$t8-$t9 24-25 More temporaries No

$gp 28 Global pointer Yes

$sp 29 Stack pointer Yes

$fp 30 Frame pointer Yes

$ra 31 Return address Yes

Page 140, Figure 3.13

MIPS-specific info

Page 5: MIPS Calling Convention Chapter 2.7 Appendix A.6

MIPS-specific info – who cares?• Preserved – Value is same after call

– Caller

– Procedure

• Not preserved – No guarantees– Caller

– Procedure

Page 6: MIPS Calling Convention Chapter 2.7 Appendix A.6
Page 7: MIPS Calling Convention Chapter 2.7 Appendix A.6

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

Page 8: MIPS Calling Convention Chapter 2.7 Appendix A.6

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

Page 9: MIPS Calling Convention Chapter 2.7 Appendix A.6

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);}

Page 10: MIPS Calling Convention Chapter 2.7 Appendix A.6

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?

Page 11: MIPS Calling Convention Chapter 2.7 Appendix A.6
Page 12: MIPS Calling Convention Chapter 2.7 Appendix A.6

Definitions

• Leaf function– Makes no function calls

• Non-leaf function– Contains a function call

Page 13: MIPS Calling Convention Chapter 2.7 Appendix A.6

Allocating stack space

$sp

$sp$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

Page 14: MIPS Calling Convention Chapter 2.7 Appendix A.6

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)

Page 15: MIPS Calling Convention Chapter 2.7 Appendix A.6

What actually goes in stack

$ra

Extra Arguments

Extra outgoing arguments

$spbefore call

$spduring call

padding

local dataL*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)

$fpduring call

Page 16: MIPS Calling Convention Chapter 2.7 Appendix A.6

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

Page 17: MIPS Calling Convention Chapter 2.7 Appendix A.6

Caller’s Stackaddi $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 = 5sw $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)*4jr $ra

$a3

myarray

$spbefore call

$spduring call

$a2

$s1$s0

padding

outgoing arg 5

$ra$fp$a0$a1

Page 18: MIPS Calling Convention Chapter 2.7 Appendix A.6
Page 19: MIPS Calling Convention Chapter 2.7 Appendix A.6

Recursive call: SumToN

int SumToN(int N){ if (N < 2) return 1; else return (SumToN(N-1) + N);}

Both the caller and the callee!!!

Page 20: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

Page 21: MIPS Calling Convention Chapter 2.7 Appendix A.6

MIPS Stack SumToN(2)$sp

$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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

Page 22: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

Page 23: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

Page 24: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

$fp

$sp

$ra

Page 25: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

$fp

$sp

$ra

$v0 = 1

Page 26: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

$fp$ra

$v0 = 1

Page 27: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

$fp$ra

$v0 = 3

Page 28: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: lw $ra, 20 ($sp) lw $fp, 16 ($sp) addi $sp, $sp, 24 jr $ra

$fp$ra

$v0 = 3

Page 29: MIPS Calling Convention Chapter 2.7 Appendix A.6

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, $a0end: 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?

Page 30: MIPS Calling Convention Chapter 2.7 Appendix A.6

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: