Calling Conventions Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University See P&H 2.8 and 2.12.

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.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
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.
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
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.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Calling Conventions See: P&H 2.8, 2.12.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University See P&H 2.8 and 2.12.
RISC, CISC, and Assemblers Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University See P&H Appendix B.1-2, and Chapters 2.8 and 2.12.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Assemblers See: P&H Appendix B.1-2.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Intro to Computer Architecture
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University See: P&H Appendix A1-2, A.3-4 and 2.12.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
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 10: MIPS Simulator Today’s topic –SPIM Simulator Readings –Appendix B 1.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
Assemblers, Linkers, and Loaders Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See: P&H Appendix B.3-4 and 2.12.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer structure: Procedure Calls
MIPS Assembly Language Programming
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013
Prof. Kavita Bala and Prof. Hakim Weatherspoon
Prof. Hakim Weatherspoon
Prof. Kavita Bala and Prof. Hakim Weatherspoon
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Prof. Hakim Weatherspoon
Hakim Weatherspoon CS 3410 Computer Science Cornell University
The University of Adelaide, School of Computer Science
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013
MIPS Functions.
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Procedures and Calling Conventions
Hakim Weatherspoon CS 3410 Computer Science Cornell University
Program and memory layout
Program and memory layout
Program and memory layout
MIPS Functions.
Calling Conventions Hakim Weatherspoon CS 3410 Computer Science
Topic 2b ISA Support for High-Level Languages
Implementing Functions: Overview
Presentation transcript:

Calling Conventions Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University See P&H 2.8 and 2.12

2 Announcements PA1 due this Friday Work in pairs Use your resources FAQ, class notes, book, Sections, office hours, newsgroup, CSUGLab, etc PA2 will be available this Friday PA2 builds from PA1 Work with same partner Due right before spring break

3 Announcements Prelims1: next Thursday, March 10 th in class We will start at 1:25pm sharp, so come early Closed Book Cannot use electronic device or outside material Practice prelims are online in CMS Material covered Appendix C (logic, gates, FSMs, memory, ALUs) Chapter 4 (pipelined [and non-pipeline] MIPS processor with hazards) Chapters 2 and Appendix B (RISC/CISC, MIPS, and calling conventions) Chapter 1 (Performance) HW1, HW2, PA1, PA2

4 Goals for Today Calling Conventions Anatomy of an executing program Register assignment conventions, Function arguments, return values Stack frame, Call stack, Stack growth Variable arguments Next time More on stack frames globals vs local accessible data callee vs callrer saved registers

5 Example program vector v = malloc(8); v->x = prompt(“enter x”); v->y = prompt(“enter y”); int c = pi + tnorm(v); print(“result”, c); calc.c int tnorm(vector v) { return abs(v->x)+abs(v->y); } math.c global variable: pi entry point: prompt entry point: print entry point: malloc lib3410.o

6 Anatomy of an executing program 0xfffffffc 0x top bottom 0x7ffffffc 0x x x

7 math.s int abs(x) { return x < 0 ? –x : x; } int tnorm(vector v) { return abs(v->x)+abs(v->y); } math.c tnorm: # arg in r4, return address in r31 # leaves result in r4 abs: # arg in r3, return address in r31 # leaves result in r3

8 calc.s vector v = malloc(8); v->x = prompt(“enter x”); v->y = prompt(“enter y”); int c = pi + tnorm(v); print(“result”, c); calc.c dostuff: # no args, no return value, return addr in r31 MOVE r30, r31 LI r3, 8 # call malloc: arg in r3, ret in r3 JAL malloc MOVE r6, r3 # r6 now holds v LA r3, str1 # call prompt: arg in r3, ret in r3 JAL prompt SW r3, 0(r6) LA r3, str2 # call prompt: arg in r3, ret in r3 JAL prompt SW r3, 4(r6) MOVE r4, r6 # call tnorm: arg in r4, ret in r4 JAL tnorm LA r5, pi LW r5, 0(r5) ADD r5, r4, r5 LA r3, str3 # call print: args in r3 and r4 MOVE r4, r5 JAL print JR r30.data str1:.asciiz “enter x” str2:.asciiz “enter y” str3:.asciiz “result”.text.extern prompt.extern print.extern malloc.extern tnorm.global dostuff

