Sunteți pe pagina 1din 4

ContentsUp<<>>

Registers
In16bitmode,suchasprovidedbythePentiumprocessorwhenoperatingasaVirtual8086(thisisthemodeusedwhenWindows95displaysaDOSprompt),
theprocessorprovidestheprogrammerwith14internalregisters,each16bitswide.Theyaregroupedintoseveralcategoriesasfollows:
Fourgeneralpurposeregisters,AX,BX,CX,andDX.Eachoftheseisacombinationoftwo8bitregisterswhichareseparatelyaccessibleasAL,BL,
CL,DL(the"low''bytes)andAH,BH,CH,andDH(the"high''bytes).Forexample,ifAXcontainsthe16bitnumber1234h,thenALcontains34hand
AHcontains12h.
Fourspecialpurposeregisters,SP,BP,SI,andDI.
Foursegmentregisters,CS,DS,ES,andSS.
Theinstructionpointer,IP(sometimesreferredtoastheprogramcounter).
Thestatusflagregister,FLAGS.
AlthoughIrefertothefirstfourregistersas"generalpurpose'',eachofthemisdesignedtoplayaparticularroleincommonuse:
AXisthe"accumulator''someoftheoperations,suchasMULandDIV,requirethatoneoftheoperandsbeintheaccumulator.Someotheroperations,such
asADDandSUB,maybeappliedtoanyoftheregisters(thatis,anyoftheeightgeneralandspecialpurposeregisters)butaremoreefficientwhenworking
withtheaccumulator.
BXisthe"base''registeritistheonlygeneralpurposeregisterwhichmaybeusedforindirectaddressing.Forexample,theinstructionMOV[BX],AX
causesthecontentsofAXtobestoredinthememorylocationwhoseaddressisgiveninBX.
CXisthe"count''register.Theloopinginstructions(LOOP,LOOPE,andLOOPNE),theshiftandrotateinstructions(RCL,RCR,ROL,ROR,SHL,SHR,andSAR),and
thestringinstructions(withtheprefixesREP,REPE,andREPNE)allusethecountregistertodeterminehowmanytimestheywillrepeat.
DXisthe"data''registeritisusedtogetherwithAXforthewordsizeMULandDIVoperations,anditcanalsoholdtheportnumberfortheINandOUT
instructions,butitismostlyavailableasaconvenientplacetostoredata,asarealloftheothergeneralpurposeregisters.
Herearebriefdescriptionsofthefourspecialpurposeregisters:
SPisthestackpointer,indicatingthecurrentpositionofthetopofthestack.Youshouldgenerallynevermodifythisdirectly,sincethesubroutineand
interruptcallandreturnmechanismsdependonthecontentsofthestack.
BPisthebasepointer,whichcanbeusedforindirectaddressingsimilartoBX.
SIisthesourceindex,usedasapointertothecurrentcharacterbeingreadinastringinstruction(LODS,MOVS,orCMPS).Itisalsoavailableasanoffsetto
addtoBXorBPwhendoingindirectaddressingforexample,theinstructionMOV[BX+SI],AXcopiesthecontentsofAXintothememorylocationwhose
addressisthesumofthecontentsofBXandSI.
DIisthedestinationindex,usedasapointertothecurrentcharacterbeingwrittenorcomparedinastringinstruction(MOVS,STOS,CMPS,orSCAS).Itisalso
availableasanoffset,justlikeSI.

