资源描述:
《unique nature of assembly language optimization (ebook)外语英文电子书》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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