Tải bản đầy đủ (.pdf) (728 trang)

No starch write great code understanding the machine volume 1 oct 2004 ISBN 1593270038

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.8 MB, 728 trang )

WriteGreatCode:Understandingthe
Machine,VolumeI
ISBN:1593270038
byRandallHyde
NoStarchPress©2004(410pages)
Thisfirstvolumeteachesimportantconcepts
ofmachineorganizationinalanguageindependentfashion,givingprogrammerswhat
theyneedtoknowtowritegreatcodeinany
language,withouttheusualoverheadof
learningassemblylanguage.
TableofContents
WriteGreatCode—UnderstandingtheMachine,
VolumeI
Chapter1 - WhatyouNeedtoKnowtoWriteGreat
Code
Chapter2 - NumericRepresentation
Chapter3 - BinaryArithmeticandBitOperations
Chapter4 - Floating-PointRepresentation
Chapter5 - CharacterRepresentation
Chapter6 - MemoryOrganizationandAccess
Chapter7 - CompositeDataTypesandMemory
Objects
Chapter8 - BooleanLogicandDigitalDesign
Chapter9 - CPUArchitecture
Chapter10 - InstructionSetArchitecture
Chapter11 - MemoryArchitectureandOrganization
Chapter12 - InputandOutput(I/O)


AppendixA - ASCIICharacterSet
Index


ListofFigures
ListofTables


BackCover
This,thefirstvolumeinRandallHyde’sWriteGreat
Codeseries,divesintomachineorganizationwithout
theextraoverheadoflearningassemblylanguage
programming.WrittenforC/C++,VB,Pascal,Java,
andotherhigh-levellanguageprogrammers,VolumeI,
“UnderstandingtheMachine,”fillsinthelow-level
detailsofmachineorganizationthatareoftenleftout
ofcomputerscienceandengineeringcourses.Learn:
Howthemachinerepresentsnumbers,strings,
andhigh-leveldatastructures,soyou’llknowthe
inherentcostofusingthem.
Howtoorganizeyourdata,sothemachinecan
accessitefficiently.
HowtheCPUoperates,soyoucanwritecodethat
worksthewaythemachinedoes.
HowI/Odevicesoperate,soyoucanmaximize
yourapplication’sperformancewhenaccessing
thosedevices.
Howtobestusethememoryhierarchytoproduce
thefastestpossibleprograms.
Greatcodeisefficientcode.Butbeforeyoucanwrite
trulyefficientcode,youmustunderstandhow
computersystemsexecuteprogramsandhow
abstractionsinprogramminglanguagesmaptothe
machine’slow-levelhardware.Afterall,compilersdon’t

writethebestmachinecode;programmersdo.The


informationinthisfirstvolumeoftheWriteGreatCode
seriesgivesyouthefoundationuponwhichallgreat
softwareisbuilt.
AbouttheAuthor
RandallHydeistheauthorofTheArtofAssembly
Language(NoStarchPress),oneofthemosthighly
recommendedresourcesonassembly.Heisalsothe
co-authorofTheWaiteGroup’sMASM6.0Bible.He
haswrittenforDr.Dobb’sJournal,Byte,aswellas
professionaljournals.


WriteGreatCode-UnderstandingtheMachine,
VolumeI
RandallHyde

NOSTARCHPRESS
SanFrancisco
Copyright©2004RandallHyde.
Allrightsreserved.Nopartofthisworkmaybereproducedortransmitted
inanyformorbyanymeans,electronicormechanical,including
photocopying,recording,orbyanyinformationstorageorretrieval
system,withoutthepriorwrittenpermissionofthecopyrightownerand
thepublisher.
12345678910-07060504
NoStarchPressandtheNoStarchPresslogoareregisteredtrademarks
ofNoStarchPress,Inc.Otherproductandcompanynamesmentioned

hereinmaybethetrademarksoftheirrespectiveowners.Ratherthan
useatrademarksymbolwitheveryoccurrenceofatrademarkedname,
weareusingthenamesonlyinaneditorialfashionandtothebenefitof
thetrademarkowner,withnointentionofinfringementofthetrademark.
Publisher:WilliamPollock
ManagingEditor:KarolJurado
CoverandInteriorDesign:OctopodStudios
DevelopmentalEditor:HillelHeinstein
TechnicalReviewer:MarkdeWever
Copyeditor:AndyCarroll


