MIPS Calling Convention Chapter 2.7 Appendix A.6.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More.
Procedure call frame: Hold values passed to a procedure as arguments
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
MIPS Programming Arrays Writing Procedures: Calling Convention.
Intro to Computer Architecture
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Lecture 6: Assembly Programs
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Prof. Hakim Weatherspoon
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
MIPS function continued
Presentation transcript:

MIPS Calling Convention Chapter 2.7 Appendix A.6

Procedure Calls Main Procedure Call Procedure

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

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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: