DOC PREVIEW
CORNELL CS 3410 - Calling Conventions

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1AnnouncementsAnnouncementsGoals for TodayExample programAnatomy of an executing programmath.scalc.sCalling ConventionsExampleMIPS Register ConventionsExample: InvokeCall StackStack GrowthExample: Stack frame push / popRecapArguments & Return ValuesArgument SpillingArgument SpillingVarArgsRecapCalling ConventionsHakim WeatherspoonCS 3410, Spring 2011Computer ScienceCornell UniversitySee P&H 2.8 and 2.122AnnouncementsPA1 due this Friday Work in pairsUse your resources•FAQ, class notes, book, Sections, office hours, newsgroup, CSUGLab, etcPA2 will be available this Friday•PA2 builds from PA1•Work with same partner•Due right before spring break3AnnouncementsPrelims1: next Thursday, March 10th 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, PA24Goals for TodayCalling Conventions•Anatomy of an executing program•Register assignment conventions,•Function arguments, return values•Stack frame, Call stack, Stack growth•Variable argumentsNext time •More on stack frames•globals vs local accessible data•callee vs callrer saved registers5Example programvector v = malloc(8);v->x = prompt(“enter x”);v->y = prompt(“enter y”);int c = pi + tnorm(v);print(“result”, c);calc.cint tnorm(vector v) { return abs(v->x)+abs(v->y);}math.cglobal variable: pientry point: promptentry point: printentry point: malloclib3410.o6Anatomy of an executing program0xfffffffc0x00000000topbottom0x7ffffffc0x800000000x100000000x004000007math.sint abs(x) {return x < 0 ? –x : x;}int tnorm(vector v) { return abs(v->x)+abs(v->y);}math.ctnorm:# arg in r4, return address in r31# leaves result in r4abs:# arg in r3, return address in r31# leaves result in r38calc.svector v = malloc(8);v->x = prompt(“enter x”);v->y = prompt(“enter y”);int c = pi + tnorm(v);print(“result”, c);calc.cdostuff:# no args, no return value, return addr in r31MOVE r30, r31LI r3, 8 # call malloc: arg in r3, ret in r3JAL mallocMOVE r6, r3 # r6 now holds vLA r3, str1 # call prompt: arg in r3, ret in r3JAL promptSW r3, 0(r6)LA r3, str2 # call prompt: arg in r3, ret in r3JAL promptSW r3, 4(r6)MOVE r4, r6 # call tnorm: arg in r4, ret in r4JAL tnorm LA r5, piLW r5, 0(r5)ADD r5, r4, r5 LA r3, str3 # call print: args in r3 and r4MOVE r4, r5JAL printJR r30.datastr1: .asciiz “enter x”str2: .asciiz “enter y”str3: .asciiz “result”.text.extern prompt.extern print.extern malloc.extern tnorm.global dostuff9Calling ConventionsCalling Conventions•where to put function arguments•where to put return value•who saves and restores registers, and how•stack disciplineWhy?•Enable code re-use (e.g. functions, libraries)•Reduce chance for mistakesWarning: There is no one true MIPS calling convention.lecture != book != gcc != spim != web10Examplevoid 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;}11MIPS Register Conventionsr0 $zero zeror1 $at assembler tempr2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26 $k0reservedfor OS kernelr27 $k1r28r29r30r31 $ra return address$v0functionreturn values$v1$a0functionarguments$a1$a2$a312Example: Invokevoid main() {int x = ask(“x?”);int y = ask(“y?”);test(x, y);}main:LA $a0, strXJAL ask # result in $v0LA $a0, strYJAL ask # result in $v013Call StackCall stack •contains activation records (aka stack frames)One for each function invocation:•saved return address•local variables•… and moreSimplification:•frame size & layout decided at compile time for each function14Stack GrowthConvention:•r29 is $sp(bottom eltof call stack)Stack grows downHeap grows up0x000000000x800000000x100000000x004000000xfffffffcsystem reservedsystem reservedcode (text)stackstatic datadynamic data (heap)15Example: Stack frame push / popvoid main() {int x = ask(“x?”);int y = ask(“y?”);test(x, y);}main:# allocate frameADDUI $sp, $sp, -12 # $ra, x, y# save return address in frameSW $ra, 8($sp)# restore return addressLW $ra, 8($sp)# deallocate frameADDUI $sp, $sp, 1216RecapConventions 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?17Arguments & Return Valuesint 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 stack18Argument Spillinginvoke sum(0, 1, 2, 3, 4, 5);main:...LI $a0, 0LI $a1, 1LI $a2, 2LI $a3, 3ADDI $sp, $sp, -8LI r8, 4SW r8, 0($sp)LI r8, 5SW r8, 4($sp)JAL sumADDI $sp, $sp, 8sum:...ADD $v0, $a0, $a1ADD $v0, $v0, $a2ADD $v0, $v0, $a3LW $v1, 0($sp)ADD $v0, $v0, $v1LW $v1, 4($sp)ADD $v0, $v0, $v1...JR $ra19Argument Spillingprintf(fmt, …)main:...LI $a0, str0LI $a1, 1LI $a2, 2LI $a3, 3# 2 slots on stackLI r8, 4SW r8, 0($sp)LI r8, 5SW r8, 4($sp)JAL sumprintf:...if (argno == 0)use $a0else if (argno == 1)use $a1else if (argno == 2)use $a2else if (argno == 3)use $a3elseuse $sp+4*argno...20VarArgsVariable Length ArgumentsInitially 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 fourSimplifies varargs functions•Store a0-a3 in the slots allocated in parent’s frame•Refer to all arguments through the stack21RecapConventions 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


View Full Document

CORNELL CS 3410 - Calling Conventions

Documents in this Course
Marra

Marra

43 pages

Caches

Caches

34 pages

ALUs

ALUs

5 pages

Caches!

Caches!

54 pages

Memory

Memory

41 pages

Caches

Caches

32 pages

Caches

Caches

54 pages

Caches

Caches

34 pages

Caches

Caches

54 pages

Load more
Download Calling Conventions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Calling Conventions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Calling Conventions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?