Compositor:RileyHoffman
Proofreader:StephanieProvines
Forinformationonbookdistributorsortranslations,pleasecontactNo
StarchPress,Inc.directly:
NoStarchPress,Inc.
555DeHaroStreet,Suite250,SanFrancisco,CA94107
phone:415-863-9900;fax:415-863-9950;;

Theinformationinthisbookisdistributedonan'AsIs'basis,without
warranty.Whileeveryprecautionhasbeentakeninthepreparationof
thiswork,neithertheauthornorNoStarchPress,Inc.shallhaveany
liabilitytoanypersonorentitywithrespecttoanylossordamagecaused
orallegedtobecauseddirectlyorindirectlybytheinformationcontained
init.
LibraryofCongressCataloguing-in-PublicationData
Hyde,Randall.
Writegreatcode:understandingthemachine/RandallHyde.
p.cm.

ISBN:1593270038
1.Computerprogramming.2.Computerarchitecture.I.Title.
QA76.6.H942004
005.1--dc22
2003017502
Acknowledgments
Abooksuchastheoneyouarenowholdingisrarelytheworkofone
person,evenifonlyonenameappearsonthecover.Producingthisbook
hasbeenateameffort,andIwouldliketotakethisopportunityto
acknowledgetheotherindividualswhohavecontributedgreatlytoits
quality.


MaryPhilips,awonderfulfriendwhohelpedproofreadseveralofthe
earlierchapters.
BillPollock,whoreadandofferedsuggestionsforChapters1
through6.
KarolJurado,myeditor,whoshepherdedthisprojectfrom
conceptiontoproduction.
HillelHeinstein,thedevelopmentaleditor,whokeptthisbookonthe
righttrackandhelpedcleanupthewriting.
AndyCarroll,thecopyeditor,whoalsohelpedimprovemywriting.
MarkdeWever,thetechnicalreviewer,whocaughtalargenumber
oflittletyposandtechnicalproblemstohelpensuretheaccuracyof
thematerial.
RileyHoffman,whohandledthepagelayoutchoresandhelped
ensurethatthebook(includingthelistings)wasreadable.
StephanieProvines,whoseproofreadingcaughtseveral
typographicalandlayouterrors.
LeighSacks,whohasdoneagreatjobofmarketingthisbookand

myearlierbook,TheArtofAssemblyLanguage.
Andofcourse,allthegreatpeopleatNoStarchPresswho'vebeen
supportiveofthisprojectfromtheverybeginning.
Last,butnotleast,Iwouldliketothankmywife,Mandy,whoallowed
metogetawaywithnotspendingasmuchtimeworkingaroundthe
houseasIshouldhave,sothatIcouldgetthisbookoutthedoor.
Thankstoallofyou,
RandallHyde
ThinkingLow-Level,WritingHigh-Level


Thegoalofthisvolume,UnderstandingtheMachine,wastogetyou
thinkingatthelevelofthemachine.Ofcourse,onewaytoforceyourself
towritecodeatthemachinelevelistowriteyourapplicationsin
assemblylanguage.Whenyou'vegottowritecodestatementby
statementinassemblylanguage,you'regoingtohaveaprettygoodidea
ofthecostassociatedwitheverystatement.
Unfortunately,usingassemblylanguageisn'tarealisticsolutionformost
applications.Thedisadvantagesofassemblylanguagehavebeenwell
publicized(andblownoutofproportion)overthepastseveraldecades,
somostpeoplearequitefamiliarwiththedrawbacks,realorimagined.
Assemblyjustisn'tanoptionformostpeople.
Thoughwritingcodeinassemblylanguageforcesyoutothinkdownat
themachinelevel,writingcodeinahigh-levellanguagedoesnotforce
youtothinkatahighlevelofabstraction.Thereisnothingpreventingyou
fromthinkinginlow-leveltermswhilewritinghigh-levelcode.Thegoalof
thisbookwastoprovideyouwiththebackgroundknowledgeyouneedto
doexactlythat-thinkinlow-leveltermswhilewritinghigh-levelcode.By
learninghowthecomputerrepresentsdata,you'velearnedhowhighlevellanguagedatatypestranslatetothemachinelevel.Bylearninghow
theCPUexecutesmachineinstructions,you'velearnedthecostsof

