資源描述:
《unique nature of assembly language optimization (ebook)外語(yǔ)英文電子書(shū)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、chapter2aworldaparttheuniquenatureofassemblylanguegeoptimization9.1:i“n.:I;e!:”JNatureofAssemblyLanguageOptimizationf‘AsIshowedinthd:previouschapter,optimizationisbynomeansalwaysamatterof“droppingintoasseInfact,inperformancetuninghigh-levellanguagecode,assemblyshouldbeusdthenonlyafteryou’vemadesur
2、eabadlychosenorclumsilyimplemenmisn’teatingyoualive.Certainlyifyouuseassem-blyatall,makeabsoldtelysureyouuseitright.Thepotentialofassemblycodetorunslowlyispoorlyunddstoodbyalotofpeople,butthatpotentialisgreat,especiallyination,however,happensonlyattheassemblylevel,andithappensamicsthatistotallydif
3、ferentfromthatgoverningC/C++bespeakingofassembly-leveloptimizationtimeandagain0,Ithinkitwillbehelpfulifyouhaveagraspofthoseassemblyspecificdynamics.Asusual,thebestwaytowadeinistopresentareal-worldexample.Instructions:TheIndividualversustheCollectiveSometimeago,Iwasaskedtoworkoveracriticalassemblys
4、ubroutineinordertomakeitrunasfastaspossible.Thetaskofthesubroutinewastoconstructanibbleoutoffourbitsreadfromdifferentbytes,rotatingandcombiningthebitssothattheyultimatelyendedupneatlyalignedinbits3-0ofasinglebyte.(Incaseyou’recurious,theobjectwastoconstructa16-colorpixelfrombitsscatteredover4bytes
5、.)23Iexaminedthesubroutinelinebyline,savingacyclehereandacyclethere,untilthecodetrulyseemedtobeoptimized.WhenIwasdone,thekeypartofthecodelookedsomethinglikethis:LoopTop:lodsb;getthenextbytetoextractabitfromanda1,ah;isolatethebitwewantrola1.cl;rotatethebitintothedesiredpositionorbl.a1:insertthebiti
6、ntothefinalnibbledeccx;thenextbitgoes1placetotherightdecdx;countdownthenumberofbitsjnzLoopTop:processthenextbit,ifanyNow,it’shardtowritecodethat’smuchfasterthanseveninstructions,onlyoneofwhichaccessesmemory,andmostprogrammerswouldhavecalleditadayatthispoint.Still,somethingbotheredme,soIspentabitof
7、timegoingoverthecodeagain.Suddenly,theanswerstruckme-thecodewasrotatingeachbitintoplaceseparately,sothatamultibitrotationwasbeingperformedeverytimethroughtheloop,foratotaloffourseparatetime-consumingmultibitrotat