9 Calling Conventions where to put function arguments where to put return value who saves and restores registers, and how stack discipline Why? Enable code re-use (e.g. functions, libraries) Reduce chance for mistakes Warning: There is no one true MIPS calling convention. lecture != book != gcc != spim != web

10 Example void main() { int x = ask(“x?”); int y = ask(“y?”); test(x, y); } void test(int x, int y) { int d = sqrt(x*x + y*y); if (d == 1) print(“unit”); return d; }

11 MIPS Register Conventions r0$zerozero r1$atassembler temp r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17 r18 r19 r20 r21 r22 r23 r24 r25 r26$k0reserved for OS kernel r27$k1 r28 r29 r30 r31$rareturn address $v0 function return values $v1 $a0 function arguments $a1 $a2 $a3

12 Example: Invoke void main() { int x = ask(“x?”); int y = ask(“y?”); test(x, y); } main: LA $a0, strX JAL ask # result in $v0 LA $a0, strY JAL ask # result in $v0

13 Call Stack Call stack contains activation records (aka stack frames) One for each function invocation: saved return address local variables … and more Simplification: frame size & layout decided at compile time for each function

14 Stack Growth Convention: r29 is $sp (bottom elt of call stack) Stack grows down Heap grows up 0x x x x xfffffffc system reserved code (text) stack static data dynamic data (heap)

15 Example: Stack frame push / pop void main() { int x = ask(“x?”); int y = ask(“y?”); test(x, y); } main: # allocate frame ADDUI $sp, $sp, -12 # $ra, x, y # save return address in frame SW $ra, 8($sp) # restore return address LW $ra, 8($sp) # deallocate frame ADDUI $sp, $sp, 12

16 Recap Conventions so far: args passed in $a0, $a1, $a2, $a3 return value (if any) in $v0, $v1 stack frame at $sp –contains $ra (clobbered on JAL to sub-functions) –contains local vars (possibly clobbered by sub-functions) Q: What about real argument lists?

17 Arguments & Return Values int min(int a, int b); int paint(char c, short d, struct point p); int treesort(struct Tree *root, int[] A); struct Tree *createTree(); int max(int a, int b, int c, int d, int e); Conventions: align everything to multiples of 4 bytes first 4 words in $a0...$a3, “spill” rest to stack

18 Argument Spilling invoke sum(0, 1, 2, 3, 4, 5); main:... LI $a0, 0 LI $a1, 1 LI $a2, 2 LI $a3, 3 ADDI $sp, $sp, -8 LI r8, 4 SW r8, 0($sp) LI r8, 5 SW r8, 4($sp) JAL sum ADDI $sp, $sp, 8 sum:... ADD $v0, $a0, $a1 ADD $v0, $v0, $a2 ADD $v0, $v0, $a3 LW $v1, 0($sp) ADD $v0, $v0, $v1 LW $v1, 4($sp) ADD $v0, $v0, $v1... JR $ra

19 Argument Spilling printf(fmt, …) main:... LI $a0, str0 LI $a1, 1 LI $a2, 2 LI $a3, 3 # 2 slots on stack LI r8, 4 SW r8, 0($sp) LI r8, 5 SW r8, 4($sp) JAL sum printf:... if (argno == 0) use $a0 else if (argno == 1) use $a1 else if (argno == 2) use $a2 else if (argno == 3) use $a3 else use $sp+4*argno...

20 VarArgs Variable Length Arguments Initially confusing but ultimately simpler approach: Pass the first four arguments in registers, as usual Pass the rest on the stack (in order) Reserve space on the stack for all arguments, including the first four Simplifies varargs functions Store a0-a3 in the slots allocated in parent’s frame Refer to all arguments through the stack

21 Recap Conventions so far: first four arg words passed in $a0, $a1, $a2, $a3 remaining arg words passed on the stack return value (if any) in $v0, $v1 stack frame at $sp –contains $ra (clobbered on JAL to sub-functions) –contains local vars (possibly clobbered by sub-functions) –contains extra arguments to sub-functions –contains space for first 4 arguments to sub-functions