variousoperationsinyourhigh-levellanguageapplications.Bylearning
aboutmemoryperformance,you'velearnedhowtoorganizeyourhighlevellanguagevariablesandotherdatatomaximizecacheandmemory
access.There'sonlyonepiecemissingfromthispuzzle:'Exactlyhow
doesaparticularcompilermaphigh-levellanguagestatementstothe
machinelevel?'Unfortunately,thattopicissufficientlylargethatit
deservesanentirebookonitsown.Andthat'sthepurposeofthenext
volumeintheWriteGreatCodeseries:ThinkingLow-Level,WritingHighLevel.
WriteGreatCode:ThinkingLow-Level,WritingHigh-Levelwillpickup
rightwherethisvolumeleavesoff.ThinkingLow-Level,WritingHighLevelwillteachyouhoweachstatementinatypicalhigh-levellanguage
mapstomachinecode,howyoucanchoosebetweentwoormorehighlevelsequencestoproducethebestpossiblemachinecode,andhowto
analyzethatmachinecodetodetermineitsqualityandthequalityofthe


high-levelcodethatproducedit.Andwhiledoingallofthis,itwillgiveyou
agreaterappreciationofhowcompilersdotheirjobandhowyoucan
assistthemindoingtheirjobbetter.
Congratulationsonyourprogressthusfartowardsknowinghowtowrite
greatcode.Seeyouinvolume2.


Chapter1:WhatyouNeedtoKnowtoWrite
GreatCode
WriteGreatCodewillteachyouhowtowritecodeyoucanbeproudof,
codethatwillimpressotherprogrammers,codethatwillsatisfy
customersandprovepopularwithusers,andcodethatpeople
(customers,yourboss,andsoon)won'tmindpayingtopdollartoobtain.
Ingeneral,thevolumesintheWriteGreatCodeserieswilldiscusshow
towritesoftwarethatachieveslegendarystatus,elicitingtheaweofother
programmers.



1.1TheWriteGreatCodeSeries
WriteGreatCode:UnderstandingtheMachineisthefirstoffourvolumes
intheWriteGreatCodeseries.Writinggreatcoderequiresacombination
ofknowledge,experience,andskillthatprogrammersusuallyobtainonly
afteryearsofmistakesanddiscoveries.Thepurposeofthisseriesisto
sharewithbothnewandexperiencedprogrammersafewdecade'sworth
ofobservationsandexperience.Ihopethatthesebookswillhelpshorten
thetimeandreducethefrustrationthatittakestolearnthings'thehard
way.'
Thisfirstvolume,UnderstandingtheMachine,isintendedtofillinthe
low-leveldetailsthatareoftenskimmedoverinatypicalcomputer
scienceorengineeringcurriculum.Theinformationinthisvolumeisthe
foundationuponwhichgreatsoftwareisbuilt.Youcannotwriteefficient
codewithoutthisinformation,andthesolutionstomanyproblemsrequire
athoroughgroundinginthissubject.ThoughI'mattemptingtokeepeach
volumeasindependentaspossibleoftheothers,Understandingthe
Machinemightbeconsideredaprerequisiteforallthefollowingvolumes.
Thesecondvolume,ThinkingLow-Level,WritingHigh-Level,will
immediatelyapplytheknowledgegainedinthisfirstvolume.Thinking
Low-Level,WritingHigh-Levelwillteachyouhowtoanalyzecodewritten
inahigh-levellanguagetodeterminethequalityofthemachinecodethat
acompilerwouldgenerateforthatcode.Armedwiththisknowledge,you
willbeabletowritehigh-levellanguageprogramsthatarenearlyas
efficientasprogramshandwritteninassemblylanguage.High-level
languageprogrammersoftengetthemistakenimpressionthatoptimizing
compilerswillalwaysgeneratethebestmachinecodepossible,
regardlessofthesourcecodetheprogrammergivesthem.Thissimply
isn'ttrue.Thestatementsanddatastructuresyouchooseinyoursource
filescanhaveabigimpactontheefficiencyofthemachinecodea

compilergenerates.Byteachingyouhowtoanalyzethemachinecode
yourcompilergenerates,ThinkingLow-Level,WritingHigh-Levelwill
teachyouhowtowriteefficientcodewithoutresortingtoassembly
language.


