8/4/2019 Microprocessors 1 Basics
1/120
3/3/11
1
Microprocessor1
Dr.SufyanSamara
ComputerEngineeringDepartment
AnNajahNa>onalUniversity
1
Objec>vesandnotes Provideanunderstandingofmicroprocessors,
theirstructure,andtheassemblylanguage.
Threeexamsandsomehomeworks. Textbook:
TheintelmicroprocessorsbyBarryB.Brey
2
8/4/2019 Microprocessors 1 Basics
2/120
3/3/11
2
History
500B.C.Babyloniansinventedtheabacus In1642mathema>cianBlaisePascalinventedacalculatorconstructedof
gearsandwheels.
Firstprac>calgearedmechanicalmachinestocomputeinforma>ondatetoearly1800s
handheldelectroniccalculator. firstappearedinearly1970s
In1889,HermanHollerithdevelopedthepunchedcardforstoringdata. GermaninventorKonradZuse,inventedthefirstmodern
electromechanicalcomputer.
HisZ3calcula>ngcomputerprobablyinventedforaircra\andmissiledesign.
duringWorldWarIIfortheGermanwareffort Z3arelaylogicmachineclockedat5.33Hz.
farslowerthanlatestmul>pleGHzmicroprocessors
3
Historycont. ElectronicNumericalIntegratorandCalculator(ENIAC),ahugemachine. over17,000vacuumtubes;500milesofwires weighedover30tons about100,000opera>onspersecond
Programmedbyrewiringitscircuits. processtookmanyworkersseveraldays workerschangedelectricalconnec>onsonplug-boardslikeearly
telephoneswitchboards
Requiredfrequentmaintenance. vacuumtubeservicelifeaproblem
FirstmicroprocessordevelopedatIntelCorpora>onin1971 Thefirst,machinelanguage,wasconstructedofonesandzeros
usingbinarycodes. storedinthecomputermemorysystemasgroupsofinstruc>ons
calledaprogram
4
8/4/2019 Microprocessors 1 Basics
3/120
3/3/11
3
Historycont. Mathema>cianJohnvonNeumannfirstmodernpersontodevelopa
systemtoacceptinstruc>onsandstoretheminmemory.
Computersareo\encalledvonNeumannmachinesinhishonor. Assemblerallowsprogrammertousemnemoniccodes
suchasADDforaddi>on Inplaceofabinarynumber.
suchas01000111 Assemblylanguageanaidtoprogramming. 1957GraceHopperdevelopedfirsthigh-levelprogramminglanguage
calledFLOWMATIC. computersbecameeasiertoprogram
Insameyear,IBMdevelopedFORTRANFORmulaTRANslator)foritssystems. Allowedprogrammerstodevelopprogramsthatusedformulastosolve
mathema>calproblems. FORTRANiss>llusedbysomescien>stsforcomputerprogramming.
Similarlanguage,ALGOL(ALGOrithmicLanguage)introducedaboutayearlater
5
Historycont. Firstsuccessful,widespreadprogramminglanguagefor
businessapplica>onswasCOBOL(COmputerBusinessOrientedLanguage).
SomecommonmodernprogramminglanguagesareBASIC,C#,C/C++,Java,PASCAL,andADA.
Assemblylanguages>llplaysimportantrole. manyvideogameswrienalmostexclusively
inassemblylanguage
AssemblyalsointerspersedwithC/C++toperformmachinecontrolfunc>onsefficiently. somenewerparallelinstruc>onsfoundonPen>umandCore2
microprocessorsonlyprogrammableinassemblylanguage
6
8/4/2019 Microprocessors 1 Basics
4/120
3/3/11
4
TheMicroprocessorAge WorldsfirstmicroprocessortheIntel4004.
A4-bitmicroprocessor-programmablecontrolleronachip. Addressed4096,4-bit-widememoryloca>ons.
abitisabinarydigitwithavalueofoneorzero 4-bit-widememoryloca>ono\encalledanibble
The4004instruc>onsetcontained45instruc>ons. Fabricatedwiththen-currentstate-of-the-artP-channelMOSFET
technology.
Executedinstruc>onsat50KIPs(kilo-instruconspersecond). slowcomparedto100,000instruc>onspersecondby30-tonENIACcomputer
in1946
Differencewasthat4004weighedlessthananounce. 4-bitmicroprocessordebutedinearlygamesystemsandsmallcontrol
systems. earlyshuffleboardgameproducedbyBailey
Mainproblemswithearlymicroprocessorwerespeed,wordwidth,andmemorysize. TexasInstrumentsandothersalsoproduced4-bitmicroprocessors.
s>llsurvivesinlow-endapplica>onssuchasmicrowaveovensandsmallcontrolsystems
Calculatorss>llbasedon4-bitBCD(binary-codeddecimal)codes7
TheMicroprocessorAgecont. Intelreleased8008in1971.
extended8-bitversionof4004microprocessor Addressedexpandedmemoryof16Kbytes.
Abyteisgenerallyan8-bit-widebinarynumberandaKis1024.
memorysizeo\enspecifiedinKbytes Containedaddi>onalinstruc>ons,48total. Providedopportunityforapplica>oninmore
advancedsystems.
engineersdevelopeddemandingusesfor80088
8/4/2019 Microprocessors 1 Basics
5/120
3/3/11
5
TheMicroprocessorAgecont. Intelintroduced8080microprocessorin1973. firstofthemodem8-bitmicroprocessors MotorolaCorpora>onintroducedMC6800microprocessorabout
sixmonthslater.
8080and,toalesserdegree,theMC6800wereabreakthroughintheageofthemicroprocessor. othercompaniessoonintroducedtheirownversionsofthe8-bit
microprocessor
8080addressedfour>mesmorememory. 64Kbytesvsl6Kbytesfor8008
Executedaddi>onalinstruc>ons;10xfaster. addi>ontaking20sonan8008-basedsystemrequiredonly2.0son
an8080-basedsystem
TTL(transistor-transistorlogic)compa>ble. the8008wasnotdirectlycompa>ble
Interfacingmadeeasierandlessexpensive.9
The8085Microprocessor In1977IntelCorpora>onintroducedan
updatedversionofthe8080the8085.
Last8-bit,general-purposemicroprocessordevelopedbyIntel.
Slightlymoreadvancedthan8080;executedso\wareatanevenhigherspeed.
769,230instruc>onspersecondvs500,000persecondonthe8080).
10
8/4/2019 Microprocessors 1 Basics
6/120
3/3/11
6
TheModernMicroprocessor
In1978Intelreleasedthe8086;ayearorsolater,itreleasedthe8088.
Bothdevicesare16-bitmicroprocessors. executedinstruc>onsinaslileas400ns(2.5millionsofinstruconspersecond)
majorimprovementoverexecu>onspeedof8085 8086&8088addressed1Mbyteofmemory.
16>mesmorememorythanthe8085
1M-bytememorycontains1024Kbyte-sizedmemoryloca>onsor1,048,576bytes
11
TheModernMicroprocessorcont. Higherspeedandlargermemorysizeallowed8086&8088
toreplacesmallerminicomputersinmanyapplica>ons.
Anotherfeaturewasa4-or6-byteinstruc>oncacheorqueuethatprefetchedinstruc>onsbeforetheywereexecuted. queuespedopera>onofmanysequencesofinstruc>on basisforthemuchlargerinstruc>oncaches
foundinmodemmicroprocessors.
ThesemicroprocessorsarecalledCISC(complexinstruconsetcomputers). addi>onalinstruc>onseasedtaskofdevelopingefficientand
sophis>catedapplica>ons
16-bitmicroprocessoralsoprovidedmoreinternalregisterstoragespace. addi>onalregistersallowedso\waretobe
wrienmoreefficiently
evolvedtomeetneedforlargermemorysystems12
8/4/2019 Microprocessors 1 Basics
7/120
3/3/11
7
TheModernMicroprocessorcont.
PopularityofIntelensuredin1981whenIBMchosethe8088initspersonalcomputer.
Spreadsheets,wordprocessors,spellingcheckers,andcomputer-basedthesauruses
werememory-intensive.
requiredmorethan64Kbytesofmemoryfoundin8-bitmicroprocessorstoexecuteefficiently
The16-bit8086and8088provided1Mbyteofmemoryfortheseapplica>ons
13
The80286Microprocessor Eventhe1M-bytememorysystemprovedlimi>ngfordatabasesandotherapplica>ons. Intelintroducedthe80286in1983 anupdated8086
Almostiden>caltothe8086/8088. addressed16M-bytememorysysteminstead
ofa1M-bytesystem
Instruc>onsetalmostiden>calexceptforafewaddi>onalinstruc>ons. managedtheextra15Mbytesofmemory
80286clockspeedincreasedin8.0Mhzversion. executedsomeinstruc>onsinaslileas250ns(4.0MIPs)
Somechangestointernalexecu>onofinstruc>onsledtoeigholdincreaseinspeedformanyinstruc>ons
14
8/4/2019 Microprocessors 1 Basics
8/120
3/3/11
8
The32-BitMicroprocessor Applica>onsdemandedfastermicroprocessorspeeds,more
memory,andwiderdatapaths. Ledtothe80386in1986byIntel.
majoroverhaulof16-bit808680286architecture Intelsfirstprac>calmicroprocessortocontaina32-bitdatabus
and32-bitmemoryaddress. Intelproducedanearlier,unsuccessful32-bitmicroprocessorcalled
iapx-432
Through32-bitbuses,80386addressedupto4Gbytesofmemory. 1Gmemory=1024M,or1,073,741,824loca>ons 1,000,000typewrien,double-spacedpagesofASCIItextdata
80386SXaddressed16Mbytesofmemorythrougha16-bitdataand24-bitaddressbus.
80386SL/80386SLCaddressed32Mbytesmemoryvia16-bitdata,25-bitaddressbus.
80386SLCcontainedaninternalcachetoprocessdataatevenhigherrates.
15
The32-BitMicroprocessorcont. 32-bitmicroprocessorneededduetosizeofitsdatabus.
transfersreal(single-precisionfloa>ng-point)numbersthatrequire32-bit-widememory
Toprocess32-bitrealnumbers,themicroprocessormustefficientlypassthembetweenitselfandmemory. with8-bitdatabus,takesfourreadorwritecycles onlyonereadorwritecycleisrequiredfor32bit
Significantlyincreasesspeedofanyprogramthatmanipulatesrealnumbers.
High-levellanguages,spreadsheets,anddatabasemanagementsystemsuserealnumbersfordatastorage. alsousedingraphicaldesignpackagesthatusevectorstoplotimages
onthevideoscreen
CAD(computer-aideddraing/design)systemsasAUTOCAD,ORCAD 80386hadhigherclockingspeedsandincludedamemory
managementunit. allowedmemoryresourcestobeallocated
andmanagedbytheopera>ngsystem16
8/4/2019 Microprocessors 1 Basics
9/120
3/3/11
9
The80486Microprocessor
In1989Intelreleasedthe8048. Highlyintegratedpackage. 80386-likemicroprocessor. 80387-likenumericcoprocessor. 8K-bytecachememorysystem. Internalstructureof80486modifiedsoabouthalfof
itsinstruc>onsexecutedinoneclockinsteadoftwoclocks. ina50MHzversion,abouthalfofinstruc>onsexecutedin
25ns(50MIPs)
50%over80386operatedatsameclockspeed Double-clocked80486DX2executedinstruc>onsat66
MHz,withmemorytransfersat33MHz. calledadouble-clockedmicroprocessor
17
ThePenumMicroprocessor Introduced1993,Pen>umwassimilarto80386and80486microprocessors. OriginallylabeledtheP5or80586.
Inteldecidednottouseanumberbecauseitappearedtobeimpossibletocopyrightanumber
Introductoryversionsoperatedwithaclockingfrequencyof60MHz&66MHz,andaspeedof110MIPs.
Double-clockedPen>umat120MHzand133MHz,alsoavailable. fastestversionproduced233MHzPen>um
athreeandone-halfclockedversion
Cachesizewasincreasedto16Kbytesfromthe8Kcachefoundin80486.
8K-byteinstruc>oncacheanddatacache. Memorysystemupto4Gbytes. Databuswidthincreasedtoafull64bits. Databustransferspeed60MHzor66MHz.
dependingontheversionofthePen>um18
8/4/2019 Microprocessors 1 Basics
10/120
3/3/11
10
TheInteliCOMP-ra>ngindex
Tocomparespeedsofmicroprocessors,InteldevisedtheiCOMP-ra>ngindex.
compositeofSPEC92,ZDBench,PowerMeter TheiCOMP1ra>ngindexisusedtoratethe
speedofallIntelmicroprocessorsthroughthePen>um.
Figure12showsrela>vespeedsofthe80386DX25MHzversionthroughthePen>um233MHzversion.
19
20
8/4/2019 Microprocessors 1 Basics
11/120
3/3/11
11
TheInteliCOMP2-ra>ngindex.
A\erPen>umII Switchedto
iCOMP2-ra>ng
21
TheMemoryandI/OSystem MemorystructureofallIntel-basedpersonalcomputerssimilar
22
8/4/2019 Microprocessors 1 Basics
12/120
3/3/11
12
Mainmemorysystemdividedintothreeparts: TPA(transientprogramarea) systemarea XMS(extendedmemorysystem)
First1Mbyteofmemoryo\encalledtherealorconven>onalmemorysystem. Intelmicroprocessorsdesignedtofunc>on
inthisareausingrealmodeopera>on
80286throughtheCore2containtheTPA(640Kbytes)andsystemarea(384Kbytes). alsocontainextendedmemory o\encalledATclassmachines
23
TheTPA Thetransientprogramarea(TPA)holdstheDOS
(diskoperangsystem)opera>ngsystem;other
programsthatcontrolthecomputersystem.
InterruptvectorsaccessDOS,BIOS(basicI/Osystem),andapplica>ons.
AreascontaintransientdatatoaccessI/Odevicesandinternalfeaturesofthesystem. thesearestoredintheTPAsotheycanbechangedas
DOSoperates
24
8/4/2019 Microprocessors 1 Basics
13/120
3/3/11
13
TheSystemArea
Thesystemareacontainsprogramsonread-only(ROM)orflashmemory,andareasof
read/write(RAM)memoryfordatastorage.
containsROM/RAMdatafromdevicessuchasthevideodisplayRAM,thevideocontrol
programsonROMorflashmemory,andthe
SystemBIOSROM.
25
I/OSpace I/Odevicesallowthemicroprocessorto
communicatewiththeoutsideworld.
I/O(input/output)spaceinacomputersystemextendsfromI/Oport0000HtoportFFFFH
I/Oportaddressissimilartoamemoryaddressinsteadofmemory,itaddressesanI/Odevice
26
8/4/2019 Microprocessors 1 Basics
14/120
3/3/11
14
TheMicroprocessor CalledtheCPU(centralprocessingunit)
ControlsmemoryandI/Othroughconnec>onscalledbuses. Microprocessorperformsthreemaintasks:
datatransferbetweenitselfandthememoryorI/Osystems simplearithme>candlogicopera>ons programflowviasimpledecisions
Buses Acommongroupofwiresthatinterconnectcomponentsina
computersystem.
Transferaddress,data,&controlinforma>onbetweenmicroprocessor,memoryandI/O.
Threebusesexistforthistransferofinforma>on:address,data,andcontrol.
27
Theblockdiagramofacomputer
system
28
8/4/2019 Microprocessors 1 Basics
15/120
3/3/11
15
Buses
Theaddressbusrequestsamemoryloca>onfromthememoryoranI/Oloca>onfromthe
I/Odevices.
Thedatabustransfersinforma>onbetweenthemicroprocessoranditsmemoryandI/O
addressspace
29
Buses ControlbuslinesselectandcausememoryorI/O
toperformareadorwriteopera>on.
Inmostcomputersystems,therearefourcontrolbusconnec>ons:
MRDC(memoryreadcontrol) MWTC (memorywritecontrol) IORC (I/Oreadcontrol) IOWC (I/Owritecontrol). overbarindicatesthecontrolsignalisac>ve-low;
(ac>vewhenlogiczeroappearsoncontrolline)
30
8/4/2019 Microprocessors 1 Basics
16/120
3/3/11
16
opera>on
Themicroprocessorreadsamemoryloca>onbysendingthememoryanaddressthroughtheaddressbus.
Next,itsendsamemoryreadcontrolsignaltocausethememorytoreaddata.
Datareadfrommemoryarepassedtothemicroprocessorthroughthedatabus.
Wheneveramemorywrite,I/Owrite,orI/Oreadoccurs,thesamesequenceiscarriedout.
31
NUMBERSYSTEMS Useofamicroprocessorrequiresworking
knowledgeofnumberingsystems.
binary,decimal,andhexadecimal Conversionsaredescribed.
decimalandbinarydecimalandhexadecimalbinaryandhexadecimal
32
8/4/2019 Microprocessors 1 Basics
17/120
3/3/11
17
TheNumbersystem
Decimal0-9 Hexadecimal0F Octal0-7 Binary0,1
33
ConversiontoDecimal Toconvertfromanynumberbasetodecimal,
determinetheweightsorvaluesofeachposi>on
ofthenumber.
Sumtheweightstoformthedecimalequivalent.
34
8/4/2019 Microprocessors 1 Basics
18/120
3/3/11
18
ConversionfromDecimal Conversionsfromdecimaltoothernumber
systemsmoredifficulttoaccomplish.
Toconvertthewholenumberpor>onofanumbertodecimal,divideby1radix.
35
WholeNumberConversionfromDecimal
Toconvertadecimalwholenumbertoanothernumbersystem,dividebytheradixandsave
remaindersassignificantdigitsoftheresult.
Analgorithmforthisconversion:dividethedecimalnumberbytheradix
(numberbase)
savetheremainder(firstremainderistheleastsignificantdigit)
repeatsteps1and2un>lthequo>entiszero36
8/4/2019 Microprocessors 1 Basics
19/120
3/3/11
19
Toconvert10decimaltobinary,divideitby2.
theresultis5,witharemainderof0 Firstremainderisunitsposi>onoftheresult.
inthisexample,a0 Next,dividethe5by2;resultis2,witha
remainderof1.
the1isthevalueofthetwos(2 1)posi>on Con>nuedivisionun>lthequo>entisazero.
Theresultiswrienas10102fromtheboomtothetop.
37
ConverngfromaDecimalFracon Conversionisaccomplishedwithmul>plica>onby
theradix.
Wholenumberpor>onofresultissavedasasignificantdigitoftheresult.
frac>onalremainderagainmul>pliedbytheradixwhenthefrac>onremainderiszero,mul>plica>on
ends
Somenumbersarenever-ending(repetend).azeroisneveraremainder
38
8/4/2019 Microprocessors 1 Basics
20/120
3/3/11
20
Algorithmforconversionfromadecimalfrac>on:
mul>plythedecimalfrac>onbytheradix(numberbase).
savethewholenumberpor>onoftheresult(evenifzero)asadigit;firstresultiswrien
immediatelytotherightoftheradixpoint
repeatsteps1and2,usingthefrac>onalpartofstep2un>lthefrac>onalpartofstep2iszero
Sametechniqueconvertsadecimalfrac>onintoanynumberbase.
39
Binary-CodedHexadecimal Binary-codedhexadecimal(BCH)isahexadecimal
numberwrieneachdigitisrepresentedbya4-
bitbinarynumber.
BCHcodeallowsabinaryversionofahexadecimalnumbertobewrieninaformeasilyconverted
betweenBCHandhexadecimal.
Hexadecimalrepresentedbyconver>ngdigitstoBCHcodewithaspacebetweeneachdigit.
40
8/4/2019 Microprocessors 1 Basics
21/120
3/3/11
21
Complements At>mes,dataarestoredincomplementformto
representnega>venumbers.
Twosystemsusedtorepresentnega>vedata:radixradix-1complement(earliest)
41
14COMPUTERDATAFORMATS Successfulprogrammingrequiresaprecise
understandingofdataformats.
Commonly,dataappearasASCII,Unicode,BCD,signedandunsignedintegers,andfloa>ng-point
numbers(realnumbers).
Otherformsareavailablebutarenotcommonlyfound.
42
8/4/2019 Microprocessors 1 Basics
22/120
3/3/11
22
ASCIIandUnicodeData ASCII(AmericanStandardCodeforInformaonInterchange)datarepresentalphanumeric
charactersincomputermemory.
StandardASCIIcodeisa7-bitcode.eighthandmostsignificantbitusedtoholdparity
Ifusedwithaprinter,mostsignificantbitsare0foralphanumericprin>ng;1forgraphics.
InPC,anextendedASCIIcharactersetisselectedbyplacing1inthele\mostbit.
43
ManyWindows-basedapplica>onsusetheUnicodesystemtostorealphanumericdata.storeseachcharacteras16-bitdata
Codes0000H00FFHarethesameasstandardASCIIcode.
Remainingcodes,0100HFFFFH,storeallspecialcharactersfrommanycharactersets.
Allowsso\wareforWindowstobeusedinmanycountriesaroundtheworld.
Forcompleteinforma>ononUnicode,visit:h+p://www.unicode.org
44
8/4/2019 Microprocessors 1 Basics
23/120
3/3/11
23
BCD(Binary-CodedDecimal)Data TherangeofaBCDdigitextendsfrom00002to
10012,or09decimal,storedintwoforms:
Storedinpackedform:packedBCDdatastoredastwodigitsperbyte;usedforBCDaddi>onandsubtrac>oninthe
instruc>onsetofthemicroprocessor
Storedinunpackedform:unpackedBCDdatastoredasonedigitperbytereturnedfromakeypadorkeyboard
45
Byte-SizedData Storedasunsignedandsignedintegers. Differenceintheseformsistheweightofthe
le\mostbitposi>on.
value128fortheunsignedintegerminus128forthesignedinteger
Insignedintegerformat,thele\mostbitrepresentsthesignbitofthenumber.
alsoaweightofminus128
46
8/4/2019 Microprocessors 1 Basics
24/120
3/3/11
24
Figure114Theunsignedandsignedbytesillustra>ngtheweightsofeachbinary-bitposi>on.
47
Unsignedintegersrange00HtoFFH(0255) Signedintegersfrom-128to0to+127. Nega>vesignednumbersrepresentedinthisway
arestoredinthetwoscomplementform.
Evalua>ngasignednumberbyusingweightsofeachbitposi>onismucheasierthantheactof
twoscomplemen>nganumbertofinditsvalue.
especiallytrueintheworldofcalculatorsdesignedforprogrammers
48
8/4/2019 Microprocessors 1 Basics
25/120
3/3/11
25
Word-SizedData Aword(16-bits)isformedwithtwobytesofdata. Theleastsignificantbytealwaysstoredinthe
lowest-numberedmemoryloca>on.
Mostsignificantbyteisstoredinthehighest. Thismethodofstoringanumberiscalledthelileendianformat.
49
50
8/4/2019 Microprocessors 1 Basics
26/120
3/3/11
26
Alternatemethodiscalledthebigendianformat. Numbersarestoredwiththelowestloca>oncontainingthemostsignificantdata. NotusedwithIntelmicroprocessors. ThebigendianformatisusedwiththeMotorola
familyofmicroprocessors.
51
Doubleword-sizeddatarequiresfourbytesofmemorybecauseitisa32-bitnumber.
appearsasaproducta\eramul>plica>onalsoasadividendbeforeadivision
Defineusingtheassemblerdirec>vedefinedoubleword(s),orDD.
alsousetheDWORDdirec>veinplaceofDD
Doubleword-SizedData
52
8/4/2019 Microprocessors 1 Basics
27/120
3/3/11
27
RealNumbers Sincemanyhigh-levellanguagesuseIntelmicroprocessors,realnumbersareo\en
encountered.
Areal,orafloang-pointnumbercontainstwoparts:
aman>ssa,significand,orfrac>onanexponent.
A4-bytenumberiscalledsingle-precision. The8-byteformiscalleddouble-precision.
53
54
8/4/2019 Microprocessors 1 Basics
28/120
3/3/11
28
Theassemblercanbeusedtodefinerealnumbersinsingle-&double-precisionforms:usetheDDdirec>veforsingle-precision32-bit
numbers
usedefinequadword(s),orDQtodefine64-bitdouble-precisionrealnumbers
Op>onaldirec>vesareREAL4,REAL8,andREAL10.
fordefiningsingle-,double-,andextendedprecisionrealnumbers
55
21INTERNALMICROPROCESSOR
ARCHITECTURE
Beforeaprogramiswrienorinstruc>oninves>gated,internalconfigura>onofthe
microprocessormustbeknown.
8086throughCore2consideredprogramvisible. registersareusedduringprogrammingandarespecifiedby
theinstruc>ons
Otherregistersconsideredtobeprograminvisible. notaddressabledirectlyduringapplica>onsprogramming
56
8/4/2019 Microprocessors 1 Basics
29/120
3/3/11
29
57
MulpurposeRegisters RAX-a64-bitregister(RAX),a32-bitregister(accumulator)(EAX),a16-bitregister(AX),oraseitheroftwo8-bitregisters(AHandAL).
Theaccumulatorisusedforinstruc>onssuchasmul>plica>on,division,andsomeoftheadjustmentinstruc>ons.
Intelplanstoexpandtheaddressbusto52bitstoaddress4P(peta)bytesofmemory.
58
8/4/2019 Microprocessors 1 Basics
30/120
3/3/11
30
RBX,addressableasRBX,EBX,BX,BH,BL.
BXregister(baseindex)some>mesholdsoffsetaddressofaloca>oninthememorysysteminall
versionsofthemicroprocessor
RCX,asRCX,ECX,CX,CH,orCL.a(count)general-purposeregisterthatalsoholdsthe
countforvariousinstruc>ons
RDX,asRDX,EDX,DX,DH,orDL.a(data)general-purposeregisterholdsapartoftheresultfromamul>plica>onorpartofdividendbeforeadivision
59
RBP,asRBP,EBP,orBP.pointstoamemory(basepointer)loca>on
formemorydatatransfers RDIaddressableasRDI,EDI,orDI.
o\enaddresses(desnaonindex)stringdes>na>ondataforthestringinstruc>ons
RSIusedasRSI,ESI,orSI.the(sourceindex)registeraddressessourcestring
dataforthestringinstruc>ons
likeRDI,RSIalsofunc>onsasageneral-purposeregister
60
8/4/2019 Microprocessors 1 Basics
31/120
3/3/11
31
R8-R15foundinthePen>um4andCore2if64-bitextensionsareenabled.dataareaddressedas64-,32-,16-,or8-bit
sizesandareofgeneralpurpose
Mostapplica>onswillnotusetheseregistersun>l64-bitprocessorsarecommon.
the8-bitpor>onistherightmost8-bitonlybits8to15arenotdirectlyaddressableas
abyte
61
Special-PurposeRegisters IncludeRIP,RSP,andRFLAGS
segmentregistersincludeCS,DS,ES,SS,FS,andGS RIPaddressesthenextinstruc>oninasec>onof
memory.
definedas(instruconpointer)acodesegment RSPaddressesanareaofmemorycalled
thestack.
the(stackpointer)storesdatathroughthispointer
62
8/4/2019 Microprocessors 1 Basics
32/120
3/3/11
32
RFLAGSindicatethecondi>onofthemicroprocessorandcontrolitsopera>on.
Figure22showstheflagregistersofallversionsofthemicroprocessor.
Flagsareupward-compa>blefromthe8086/8088throughCore2.
Therightmostfiveandtheoverflowflagarechangedbymostarithme>candlogicopera>ons.
althoughdatatransfersdonotaffectthem
63
Figure22TheEFLAGandFLAGregistercountsfortheen>re8086andPen>um
microprocessorfamily.
Flagsneverchangeforanydatatransferorprogramcontrolopera>on.
Someoftheflagsarealsousedtocontrolfeaturesfoundinthemicroprocessor.
64
8/4/2019 Microprocessors 1 Basics
33/120
3/3/11
33
Flagbits,withabriefdescrip>onoffunc>on. C(carry)holdsthecarrya\eraddi>onorborrowa\ersubtrac>on.
alsoindicateserrorcondi>ons P(parity)isthecountofonesinanumber
expressedasevenorodd.Logic0foroddparity;
logic1forevenparity.
ifanumbercontainsthreebinaryonebits,ithasoddparity
ifanumbercontainsnoonebits,ithasevenparity
65
ListofEachFlagbit,withabrief
descrip>onoffunc>on.
C(carry)holdsthecarrya\eraddi>onorborrowa\ersubtrac>on.
alsoindicateserrorcondi>ons P(parity)isthecountofonesinanumber
expressedasevenorodd.Logic0foroddparity;
logic1forevenparity.
ifanumbercontainsthreebinaryonebits,ithasoddparity;Ifanumbercontainsnoonebits,it
hasevenparity
66
8/4/2019 Microprocessors 1 Basics
34/120
3/3/11
34
A(auxiliarycarry)holdsthecarry(half-carry)a\eraddi>onortheborrowa\ersubtrac>onbetweenbitposi>ons3and4oftheresult.
Z(zero) showsthattheresultofanarithme>corlogicopera>oniszero.
S(sign)flagholdsthearithme>csignoftheresulta\eranarithme>corlogicinstruc>onexecutes.
T(trap) Thetrapflagenablestrappingthroughanon-chipdebuggingfeature.
67
I(interrupt)controlsopera>onoftheINTR(interruptrequest)inputpin.
D(direcon) selectsincrementordecrementmodefortheDIand/orSIregisters.
O(overflow) occurswhensignednumbersareaddedorsubtracted.
anoverflowindicatestheresulthasexceededthecapacityofthemachine
68
8/4/2019 Microprocessors 1 Basics
35/120
3/3/11
35
IOPLusedinprotectedmodeopera>ontoselecttheprivilegelevelforI/Odevices.
NT(nestedtask)flagindicatesthecurrenttaskisnestedwithinanothertaskinprotectedmode
opera>on.
RF(resume) usedwithdebuggingtocontrolresump>onofexecu>ona\erthenext
instruc>on.
VM(virtualmode)flagbitselectsvirtualmodeopera>oninaprotectedmodesystem.
69
AC,(alignmentcheck)flagbitac>vatesifawordordoublewordisaddressedonanon-wordornon-doublewordboundary.
VIFisacopyoftheinterruptflagbitavailabletothePen>um4(virtualinterrupt)
VIP(virtual)providesinforma>onaboutavirtualmodeinterruptfor(interruptpending)Pen>um.
usedinmul>taskingenvironmentstoprovidevirtualinterruptflags
70
8/4/2019 Microprocessors 1 Basics
36/120
3/3/11
36
ID(idenficaon) flagindicatesthatthePen>ummicroprocessorssupporttheCPUIDinstruc>on.
CPUIDinstruc>onprovidesthesystemwithinforma>onaboutthePen>ummicroprocessor
71
SegmentRegisters Generatememoryaddresseswhencombinedwith
otherregistersinthemicroprocessor.
Fourorsixsegmentregistersinvariousversionsofthemicroprocessor.
Asegmentregisterfunc>onsdifferentlyinrealmodethaninprotectedmode.
Followingisalistofeachsegmentregister,alongwithitsfunc>oninthesystem.
72
8/4/2019 Microprocessors 1 Basics
37/120
3/3/11
37
CS(code)segmentholdscode(programsandprocedures)usedbythemicroprocessor.
DS(data)containsmostdatausedbyaprogram.Dataareaccessedbyanoffsetaddressorcontentsof
otherregistersthatholdtheoffsetaddress
ES(extra)anaddi>onaldatasegmentusedbysomeinstruc>onstoholddes>na>ondata.
73
22REALMODEMEMORYADDRESSING
80286andaboveoperateineithertherealorprotectedmode.
Realmodeoperaonallowsaddressingofonlythefirst1MbyteofmemoryspaceeveninPen>um4
orCore2microprocessor.
thefirst1Mbyteofmemoryiscalledthe realmemory,convenonalmemory ,orDOSmemorysystem
8/4/2019 Microprocessors 1 Basics
38/120
3/3/11
38
SegmentsandOffsets Allrealmodememoryaddressesmustconsistofa
segmentaddressplusanoffsetaddress.
segmentaddressdefinesthebeginningaddressofany64K-bytememorysegment
offsetaddressselectsanyloca>onwithinthe64Kbytememorysegment
Figure23showshowthesegmentplusoffsetaddressingschemeselectsamemoryloca>on.
Figure23Therealmodememory-addressingscheme,usingasegmentaddressplusan
offset.
thisshowsamemorysegmentbeginningat
10000H,endingat
loca>on1FFFFH 64Kbytesinlength
alsoshowshowanoffsetaddress,calleda
displacement,ofF000H
selectsloca>on
1F000Hinthememory
8/4/2019 Microprocessors 1 Basics
39/120
3/3/11
39
Oncethebeginningaddressisknown,theendingaddressisfoundbyaddingFFFFH.becausearealmodesegmentofmemoryis64Kin
length
Theoffsetaddressisalwaysaddedtothesegmentstar>ngaddresstolocatethedata.
Segmentandoffsetaddressissome>meswrienas1000:2000.
asegmentaddressof1000H;anoffsetof2000H
DefaultSegmentandOffsetRegisters
Themicroprocessorhasrulesthatapplytosegmentswhenevermemoryisaddressed.
thesedefinethesegmentandoffsetregistercombina>on
Thecodesegmentregisterdefinesthestartofthecodesegment.
Theinstruconpointerlocatesthenextinstruc>onwithinthecodesegment.
8/4/2019 Microprocessors 1 Basics
40/120
3/3/11
40
Anotherofthedefaultcombina>onsisthestack.
stackdataarereferencedthroughthestacksegmentatthememoryloca>onaddressedbyeitherthestack
pointer(SP/ESP)orthepointer(BP/EBP)
Figure24showsasystemthatcontainsfourmemorysegments.
amemorysegmentcantouchoroverlapif64Kbytesofmemoryarenotrequiredforasegment
Figure24Amemorysystemshowingtheplacementoffourmemorysegments.
thinkofsegmentsaswindowsthatcanbe
movedoveranyarea
ofmemorytoaccessdata
orcode
aprogramcanhavemorethanfourorsixsegments,
butonlyaccessfourorsixsegmentsata>me
8/4/2019 Microprocessors 1 Basics
41/120
3/3/11
41
Figure25Anapplica>onprogramcontainingacode,data,andstacksegmentloadedintoaDOSsystemmemory.
aprogramplacedinmemorybyDOSisloadedintheTPAatthefirstavailableareaof
memoryabovedriversand
otherTPAprograms
areaisindicatedbyafree-pointermaintainedbyDOS
programloadingishandledautoma>callybytheprogram
loaderwithinDOS
SegmentandOffsetAddressingScheme
AllowsRelocaon
SegmentplusoffsetaddressingallowsDOSprogramstoberelocatedinmemory.
Arelocatableprogramisonethatcanbeplacedintoanyareaofmemoryandexecutedwithout
change.
Relocatabledataaredatathatcanbeplacedinanyareaofmemoryandusedwithoutanychangetotheprogram.
8/4/2019 Microprocessors 1 Basics
42/120
3/3/11
42
Becausememoryisaddressedwithinasegmentbyanoffsetaddress,thememorysegmentcanbemovedtoanyplaceinthememorysystem
withoutchanginganyoftheoffsetaddresses.
Onlythecontentsofthesegmentregistermustbechangedtoaddresstheprogram
inthenewareaofmemory.
Windowsprogramsarewrienassumingthatthefirst2Gofmemoryareavailableforcodeand
data.
24MEMORYPAGING
Thememorypagingmechanismallowsanyphysicalmemoryloca>ontobeassignedtoanylinearaddress.
Iinearaddressisdefinedastheaddressgeneratedbyaprogram.
Physicaladdressistheactualmemoryloca>onaccessedbyaprogram.
Withmemorypaging,thelinearaddressisinvisiblytranslatedtoanyphysicaladdress.
8/4/2019 Microprocessors 1 Basics
43/120
3/3/11
43
PagingRegisters Thepagingunitiscontrolledbythecontents
ofthemicroprocessorscontrolregisters.
BeginningwithPen>um,anaddi>onalcontrolregisterlabeledCR4controlsextensionstothe
basicarchitecture.
SeeFigure211forthecontentsofcontrolregistersCR0throughCR4.
Figure211Thecontrolregisterstructureofthemicroprocessor.
8/4/2019 Microprocessors 1 Basics
44/120
3/3/11
44
Thelinearaddress,asgeneratedbyso\ware,isbrokenintothreesec>onsthatareusedtoaccessthepagedirectoryentry,pagetableentry,and
memorypageoffsetaddress.
Figure212showsthelinearaddressanditsmakeupforpaging.
Whentheprogramaccessesaloca>onbetween00000000Hand00000FFFH,themicroprocessor
physicallyaddressesloca>on00100000H
00100FFFH.
Figure212Theformatforthelinearaddress(a)andapagedirectoryorpagetableentry(b).#
8/4/2019 Microprocessors 1 Basics
45/120
3/3/11
45
IntelhasincorporatedaspecialtypeofcachecalledTLB(translaonlook-asidebuffer).
becauserepaginga4K-bytesec>onofmemoryrequiresaccesstothepagedirectoryandapagetable,bothlocatedinmemory
The80486cacheholdsthe32mostrecentpagetransla>onaddresses.
ifthesameareaofmemoryisaccessed,theaddressisalreadypresentintheTLB
Thisspeedsprogramexecu>on Pen>umcontainsseparateTLBsforeachoftheirinstruc>onanddatacaches.
ThePageDirectoryandPageTable Onlyonepagedirectoryinthesystem. Thepagedirectorycontains1024doubleword
addressesthatlocateupto1024pagetables.
Pagedirectoryandeachpagetableare4Kbytesinlength.
Figure213showsthepagedirectory,afewpagetables,andsomememorypages.
8/4/2019 Microprocessors 1 Basics
46/120
3/3/11
46
Figure213Thepagingmechanisminthe80386throughCore2microprocessors.#
25FlatModeMemory Aflatmodememorysystemisoneinwhichthere
isnosegmenta>on.
doesnotuseasegmentregistertoaddressaloca>oninthememory
Firstbyteaddressisat0000000000H;thelastloca>onisatFFFFFFFFFFH.
addressis40-bits Thesegmentregisters>llselectstheprivilegelevel
oftheso\ware.
8/4/2019 Microprocessors 1 Basics
47/120
3/3/11
47
Figure215The64-bitflatmodememorymodel.
31DATAADDRESSINGMODES
MOVinstruc>onisacommonandflexibleinstruc>on. providesabasisforexplana>onofdata-addressingmodes
Figure31illustratestheMOVinstruc>onanddefinesthedirec>onofdataflow.
Sourceistotherightanddesnaonthele\,nexttotheopcodeMOV.
anopcode,oropera>oncode,tellsthemicroprocessorwhichopera>ontoperform
8/4/2019 Microprocessors 1 Basics
48/120
3/3/11
48
Figure31TheMOVinstruc>onshowingthesource,des>na>on,anddirec>onofdataflow.
Figure32showsallpossiblevaria>onsofthedata-addressingmodesusingMOV.
Thesedata-addressingmodesarefoundwithallversionsoftheIntelmicroprocessor.
exceptforthescaled-index-addressingmode,foundonlyin80386throughCore2
RIPrela>veaddressingmodeisnotillustrated.onlyavailableonthePen>um4andCore2
inthe64-bitmode
8/4/2019 Microprocessors 1 Basics
49/120
3/3/11
49
Figure328086Core2data-addressingmodes.
RegisterAddressing Themostcommonformofdataaddressing.
onceregisternameslearned,easiesttoapply. Themicroprocessorcontainsthese8-bitregister
namesusedwithregisteraddressing:AH,AL,BH,
BL,CH,CL,DH,andDL.
16-bitregisternames:AX,BX,CX,DX,SP,BP,SI,andDI.
8/4/2019 Microprocessors 1 Basics
50/120
3/3/11
50
In80386&above,extended32-bitregisternamesare:EAX,EBX,ECX,EDX,ESP,EBP,EDI,andESI.
64-bitmoderegisternamesare:RAX,RBX,RCX,RDX,RSP,RBP,RDI,RSI,andR8throughR15.
Importantforinstruc>onstouseregistersthatarethesamesize.
nevermixan8-bitwitha16-bitregister,an8-ora16-bitregisterwitha32-bitregister
thisisnotallowedbythemicroprocessorandresultsinanerrorwhenassembled
Figure33Theeffectofexecu>ngtheMOVBX,CXinstruc>onatthepointjustbeforetheBXregisterchanges.Notethatonlytherightmost16bitsofregisterEBXchange.
8/4/2019 Microprocessors 1 Basics
51/120
3/3/11
51
Figure33showstheopera>onoftheMOVBX,CXinstruc>on.
Thesourceregisterscontentsdonotchange.thedes>na>onregisterscontentsdochange
Thecontentsofthedes>na>onregisterordes>na>onmemoryloca>onchangeforall
instruc>onsexcepttheCMPandTEST
instruc>ons.
TheMOVBX,CXinstruc>ondoesnotaffectthele\most16bitsofregisterEBX.
ImmediateAddressing Termimmediateimpliesthatdataimmediately
followthehexadecimalopcodeinthememory.
immediatedataareconstantdatadatatransferredfromaregisterormemoryloca>on
arevariabledata
Immediateaddressingoperatesuponabyteorwordofdata.
Figure34showstheopera>onofaMOVEAX,13456Hinstruc>on.
8/4/2019 Microprocessors 1 Basics
52/120
3/3/11
52
Figure34Theopera>onoftheMOVEAX,3456Hinstruc>on.Thisinstruc>oncopiestheimmediatedata(13456H)intoEAX.
AswiththeMOVinstruc>onillustratedinFigure33,thesourcedataoverwritesthedes>na>ondata.
Insymbolicassemblylanguage,thesymbol#precedesimmediatedatainsomeassemblers.MOVAX,#3456Hinstruc>onisanexample
Mostassemblersdonotusethe#symbol,butrepresentimmediatedataasintheMOVAX,
3456Hinstruc>on.
anolderassemblerusedwithsomeHewle-Packardlogicdevelopmentdoes,asmayothers
inthistext,the#isnotusedforimmediatedata
8/4/2019 Microprocessors 1 Basics
53/120
3/3/11
53
Thesymbolicassemblerportraysimmediatedatainmanyways.
TheleerHappendshexadecimaldata. Ifhexadecimaldatabeginwithaleer,the
assemblerrequiresthedatastartwitha0.
torepresentahexadecimalF2,0F2Hisusedinassemblylanguage
Decimaldataarerepresentedasisandrequirenospecialcodesoradjustments.
anexampleisthe100decimalintheMOVAL,100instruc>on
AnASCII-codedcharacterorcharactersmaybedepictedintheimmediateformiftheASCIIdataareenclosedinapostrophes.
becarefultousetheapostrophe()forASCIIdata
BinarydataarerepresentedifthebinarynumberisfollowedbytheleerB.
insomeassemblers,theleerY
8/4/2019 Microprocessors 1 Basics
54/120
3/3/11
54
Eachstatementinanassemblylanguageprogramconsistsoffourpartsorfields.
Thele\mostfieldiscalledthelabel.usedtostoreasymbolicnameforthememory
loca>onitrepresents
Alllabelsmustbeginwithaleeroroneofthefollowingspecialcharacters:@,$,-,or?.
alabelmayanylengthfrom1to35characters Thelabelappearsinaprogramtoiden>fythe
nameofamemoryloca>onforstoringdataandforotherpurposes.
Thenextfieldtotherightistheopcodefield.designedtoholdtheinstruc>on,oropcodetheMOVpartofthemovedatainstruc>onisan
exampleofanopcode
Rightoftheopcodefieldistheoperandfield.containsinforma>onusedbytheopcodetheMOVAL,BLinstruc>onhastheopcodeMOVand
operandsALandBL
Thecommentfield,thefinalfield,containsacommentabouttheinstruc>on(s).
commentsalwaysbeginwithasemicolon(;)
8/4/2019 Microprocessors 1 Basics
55/120
3/3/11
55
DirectDataAddressing Appliedtomanyinstruc>onsinatypicalprogram. Twobasicformsofdirectdataaddressing:
directaddressing,whichappliestoaMOVbetweenamemoryloca>onandAL,AX,orEAX
displacementaddressing,whichappliestoalmostanyinstruc>onintheinstruc>onset
Addressisformedbyaddingthedisplacementtothedefaultdatasegmentaddressoranalternate
segmentaddress.
DirectAddressing DirectaddressingwithaMOVinstruc>ontransfers
databetweenamemoryloca>on,locatedwithin
thedatasegment,andtheAL(8-bit),AX(16-bit),
orEAX(32-bit)register.
usuallya3-bytelonginstruc>on MOVAL,DATAloadsALfromthedatasegment
memoryloca>onDATA(1234H).
DATAisasymbolicmemoryloca>on,while1234Histheactualhexadecimalloca>on
8/4/2019 Microprocessors 1 Basics
56/120
3/3/11
56
Figure35Theopera>onoftheMOVAL,[1234H]instruc>onwhenDS=1000H.
Thisinstruc>ontransfersacopycontentsofmemoryloca>on11234HintoAL.
theeffec>veaddressisformedbyadding1234H(theoffsetaddress)and10000H(thedatasegmentaddressof1000H>mes
10H)inasystemopera>ngintherealmode
DisplacementAddressing Almostiden>caltodirectaddressing,exceptthe
instruc>onis4byteswideinsteadof3.
In80386throughPen>um4,thisinstruc>oncanbeupto7byteswideifa32-bitregisteranda32-
bitdisplacementarespecified.
Thistypeofdirectdataaddressingismuchmoreflexiblebecausemostinstruc>onsuseit.
8/4/2019 Microprocessors 1 Basics
57/120
3/3/11
57
RegisterIndirectAddressing Allowsdatatobeaddressedatanymemory
loca>onthroughanoffsetaddressheldinanyof
thefollowingregisters:BP,BX,DI,andSI.
Inaddi>on,80386andaboveallowregisterindirectaddressingwithanyextendedregister
exceptESP.
Inthe64-bitmode,thesegmentregistersservenopurposeinaddressingaloca>on
intheflatmodel.
Figure36Theopera>onoftheMOVAX,[BX]instruc>onwhenBX=1000HandDS=0100H.Notethatthisinstruc>onisshowna\erthecontentsofmemoryaretransferredtoAX.#
8/4/2019 Microprocessors 1 Basics
58/120
3/3/11
58
ThedatasegmentisusedbydefaultwithregisterindirectaddressingoranyothermodethatusesBX,DI,orSItoaddressmemory.
IftheBPregisteraddressesmemory,thestacksegmentisusedbydefault.
thesesengsareconsideredthedefaultforthesefourindexandbaseregisters
Forthe80386andabove,EBPaddressesmemoryinthestacksegmentbydefault.
EAX,EBX,ECX,EDX,EDI,andESIaddressmemoryinthedatasegmentbyfault.
Whenusinga32-bitregistertoaddressmemoryintherealmode,contentsoftheregistermustneverexceed0000FFFFH.
Inthe64-bitmode,segmentregistersarenotusedinaddresscalcula>on;theregister
containstheactuallinearmemoryaddress.
8/4/2019 Microprocessors 1 Basics
59/120
3/3/11
59
Insomecases,indirectaddressingrequiresspecifyingthesizeofthedatabythespecialassemblerdirecveBYTEPTR,WORDPTR,
DWORDPTR,orQWORDPTR.
thesedirec>vesindicatethesizeofthememorydataaddressedbythememorypointer(PTR)
Thedirec>vesarewithinstruc>onsthataddressamemoryloca>onthrougha
pointerorindexregisterwithimmediatedata.
WithSIMDinstruc>ons,theoctalOWORDPTR,representsa128-bit-widenumber.
Indirectaddressingo\enallowsaprogramtorefertotabulardatalocatedinmemory.
Figure37showsthetableandtheBXregisterusedtosequen>allyaddresseachloca>oninthe
table.
Toaccomplishthistask,loadthestar>ngloca>onofthetableintotheBXregister
withaMOVimmediateinstruc>on.
A\erini>alizingthestar>ngaddressofthetable,useregisterindirectaddressingto
storethe50samplessequen>ally.
8/4/2019 Microprocessors 1 Basics
60/120
3/3/11
60
Figure37Anarray(TABLE)containing50bytesthatareindirectlyaddressedthroughregisterBX.
Base-Plus-IndexAddressing Similartoindirectaddressingbecauseitindirectly
addressesmemorydata.
Thebaseregistero\enholdsthebeginningloca>onofamemoryarray.
theindexregisterholdstherela>veposi>onofanelementinthearray
wheneverBPaddressesmemorydata,boththestacksegmentregisterandBPgeneratetheeffec>ve
address
8/4/2019 Microprocessors 1 Basics
61/120
3/3/11
61
LocangDatawithBase-Plus-Index
Addressing
Figure38showshowdataareaddressedbytheMOVDX,[BX+DI]instruc>onwhenthe
microprocessoroperatesintherealmode.
TheIntelassemblerrequiresthisaddressingmodeappearas[BX][DI]insteadof[BX+DI].
TheMOVDX,[BX+DI]instruc>onisMOVDX,[BX][DI]foraprogramwrienfortheIntelASMassembler.
Figure38Anexampleshowinghowthebase-plus-indexaddressingmodefunc>onsfortheMOVDX,[BX
+DI]instruc>on.No>cethatmemoryaddress02010HisaccessedbecauseDS=0100H,BX=100Hand
DI=0010H.
#
8/4/2019 Microprocessors 1 Basics
62/120
3/3/11
62
LocangArrayDataUsingBase-Plus-Index
Addressing
Amajoruseistoaddresselementsinamemoryarray.
Toaccomplishthis,loadtheBXregister(base)withthebeginningaddressofthearrayandtheDI
register(index)withtheelementnumbertobe
accessed.
Anexampleofthebase-plus-indexaddressingmode.Hereanelement(DI)ofanARRAY(BX)isaddressed.
8/4/2019 Microprocessors 1 Basics
63/120
3/3/11
63
RegisterRelaveAddressing Similartobase-plus-indexaddressingand
displacementaddressing.
datainasegmentofmemoryareaddressedbyaddingthedisplacementtothecontentsofabaseoranindex
register(BP,BX,DI,orSI)
Figure310showstheopera>onoftheMOVAX,[BX+1000H]instruc>on.
Arealmodesegmentis64Kbyteslong.
Figure310Theopera>onoftheMOVAX,[BX+1000H]instructon,whenBX=1000HandDS=0200H.#
8/4/2019 Microprocessors 1 Basics
64/120
3/3/11
64
AddressingArrayDatawithRegisterRelave
Itispossibletoaddressarraydatawithregisterrela>veaddressing.
suchaswithbase-plus-indexaddressing InFigure311,registerrela>veaddressingis
illustratedwiththesameexampleasforbase-
plus-indexaddressing.
thisshowshowthedisplacementARRAYaddstoindexregisterDItogenerateareferencetoanarrayelement
Figure311Registerrela>veaddressingusedtoaddressanelementofARRAY.ThedisplacementaddressesthestartofARRAY,andDIaccessesanelement.
8/4/2019 Microprocessors 1 Basics
65/120
3/3/11
65
BaseRelave-Plus-IndexAddressing
Similartobase-plus-indexaddressing.addsadisplacementusesabaseregisterandanindexregisterto
formthememoryaddress
Thistypeofaddressingmodeo\enaddressesatwo-dimensionalarrayofmemorydata.
AddressingDatawithBaseRelave-Plus-
Index
Least-usedaddressingmode. Figure312showshowdataarereferencedifthe
instruc>onexecutedbythemicroprocessoris
MOVAX,[BX+SI+100H].
displacementof100HaddstoBXandSItoformtheoffsetaddresswithinthedatasegment
Thisaddressingmodeistoocomplexforfrequentuseinprogramming.
8/4/2019 Microprocessors 1 Basics
66/120
3/3/11
66
Figure312Anexampleofbaserela>ve-plus-indexaddressingusingaMOVAX,[BX+SI+1000H]instruc>on.Note:DS=1000H#
AddressingArrayswithBaseRelave-Plus-
Index
Supposeafileofmanyrecordsexistsinmemory,eachrecordwithmanyelements.
displacementaddressesthefile,baseregisteraddressesarecord,theindexregisteraddressesan
elementofarecord
Figure313illustratesthisverycomplexformofaddressing.
8/4/2019 Microprocessors 1 Basics
67/120
3/3/11
67
Figure313Baserela>ve-plus-indexaddressingusedtoaccessaFILEthatcontainsmul>plerecords(REC).
DataStructures Usedtospecifyhowinforma>onisstoredina
memoryarray.
atemplatefordata Thestartofastructureisiden>fiedwiththe
STRUCassemblylanguagedirec>veandtheend
withtheENDSstatement.
8/4/2019 Microprocessors 1 Basics
68/120
3/3/11
68
32PROGRAMMEMORY-ADDRESSING
MODES
UsedwiththeJMP(jump)andCALLinstruc>ons. Consistofthreedis>nctforms:
direct,rela>ve,andindirect
DirectProgramMemoryAddressing Usedforalljumpsandcallsbyearly
microprocessor;alsousedinhigh-levellanguages,
suchasBASIC.
GOTOandGOSUBinstruc>ons Themicroprocessorusesthisform,butnotas
o\enasrela>veandindirectprogrammemory
addressing.
Theinstruc>onsfordirectprogrammemoryaddressingstoretheaddresswiththeopcode.
8/4/2019 Microprocessors 1 Basics
69/120
3/3/11
69
Figure314The5-bytemachinelanguageversionofaJMP[10000H]instruc>on.
ThisJMPinstruc>onloadsCSwith1000HandIPwith0000Htojumptomemoryloca>on10000Hforthenextinstruc>on.
anintersegmentjumpisajumptoanymemoryloca>onwithintheen>rememorysystem
O\encalledafarjumpbecauseitcanjumptoanymemoryloca>onforthenextinstruc>on.
inrealmode,anyloca>onwithinthefirst1MbyteInprotectedmodeopera>on,thefarjumpcanjump
toanyloca>oninthe4G-byteaddressrangeinthe80386-Core2microprocessors
8/4/2019 Microprocessors 1 Basics
70/120
3/3/11
70
Theonlyotherinstruc>onusingdirectprogramaddressingistheintersegmentorfarCALLinstruc>on.
Usually,thenameofamemoryaddress,calledalabel,referstotheloca>onthatiscalledor
jumpedtoinsteadoftheactualnumericaddress.
WhenusingalabelwiththeCALLorJMPinstruc>on,mostassemblersselectthebestform
ofprogramaddressing.
RelaveProgramMemoryAddressing
Notavailableinallearlymicroprocessors,butitisavailabletothisfamilyofmicroprocessors.
ThetermrelaGvemeansrela>vetotheinstruc>onpointer(IP).
TheJMPinstruc>onisa1-byteinstruc>on,witha1-byteora2-bytedisplacementthataddstothe
instruc>onpointer.
AnexampleisshowninFigure315.
8/4/2019 Microprocessors 1 Basics
71/120
3/3/11
71
Figure315AJMP[2]instruc>on.Thisinstruc>onskipsoverthe2bytesofmemorythatfollowtheJMPinstruc>on.
IndirectProgramMemoryAddressing
ThemicroprocessorallowsseveralformsofprogramindirectmemoryaddressingfortheJMP
andCALLinstruc>ons.
In80386andabove,anextendedregistercanbeusedtoholdtheaddressorindirectaddressofa
rela>veJMPorCALL.
forexample,theJMPEAXjumpstotheloca>onaddressbyregisterEAX
8/4/2019 Microprocessors 1 Basics
72/120
3/3/11
72
Ifarela>veregisterholdstheaddress,thejumpisconsideredtobeanindirectjump.
Forexample,JMP[BX]referstothememoryloca>onwithinthedatasegmentattheoffset
addresscontainedinBX.
atthisoffsetaddressisa16-bitnumberusedastheoffsetaddressintheintrasegmentjump
thistypeofjumpissome>mescalledan indirect-indirectordouble-indirectjump
Figure316showsajumptablethatisstored,beginningatmemoryloca>onTABLE.
Figure316Ajumptablethatstoresaddressesofvariousprograms.TheexactaddresschosenfromtheTABLEisdeterminedbyanindexstoredwiththejumpinstruc>on.
8/4/2019 Microprocessors 1 Basics
73/120
3/3/11
73
33STACKMEMORY-ADDRESSINGMODES
Thestackplaysanimportantroleinallmicroprocessors.
holdsdatatemporarilyandstoresreturnaddressesusedbyprocedures
StackmemoryisLIFO(last-in,first-out)memorydescribesthewaydataarestoredandremovedfrom
thestack
DataareplacedonthestackwithaPUSHinstrucon;removedwithaPOPinstrucon.
Stackmemoryismaintainedbytworegisters:thestackpointer(SPorESP)thestacksegmentregister(SS)
Wheneverawordofdataispushedontothestack,thehigh-order8bitsareplacedintheloca>on
addressedbySP1.
low-order8bitsareplacedintheloca>onaddressedbySP2
8/4/2019 Microprocessors 1 Basics
74/120
3/3/11
74
TheSPisdecrementedby2sothenextwordisstoredinthenextavailablestackloca>on.
theSP/ESPregisteralwayspointstoanareaofmemorylocatedwithinthestacksegment.
Inprotectedmodeopera>on,theSSregisterholdsaselectorthataccessesadescriptorforthebaseaddressofthestacksegment.
Whendataarepoppedfromthestack,thelow-order8bitsareremovedfromtheloca>onaddressedbySP.
high-order8bitsareremoved;theSPregisterisincrementedby2
Figure317ThePUSHandPOPinstruc>ons:(a)PUSHBXplacesthecontentsofBXontothestack;(b)
POPCXremovesdatafromthestackandplacesthemintoCX.Bothinstruc>onsareshowna\er
execu>on.
8/4/2019 Microprocessors 1 Basics
75/120
3/3/11
75
NotethatPUSHandPOPstoreorretrievewordsofdataneverbytesin8086-80286.
80386andaboveallowwordsordoublewordstobetransferredtoandfromthestack.
Datamaybepushedontothestackfromany16-bitregisterorsegmentregister.
in80386andabove,fromany32-bitextendedregister Datamaybepoppedoffthestackintoany
registeroranysegmentregisterexceptCS.
PUSHAandPOPAinstruc>onspushorpopallexceptsegmentregisters,onthestack.
Notavailableonearly8086/8088processors. 80386andaboveallowextendedregisterstobe
pushedorpopped.
64-bitmodeforPen>umandCore2doesnotcontainaPUSHAorPOPAinstruc>on
8/4/2019 Microprocessors 1 Basics
76/120
3/3/11
76
Introducon Thischapterconcentratesonthedatamovement
instruc>ons.
Thedatamovementinstruc>onsincludeMOV,MOVSX,MOVZX,PUSH,POP,BSWAP,XCHG,XLAT,
IN,OUT,LEA,LDS,LES,LFS,LGS,LSS,LAHF,SAHF.
Stringinstruc>ons:MOVS,LODS,STOS,INS,andOUTS.
ChapterObjecves
Explaintheopera>onofeachdatamovementinstruc>onwithapplicableaddressingmodes.
Explainthepurposesoftheassemblylanguagepseudo-opera>onsandkeywordssuchasALIGN,
ASSUME,DB,DD,DW,END,ENDS,ENDP,
EQU,.MODEL,OFFSET,ORG,PROC,PTR,
SEGMENT,USEI6,USE32,andUSES.
Uponcompleonofthischapter,youwillbeableto:
8/4/2019 Microprocessors 1 Basics
77/120
3/3/11
77
ChapterObjecves
Selecttheappropriateassemblylanguageinstruc>ontoaccomplishaspecificdata
movementtask.
Determinethesymbolicopcode,source,des>na>on,andaddressingmodefora
hexadecimalmachinelanguageinstruc>on.
Usetheassemblertosetupadatasegment,stacksegment,andcodesegment.
Uponcompleonofthischapter,youwillbeableto:
(cont.)
ChapterObjecves
ShowhowtosetupaprocedureusingPROCandENDP.
Explainthedifferencebetweenmemorymodelsandfull-segmentdefini>onsfortheMASM
assembler.
UsetheVisualonlineassemblertoperformdatamovementtasks.
Uponcompleonofthischapter,youwillbeableto:
(cont.)
8/4/2019 Microprocessors 1 Basics
78/120
3/3/11
78
41MOVRevisited Inthischapter,theMOVinstruc>onintroduces
machinelanguageinstruc>onsavailablewith
variousaddressingmodesandinstruc>ons.
Itmaybenecessarytointerpretmachinelanguageprogramsgeneratedbyanassembler.
Occasionally,machinelanguagepatchesaremadebyusingtheDEBUGprogramavailable
withDOSandVisualforWindows.
MachineLanguage Na>vebinarycodemicroprocessorusesasits
instruc>onstocontrolitsopera>on.
instruc>onsvaryinlengthfrom1to13bytes Over100,000varia>onsofmachinelanguage
instruc>ons.
thereisnocompletelistofthesevaria>ons Somebitsinamachinelanguageinstruc>onare
given;remainingbitsaredeterminedforeachvaria>onoftheinstruc>on.
8/4/2019 Microprocessors 1 Basics
79/120
3/3/11
79
Figure41Theformatsofthe8086Core2instruc>ons.(a)The16-bitformand(b)the32-bitform.
80386andaboveassumeallinstruc>onsare16-bitmodeinstruc>onswhenthemachineisoperatedin
therealmode(DOS).
inprotectedmode(Windows),theupperbyteofthedescriptorcontainstheD-bitthatselectseitherthe16-or32-bitinstruc>onmode
TheOpcode Selectstheopera>on(addi>on,subtrac>on,etc.,)
performedbythemicroprocessor.
either1or2byteslongformostinstruc>ons Figure42illustratesthegeneralformofthefirst
opcodebyteofmanyinstruc>ons.
first6bitsofthefirstbytearethebinaryopcoderemaining2bitsindicatethedirecon(D)ofthedata
flow,andindicatewhetherthedataareabyteoraword(W)
8/4/2019 Microprocessors 1 Basics
80/120
3/3/11
80
Figure42Byte1ofmanymachinelanguageinstruc>ons,showingtheposi>onoftheD-andW-bits.#
Figure43Byte2ofmanymachinelanguageinstruc>ons,showingtheposi>onoftheMOD,REG,andR/Mfields.
8/4/2019 Microprocessors 1 Basics
81/120
3/3/11
81
MODField Specifiesaddressingmode(MOD)andwhethera
displacementispresentwiththeselectedtype.
IfMODfieldcontainsan11,itselectstheregister-addressingmode
Registeraddressingspecifiesaregisterinsteadofamemoryloca>on,usingtheR/Mfield
IftheMODfieldcontainsa00,01,or10,theR/Mfieldselectsoneofthedatamemory-addressing
modes.
All8-bitdisplacementsaresign-extendedinto16-bitdisplacementswhentheprocessorexecutestheinstruc>on.
ifthe8-bitdisplacementis00H7FH(posi>ve),itissign-extendedto0000H007FHbeforeaddingto
theoffsetaddress
ifthe8-bitdisplacementis80HFFH(nega>ve),itissign-extendedtoFF80HFFFFH
Someassemblerprogramsdonotusethe8-bitdisplacementsandinplacedefaulttoall16-bitdisplacements.
8/4/2019 Microprocessors 1 Basics
82/120
3/3/11
82
RegisterAssignments Supposea2-byteinstruc>on,8BECH,appearsina
machinelanguageprogram.
neithera67H(operandaddress-sizeoverrideprefix)nora66H(register-sizeoverrideprefix)appearsasthe
firstbyte,thusthefirstbyteistheopcode
In16-bitmode,thisinstruc>onisconvertedtobinaryandplacedintheinstruc>onformatof
bytes1and2,asillustratedinFigure44.
Figure44The8BECinstruc>onplacedintobytes1and2formatsfromFigures42and43.Thisinstruc>onisaMOVBP,SP.
theopcodeis100010,aMOVinstruc>on
DandWbitsarealogic1,soawordmovesintothedes>na>onregisterspecifiedintheREGfield
REGfieldcontains101,indica>ngregisterBP,sotheMOVinstruc>onmovesdataintoregisterBP
8/4/2019 Microprocessors 1 Basics
83/120
3/3/11
83
R/MMemoryAddressing IftheMODfieldcontainsa00,01,or10,theR/M
fieldtakesonanewmeaning.
Figure45illustratesthemachinelanguageversionofthe16-bitinstruc>onMOVDL,[DI]or
instruc>on(8AI5H).
Thisinstruc>onis2byteslongandhasanopcode100010,D=1(toREGfromR/M),W=0(byte),
MOD=00(nodisplacement),REG=010(DL),andR/
M=101([DI]).
Figure45AMOVDL,[DI]instruc>onconvertedtoitsmachinelanguageform.
Iftheinstruc>onchangestoMOVDL,[DI+1],theMODfieldchangesto01for8-bitdisplacement
first2bytesoftheinstruc>onremainthesame instruc>onnowbecomes8A5501Hinsteadof8A15H
8/4/2019 Microprocessors 1 Basics
84/120
3/3/11
84
BecausetheMODfieldcontainsa11,theR/Mfieldalsoindicatesaregister.
R/M=100(SP);therefore,thisinstruc>onmovesdatafromSPintoBP.
wrieninsymbolicformasaMOVBP,SPinstruc>on Theassemblerprogramkeepstrackofthe
register-andaddress-sizeprefixesandthemode
ofopera>on.
SpecialAddressingMode Aspecialaddressingmodeoccurswhenmemory
dataarereferencedbyonlythedisplacement
modeofaddressingfor16-bitinstruc>ons.
ExamplesaretheMOV[1000H],DLandMOVNUMB,DLinstruc>ons.
firstinstruc>onmovescontentsofregisterDLintodatasegmentmemoryloca>on1000H
secondmovesregisterDLintosymbolicdatasegmentmemoryloca>onNUMB
8/4/2019 Microprocessors 1 Basics
85/120
3/3/11
85
Whenaninstruc>onhasonlyadisplacement,MODfieldisalways00;R/Mfieldalways110.Youcannotactuallyuseaddressingmode[BP]without
adisplacementinmachinelanguage
Iftheindividualtransla>ngthissymbolicinstruc>onintomachinelanguagedoesnotknow
aboutthespecialaddressingmode,the
instruc>onwouldincorrectlytranslatetoaMOV
[BP],DLinstruc>on.
Figure46TheMOV[1000H],DIinstruc>onusesthespecialaddressingmode.#
bitpaernrequiredtoencodetheMOV[1000H],DLinstruc>on
inmachinelanguage
8/4/2019 Microprocessors 1 Basics
86/120
3/3/11
86
Figure47TheMOV[BP],DLinstruc>onconvertedtobinarymachinelanguage.
actualformoftheMOV[BP],DLinstruc>on
a3-byteinstruc>onwithadisplacementof00H
32-BitAddressingModes Foundin80386andabove.
byrunningin32-bitinstruc>onmodeorIn16-bitmodebyusingaddress-sizeprefix67H
Ascaled-indexbyteindicatesaddi>onalformsofscaled-indexaddressing.
mainlyusedwhentworegistersareaddedtospecifythememoryaddressinaninstruc>on
Ascaled-indexinstruc>onhas215(32K)possiblecombina>ons.
8/4/2019 Microprocessors 1 Basics
87/120
3/3/11
87
Over32,000varia>onsoftheMOVinstruc>onaloneinthe80386-Core2microprocessors.
Figure48showstheformatofthescaled-index
byteasselectedbyavalueof100intheR/Mfield
ofaninstruc>onwhenthe80386andaboveusea
32-bitaddress.
Thele\most2bitsselectascalingfactor(mul>plier)of1x,2x,4x,8x.
Scaled-indexaddressingcanalsouseasingleregistermul>pliedbyascalingfactor.
Figure48Thescaled-indexbyte.
theindexandbasefieldsbothcontainregisternumbers
8/4/2019 Microprocessors 1 Basics
88/120
3/3/11
88
AnImmediateInstrucon Anexampleofa16-bitinstruc>onusingimmediateaddressing.
MOVWORDPTR[BX+1000H],1234Hmovesa1234Hintoaword-sizedmemoryloca>onaddressedbysumof1000H,BX,andDSx10H
6-byteinstruc>on2bytesfortheopcode;2bytesarethedataof1234H;
2bytesarethedisplacementof1000H
Figure49showsthebinarybitpaernforeachbyteofthisinstruc>on.
Figure49AMOVWORDPTR,[BX=1000H]1234Hinstruc>onconvertedtobinarymachinelanguage.
8/4/2019 Microprocessors 1 Basics
89/120
3/3/11
89
Thisinstruc>on,insymbolicform,includesWORDPTR.direc>veindicatestotheassemblerthatthe
instruc>onusesaword-sizedmemorypointer
Iftheinstruc>onmovesabyteofimmediatedata,BYTEPTRreplacesWORDPTR.
ifadoublewordofimmediatedata,theDWORDPTRdirec>vereplacesBYTEPTR
Instruc>onsreferringtomemorythroughapointerdonotneedtheBYTEPTR,WORDPTR,orDWORDPTRdirec>ves.
SegmentMOVInstrucons Ifcontentsofasegmentregisteraremovedby
MOV,PUSH,orPOPinstruc>ons,aspecialbits(REGfield)selectthesegmentregister.theopcodeforthistypeofMOVinstruc>onisdifferent
forthepriorMOVinstruc>ons
animmediatesegmentregisterMOVisnotavailableintheinstruc>onset
Toloadasegmentregisterwithimmediatedata,firstloadanotherregisterwiththedataandmoveittoasegmentregister.
8/4/2019 Microprocessors 1 Basics
90/120
3/3/11
90
Figure410AMOVBX,CSinstruc>onconvertedtobinarymachinelanguage.
Figure410showsaMOVBX,CSinstruc>onconvertedtobinary.
Segmentregisterscanbemovedbetweenany16-bitregisteror16-bitmemoryloca>on.
Aprogramwrieninsymbolicassemblylanguage(assemblylanguage)israrelyassembledbyhandintobinarymachinelanguage.
Anassemblerprogramconvertssymbolicassemblylanguageintomachinelanguage.
8/4/2019 Microprocessors 1 Basics
91/120
3/3/11
91
The64-BitModeforthePenum4andCore2
In64-bitmode,aprefixcalledREX(registerextension)isadded.
encodedasa40H4FH,followsotherprefixes;placedimmediatelybeforetheopcode
Purposeistomodifyregandr/mfieldsinthesecondbyteoftheinstruc>on.
REXisneededtobeabletoaddressregistersR8throughR15
Figure411illustratesthestructureandapplica>onofREXtothesecondbyteoftheopcode.
Theregfieldcanonlycontainregisterassignmentsasinothermodesofopera>on
Ther/mfieldcontainseitheraregisterormemoryassignment.
Figure412showsthescaled-indexbytewiththeREXprefixformorecomplexaddressingmodesandalsoforusingascalingfactorinthe64-bit
modeofopera>on.
8/4/2019 Microprocessors 1 Basics
92/120
3/3/11
92
Figure411Theapplica>onofREXwithoutscaledindex.#
Figure412Thescaled-indexbyteandREXprefixfor64-bitopera>ons.
8/4/2019 Microprocessors 1 Basics
93/120
3/3/11
93
42PUSH/POP Importantinstruc>onsthatstoreandretrievedata
fromtheLIFO(last-in,first-out)stackmemory.
SixformsofthePUSHandPOPinstruc>ons:register,memory,immediatesegmentregister,flags,allregisters
ThePUSHandPOPimmediate&PUSHAandPOPA(allregisters)available80286-Core2.
Registeraddressingallowscontentsofany16-bitregistertotransferto&fromthestack.
Memory-addressingPUSHandPOPinstruc>onsstorecontentsofa16-or32bitmemoryloca>on
onthestackorstackdataintoamemoryloca>on.
Immediateaddressingallowsimmediatedatatobepushedontothestack,butnotpoppedoffthe
stack.
8/4/2019 Microprocessors 1 Basics
94/120
3/3/11
94
Segmentregisteraddressingallowscontentsofanysegmentregistertobepushedontothestackorremovedfromthestack.
ESmaybepushed,butdatafromthestackmayneverbepoppedintoES
Theflagsmaybepushedorpoppedfromthatstack.
contentsofallregistersmaybepushedorpopped
PUSH Alwaystransfers2bytesofdatatothestack;
80386andabovetransfer2or4bytes PUSHAinstruc>oncopiescontentsoftheinternal
registerset,exceptthesegmentregisters,tothe
stack.
PUSHA(pushall)instruc>oncopiestheregisterstothestackinthefollowingorder:AX,CX,DX,BX,
SP,BP,SI,andDI.
8/4/2019 Microprocessors 1 Basics
95/120
3/3/11
95
PUSHF(pushflags)instruc>oncopiesthecontentsoftheflagregistertothestack.
PUSHADandPOPADinstruc>onspushandpopthecontentsofthe32-bitregistersetin80386-
Pen>um4.
PUSHAandPOPAinstruc>onsdonotfunc>oninthe64-bitmodeofopera>onforthePen>um4
Figure413TheeffectofthePUSHAXinstruc>ononESPandstackmemoryloca>ons37FFHand37FEH.Thisinstruc>onisshownatthepointa\erexecu>on.#
8/4/2019 Microprocessors 1 Basics
96/120
3/3/11
96
PUSHAinstruc>onpushesalltheinternal16-bitregistersontothestack,illustratedin414.requires16bytesofstackmemoryspaceto
storealleight16-bitregisters
A\erallregistersarepushed,thecontentsoftheSPregisteraredecrementedby16.
PUSHAisveryusefulwhentheen>reregistersetof80286andabovemustbesaved.
PUSHADinstruc>onplaces32-bitregistersetonthestackin80386-Core2.PUSHADrequires32bytesofstackstorage
Figure414Theopera>onofthePUSHAinstruc>on,showingtheloca>onandorderofstackdata.
8/4/2019 Microprocessors 1 Basics
97/120
3/3/11
97
POP Performstheinverseopera>onofPUSH. POPremovesdatafromthestackandplacesitina
target16-bitregister,segmentregister,ora16-bit
memoryloca>on.
notavailableasanimmediatePOP POPF(popflags)removesa16-bitnumberfrom
thestackandplacesitintheflagregister;
POPFDremovesa32-bitnumberfromthestackandplacesitintotheextendedflagregister
POPA(popall)removes16bytesofdatafromthestackandplacesthemintothefollowingregisters,intheordershown:DI,SI,BP,SP,BX,DX,CX,and
AX.
reverseorderfromplacementonthestackbyPUSHAinstruc>on,causingthesamedatatoreturntothe
sameregisters
Figure415showshowthePOPBXinstruc>onremovesdatafromthestackandplacestheminto
registerBX.
8/4/2019 Microprocessors 1 Basics
98/120
3/3/11
98
Figure415ThePOPBXinstruc>on,showinghowdataareremovedfromthestack.Thisinstruc>onisshowna\erexecu>on.#
InializingtheStack Whenthestackareaisini>alized,loadboththe
stacksegment(SS)registerandthestackpointer
(SP)register.
Figure416showshowthisvaluecausesdatatobepushedontothetopofthestacksegmentwith
aPUSHCXinstruc>on.
Allsegmentsarecyclicinnaturethetoploca>onofasegmentiscon>guouswiththeboomloca>onofthesegment
8/4/2019 Microprocessors 1 Basics
99/120
3/3/11
99
Figure416ThePUSHCXinstruc>on,showingthecyclicalnatureofthestacksegment.Thisinstruc>onis
shownjustbeforeexecu>on,toillustratethatthestackboomiscon>guoustothetop.
Assemblylanguagestacksegmentsetup:firststatementiden>fiesstartofthesegmentlaststatementiden>fiesendofthestacksegment
AssemblerandlinkerprogramsplacecorrectstacksegmentaddressinSSandthelengthofthe
segment(topofthestack)intoSP.
Thereisnoneedtoloadtheseregistersinyourprogram.
unlessyouwishtochangetheini>alvaluesforsomereason
8/4/2019 Microprocessors 1 Basics
100/120
3/3/11
100
Ifthestackisnotspecified,awarningwillappearwhentheprogramislinked.
Memorysec>onislocatedintheprogramsegmentprefix(PSP),appendedtothebeginning
ofeachprogramfile.
Ifyouusemorememoryforthestack,youwilleraseinforma>oninthePSP.
informa>oncri>caltotheopera>onofyourprogramandthecomputer
Erroro\encausestheprogramtocrash.
LEA Loadsa16-or32-bitregisterwiththeoffset
addressofthedataspecifiedbytheoperand.
EarlierexamplespresentedbyusingtheOFFSETdirec>ve.
OFFSETperformssamefunc>onasLEAinstruc>oniftheoperandisadisplacement
LEAandMOVwithOFFSETinstruc>onsareboththesamelength(3bytes).
8/4/2019 Microprocessors 1 Basics
101/120
3/3/11
101
WhyisLEAinstruc>onavailableifOFFSETaccomplishesthesametask?OFFSETfunc>onswith,andismoreefficientthanLEA
instruc>on,forsimpleoperandssuchasLIST
MicroprocessortakeslongertoexecutetheLEABX,LISTinstruc>ontheMOVBX,OFFSETLIST
TheMOVBX,OFFSETLISTinstruc>onisactuallyassembledasamoveimmediateinstruc>onand
ismoreefficient.
43LOADEFFECTIVEADDRESS LEAinstruc>onloadsany16-bitregisterwiththe
offsetaddress
determinedbytheaddressingmodeselected LDSandLESloada16-bitregisterwithoffset
addressretrievedfromamemoryloca>onthenloadeitherDSorESwithasegment
addressretrievedfrommemory
In80386andabove,LFS,LGS,andLSSareaddedtotheinstruc>onset.
8/4/2019 Microprocessors 1 Basics
102/120
3/3/11
102
LDS,LES,LFS,LGS,andLSS Loadany16-or32-bitregisterwithanoffset
address,andtheDS,ES,FS,GS,orSSsegment
registerwithasegmentaddress.
instruc>onsuseanymemory-addressingmodestoaccessa32-bitor48-bitmemorysec>onthat
containbothsegmentandoffsetaddress
Figure417illustratesanexampleLDSBX,[DI]instruc>on.
Figure417TheLDSBX,[DI]instruc>onloadsregisterBXfromaddresses11000Hand11001Hand
registerDSfromloca>ons11002Hand11003H.Thisinstruc>onisshownatthepointjustbeforeDS
changesto3000HandBXchangesto127AH.
8/4/2019 Microprocessors 1 Basics
103/120
3/3/11
103
Thisinstruc>ontransfersthe32-bitnumber,addressedbyDIinthedatasegment,intotheBXandDSregisters.
LDS,LES,LFS,LGS,andLSSinstruc>onsobtainanewfaraddressfrommemory.
offsetaddressappearsfirst,followedbythesegmentaddress
Thisformatisusedforstoringall32-bitmemoryaddresses.
Afaraddresscanbestoredinmemorybytheassembler.
Themostusefuloftheloadinstruc>onsistheLSSinstruc>on.
a\erexecu>ngsomedummyinstruc>ons,theoldstackareaisreac>vatedbyloadingbothSSandSP
withtheLSSinstruc>on
CLI(disableinterrupt)andSTI(enableinterrupt)instruc>onsmustbeincludedtodisable
interrupts.
8/4/2019 Microprocessors 1 Basics
104/120
3/3/11
104
44STRINGDATATRANSFERS Fivestringdatatransferinstruc>ons:LODS,STOS,
MOVS,INS,andOUTS.
Eachallowsdatatransfersasasinglebyte,word,ordoubleword.
Beforethestringinstruc>onsarepresented,theopera>onoftheDflag-bit(direc>on),DI,andSI
mustbeunderstoodastheyapplytothestring
instruc>ons.
TheDireconFlag Thedirec>onflag(D,locatedintheflagregister)
selectstheauto-incrementortheauto-
decrementopera>onfortheDIandSIregisters
duringstringopera>ons.
usedonlywiththestringinstruc>ons TheCLDinstruc>onclearstheDflagandtheSTD
instruc>onsetsit.
CLDinstruc>onselectstheauto-incrementmodeandSTDselectstheauto-decrementmode
8/4/2019 Microprocessors 1 Basics
105/120
3/3/11
105
DIandSI Duringexecu>onofstringinstruc>on,memoryaccessesoccurthroughDIandSIregisters.
DIoffsetaddressaccessesdataintheextrasegmentforallstringinstruc>onsthatuseit
SIoffsetaddressaccessesdatabydefaultinthedatasegment
Opera>ngin32-bitmodeEDIandESIregistersareusedinplaceofDIandSI.
thisallowsstringusinganymemoryloca>onintheen>re4G-byteprotectedmodeaddressspace
LODS LoadsAL,AX,orEAXwithdataatsegmentoffset
addressindexedbytheSIregister.
A1isaddedtoorsubtractedfromSIforabyte-sizedLODS
A2isaddedorsubtractedforaword-sizedLODS. A4isaddedorsubtractedforadoubleword-sized
LODS.
Figure418showstheLODSWinstruc>on.
8/4/2019 Microprocessors 1 Basics
106/120
3/3/11
106
Figure418Theopera>onoftheLODSWinstruc>onifDS=1000H,D=0,11000H,11001H=A0.Thisinstruc>onisshowna\erAXisloadedfrommemory,butbeforeSIincrementsby2.#
STOS StoresAL,AX,orEAXattheextrasegment
memoryloca>onaddressedbytheDIregister.
STOSB(storesabyte)storesthebyteinALattheextrasegmentmemoryloca>onaddressedbyDI.
STOSW(storesaword)storesAXinthememoryloca>onaddressedbyDI.
A\erthebyte(AL),word(AX),ordoubleword(EAX)isstored,contentsofDIincrementordecrement.
8/4/2019 Microprocessors 1 Basics
107/120
3/3/11
107
STOSwithaREP Therepeatprefix(REP)isaddedtoanystringdatatransferinstruc>onexceptLODS.
REPprefixcausesCXtodecrementby1each>methestringinstruc>onexecutes;a\erCXdecrements,thestringinstruc>onrepeats
IfCXreachesavalueof0,theinstruc>onterminatesandtheprogramcon>nues.
IfCXisloadedwith100andaREPSTOSBinstruc>onexecutes,themicroprocessorautoma>callyrepeatstheSTOSB100>mes.
MOVS Transfersabyte,word,ordoublewordadata
segmentaddressedbySItoextrasegmentloca>onaddressedbySI.pointersareincrementedordecremented,asdictated
bythedirec>onflag
Onlythesourceoperand(SI),locatedinthedatasegmentmaybeoverriddensoanothersegmentmaybeused.
Thedes>na>onoperand(DI)mustalwaysbelocatedintheextrasegment.
8/4/2019 Microprocessors 1 Basics
108/120
3/3/11
108
INS Transfersabyte,word,ordoublewordofdatafromanI/Odeviceintotheextrasegment
memoryloca>onaddressedbytheDIregister.I/OaddressiscontainedintheDXregister
UsefulforinpungablockofdatafromanexternalI/Odevicedirectlyintothememory.
Oneapplica>ontransfersdatafromadiskdrivetomemory.
diskdrivesareo\enconsideredandinterfacedasI/Odevicesinacomputersystem
ThreebasicformsoftheINS. INSBinputsdatafroman8-bitI/Odeviceand
storesitinamemoryloca>onindexedbySI.
INSWinstruc>oninputs16-bitI/Odataandstoresitinaword-sizedmemoryloca>on.
INSDinstruc>oninputsadoubleword. Theseinstruc>onscanberepeatedusingtheREP
prefix
allowsanen>reblockofinputdatatobestoredinthememoryfromanI/Odevice
8/4/2019 Microprocessors 1 Basics
109/120
3/3/11
109
OUTS Transfersabyte,word,ordoublewordofdata
fromthedatasegmentmemoryloca>onaddress
bySItoanI/Odevice.
I/OdeviceaddressedbytheDXregisteraswiththeINSinstruc>on
Inthe64-bitmodeforPen>um4andCore2,thereisno64-bitoutput
buttheaddressinRSIis64bitswide
45MISCELLANEOUSDATATRANSFER
INSTRUCTIONS
Usedinprograms,datatransferinstruc>onsdetailedinthissec>onareXCHG,LAHF,SAHF,
XLAT,IN,OUT,BSWAP,MOVSX,MOVZX,and
CMOV.
8/4/2019 Microprocessors 1 Basics
110/120
3/3/11
110
XCHG Exchangescontentsofaregisterwithanyother
registerormemoryloca>on.
cannotexchangesegmentregistersormemory-to-memorydata
Exchangesarebyte-,word-,ordoublewordanduseanyaddressingmodeexceptimmediate
addressing.
XCHGusingthe16-bitAXregisterwithanother16-bitregister,ismostefficientexchange.
LAHFandSAHF Seldomusedbridgeinstruc>ons. LAHFinstruc>ontransferstherightmost8bitsof
theflagregisterintotheAHregister.
SAHFinstruc>ontransferstheAHregisterintotherightmost8bitsoftheflagregister.
SAHFinstruc>onmayfindsomeapplica>onwiththenumericcoprocessor.
Aslegacyinstruc>ons,theydonotfunc>oninthe64-bitmodeandareinvalidinstruc>ons.
8/4/2019 Microprocessors 1 Basics
111/120
3/3/11
111
XLAT ConvertsthecontentsoftheALregisterintoa
numberstoredinamemorytable.
performsthedirecttablelookuptechniqueo\enusedtoconvertonecodetoanother
AnXLATinstruc>onfirstaddsthecontentsofALtoBXtoformamemoryaddresswithinthedata
segment.
copiesthecontentsofthisaddressintoALonlyinstruc>onaddingan8-bittoa16-bitnumber
Figure419Theopera>onoftheXLATinstruc>onatthepointjustbefore6DHisloadedintoAL.
8/4/2019 Microprocessors 1 Basics
112/120
3/3/11
112
INandOUT IN&OUTinstruc>onsperformI/Oopera>ons. ContentsofAL,AX,orEAXaretransferredonly
betweenI/Odeviceandmicroprocessor.
anINinstruc>ontransfersdatafromanexternalI/OdeviceintoAL,AX,orEAX
anOUTtransfersdatafromAL,AX,orEAXtoanexternalI/Odevice
Onlythe80386andabovecontainEAX
O\en,instruc>onsarestoredinROM.afixed-portinstruc>onstoredinROMhasitsport
numberpermanentlyfixedbecauseofthenatureof
read-onlymemory
Afixed-portaddressstoredinRAMcanbemodified,butsuchamodifica>ondoesnot
conformtogoodprogrammingprac>ces.
TheportaddressappearsontheaddressbusduringanI/Oopera>on.
8/4/2019 Microprocessors 1 Basics
113/120
3/3/11
113
TwoformsofI/Odevice(port)addressing: Fixed-portaddressingallowsdatatransferbetweenAL,AX,orEAXusingan8-bitI/Oport
address.
portnumberfollowstheinstruc>onsopcode Variable-portaddressingallowsdatatransfers
betweenAL,AX,orEAXanda16-bitportaddress.
theI/OportnumberisstoredinregisterDX,whichcanbechanged(varied)duringtheexecu>onof
aprogram.
Figure420Thesignalsfoundinthemicroprocessor-basedsystemforanOUT19H,AXinstruc>on.
8/4/2019 Microprocessors 1 Basics
114/120
3/3/11
114
BSWAP Takesthecontentsofany32-bitregisterand
swapsthefirstbytewiththefourth,andthe
secondwiththethird.
BSWAP(byteswap)isavailableonlyin80486Pen>um4microprocessors
Thisinstruc>onisusedtoconvertdatabetweenthebigandlileendianforms.
In64-bitopera>onforthePen>um4,all8bytesintheselectedoperandareswapped.
CMOV Manyvaria>onsoftheCMOVinstruc>on.
thesemovethedataonlyifthecondi>onistrue CMOVZinstruc>onmovesdataonlyiftheresult
fromsomepriorinstruc>onwasazero.
des>na>onislimitedtoonlya16-or32-bitregister,butthesourcecanbea16-or32-bitregisterormemoryloca>on
Becausethisisanewinstruc>on,youcannotuseitwiththeassemblerunlessthe.686switchisaddedtotheprogram
8/4/2019 Microprocessors 1 Basics
115/120
3/3/11
115
46SEGMENTOVERRIDEPREFIX Maybeaddedtoalmostanyinstruc>oninanymemory-addressingmode
allowstheprogrammertodeviatefromthedefaultsegment
onlyinstruc>onsthatcannotbeprefixedarejumpandcallinstruc>onsusingthecodesegmentregisterforaddressgenera>on
Addi>onalbyteappendedtothefrontofaninstruc>ontoselectalternatesegmentregister
47ASSEMBLERDETAIL Theassemblercanbeusedtwoways:
withmodelsuniquetoapar>cularassembler withfull-segmentdefini>onsthatallowcompletecontrol
overtheassemblyprocessandareuniversaltoallassemblers
Inmostcases,theinlineassemblerfoundinVisualisusedfordevelopingassemblycodeforuseinaprogram
occasionsrequireseparateassemblymodulesusingtheassembler
8/4/2019 Microprocessors 1 Basics
116/120
3/3/11
116
Direcves Indicatehowanoperandorsec>onofaprogramistobeprocessedbytheassembler.
somegenerateandstoreinforma>oninthememory;othersdonot
TheDB(definebyte)direc>vestoresbytesofdatainthememory.
BYTEPTRindicatesthesizeofthedatareferencedbyapointerorindexregister.
Complexsec>onsofassemblycodeares>llwrienusingMASM.
StoringDatainaMemorySegment DB(definebyte),DW(defineword),andDD
(definedoubleword)aremosto\enusedwith
MASMtodefineandstorememorydata.
Ifanumericcoprocessorexecutesso\wareinthesystem,theDQ(definequadword)andDT(define
tenbytes)direc>vesarealsocommon.
Thesedirec>veslabelamemoryloca>onwithasymbolicnameandindicateitssize.
8/4/2019 Microprocessors 1 Basics
117/120
3/3/11
117
Memoryisreservedforuseinthefuturebyusingaques>onmark(?)asanoperandforaDB,DW,orDDdirec>ve.
when?isusedinplaceofanumericorASCIIvalue,theassemblersetsasidealoca>onanddoesnot
ini>alizeittoanyspecificvalue
Itisimportantthatword-sizeddataareplacedatwordboundariesanddoubleword-sizeddataare
placedatdoublewordboundaries.
ifnot,themicroprocessorspendsaddi>onal>meaccessingthesedatatypes
ASSUME,EQU,andORG Equatedirec>ve(EQU)equatesanumeric,ASCII,
orlabeltoanotherlabel.
equatesmakeaprogramclearerandsimplifydebugging
TheTHISdirec>vealwaysappearsasTHISBYTE,THISWORD,THISDWORD,orTHISQWORD.
Theassemblercanonlyassigneitherabyte,word,ordoublewordaddresstoalabel.
8/4/2019 Microprocessors 1 Basics
118/120
3/3/11
118
TheORG(origin)statementchangesthestar>ngoffsetaddressofthedatainthedatasegmenttoloca>on300H.
At>mes,theoriginofdataorthecodemustbeassignedtoanabsoluteoffsetaddresswiththe
ORGstatement.
ASSUMEtellstheassemblerwhatnameshavebeenchosenforthecode,data,extra,andstack
segments.
PROCandENDP Indicatestartandendofaprocedure(subrou>ne).
theyforcestructurebecausetheprocedureisclearlydefined
Ifstructureistobeviolatedforwhateverreason,usetheCALLF,CALLN,RETF,andRETN
instruc>ons.
BoththePROCandENDPdirec>vesrequirealabeltoindicatethenameoftheprocedure.
8/4/2019 Microprocessors 1 Basics
119/120
3/3/11
119
ThePROCdirec>ve,whichindicatesthestartofaprocedure,mustalsobefollowedwithaNEARorFAR.ANEARprocedureisonethatresidesinthesame
codesegmentastheprogram,o\enconsideredtobelocal
AFARproceduremayresideatanyloca>oninthememorysystem,considered global
Thetermglobaldenotesaprocedurethatcanbeusedbyanyprogram.
Localdefinesaprocedurethatisonlyusedbythecurrentprogram.
MemoryOrganizaon Theassemblerusestwobasicformatsfor
developingso\ware:
onemethodusesmodels;theotherusesfull-segmentdefini>ons
MemorymodelsareuniquetoMASM. Themodelsareeasiertouseforsimpletasks. Thefull-segmentdefini>onsofferbeercontrol
overtheassemblylanguagetaskandarerecommendedforcomplexprograms.
8/4/2019 Microprocessors 1 Basics
120/120
3/3/11
Models TherearemanymodelsavailabletotheMASM
assembler,rangingfrom>nytohuge.
Specialdirec>vessuchas@DATAareusedtoiden>fyvarioussegments.
ModelsareimportantwithbothMicroso\VisualandBorlanddevelopmentsystemsifassembly
languageisincludedwithprograms.
Full-SegmentDefinions Full-segmentdefini>onsarealsousedwiththe
BorlandandMicroso\environmentsfor
d d l d bl l