/
Whyweusesubroutines149moremodularprogramsmallroutinesoutsidedatap Whyweusesubroutines149moremodularprogramsmallroutinesoutsidedatap

Whyweusesubroutines149moremodularprogramsmallroutinesoutsidedatap - PDF document

jalin
jalin . @jalin
Follow
343 views
Uploaded On 2021-06-07

Whyweusesubroutines149moremodularprogramsmallroutinesoutsidedatap - PPT Presentation

MemoryUsageAsoftwareconventionstacksegment149holdsprocesslocalstoragemanagedinLIFOfashion149automaticallyallocatedbytheoperatingsystem149stackgrowsdowntowardloweraddressesasdataisputontoi ID: 837189

146 149 147 148 149 146 148 147 caller savedregisters savesthe registersonthestack save adjustthestackpointersw notalwaysused onthestack adjustthestackpointer software subu

Share:

Link:

Embed:

Download Presentation from below link

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.


Presentation Transcript

1 Whyweusesubroutines•moremodularprog
Whyweusesubroutines•moremodularprogram(smallroutines,outsidedatapassedin)•morereadable,easiertodebug•codereusei.e.smallercodespace MemoryUsageAsoftwareconventionstacksegment:•holdsprocess-localstoragemanagedinLIFOfashion•automaticallyallocatedbytheoperatingsystem•stackgrowsdown(towardloweraddresses)asdataisputontoitstackpointerisautomaticallyloadedtopointtothelastallocatedlocationonthestack 0x00000000 0x04000000 0x10

2 000000 0x7fffffff Reserved staticdata dy
000000 0x7fffffff Reserved staticdata dynamic stack UsingtheStackStackpointer($spor$29)•pointstolastallocatedlocationonthestackStackoperations:•eachrequires2instructions•onetoadjustthestackpointer•onetotransferthedataonto/offthestacksubu$sp,$sp,4#allocateanewwordonthestacksw$8,0($sp)#pushthecontentsof$8lw$8,0($sp)#popthecontentsinto$8addu$sp,$sp,4#adjustthestackpointer•theassemblerrecognizesastackpointeroperation&generate

3 s•moreefficienttoallocateallthespac
s•moreefficienttoallocateallthespaceyou’llneedatonce•oftenallocate&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,12•allocateallthestackspaceyouwillneedforagroupofdata•thestacklocationsare“above”wherethestackpointerispointing,sothedisplacementsfrom$spwillbepositive $sp P

4 rocedureCallingConventionProtocolbetween
rocedureCallingConventionProtocolbetweenthecaller&thecallee:•whosaveswhichregisters•howparametersarepassed:inregisters?onthestack?•wherethereturnaddressislocatedWhyhaveacallingconvention?•caller&calleecaninteractcorrectly&efficiently•canhavesubroutinesthatarewrittenindifferentlanguages&compiledwithdifferentcompilers•easierforastandarddebuggerAcontractbetweenthehardware&software•hardware:•performssimpleinstr

5 uctions•hasdedicatedregisters•
uctions•hasdedicatedregisters•software:•controlsthesequenceofinstructionsforcalling&•determineswhichregisterpointstothestack•therulesoftheprotocolEacharchitecturehasitsownprotocol(s) WhyHaveTwoClassesofRegisters?Toreduceregisterspilling(storingregistersinmemorybecauseyouneedtousethemforsomethingelse)Callerknowswhatregistersitneedsaftertheprocedurecall.Calleeknowswhatregistersitisgoingtouse.Thereforedividetheresponsibiliti

6 es.Caller-savedregisters(t’s):notpr
es.Caller-savedregisters(t’s):notpreservedacrossprocedurecalls•callersavesthemifitwantstousethemaftertheprocedure•ifitdoesn’tneedthemlater,itdoesn’thavetosavethem•soacallerusesthemforshort-livedvalues•calleeknowsitcanusetheseregisterswithoutsavingthemsavings:caller-savedregistersonlyspilledifthecallerusesthemaftertheprocedurecallCallee-savedregisters(s’s):preservedacrossprocedurecalls•callerdoesn’

7 tsavethemeveniftheyareusedafterthe•
tsavethemeveniftheyareusedafterthe•acallerusesthemforlong-livedvalues•ifthecalleeneedstousethem,itmustsavethemfirstsavingscallee-savedregistersonlyspilledifthecalleeusesthem MIPSProcedureCallingConvention•savesthe“a”registersonthestack•savesthe“t”registersonthestack:$t0-$t9•passestheargumentsin$a0-$a3•savesadditionalpassedvaluesonthestack•executeswhichputsthereturnPCin$ra•determinesthesi

8 zeofthestackframe&changes$sptopointtothe
zeofthestackframe&changes$sptopointtotheendoftheframestackframe:aregionofthestackthatholdsallthedataforasingleprocedure(alsocalledprocedurecallframe,activationrecord)subu$sp,$sp,frameSize•putsitsreturnaddress&framepointer($fp)onthestack•savesthe“s”registersonthestack:$s0-$s7•sets$fpto$sp+stackframesize-4•putsitslocalvariablesonthestack•ifthecalleecallsaprocedure,itbecomesacallerNeithermodifiestheother’sport

9 ionofthestack StackFrameframepointer($fp
ionofthestack StackFrameframepointer($fpor$30):•pointstothefirstwordofastackframe•valueneverchanges:stableoffsetbaseforlocationswithinthestackframe•notalwaysused ExampleCallSequenceAssume:•argumentsarein$t0&$t1•wanttosavecaller-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,$v1•restorethe“s”(caller-saved)registers•restores$fp&$ra•poptheactivationrecordbyaddingitssizeto$sp•returntothecallerbyjr$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:•passingparametersifmorethan4•savingthecaller’sregisterparameters(a’s)(mightbeusedbythecallee)•savingthecaller’sregistersthataregoingtobeusedbythecalleraftertheprocedurereturns($t’s)•localdataforthecallee•thereturnaddressofthecaller(incasethecalleecallsaprocedure)•theframepointer(samereason)•anycallee-savedregistersthecalleeisgoingtouse($s’s)•stackframe•nota

Related Contents


Next Show more