Therearemanyotherattributesofgreatcodebesidesefficiency,andthe
thirdvolumeinthisseries,EngineeringSoftware,willcoversomeof
those.EngineeringSoftwarewilldiscusshowtocreatesourcecodethat
iseasilyreadandmaintainedbyotherindividualsandhowtoimprove
yourproductivitywithoutburdeningyouwiththe'busywork'thatmany
softwareengineeringbooksdiscuss.EngineeringSoftwarewillteachyou
howtowritecodethatotherprogrammerswillbehappytoworkwith,
ratherthancodethatcausesthemtousesomechoicewordsaboutyour
capabilitiesbehindyourback.
Greatcodeworks.Therefore,Iwouldberemissnottoincludeavolume
ontesting,debugging,andqualityassurance.Whetheryouviewsoftware
testingwithfearorwithdisgust,oryoufeelit'ssomethingthatonlyjunior
engineersshouldgetstuckdoing,analmostuniversaltruthisthatfew
programmersproperlytesttheircode.Thisgenerallyisn'tbecause
programmersactuallyfindtestingboringorbeneaththem,butbecause
theysimplydon'tknowhowtotesttheirprograms,eradicatedefects,and
ensurethequalityoftheircode.Asaresult,fewapplicationsreceive
high-qualitytesting,whichhasledtheworldatlargetohaveaverylow
opinionofthesoftwareengineeringprofession.Tohelpovercomethis
problem,thefourthvolumeinthisseries,Testing,Debugging,andQuality
Assurance,willdescribehowtoefficientlytestyourapplicationswithout
allthedrudgeryengineersnormallyassociatewiththistask.



1.2WhatThisVolumeCovers
Inordertowritegreatcode,youneedtoknowhowtowriteefficientcode,
andtowriteefficientcode,youmustunderstandhowcomputersystems
executeprogramsandhowabstractionsfoundinprogramming
languagesmaptothelow-levelhardwarecapabilitiesofthemachine.
Thisfirstvolumeteachesyouthedetailsoftheunderlyingmachineso
you'llknowhowtowritesoftwarethatbestusestheavailablehardware
resources.Whileefficiencyisnottheonlyattributegreatcodepossesses,
inefficientcodeisnevergreat.Soifyou'renotwritingefficientcode,
you'renotwritinggreatcode.
Inthepast,learninggreatcodingtechniqueshasrequiredlearning
assemblylanguage.Whilethisisnotabadapproach,itisoverkill.
Learningassemblylanguageinvolveslearningtworelatedsubjects:(1)
machineorganizationand(2)programminginassemblylanguage.While
learningassemblylanguageprogramminghelps,therealbenefitsof
learningassemblylanguagecomefromlearningmachineorganizationat
thesametime.Fewbookshavetaughtmachineorganizationwithoutalso
teachingassemblylanguageprogramming.Torectifythisproblem,this
bookteachesmachineorganizationindependentlyofassemblylanguage
soyoucanlearntowritegreatcodewithouttheexcessiveoverheadof
learningassemblylanguage.
'Sowhatismachineorganization?'you'reprobablywondering.Well,
machineorganizationisasubsetofcomputerarchitecture,andthisbook
concentratesonthosepartsofcomputerarchitectureandmachine
organizationthatarevisibletotheprogrammerorarehelpfulfor
understandingwhysystemarchitectschoseaparticularsystemdesign.
Thegoaloflearningmachineorganizationisnottoenableyoutodesign
yourownCPUorcomputersystem,buttoteachyouhowtomakethe
mostefficientuseofexistingcomputerdesigns.
'Okay,sowhatismachineorganization?'you'reprobablystillasking.

Well,aquickglanceatthetableofcontentswillgiveyouanideaofwhat
thissubjectisallabout.Let'sdoaquickrun-throughofthebook.
Chapters2,4,and5dealwithbasiccomputerdatarepresentation-how


