1 / 30

MIPS Calling Convention

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.

solada
Download Presentation

MIPS Calling Convention

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIPS Calling Convention Chapter 2.7 Appendix A.6

  2. Procedure Calls Main Procedure Call Procedure Call Procedure

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

  4. MIPS-specific info Page 140, Figure 3.13

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

  6. 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

  7. 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

  8. 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:

  9. 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

  10. Definitions • Leaf function • Makes no function calls • Non-leaf function • Contains a function call

  11. 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?

  12. 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)

  13. 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

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

  15. 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

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

  17. 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)

  18. 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)

  19. 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

  20. 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)

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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:

More Related