5brnePC2rjmpisOnecpir162IfjmpwasusedinthisexamplethebrancheswouldskiponemorewordasinbrnePC3brnePC2rjmpisTwoAgainitispossiblethatthecapabilitiesofijmparenotenoughSomeAVRshavemorethan128Kbyt ID: 480179
Download Pdf The PPT/PDF document "2Inthisexample,ajmpinstructionisplacedat..." 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.
2Inthisexample,ajmpinstructionisplacedattheresetvector(0x00),anditisusedtojumptotheapplication'smainroutine,whichislabeledmain.Ithasbeenplacedataddress0x900usingthe.orgdirective..orgitselfisnotajumpinstruction,butandassemblerdirective.Ittellstheassem-blertoplacethefollowingexpressionsataspecicaddress.Thejumpdestinationisstoredasanabsoluteaddress:whentheaboveexampleisassembled,thevalue0x900isstoredtogetherwiththeopcodein2words(4bytes)ofprogrammemory.Thisisoneofthedisadvantagesofjmp.Theotheroneisthatitneeds3CPUcyclesforexecution.Therearetworeasonstoprovideajumpinstructionwithsomewhatlimitedcapabilitiescomparedwithjmp:normalAVRsdon'thavethatmuchmemory,andmostjumpsdesti-nationsarewithinacloseregionaroundtheaddresswherethejumpisstored.Therelativejumptakesadvantageofthissituation.RelativeJumpThedestinationaddressofrjmpisnotstoredasanabsolutevalue,butasthedifferencebetweenthejumpdestinationandthestoragelocationfollowingthejump.Thisisbestexplainedwithafewexamples.Inthecodesnippetaccompanyingtheexplanationofjmp(seeabove),rjmpisusedtoconstructanendlessloop:....org0x900main:rjmpmain//sameasrjmp-1Theinstructionfollowingtherjmpwouldbeataddress0x901.Thejumpdestinationis0x900,sotherelativejumplengthis0x9000x901=1.Again:repeatingthesameinstructionoverandoverisstoredasajumpbackwards.Consequently,arelativejumpofzerowordsasinrjmp0orhere:rjmpthere//sameasrjmp0!there:...issimilartoanop,butneedstwoCPUcyclesforexecution.Thisisonecyclelessthatjmpneeds.Also,thejumplengthforrjmpislimitedto2Kwords,whichmeansthatitcanbestoredinjustonewordofprogrammemory.IndeviceswithnotmoreHowtochoosebetweenjmpandrjmpthan4Kwords(8Kbytes),rjmpcanaddresstheentirememoryregion,becausearelativejumpcanwraparoundtheendof 5brnePC+2rjmpisOnecpir16,2Ifjmpwasusedinthisexample,thebrancheswouldskiponemoreword,asinbrnePC+3.brnePC+2rjmpisTwo...Again,itispossiblethatthecapabilitiesofijmparenotenough.SomeAVRshavemorethan128Kbytesofprogrammemory,soijmpcanonlyreachthelowerpartofprogrammemoryinthosedevices.Inthosedevices,itcanbeextended.ExtendedIndirectJumpTheextendedindirectjumpworksjustlikeijmp,buttheZregistereijmpisonlyrelevantfordeviceswithmorethan128Kbytesofprogrammem-ory,liketheATmega1280.pairisextendedbytheEINDregisterintheI/Ospace,whichactsasathirdbytetoZLandZH:...clrr0ldiZL,low(jumpTable)ldiZH,high(jumpTable)ldir16,byte3(jumpTable)//now:Z-jumpTableaddZL,r17//r17=offsetinjumpTableadcZH,r0adcr16,r0//thirdbyteforthecalculationoutEIND,r16eijmp....org0x10000//can'tgetherewithijmp!jumpTable:rjmpisZerorjmpisOnerjmpisTwoAgain,theoffsetusedforaddressingaroutineinthejumptableisstoredinr17.r0isusedasazeroregister,andr17asathirdbyteinthecalculationofthedestinationaddress.CallInstructionsCallinstructionsleaveareturnaddressonthestack(thestackwillbeexplainedinthenextsection)andjumptothecalldestination.Thejumppartofthisworksexactlylikethejumpsexplainedintheprevioussection,andthereareindeedthevariantscall(callsubroutine),rcall(relativecallsubroutine),icall(indirectcalltoZ)andeicall(extendedindirectcalltoZ) 8ldir16,0xAArcallsetLEDs...setLEDs:comr16outPORTB,r16retOneconventionhereisthatthefunction'sargumentisstoredinHavinggoodcallingconventionscanmakeyourcodefaster.Havingbadcall-ingconventionscanslowitdown.Hav-ingnocallingconventionsatallusuallybreaksyourcodeandyourplansfortheweekend.r16.Additionally,ithasnoreturnvalue,anddoesnotpreservethevaluestoredinr16.Thisispartofthecallingconventions,andthecallingcodemustsaver16(onthestack,forexample)ifthevalueisneededagainaftertheroutinehasreturned.Attheveryleast,documenttheminsomekindoffunctionheader:...//********************************************//setLEDs//arguments:LEDstoswitchonaresetinr16//returns:nothing//changes:r16//(LEDsarebetweenPORTBandVCC)//********************************************setLEDs:comr16outPORTB,r16retAdditionalargumentsandreturnvaluescanbestoredinmoreregisters,oronthestack.Ifyouwanttousethestackforargu-ments,bewarned:itrequiressometrickery.Ifyoureallyneedtousethestackforfunctionarguments,thinkaboutredesigningyourAPI.That'swhythisusageofthestackisnotcoveredbythistutorial.TheStackThestackisusedforstoringreturnaddresses,registersthatneedtobepreservedwhilesomecodeischangingthem,andifnecessaryfunctionarguments.ThestackispartoftheAVR'sSRAM.GeneralFunctionalityTherearetwoimportantpartsofthestack:TheStackPointer(SP)andthestackspace,whichispartoftheSRAM.ThismeansthattheSRAMissharedbetweenyour.datasectionsandthestack.IfalltheavailableSRAMisusedby.datasections,the 9stackcannotbeused.Ifitisusednonetheless,itwouldcorruptyourdata.Thisisknownasastackcollision.InAVRs,thestackgrowsdownwards.Thismeansthathighermemoryaddressesareusedrst,andthestackpointerpointsatalocationbelowthoselocationsthatmakeupthestackspace.Thisisillustratedintheguretotheright. SRAMAddress contents ...X data[0] ... X(n1) data[n1] SP!Xn unused ...Figure1:Stackspaceandstackpointer(SP):ndatabytesarestoredonthestack,startingatSRAMaddressX,andthestackpointerpointsatthehighestun-usedaddress.Thetwobasicstackoperationsarepushandpop.Whendataispushedontothestack,itisstoredattheaddressthestackpointeriscurrentlypointingat.Thestackpointerisdecrementedafterwards.Thisisexactlyhowthestackgrowsdownwards.NotitshouldalsobeclearwhythestackpointerisusuallyinitialisedwithRAMEND:itthengrowsdownwardsfromthehighestavailablememorylocation.Whendataispoppedfromthestack,thestackpointerisrstincremented,andthenthedataisstoredinthepopopera-tion'sdestinationregister.Hereisanexampleshowingbothoperations:ldir16,low(0x200)outSPL,r16ldir16,high(0x200)outSPH,r16pushr16//r16isstoredat0x200,SPisnow0x1FFldir16,0xFF//ormorecode...popr16//overwritesr16,whichwas0xFF//SPisnow0x200again...Thisishowregisterscanbepreservedwhileotherroutinesarechangingthem.Interruptserviceroutines(ISRs)makeuseofthistechniquetopreservethecontentsofSREG:pushr16Youcanalsoreserveoneregisterforpre-servingtheSREG.ThiswillspeedupmostISRs,butyouhaveoneregisterlessinyourmaincode.inr16,SREGpushr16...//actualISRcodepopr16outSREG,r16popr16retiretiisnotatypo.Itreturnsfromasubroutine,andsetstheglobalinterruptenablebitinSREG.ItisusuallyonlyusedinISRs.ReturnAddressesTothestack,returnaddressesarejustlikeanyotherkindofdata.Thereturnaddressispushedontothestackwhenasubroutineiscalled,andthestackpointerissettopointatthehighestfreelocationinSRAM.Anormalsubroutinecall