computersrepresentsignedandunsignedintegervalues,characters,
strings,charactersets,realvalues,fractionalvalues,andothernumeric
andnonnumericquantities.Ifyoudonothaveasolidunderstandingof
howcomputersrepresentthesevariousdatatypesinternally,it'sdifficult
tounderstandwhysomeoperationsthatusethesedatatypesareso
inefficient.Andifyoudon'trealizethey'reinefficient,you'lllikelyusethem
inaninappropriatefashionandtheresultingcodewillnotbegreat.
Chapter3discussesbinaryarithmeticandbitoperationsusedbymost
moderncomputersystems.Becausetheseoperationsaregenerally
availableinprogramminglanguages,Chapter3alsooffersseveral
insightsintohowyoucanwritebettercodebyusingarithmeticandlogical
operationsinwaysnotnormallytaughtinbeginningprogramming
courses.Learningstandard'tricks'suchastheseispartofhowyou
becomeagreatprogrammer.
Chapter6beginsadiscussionofoneofthemoreimportanttopicsinthis
book:memoryorganizationandaccess.Memoryaccessisacommon
performancebottleneckinmoderncomputerapplications.Chapter6
providesanintroductiontomemory,discussinghowthecomputer
accessesitsmemory,anddescribingtheperformancecharacteristicsof
memory.Thischapteralsodescribesvariousmachinecodeaddressing
modesthatCPUsusetoaccessdifferenttypesofdatastructuresin
memory.Inmodernapplications,poorperformanceoftenoccursbecause
theprogrammerdoesnotunderstandtheramificationsofmemoryaccess
intheirprograms,andChapter6addressesmanyoftheseramifications.
Chapter7returnstothediscussionofdatatypesandrepresentationby

coveringcompositedatatypesandmemoryobjects.Unliketheearlier
chapters,Chapter7discusseshigher-leveldatatypeslikepointers,
arrays,records,structures,andunions.Alltoooftenprogrammersuse
largecompositedatastructureswithoutevenconsideringthememory
andperformanceissuesofdoingso.Thelow-leveldescriptionofthese
high-levelcompositedatatypeswillmakecleartheirinherentcosts
enablingyoutousetheminyourprogramssparinglyandwisely.
Chapter8discussesBooleanlogicanddigitaldesign.Thischapter
providesthemathematicalandlogicalbackgroundyou'llneedto


understandthedesignofCPUsandothercomputersystemcomponents.
Althoughthisparticularchapterismorehardwareorientedthanthe
previouschapters,therearestillsomegoodideasthatyoucan
incorporateintoreallygreatcode.Inparticular,thischapterdiscusses
howtooptimizeBooleanexpressions,suchasthosefoundincommon
high-levelprogramminglanguagestatementslikeif,while,andso
on.
ContinuingthehardwarediscussionbeguninChapter8,Chapter9
discussesCPUarchitecture.Althoughthegoalofthisbookisnottoteach
youhowtodesignyourownCPU,abasicunderstandingofCPUdesign
andoperationisabsolutelynecessaryifyouwanttowritegreatcode.By
writingyourcodeinamannerconsistentwiththewayaCPUwillexecute
thatcode,you'llgetmuchbetterperformanceusingfewersystem
resources.BywritingyourapplicationsatoddswiththewayCPUs
executecode,you'llwindupwithslower,resource-hoggingprograms.
Chapter10discussesCPUinstructionsetarchitecture.Machine
instructionsaretheprimitiveunitsofexecutiononanyCPU,andthetime
spentduringprogramexecutionisdirectlydeterminedbythenumberand
typeofmachineinstructionstheCPUexecutes.Understandinghow

computerarchitectsdesignmachineinstructionscanprovidevaluable
insightintowhycertainoperationstakelongertoexecutethanothers.
Onceyouunderstandthelimitationsofmachineinstructionsandhowthe
CPUinterpretsthem,youcanusethisinformationtoturnmediocrecode
sequencesintogreatcodesequences.
Chapter11returnstothesubjectofmemory,coveringmemory
architectureandorganization.Thischapterwillprobablybeoneofthe
mostimportanttotheindividualwantingtowritefastcode.Itdescribes
thememoryhierarchyandhowtomaximizetheuseofcacheandother
fastmemorycomponents.Greatcodeavoidsthrashing,acommon
sourceofperformanceproblemsinmodernapplications.Byreadingthis
chapteryouwilllearnaboutthrashingandhowtoavoidlow-performance
memoryaccessinyourapplications.
Chapter12,'InputandOutput,'describeshowcomputersystems
communicatewiththeoutsideworld.Manyperipheral(input/output)


