MIPS Programming Arrays Writing Procedures: Calling Convention.

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
Lecture 9: MIPS Instruction Set
©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.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
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
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Assembly Language Chapter 2 MIPS –arithmetic instructions –control instructions Translating Instructions to binary MIPS –memory instructions Advanced data.
Intro to Computer Architecture
9/16/091 From C to MIPS David E. Culler CS61CL Sept 16, 2009 Lecture 4 UCB CS61CL F09 Lec 4.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
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 )
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
MIPS Functions and the Runtime Stack
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
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Prof. Hakim Weatherspoon
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Computer Architecture
Presentation transcript:

MIPS Programming Arrays Writing Procedures: Calling Convention

Memory Setup in C/Java C++: int *intarray = new int[10]; Java: int[] intarray = new int[10]; What does this do? What does the memory look like? Where is intarray[5] located? intarray + 20 Where is intarray[i] located? intarray + 4*i intarray intarray + 20 &(intarray[0]) + 20 intarray &(intarray[0])

Declaring & Initializing Global Arrays in HLL int GlobalA = 3; int GlobalB[] = {0x , 0x , 0x200b0001, 0x8b502a, 0x , 0x , 0x , 0x800fffa, 0x , 0x }; int main(int argc, char *argv[]) { } public class MyClass{ public static int GlobalA = 3; public static int GlobalB[] = {0x , 0x , 0x200b0001, 0x8b502a, 0x , 0x , 0x , 0x800fffa, 0x , 0x }; };

Declaring & Initializing Global Arrays in MIPS.data GlobalA:.word 0x03; GlobalB:.word 0x x x200b0001 0x8b502a.word 0x x x x800fffa.word 0x x text main:

Declaring & Initializing Local Arrays int main(int argc, char *argv[]) { int LocalA = 5; int LocalB[] = {1,2,3}; } add $sp, $sp, -(24 + x )# where x is space for preserved regs addi $t0, $0, 5 sw $t0, 12($sp) addi $t0, $0, 1 sw $t0, 0($sp) addi $t0, $0, 2 sw $t1, 4($sp) addi $t0, $0, 3 sw $t1, 8($sp) // and so forth Not possible in Java!!!!! In Java, arrays are references to Array objects.

Declaring & Initializing Heap Arrays in HLL int main(int argc, char *argv[]) { int *LocalA = (int *)malloc(4); *LocalA = 5; int *LocalB = (int *)malloc(12); LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; } public class MyClass{ public static void main(int argc, String argv) { int LocalA[] = new int[1]; LocalA[0] = 5; int LocalB[] = new int[3]; LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; } };

Declaring & Initializing Heap Arrays in MIPS add $sp, $sp, -(24 + x + 8)# where x is space for preserved regs addi $a0, $0, 4 jal malloc sw $v0, 4($sp) // store the reference into the stack addi $t0, $0, 5 sw $t0, 0($v0) // initialize first elements as 5 (*LocalA = 5) addi $a0, $0, 12 jal malloc sw $v0, 0($sp) // store the reference into the stack addi $t0, $0, 1 sw $t0, 0($v0) // LocalB[0] = 1 addi $t0, $0, 2 sw $t0, 4($v0) // LocalB[1] = 2 addi $t0, $0, 3 sw $t0, 8($v0) // LocalB[2] = 3

MIPS Example 5 Translate int A[100]; // ints are 4 bytes in C/Java char B[100]; // chars are 1 byte in C char c = B[50]; A[1] = A[5] + 7; Assumptions: A & B are global c is in the stack, 6 bytes from $sp Use LoadByte, not LoadWord, because char (in C) is 1 byte la $s1, B lb $t1, 50($s1)# $t1 = B[50]; lw $t0, 5 ($s0); x = A[5];lw $t0, 20 ($s0)# $t0 = A[5]; addi $t0, $t0, 7# $t0 = A[5] + 7; sw $t0, 4 ($s0)# A[1] = A[5] + 7; sb $t1, 6($sp)# c = B[50]; la $s0, A

MIPS Example 6 Translate int A[100]; int i; … x = A[i]; sll $t1, $s1, 2# $t1 = i<<2 or i * 4 add $t1, $s0, $t1# $t1 = (i*4+&A[0]) or &(A[i]) lw $t0, 0($t1)# $t0 = A[i]; Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass arguments –preserve registers, stack –pass return values Main Procedure Call Procedure

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 – excellent! no worries! –Procedure – may not destroy value must store at beginning and restore at end to use Not preserved – No guarantees –Caller – loses value in register most store before call and restore after call –Procedure – may use without worries

Steps for caller 0. Store any temp regs whose values we need 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 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

Caller: 4. Restore regs Assume: g,h are in $s2,$s3. We want return value in $s0. Caller wants to preserve $t0 across function call. Callee: int MyFunc(int g, int h) {return (g + h);} Caller: sw $t0, 0($sp) add $a0, $s2, $zero add $a1, $s3, $zero jal MyFunc add $s0, $v0, $zero lw $t0, 0($sp) Load from same location in the stack

Definitions Leaf function –Makes no function calls –You need not store $ra into the stack Non-leaf function –Contains a function call –You MUST store $ra in the stack and restore before returning to caller

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

Callee: 7. Return to caller 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: sw $t0, 0($sp) add $a0, $s2, $zero add $a1, $s3, $zero jal MyFunc add $s0, $v0, $zero lw $t0, 0($sp) Callee: addi $sp, $sp, -32 sw $s0, 0 ($sp) add $s0, $a0, $a1 add $v0, $s0, $zero lw $s0, 0 ($sp) addi $sp, $sp, 32 jr $ra

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 Assume it needs 2 saved registers

outgoing arg 5 $s1 $s0 padding 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 myarray $sp before call $sp during call Caller’s Stack $a3 $a2 $ra $fp $a0 $a1 Minimum Allocation

Another Example: int SumToN(int N) { int sum = 0; while (N > 0) { sum += N; N--; } return sum; } SumToN: add $v0, $0, $0 loop: slt $t0, $0, $a0 beq $t0, $0, end add $v0, $v0, $a0 addi $a0, $a0, -1 j loop end: jr $ra

Third Example: int SumNtotheM(int N, int M) { int sum = 0; while (N > 0) { sum += power(N,M); N--; } return sum; } SumToNtotheM: add $sp, $sp, -32 sw $ra, 14($sp) add $t0, $0, $0 loop:slt $t0, $0, $a0 beq $t0, $0, end sw $a0, 28($sp) sw $a1, 24($sp) sw $t0, 0($sp) jal power lw $t0, 0($sp) lw $a0, 28($sp) lw $a1, 24($sp) add $t0, $v0, $t0 addi $a0, $a0, -1 j loop end:add $v0, $t0, $0 lw $ra, 14($sp) add $sp, $sp, 32 jr $ra