Sincealloftheseregistersare16bitswide,theycanonlycontainaddressesformemorywithinarangeof64K(=2^16)bytes.Tosupportmachineswithmore
than64Kofphysicalmemory,Intelimplementedtheconceptofsegmentedmemory.Atanygiventime,a16bitaddresswillbeinterpretedasanoffsetwithina
64Ksegmentdeterminedbyoneofthefoursegmentregisters(CS,DS,ES,andSS).
Asanexample,intheinstructionMOV[BX],AXmentionedabove,theBXregisterreallyprovidestheoffsetofalocationinthecurrentdatasegmenttofindthe
truephysicaladdressintowhichthecontentsoftheaccumulatorwillbestored,youhavetoaddthevalueinBXtotheaddressofthestartofthedatasegment.
Thissegmentstartaddressisdeterminedbytakingthe16bitnumberinDSandmultiplyingby16.Therefore,ifDScontains1234handBXcontains0017h,then
thephysicaladdresswillbe1234hTIMES16+0017h=12340h+0017h=12357h.(Thiscomputationillustratesonereasonwhyhexadecimalissouseful
multiplicationby16correspondstoshiftingthehexdigitsleftoneplaceandappendingazero.)Werefertothiscombinedaddressas1234:0017or,more
generally,asDS:BX.
Sincesegmentstartsarecomputedbymultiplyinga16bitnumberby16=2^4,theeffectisthatphysicaladdresseshavea20bitrange,sothatatotalof1M
(=2^20)ofmemorymaybeused.Intelconsideredthatthiswouldbeenoughforapplicationsofthe8086overitsprojectedlifetimeofaboutfiveyearsfromits
introductionin1978bythetimemicrocomputerswereneedingmorethanamegofmainmemory,thenextIntelprocessor(theiAPX432)wasduetobe
available,witha32bitaddressspace(abletoaddress4Gover4billionmemorylocations).However,theIBMPC'sdebutin1981andsubsequentpopularity
hasforcedInteltocontinuethe80x86familyofbackwardcompatibleprocessorstothepresent,includingsupportforamodeinwhichonly1Mofmemoryis
accessible.Processorssincethe80286havealsoprovidedthe"protected''modeofoperation,whichinthePentiumgiveseachprocessaflat32bitaddressspace
ofupto4G.
Youmightthinkthatasegmentregisterwouldonlyneedtoprovidetheuppermost4bitstoextendanaddressoutto20bits,butconsideroneoftheimplications
ofonlyhaving16different,nonoverlappingsegments:everysegmentwouldhavetooccupyafull64Kofmemory,evenifonlyasmallfractionofthisspace
wereneeded.Byallowingasegmenttostartatanyaddressdivisibleby16,thememorymaybeallocatedmuchmoreefficientlyifoneprogramonlyneeds4K
foritscodesegment,thentheoreticallytheoperatingsystemcouldloadanotherprogramintoasegmentstartingjust4Kabovethestartofthefirst.Ofcourse,
MSDOSisnotreallythissophisticated,buttheInteldesignerswantedittobepossible.
Eachsegmentregisterhasitsownspecialuses:
CSdeterminesthe"code''segmentthisiswheretheexecutablecodeofaprogramislocated.Itisnotdirectlymodifiablebytheprogrammer,exceptby
executingoneofthebranchinginstructions.Oneofthereasonsforseparatingthecodesegmentfromothersegmentsisthatwellbehavedprogramsnever
modifytheircodewhileexecutingtherefore,thecodesegmentcanbeidentifiedas"readonly''.Thissimplifiestheworkofacache,sincenoeffortis
requiredtomaintainconsistencybetweenthecacheandmainmemory.Italsopermitsseveralinstancesofasingleprogramtorunatonce(ina
multitaskingoperatingsystem),allsharingthesamecodesegmentinmemoryeachinstancehasitsowndataandstacksegmentswheretheinformation
specifictotheinstanceiskept.Picturemultiplewindows,eachrunningWordonadifferentdocumenteachoneneedsitsowndatasegmenttostoreits
document,buttheycanallexecutethesameloadedcopyofWord.
DSdeterminesthe"data''segmentitisthedefaultsegmentformostmemoryaccesses.
ESdeterminesthe"extra''segmentitcanbeusedinsteadofDSwhendatafromtwosegmentsneedtobeaccessedatonce.Inparticular,theDIregister
givesanoffsetrelativetoESwhenusedinthestringinstructionsforexample,theMOVSBinstructioncopiesabytefromDS:SItoES:DI(andalsocausesSI
andDItobeincrementedordecremented,readytocopythenextbyte).