devicesoperateatmuchlowerspeedsthantheCPUandmemory.You
canwritethefastestexecutingsequenceofinstructionspossible,andstill
haveyourapplicationrunslowlybecauseyoudon'tunderstandthe
limitationsoftheI/Odevicesinyoursystem.Chapter12presentsa
discussionofgenericI/Oports,systembuses,buffering,handshaking,
polling,andinterrupts.Italsodiscusseshowtoeffectivelyusemany
popularPCperipheraldevices,includingkeyboards,parallel(printer)
ports,serialports,diskdrives,tapedrives,flashstorage,SCSI,IDE/ATA,
USB,andsoundcards.Understandingtheimpactofthesedeviceson
yourapplicationscanhelpyouwritegreat,efficientcode.


1.3AssumptionsThisVolumeMakes

Forthepurposesofthisbook,youshouldbereasonablycompetentinat
leastoneimperative(procedural)programminglanguage.Thisincludes
CandC++,Pascal,BASIC,andassembly,aswellaslanguageslikeAda,
Modula-2,FORTRAN,andthelike.Youshouldbecapable,onyourown,
oftakingasmallproblemdescriptionandworkingthroughthedesignand
implementationofasoftwaresolutionforthatproblem.Atypicalsemester
orquartercourseatacollegeoruniversity(orseveralmonths'experience
onyourown)shouldbesufficientbackgroundforthisbook.
Atthesametime,thisbookisnotlanguagespecific;itsconcepts
transcendwhateverprogramminglanguage(s)you'reusing.Tohelp
maketheexamplesmoreaccessibletoreaders,theprogramming
examplesinthisbookwillrotateamongseverallanguages(suchas
C/C++,Pascal,BASIC,andassembly).Furthermore,thisbookdoesnot
assumethatyouuseorknowanyparticularlanguage.Whenpresenting
examples,thisbookexplainsexactlyhowthecodeoperatessothateven
ifyouareunfamiliarwiththespecificprogramminglanguage,youwillbe
abletounderstanditsoperationbyreadingtheaccompanying
description.
Thisbookusesthefollowinglanguagesandcompilersinvarious
examples:
C/C++:GCC,Microsoft'sVisualC++,BorlandC++
Pascal:Borland'sDelphi/Kylix
Assemblylanguage:Microsoft'sMASM,HLA(theHighLevel
Assembler),Gas(onthePowerPC)
BASIC:Microsoft'sVisualBasic
Youcertainlydon'tneedtoknowalltheselanguagesorhaveallthese
compilerstoreadandunderstandtheexamplesinthisbook.Often,the
examplesappearinmultiplelanguages,soit'susuallysafetoignorea
specificexampleifyoudon'tcompletelyunderstandthesyntaxofthe
languagetheexampleuses.



1.4CharacteristicsofGreatCode
Whatdowemeanbygreatcode?Differentprogrammerswillhave
differentdefinitionsforgreatcode,soitisimpossibletoprovideanallencompassingdefinitionthatwillsatisfyeveryone.However,thereare
certainattributesofgreatcodethatnearlyeveryonewillagreeupon,and
we'llusesomeofthesecommoncharacteristicstoformourdefinition.
Forourpurposes,herearesomeattributesofgreatcode:
UsestheCPUefficiently(whichmeansthecodeisfast)
Usesmemoryefficiently(whichmeansthecodeissmall)
Usessystemresourcesefficiently
Iseasytoreadandmaintain
Followsaconsistentsetofstyleguidelines
Usesanexplicitdesignthatfollowsestablishedsoftware
engineeringconventions
Iseasytoenhance
Iswell-testedandrobust(meaningthatitworks)
Iswell-documented
Wecouldeasilyadddozensofitemstothislist.Someprogrammers,for
example,mayfeelthatgreatcodemustbeportable,thatitmustfollowa
givensetofprogrammingstyleguidelines,orthatitmustbewrittenina
certainlanguage(orthatitmustnotbewritteninacertainlanguage).
Somemayfeelthatgreatcodemustbewrittenassimplyaspossible,
whileothersmayfeelthatgreatcodeiswrittenquickly.Stillothersmay
feelthatgreatcodeiscreatedontimeandunderbudget.Youcan
probablythinkofadditionalcharacteristics.
Sowhatisgreatcode?Hereisareasonabledefinition:
Greatcodeissoftwarethatiswrittenusingaconsistentand



