MemoryUsageAsoftwareconventionstacksegment149holdsprocesslocalstoragemanagedinLIFOfashion149automaticallyallocatedbytheoperatingsystem149stackgrowsdowntowardloweraddressesasdataisputontoi ID: 837189
Download Pdf The PPT/PDF document "Whyweusesubroutines149moremodularprogram..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
1 Whyweusesubroutinesmoremodularprog
Whyweusesubroutinesmoremodularprogram(smallroutines,outsidedatapassedin)morereadable,easiertodebugcodereusei.e.smallercodespace MemoryUsageAsoftwareconventionstacksegment:holdsprocess-localstoragemanagedinLIFOfashionautomaticallyallocatedbytheoperatingsystemstackgrowsdown(towardloweraddresses)asdataisputontoitstackpointerisautomaticallyloadedtopointtothelastallocatedlocationonthestack 0x00000000 0x04000000 0x10
2 000000 0x7fffffff Reserved staticdata dy
000000 0x7fffffff Reserved staticdata dynamic stack UsingtheStackStackpointer($spor$29)pointstolastallocatedlocationonthestackStackoperations:eachrequires2instructionsonetoadjustthestackpointeronetotransferthedataonto/offthestacksubu$sp,$sp,4#allocateanewwordonthestacksw$8,0($sp)#pushthecontentsof$8lw$8,0($sp)#popthecontentsinto$8addu$sp,$sp,4#adjustthestackpointertheassemblerrecognizesastackpointeroperation&generate
3 smoreefficienttoallocateallthespac
smoreefficienttoallocateallthespaceyoullneedatonceoftenallocate&deallocatethesameamount UsingtheStackExamplesavingvaluesonthestacksubu$sp,$sp,12sw$t1,8($sp)sw$t0,4($sp)sw$s0,0($sp)restoringvaluesfromthestacklw$t1,8($sp)lw$t0,4($sp)lw$s0,0($sp)addu$sp,$sp,12allocateallthestackspaceyouwillneedforagroupofdatathestacklocationsareabovewherethestackpointerispointing,sothedisplacementsfrom$spwillbepositive $sp P
4 rocedureCallingConventionProtocolbetween
rocedureCallingConventionProtocolbetweenthecaller&thecallee:whosaveswhichregistershowparametersarepassed:inregisters?onthestack?wherethereturnaddressislocatedWhyhaveacallingconvention?caller&calleecaninteractcorrectly&efficientlycanhavesubroutinesthatarewrittenindifferentlanguages&compiledwithdifferentcompilerseasierforastandarddebuggerAcontractbetweenthehardware&softwarehardware:performssimpleinstr
5 uctionshasdedicatedregisters
uctionshasdedicatedregisterssoftware:controlsthesequenceofinstructionsforcalling&determineswhichregisterpointstothestacktherulesoftheprotocolEacharchitecturehasitsownprotocol(s) WhyHaveTwoClassesofRegisters?Toreduceregisterspilling(storingregistersinmemorybecauseyouneedtousethemforsomethingelse)Callerknowswhatregistersitneedsaftertheprocedurecall.Calleeknowswhatregistersitisgoingtouse.Thereforedividetheresponsibiliti
6 es.Caller-savedregisters(ts):notpr
es.Caller-savedregisters(ts):notpreservedacrossprocedurecallscallersavesthemifitwantstousethemaftertheprocedureifitdoesntneedthemlater,itdoesnthavetosavethemsoacallerusesthemforshort-livedvaluescalleeknowsitcanusetheseregisterswithoutsavingthemsavings:caller-savedregistersonlyspilledifthecallerusesthemaftertheprocedurecallCallee-savedregisters(ss):preservedacrossprocedurecallscallerdoesn
7 tsavethemeveniftheyareusedafterthe
tsavethemeveniftheyareusedaftertheacallerusesthemforlong-livedvaluesifthecalleeneedstousethem,itmustsavethemfirstsavingscallee-savedregistersonlyspilledifthecalleeusesthem MIPSProcedureCallingConventionsavesthearegistersonthestacksavesthetregistersonthestack:$t0-$t9passestheargumentsin$a0-$a3savesadditionalpassedvaluesonthestackexecuteswhichputsthereturnPCin$radeterminesthesi
8 zeofthestackframe&changes$sptopointtothe
zeofthestackframe&changes$sptopointtotheendoftheframestackframe:aregionofthestackthatholdsallthedataforasingleprocedure(alsocalledprocedurecallframe,activationrecord)subu$sp,$sp,frameSizeputsitsreturnaddress&framepointer($fp)onthestacksavesthesregistersonthestack:$s0-$s7sets$fpto$sp+stackframesize-4putsitslocalvariablesonthestackifthecalleecallsaprocedure,itbecomesacallerNeithermodifiestheothersport
9 ionofthestack StackFrameframepointer($fp
ionofthestack StackFrameframepointer($fpor$30):pointstothefirstwordofastackframevalueneverchanges:stableoffsetbaseforlocationswithinthestackframenotalwaysused ExampleCallSequenceAssume:argumentsarein$t0&$t1wanttosavecaller-savedregisters$t6&$t7move$a0,$t0#firstargumentmove$a1,$t3#secondargumentsubu$sp,$sp,8#adjustthestackpointersw$t6,4($sp)#save$t6onthestacksw$t7,0($sp)#save$t7onthestackjaltargetThefirstthingthecalle
10 ewilldo(assumingitdoesnothavetosaveany$s
ewilldo(assumingitdoesnothavetosaveany$sregisters)subu$sp,$sp,4#adjustthestackpointersw$ra,0($sp)#savethereturnaddress MIPSProcedureReturnConventionReturnconvention:putthereturnvaluesin$v0,$v1restorethes(caller-saved)registersrestores$fp&$rapoptheactivationrecordbyaddingitssizeto$spreturntothecallerbyjr$ra ExampleReturnSequenceBeforereturning,thecalleewillputresultsin$v0&$v1ifneeded&lw$ra,0($sp)#loadthere
11 turnaddressinto$raaddu$sp,$sp,4#adjustth
turnaddressinto$raaddu$sp,$sp,4#adjustthestackpointerjr$ra#returnThecallerwillrestore$t6&$t7andadjustthestacklw$t6,4($sp)#restore$t6lw$t7,0($sp)#restore$t7addu$sp,$sp,8#adjustthestackpointer UseofRegisters&theStackRegistersareusedfor:passingasmallnumberofarguments(upto4,$a0-$a3)passingthereturnaddresstothecallee($ra)locatingthebeginningofthestackframe($fp)keepingtrackofthetopofthestack($sp)returningfunctionvalues($v0
12 ,$v1)Stackisusedfor:passingparamet
,$v1)Stackisusedfor:passingparametersifmorethan4savingthecallersregisterparameters(as)(mightbeusedbythecallee)savingthecallersregistersthataregoingtobeusedbythecalleraftertheprocedurereturns($ts)localdataforthecalleethereturnaddressofthecaller(incasethecalleecallsaprocedure)theframepointer(samereason)anycallee-savedregistersthecalleeisgoingtouse($ss)stackframenota