SSdeterminesthe"stack''segmentthestackpointerSPgivestheoffsetofthecurrenttopofstackwithinthestacksegment.TheBPregisteralsogivesan
offsetrelativetothestacksegmentbydefault,forconvenientaccesstodatafurtherdowninthestackwithouthavingtomodifySP.JustaswithSP,you
shouldnotmodifySSunlessyouknowexactlywhatyouaredoing.
Theinstructionpointer,IP,givestheaddressofthenextinstructiontobeexecuted,relativetothecodesegment.Theonlywaytomodifythisiswithabranch
instruction.
Thestatusregister,FLAGS,isacollectionof1bitvalueswhichreflectthecurrentstateoftheprocessorandtheresultsofrecentoperations.Nineofthesixteen
bitsareusedinthe8086:
Carry(bit0):setifthelastarithmeticoperationendedwithaleftovercarrybitcomingofftheleftendoftheresult.Thissignalsanoverflowonunsigned
numbers.
Parity(bit2):setiftheloworderbyteofthelastdataoperationcontainedanevennumberof1bits(thatis,itsignalsanevenparitycondition).
AuxiliaryCarry(bit4):usedwhenworkingwithbinarycodeddecimal(BCD)numbers.
Zero(bit6):setifthelastcomputationhadazeroresult.Afteracomparison(CMP,CMPS,orSCAS),thisindicatesthatthevaluescomparedwereequal(since
theirdifferencewaszero).
Sign(bit7):setifthelastcomputationhadanegativeresult(a1intheleftmostbit).
Trace(bit8):whenset,thisputstheCPUintosinglestepmode,asusedbydebuggers.
Interrupt(bit9):whenset,interruptsareenabled.Thisbitshouldbeclearedwhiletheprocessorisexecutingacriticalsectionofcodethatshouldnotbe
interrupted(forexample,whenprocessinganotherinterrupt).
Direction(bit10):whenclear,thestringoperationsmovefromlowaddressestohigh(theSIandDIregistersareincrementedaftereachcharacter).When
set,thedirectionisreversed(SIandDIaredecremented).
Overflow(bit11):setifthelastarithmeticoperationcausedasignedoverflow(forexample,afteradding0001hto7FFFh,resultingin8000hreadastwo's
complementnumbers,thiscorrespondstoadding1to32767andendingupwith32768).
Therearenumerousoperationsthatwilltestandmanipulatevariousoftheseflags,buttogetthecontentsoftheentireFLAGSregisteronehastopushtheflags
ontothestack(withPUSHForbycallinganappropriateinterrupthandlerwithINT)andthenpopthemoffintoanotherregister.TosettheentireFLAGSregister,
thesequenceisreversed(withPOPForIRET).Forexample,onewaytosetthecarryflag(therearemuchbetterways,includingtheSTCinstruction)isthe
following:
PUSHF
POPAX
ORAX,1
PUSHAX
POPF

MostofthetimeyouwillnothavetodealwiththeFLAGSregisterexplicitlyinstead,youwillexecuteoneoftheconditionalbranchinstructions,Jcc,wherecc
isoneofthefollowingmnemonicconditioncodes:
O,Overflow
NO,NotOverflow

B,BelowC,CarryNAE,NotAboveorEqual
NB,NotBelowNC,NotCarryAE,AboveorEqual
E,EqualZ,Zero
NE,NotEqualNZ,NotZero
BE,BeloworEqualNA,NotAbove(trueifeitherCarryorZeroisset)
NBE,NotBeloworEqualA,Above
S,Sign
NS,NotSign
P,ParityPE,ParityEven
NP,NotParityPO,ParityOdd
L,LessNGE,NotGreaterorEqual(trueifSignandOverflowaredifferent)
NL,NotLessGE,GreaterorEqual
LE,LessorEqualNG,NotGreater(trueifSignandOverflowaredifferent,orZeroisset)
NLE,NotLessorEqualG,Greater

Alloftheconditionsonthesamelinearesynonyms.TheAboveandBelowconditionsrefertocomparisonsofunsignednumbers,andtheLessandGreater
conditionsrefertocomparisonsofsigned(two'scomplement)numbers.

S-ar putea să vă placă și