prioritizedsetofgoodsoftwarecharacteristics.Inparticular,great
codefollowsasetofrulesthatguidethedecisionsaprogrammer
makeswhenimplementinganalgorithmassourcecode.
Twodifferentprogramsdonothavetofollowthesamesetofrules(that
is,theyneednotpossessthesamesetofcharacteristics)inorderfor
bothtobegreatprograms.Aslongastheyeachconsistentlyobeytheir
particularsetofrules,theycanbothbeexamplesofgreatcode.Inone
environment,agreatprogrammaybeonethatisportableacross
differentCPUsandoperatingsystems.Inadifferentenvironment,
efficiency(speed)maybetheprimarygoal,andportabilitymaynotbean
issue.Bothcouldbeshiningexamplesofgreatcode,eventhoughtheir
goalsmightbemutuallyexclusive.Clearly,neitherprogramwouldbean
exampleofgreatcodewhenexaminedaccordingtotherulesoftheother
program;butaslongasthesoftwareconsistentlyfollowstheguidelines
establishedforthatparticularprogram,youcanarguethatitisan
exampleofgreatcode.


1.5TheEnvironmentforThisVolume
Althoughthisbookpresentsgenericinformation,partsofthediscussion
willnecessarilybespecifictoaparticularsystem.BecausetheIntel
ArchitecturePCsare,byfar,themostcommoninusetoday,thisbook
willusethatplatformwhendiscussingspecificsystem-dependent
concepts.However,thoseconceptswillstillapplytoothersystemsand
CPUs(forexample,thePowerPCCPUinthePowerMacintoshorsome
otherRISCCPUinaUnixbox)thoughyoumaywellneedtoresearch
thesolutionforyourspecificplatformwhenanexampledoesnot
explicitlyapplytoyoursystem.
MostexamplesappearinginthisbookrununderbothWindowsand
Linux.Thisbookattemptstostickwithstandardlibraryinterfacestothe

operatingsystem(OS)whereverpossible,anditmakesOS-specificcalls
onlywhenthealternativeistowrite'lessthangreat'code.
Mostofthespecificexamplesinthisbookrunonalate-modelIntel
Architecture(includingAMD)CPUunderWindowsorLinux,witha
reasonableamountofRAMandothersystemperipheralsnormallyfound
onalate-modelPC.Theconcepts,ifnotthesoftwareitself,willapplyto
Macs,Unixboxes,embeddedsystems,andevenmainframes.


1.6ForMoreInformation
Nosinglebookcancompletelycovereverythingaboutmachine
organizationthatyouneedtoknowinordertowritegreatcode.This
book,therefore,concentratesonthoseaspectsofmachineorganization
thataremostpertinentforwritinggreatsoftware,providingthe90percent
solutionforthosewhoareinterestedinwritingthebestpossiblecode.To
learnthatlast10percentofmachineorganization,you'regoingtoneed
additionalresources.
Learnassemblylanguage.Fluencyinatleastoneassembly
languagewillfillinmanymissingdetailsthatyoujustwon'tgetby
learningmachineorganizationalone.Unlessyouplanonusing
assemblylanguageinyoursoftwaresystems,youdon't
necessarilyhavetolearnassemblylanguageontheplatform(s)
towhichyou'retargetingyoursoftware.Probablyyourbestbet,
then,istolearn80x86assemblylanguageonaPC.TheIntel
Architectureisn'tthebest,buttherearelotsofgreatsoftware
toolsforlearningassemblylanguage(forexample,theHighLevel
Assembler)thatsimplydon'texistonotherplatforms.Thepointof
learningassemblylanguagehereisnotsoyoucanwrite
assemblycode,butrathertolearntheassemblyparadigm.Ifyou
know80x86assemblylanguage,you'llhaveagoodideaofhow

