DOC PREVIEW
CORNELL CS 3410 - Calling Conventions

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 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 46 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 46 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 46 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 46 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 46 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 46 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 46 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 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Calling Conventions!Hakim&Weatherspoon&CS&3410,&Spring&2011&Computer)Science)Cornell)University)See)P&H)2.8)and)2.12))2)Announcements!PA2)due)next)Friday))• PA2)builds)f ro m)PA1)• Work)with)same)partner)• Due)right)before)spring)break)Use)your)resou rces )• FAQ,)class)notes,)book,)SecHons,)office)hours,)newsgroup,)CSUGLab))3)Announcements!Prelims1:)this)Thursday,)March)10th)in)class)• We)will)start)at)1:25pm)sharp,)so)come)early)• Closed)Book.)• Cannot)use)electronic)device)or)outside)material)• PracHce)prelims)are)online)in)CMS)• Material)covered)• Appendix)C)(logic,)gates,)FSMs,)memory,)ALUs)))• Chapter)4)(pipelined)[and)nonXpipeline])MIPS)processor)with)hazards))• Chapters)2)and)Appendix)B)(RISC/CISC,)MIPS,)and)calling)convenHons))• Chapter)1)(Performance))• HW1,)HW2,)PA1,)PA2)))4)Goals for Today!Last)Hme)• Anatomy)of)an)execuHng)program)• Register)assignment)convenHons,)• FuncHon)arguments,)return)values)• Stack)frame,)Call)stack,)Stack)growth)• Variable)arguments)Today))• More)on)stack)frames)• globals)vs)local)accessible)data)• callee)vs)callrer)saved)registers)FAQ)5)Example program!vector'v'='malloc(8);'v0>x'='prompt(“enter'x”);'v0>y'='prompt(“enter'y”);'int'c'='pi'+'tnorm(v);'print(“result”,'c);'calc.c)int'tnorm(vector'v)'{''return'abs(v0>x)+abs(v0>y);'}'math.c)'global'variable:'pi''entry'point:'prompt''entry'point:'print''entry'point:'malloc'lib3410.o)6)Anatomy of an executing program!0xfffffffc'0x00000000'top'bottom'0x7ffffffc'0x80000000'0x10000000'0x00400000'7)math.s!int'abs(x)'{''return'x'<'0'?'–x':'x;'}'int'tnorm(vector'v)'{''return'abs(v0>x)+abs(v0>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);'v0>x'='prompt(“enter'x”);'v0>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!Calling)ConvenHons)• where)to)put)funcHon)arguments)• where)to)put)return)value)• who)saves)and)restores)registers,)and)how)• stack)discipline)Why?)• Enable)code)reXuse)(e.g.)funcHons,)libraries))• Reduce)chance)for)mistakes))Warning:)There)is)no)one)true)MIPS)calling)convenHon.)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) $zero) zero)r1) $at) assembler)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) $k0) reserved)for)OS)kernel)r27) $k1)r28)r29)r30)r31) $ra) return)address)$v0)funcHon)return)values)$v1)$a0)funcHon)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)ac.va.on+records))(aka)stack+frames))One)for)each)funcHon)invocaHon:)• saved)return)address)• local)variables)• …)and)more)SimplificaHon:)• frame)size)&)layout)decided)at)compile)Hme)for)each)funcHon)))14)Stack Growth!ConvenHon:)• r29)is)$sp)(boqom)elt)of)call)stack)))Stack)grows)down&Heap)grows)up)0x00000000'0x80000000'0x10000000'0x00400000'0xfffffffc'system)reserved)system)reserved)code)(text))stack&staHc)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,)X12)#)$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!ConvenHons)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)subXfuncHons))– contains)local)vars)(possibly)clobbered)by)subXfuncHons))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);)ConvenHons:)• align)everything)to)mulHples)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,)X8)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!prinu(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)prinu:)...)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)IniHally)confusing)but)ulHmately)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)funcHons)• Store)a0Xa3)in)the)slots)allocated)in)parent’s)frame)• Refer)to)all)arguments)through)the)stack)21)Recap!ConvenHons)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)subXfuncHons))–


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?