GeneralPurpose Registers.docx
《GeneralPurpose Registers.docx》由会员分享,可在线阅读,更多相关《GeneralPurpose Registers.docx(67页珍藏版)》请在冰豆网上搜索。
GeneralPurposeRegisters
General-PurposeRegisters
3.4.1.General-PurposeRegisters
The32-bitgeneral-purposeregistersEAX,EBX,ECX,EDX,ESI,EDI,EBP,andESPareprovidedforholdingthefollowingitems:
•Operandsforlogicalandarithmeticoperations
•Operandsforaddresscalculations
•Memorypointers
Althoughalloftheseregistersareavailableforgeneralstorageofoperands,results,and
pointers,cautionshouldbeusedwhenreferencingtheESPregister.TheESPregisterholdsthe
stackpointerandasageneralruleshouldnotbeusedforanyotherpurpose.
Manyinstructionsassignspecificregisterstoholdoperands.Forexample,stringinstructions
usethecontentsoftheECX,ESI,andEDIregistersasoperands.Whenusingasegmented
memorymodel,someinstructionsassumethatpointersincertainregistersarerelativeto
specificsegments.Forinstance,someinstructionsassumethatapointerintheEBXregister
pointstoamemorylocationintheDSsegment.
Thespecialusesofgeneral-purposeregistersbyinstructionsaredescribedinSection5,
“InstructionSetSummary”inthisvolume;seealsoChapter3,InstructionSetReferenceA-M
andChapter4,InstructionSetReferenceN-ZoftheIA-32IntelArchitectureSoftwareDeveloper’s
Manual,Volumes2A&2B.Thefollowingisasummaryofspecialuses:
•EAX—Accumulatorforoperandsandresultsdata
•EBX—PointertodataintheDSsegment
•ECX—Counterforstringandloopoperations
•EDX—I/Opointer
•ESI—PointertodatainthesegmentpointedtobytheDSregister;sourcepointerforstringoperations
•EDI—Pointertodata(ordestination)inthesegmentpointedtobytheESregister;destinationpointerforstringoperations
•ESP—Stackpointer(intheSSsegment)
•EBP—Pointertodataonthestack(intheSSsegment)
3.4.2.SegmentRegisters
Thesegmentregisters(CS,DS,SS,ES,FS,andGS)hold16-bitsegmentselectors.Asegment
selectorisaspecialpointerthatidentifiesasegmentinmemory.Toaccessaparticularsegment
inmemory,thesegmentselectorforthatsegmentmustbepresentintheappropriatesegment
register.
Whenwritingapplicationcode,programmersgenerallycreatesegmentselectorswithassembler
directivesandsymbols.Theassemblerandothertoolsthencreatetheactualsegmentselector
valuesassociatedwiththesedirectivesandsymbols.Ifwritingsystemcode,programmersmay
needtocreatesegmentselectorsdirectly.Adetaileddescriptionofthesegment-selectordata
structureisgiveninChapter3,Protected-ModeMemoryManagement,oftheIA-32IntelArchitecture
SoftwareDeveloper’sManual,Volume3.
Howsegmentregistersareuseddependsonthetypeofmemorymanagementmodelthatthe
operatingsystemorexecutiveisusing.Whenusingtheflat(unsegmented)memorymodel,the
segmentregistersareloadedwithsegmentselectorsthatpointtooverlappingsegments,eachof
whichbeginsataddress0ofthelinearaddressspace(asshowninFigure3-5).Theseoverlapping
segmentsthencomprisethelinearaddressspacefortheprogram.Typically,twooverlapping
segmentsaredefined:
oneforcodeandanotherfordataandstacks.TheCSsegment
registerpointstothecodesegmentandalltheothersegmentregisterspointtothedataandstack
segment.
Whenusingthesegmentedmemorymodel,eachsegmentregisterisordinarilyloadedwitha
differentsegmentselectorsothateachsegmentregisterpointstoadifferentsegmentwithinthe
linearaddressspace(asshowninFigure3-6).Atanytime,aprogramcanthusaccessuptosix
segmentsinthelinearaddressspace.Toaccessasegmentnotpointedtobyoneofthesegment
registers,aprogrammustfirstloadthesegmentselectorforthesegmenttobeaccessedintoa
segmentregister.
Figure3-4.AlternateGeneral-PurposeRegisterNames
311615870
AHAL
BHBL
CHCL
DHDL
BP
SI
DI
SP
16-bit
AX
DX
CX
BX
32-bit
EAX
EBX
ECX
EDX
EBP
ESI
ESP
General-PurposeRegisters
EDI
Eachofthesegmentregistersisassociatedwithoneofthreetypesofstorage:
code,data,or
stack.Forexample,theCSregistercontainsthesegmentselectorforthecodesegment,where
theinstructionsbeingexecutedarestored.Theprocessorfetchesinstructionsfromthecode
segment,usingalogicaladdressthatconsistsofthesegmentselectorintheCSregisterandthe
contentsoftheEIPregister.TheEIPregistercontainstheoffsetwithinthecodesegmentofthe
nextinstructiontobeexecuted.TheCSregistercannotbeloadedexplicitlybyanapplication
program.Instead,itisloadedimplicitlybyinstructionsorinternalprocessoroperationsthat
changeprogramcontrol(suchas,procedurecalls,interrupthandling,ortaskswitching).
TheDS,ES,FS,andGSregisterspointtofourdatasegments.Theavailabilityoffourdata
segmentspermitsefficientandsecureaccesstodifferenttypesofdatastructures.Forexample,
fourseparatedatasegmentsmightbecreated:
oneforthedatastructuresofthecurrentmodule,
anotherforthedataexportedfromahigher-levelmodule,athirdforadynamicallycreateddata
structure,andafourthfordatasharedwithanotherprogram.Toaccessadditionaldatasegments,
theapplicationprogrammustloadsegmentselectorsforthesesegmentsintotheDS,ES,FS,and
GSregisters,asneeded.
TheSSregistercontainsthesegmentselectorforthestacksegment,wheretheprocedurestack
isstoredfortheprogram,task,orhandlercurrentlybeingexecuted.Allstackoperationsusethe
SSregistertofindthestacksegment.UnliketheCSregister,theSSregistercanbeloaded
explicitly,whichpermitsapplicationprogramstosetupmultiplestacksandswitchamongthem.
SeeSection3.3.,“MemoryOrganization”,foranoverviewofhowthesegmentregistersare
usedinreal-addressmode.
ThefoursegmentregistersCS,DS,SS,andESarethesameasthesegmentregistersfoundin
theIntel8086andIntel286processorsandtheFSandGSregisterswereintroducedintothe
IA-32ArchitecturewiththeIntel386™familyofprocessors.
3.4.3.EFLAGSRegister
The32-bitEFLAGSregistercontainsagroupofstatusflags,acontrolflag,andagroupofsystemflags.Figure3-7definestheflagswithinthisregister.Followinginitializationoftheprocessor(eitherbyassertingtheRESETpinortheINITpin),thestateoftheEFLAGSregisteris00000002H.Bits1,3,5,15,and22through31ofthisregisterarereserved.Softwareshouldnotuseordependonthestatesofanyofthesebits.SomeoftheflagsintheEFLAGSregistercanbemodifieddirectly,usingspecial-purposeinstructions(describedinthefollowingsections).Therearenoinstructionsthatallowthewholeregistertobeexaminedormodifieddirectly.ThefollowinginstructionscanbeusedtomovegroupsofflagstoandfromtheprocedurestackortheEAXregister:
LAHF,SAHF,PUSHF,PUSHFD,POPF,andPOPFD.AfterthecontentsoftheEFLAGSregisterhavebeentransferredtotheprocedurestackorEAXregister,theflagscanbeexaminedandmodifiedusingtheprocessor’sbitmanipulationinstructions(BT,BTS,BTR,andBTC).Whensuspendingatask(usingtheprocessor’smultitaskingfacilities),theprocessorautomatically
savesthestateoftheEFLAGSregisterinthetaskstatesegment(TSS)forthetaskbeingsuspended.Whenbindingitselftoanewtask,theprocessorloadstheEFLAGSregisterwithdatafromthenewtask’sTSS.Whenacallismadetoaninterruptorexceptionhandlerprocedure,theprocessorautomaticallysavesthestateoftheEFLAGSregistersontheprocedurestack.Whenaninterruptorexceptionishandledwithataskswitch,thestateoftheEFLAGSregisterissavedintheTSSforthetaskbeingsuspended.
AstheIA-32Architecturehasevolved,flagshavebeenaddedtotheEFLAGSregister,butthefunctionandplacementofexistingflagshaveremainedthesamefromonefamilyoftheIA-32processorstothenext.Asaresult,codethataccessesormodifiestheseflagsforonefamilyofIA-32processorsworksasexpectedwhenrunonlaterfamiliesofprocessors.
3.4.3.1.STATUSFLAGS
Thestatusflags(bits0,2,4,6,7,and11)oftheEFLAGSregisterindicatetheresultsofarithmetic
instructions,suchastheADD,SUB,MUL,andDIVinstructions.Thestatusflagfunctions
are:
CF(bit0)Carryflag.Setifanarithmeticoperationgeneratesacarryora
borrowoutofthemost-significantbitoftheresult;clearedotherwise.
Thisflagindicatesanoverflowconditionforunsigned-integerarithmetic.
Itisalsousedinmultiple-precisionarithmetic.
PF(bit2)Parityflag.Setiftheleast-significantbyteoftheresultcontainsan
evennumberof1bits;clearedotherwise.
AF(bit4)Adjustflag.Setifanarithmeticoperationgeneratesacarryora
borrowoutofbit3oftheresult;clearedotherwise.Thisflagisused
inbinary-codeddecimal(BCD)arithmetic.
ZF(bit6)Zeroflag.Setiftheresultiszero;clearedotherwise.
SF(bit7)Signflag.Setequaltothemost-significantbitoftheresult,whichis
thesignbitofasignedinteger.(0indicatesapositivevalueand1
indicatesanegativevalue.)
OF(bit11)Overflowflag.Setiftheintegerresultistoolargeapositivenumber
ortoosmallanegativenumber(excludingthesign-bit)tofitinthe
destinationoperand;clearedotherwise.Thisflagindicatesanoverflow
conditionforsigned-integer(two’scomplement)arithmetic.
Ofthesestatusflags,onlytheCFflagcanbemodifieddirectly,usingtheSTC,CLC,andCMC
instructions.Alsothebitinstructions(BT,BTS,BTR,andBTC)copyaspecifiedbitintotheCF
flag.
Thestatusflagsallowasinglearithmeticoperationtoproduceresultsforthreedifferentdata
types:
unsignedintegers,signedintegers,andBCDinteg