otherCPUs(suchasthePowerPCortheIA-64family)operate.
Ofcourse,ifyouneedtowriteassemblycode,youshouldlearn
theassemblylanguagefortheCPUyou'llbeusing.Anexcellent
choiceforlearningassemblylanguageisanotherbookofmine,
TheArtofAssemblyLanguage,availablefromNoStarchPress.
Studyadvancedcomputerarchitecture.Machineorganizationisa
subsetofthestudyofcomputerarchitecture,butspacelimitations
preventcoveringmachineorganizationandcomputer
architectureincompletedetailwithinthisbook.Whileyoumay
notneedtoknowhowtodesignyourownCPUs,studying
computerarchitecturemayteachyousomethingyou'vemissedin
thepresentationofmachineorganizationinthisbook.Computer
Architecture:AQuantitativeApproachbyHennessyand


Pattersonisawell-respectedtextbookthatcoversthissubject
matter.


Chapter2:NumericRepresentation
High-levellanguagesshieldprogrammersfromthepainofdealingwith
low-levelnumericrepresentation.Writinggreatcode,however,requiresa
completeunderstandingofhowcomputersrepresentnumbers.Onceyou
understandinternalnumericrepresentation,you'lldiscoverefficientways
toimplementmanyalgorithmsandseethepitfallsassociatedwithmany
commonprogrammingpractices.Therefore,thischapterlooksatnumeric
representationtoensureyoucompletelyunderstandwhatyourcomputer
languagesandsystemsaredoingwithyourdata.



2.1WhatIsaNumber?
Havingtaughtassemblylanguageprogrammingformanyyears,I've
discoveredthatmostpeopledon'tunderstandthefundamentaldifference
betweenanumberandtherepresentationofthatnumber.Mostofthe
timethisconfusionisharmless.However,manyalgorithmsdependupon
theinternalandexternalrepresentationsweusefornumberstooperate
correctlyandefficiently.Ifyoudonotunderstandthedifferencebetween
theabstractconceptofanumberandtherepresentationofthatnumber,
you'llhavetroubleunderstanding,using,orcreatingsuchalgorithms.
Fullyunderstandingthisdifferencecouldtakeyoufromcreatingsome
mediocrecodetocreatinggreatcode.
Anumberisanintangible,abstract,concept.Itisanintellectualdevice
thatweusetodenotequantity.Let'ssayIweretotellyouthat'some
bookhasonehundredpages.'Youcouldtouchthepages-theyare
tangible.Youcouldevencountthosepagestoverifythatthereareone
hundredofthem.However,'onehundred'issimplyanabstractionthatI
wouldbeapplyingtothebookasawayofdescribingitssize.
Theimportantthingtorealizeisthatthefollowingisnotonehundred:
100
Thisisnothingmorethaninkonpaperformingcertainlinesandcurves.
Youmightrecognizethissequenceofsymbolsasarepresentationofone
hundred,butthisisnottheactualvalue100.It'sjustthreesymbols
appearingonthispage.Itisn'teventheonlyrepresentationforone
hundred-considerthefollowing,whicharealldifferentrepresentationsof
thevalueonehundred:
100

decimalrepresentation

C


Romannumeralrepresentation

6416

base16/hexadecimalrepresentation


11001002

basetwo/binaryrepresentation

1448

baseeight/octalrepresentation

onehundred Englishrepresentation
Therepresentationofanumberis(generally)somesequenceof
symbols.Forexample,thecommonrepresentationofthevalueone
hundred,'100,'isreallyasequenceofthreenumericdigits:thedigit1
followedbythedigit0followedbyasecond0digit.Eachofthesedigits
hassomespecificmeaning,butwecouldhavejustaseasilyusedthe
sequence'64'torepresentthevalueonehundred.Eventheindividual
digitsthatcomprisethisrepresentationof100arenotnumbers.Theyare
numericdigits,toolsweusetorepresentnumbers,buttheyarenot
numbersthemselves.
Nowyoumaybewonderingwhyweshouldevencarewhethera
sequenceofsymbolslike'100'istheactualvalueonehundredorjustthe
representationofthisvalue.Thereasonforthisdistinctionisthatyou'll
encounterseveraldifferentsequencesofsymbolsinacomputerprogram

thatlooklikenumbers(meaningthattheylooklike'100'),andyoudon't
wanttoconfusethemwithactualnumericvalues.Conversely,thereare
manydifferentrepresentationsforthevalueonehundredthatacomputer
coulduse,andit'simportantforyoutorealizethattheyareequivalent.


×