Beruflich Dokumente
Kultur Dokumente
Aavmbly
Lulguge
htgmmmer's
o
/
T H E . A O S A 6 . B O O K
ly Inss
Rllnlsnn
7
I
PUBIISI.IED BY
Introduction
MemoryArchitecturerSegmentation 43
5 The 80386ProtectionMechanism 93
6 MemoryArchitecrurerPaging 125
Index 445
Acknowledgments
A nurnber of people deserve credit for helping make this book a realiry. Some
I have spoken with and worked with directly; others have worked behind the
scenes,doing a wonderful job nonetheless.Y/orking with the people at Microsoft
Presswas a positive experience,and I sincerely thank them all for their support and
encouragement.In addition, my thanks to RayDuncan for getting the ball rolling, to
Intel corporation for its cooperation, () Matt Trask for his technical review, to my
covr'orkersat Answer Soft$/arefor thct support, and especi^lly to Pamfor always
believingin me.
This is a book about microprocessortechnology, so of courseit was written wilh
the assistanceof microprocessortechnology, I completed a large Portion of the
manuscriptusing a ToshibaT1000portable computer while riding ShntaClara
County Transit,
RossNlson
June 1988
tx
INTRODUCTION
Number Formats
I use numbersin tluee differenr bases,binary (base 2), decimal (base 10),and hexa-
decinal (base 16).You can assune that all numbers are base10unlessthey are fol-
lowed by the suffix "B" (for binary) or "H" (for hexadecimal).F'oroc?mple,
lAH=26=000110i0B
Data Types
Tire 80386can operate on a variety of data rypes.The most common afe 8-bit, 16-
bit, and 32'bitquantities.Inthis book, an 8 bit quantityis calleda byte,a 16-bit
quaniiry is calLeda word, and a 32-bit quantity is called a doubleword, or dword.
This nomenclatureis unusualbecausethe standa.ddataitem sizeof a computeris
conmonly calleda worcL.In thc DigitalEquipmentVAXcomputers,for example,a
32-tri1quantity is a worcl, and a 16-bit quantity is a halfvord. The sameis true for the
Moroftta 6i]000farrily ancithe IBM 370 mainframes.
Althoughthc slandard80386operandsizeis 32-bits,Intcl rctaincdthc namingcon-
ventions of its carlicr processds becausethe 80386is a descendantof thc 8086and
the 80286(16 bit proccssors). This simplifiesrunningsoftwarefrom the 8086or rh
80286and lets you usc thc samcassemblerto generatecode for any of lhe three
7 0
Rirr 7 t5
k'l hi
16$it wod
Bits 7 015 24
32 bir dword
10 71 12 13
5DH 75H otH 1oH
||nrcducrbn
Assembler l{otation
An 80386instructionis a bin.rryPatterntl1.| is dccodedby tl)c logicinsiderhc CPU.
An insiructioncanbe fr'()m{l to 120brtsin lcngth Becausccodingn progrxmusing
bioary pa crns would be r.dious,progrxnrmc.sLrsea typc (t programcallcdan as
semblcrThc simplestrypc ofassemble.txkesr sctof keywoftls.rndsvmbolsxnd
translatcs l-hesel of ky$(nds xnd symboltis cxllcd the
them ink) ^n instrLrctioo.
asscml)lcr lan|Lr^gc.lypically,dlerc is I onc to-onc DrxPpingbetwccnan instruc-
in)n in assembly l^nguagc and an xcrLral m:rchineinstrLdhn lhe ^ssemblcrwould
Iakc nn insiructionsuch rs:
AOO E B X 5,
mcrning,"Ad(I 5 k) thc valuein rqtisle.lillx and st(ncthc resullin llllx," l'nd
wouldt.anshtciI inlo thc I)i! pattcrn
000101101010101010101010r0100i111r1110111118
Thc namesofthc inslrucrionsarc callcdmnenx)nics,.rnd occuPylhe
thc'yLrslrelly
li$t ficld in an inslrucli.rnline. Thc sul)sequentficlds.tc the ()pcflnds ofthc in-
stfuclion and can takca numlxr oilirrns The snnplcsr is a numcfic value'suchrs
thc 5 in the previ()uscxample. A rcgisrer nnme is xnother forln (n oper.rnd An cx
prcssbnwithin brackcts,suchastlllP+2], sisniliesan opersrd thrt is a memofv
Syntax
This book usesthe followingsyniax:
t-
Or
Shiftright
Shifrleft
crerterdranorequdlro
The8O80
The chip that, by most accounts,led to the birth of the microcomputer industry was
the 8O8O, which Intel introduced in 1974.An article in the September1975issueof
Popular Electronicsbrarght the idea of a "personal" computer to the massmarket,
and, as they say,the rest is history. The 8080was the CPU(cenrral processingunit)
lxE ao3a6BooK
in such pioneering sysrcmsas the Altair and the IMSAI. Intel did nor enjoy a
monopoly on the market for long, howeveri Motorola inrroduced the 6800,MOS
Tchnology .espondedwith the 6502,and rwo designersof rhe 8080left Intel for
Zilog Corpo.:rtion,wlich soon produced rhe 280. Unlike the 6800and the 6502,
which had completely differenr archirecrures,rhc 280 was ompariblewith rhe 8080
bur hadan expandedinstructbn setand ran rwicc asfasr.The battlefor CPU
PS\q
uc B c
DI t) E
TIL H I
sf
PC
The 8O86
In 1978,under pressurcliom other manufacrurers'faster,more powerful micropro-
ccssors,Intel moved to a 16-bit architecture.The 8086was touteclas the successorto
the 8080microprocessor, and,althoughthe instructionserwas new, it retained
compatibilitywith the 8080'sinstructionser.Figuret 2 showshow the new regisrers
of the 80815could be mapped into the sei of 8080registcrs.
Programsthat wcrc written for the 8080could nor be run on the 8086i however,
almost every 8086 instruction correspondedio an t1080insrrucrion. Ar worst, an
8080instructbn could be simularcdby rwo or three 8086operarions.An Inlel
translalorprogram could convert 8080assemblerprograms inro 8086assemblerp.o
grams,and thc firsr versionsof Microsofr'sBASICand MicroPro'sVordsrar for rhc
8086were portcd from 8080 sysremsvia rhe Intel rmnslator This concern for com,
patibility has charactcrizedIntel's preseflcc in the microcompurer market. Every
new generationof microprocessorhas been able ro run software v'.ritten for rhe
orevroussenerrlron.
AX
BX
cx
DX
AH
BX BH al
CX crl ct
DX DH DI
cs
DS
ss
ES
Flgldrel-3. The3]0116
registetset.
The next two examplesshow dedicaled rcgistcrs in use. Figure 1-4 showshow high-
level languagessuch as Pascaluse the stack pointer (sP) and base pointer (BP)
registers.
3
7
rHE 00306 BOO(
: IBP - offsetl
old tP
tsP OId BP
i
0ocals)
I B P+ o f f s e t l
j
5P
oegln leas1,j
I n o vc x . S I Z E 0 fFe(c)
r :- Ji
:
4
1r E olqtlo 0l $. aoaaaarchlr.ct!re
E-]
t l
L-] F;I
.ode
L"
::
in
.,J"u
Flgael'6. Mmory madels.
a
t||E eo3e5EooK
The tiny model mimicked the 8080addressspace.The code segmentand data seg-
ment were in the samearea of memory and the progfam was limited to 64 KB. The
small model was expected to be prevalentbec se it allowed pmgrams to double in
size.By having separatecode and data segments,programs could expand to 128KB
and still retain 16-bit addressing.The large memory model allo$d the use of mul-
tiple code and data segments.In this model, the entire 1 MB addressspaceof the
Processorcould be used.
Vhen the 8086was introduced in 1978,most mirocomputerswere limited to 64
KBi almost no one realizd how quickly the 64 KB segmentlimit would become a
serious problem. Although the large model allowed programs to fill the entire 1MB
of 8086addressspace,using the large model meant using 32-bir pointers. On a 15-bit
machine, 32-bit pointers exacteda size and perfcrmance penalty that mosr pro-
grammerswere unwilling tc)pay. By the early 1980s,even the 1 MB limit became
confining. Additional memory models with namessuch a$ "compact" and "medium"
were inFoduced to optimize performance for specialprogfamming needs.
Other processorsin the 8086family were fie 8088,the 80186,and the 80188.The
8088,introduced a 'ar after the 8086,had the same16-bit internal architecture but
a restricted 8-bit e\ternal bus. The 8088could run the sameproSramsas rhe 8086,
but rtpically 30 percent sloRer,The 8088becamewildly successfulwhen IBM chose
it for the PC and the PCIXT. The 80186and 80188were announced much latet, in
1982.These processorskept the samebasearchitecture but included features such
as direct memory access(DMA) controllers, on-chip counter/timers, and a simpli-
fied hardware interface.They also operated more quickly than did the 8086/8088
andbecamepopularin conkollerapplicaLions.
Tho 8087
An innovative part of the 8086family of CPUSis the coprocessor The ESCor
coprocessorescapeclassof instructions only generateda memory addresson the
8086.Mditional, special-purposeCPUScould be createdto monitor the instruction
streamand watch for ESCsequences,as shown in Figure 1-7.Vhenever an ESCwas
detected,the coprccessorcould decode the escapeas an instnrction for itself and
peform a function that the 8086was incapableof doing efficiently on its own.
ESCT=nMULST(2)
6
i: Evolurlonol the aoitaGarchit*turo
The first (and only) coprocessor developed for the 8086 was thc 8087. The U0871m
plementc.l a floating point instructlon set, capableofas much as 80 bits of pfcci
sjon. Inrel workecl closely $ith the lnDn and prcfessof at the Universiry of
California, Berkelcy, to crcatc x fft)ating point representationthat was flcxiblc rnd
accurltc. This rcprcscntati(n and its numeric pfoperties have sincc becn fornaliT-cd
as lElti StandafclIEEE75'i.
The 8087 contdbuted to the populrrity of thc 80i16.,^ deskbp compLrterdrat con
tained both an 8086 an.l an 8087 corl.l &) scri()Lrsscientific work. Implementing
fbating point functions in hardware improvcd rhc pcrfbrmrnce ofmathemxtical
celculalionsover existiqg software routincs. tlowcvcr, lhc {]087pointecl out the
prcblems oldre 61KB segmentsize. Once scicntistsand cn!!ineeN had the comput
in8 powef k) hanctlereal world problems, they oftcn nccdcd t() dcal with largc ar-
rxys of numbers The 6/1KB segmenthnit restrictcd a vcll(tr ol doublc'prc{is( 11
fl()ating point numbeF to no morc than 1024clcmcnts. Sofiwafe capableofgeuing
xft)und Lheresrriction $,assoon available,but thc "lrrgc" mcmory m(dcl wxs dilli-
cult to progmm in and was slow.
The 80286
'I 'l
hc ncxt mxjor int()duction fr(rn hr|cl, thc 80286,carnc in 1982. hc 80286is com
prtiltc with the u0il(rfumily, bul il rlso p()vi(lcs .r signilicxnt pcdb nancc iDprove-
Drcnt.11boxsrslw() opcrrtinit nrxlcs: rcrl nx)dc nnd p!orcclccl Drcdc. li(.iirl roclc.
which eDnrhlcs the in86, is thc defiurh |l11)dc.1hc ncw noclc is crllcd p.orcdcd
nx)clc.In prorcdc(l )clc,thc 80286supporls lhc u0U6inslfuction sct but phces a
new intcrprclrlion ()n thc coltcnts oflhc s.gnrcnt ltgistc|s th:u.|ontml bow
me|ll(xy is lcccsscd
AlthoLrghopcr^ting systernsthal arc nnplcmcntccl undc. pftnc'ctedrnodc dfc clilfcf
cnt f.onr those lbrt are designcd fof rcrl modc, .rpplicationscrn bc clcvcl()pedtlut
run in either mode.'l'he clcsignoirhcsc du.rl nnxlc xpplicati()osfequi.es thxLLhc
application observe certain mcnnry rcstriclions.
'fh!
influcncc ol l)a*-alon rhc dcsign oftbc 80286 is shown by the addition ofthe
liN l rtl instructi()ll to (hc 802U6inslruclion sct. Thc nNTER instfucrion simplilics
crcrting a slack liamc such.s the onc sh()wn in thc subroutinc c()n!cx! illuslrati()n
in tigurc 1-4. tjN'l tslt crn.ls(r copy thc c()ntextor stack li nc ofthc prcvious sub-
'l
roulinc. his ability is not ncccsslry i. lxnguagcssuch as FORTRAN or C, bul
it is uselul in langLngessuch lls Pis.:rl and Adr drat alk)w ncstcclp()cccllrc
The 80287
lntel xlso introduccd a ncw copf.rccs\o. for lhc 80286,bur thc 80287was a bit oflr
clisippoinlmcnt Altholrgh thc 80286cxcculcs programs two () th.cc limes iaster
tlun the 8086,fie perforrnancc oflhc 80287 is about thc samc as lhe il)87 Intel did
not really modify thc conpuhtional cngine ofthc 8087 in crcaling the 80287 so the
new coproccssor (locs not run any fxster. Intel clid changc the inrerface between the
CPU and the coprocessor, howcvcr. climinating the nccd ibr thc coprocessor to
monitor the instruction srcam of fie main CPU.
lnthis new interface meth.xl, illusLratcdin ligufc I 8, Lhc main CPU decodes the
ESCinsructions and rhen passes fic jnformati()n to thc coproccssor via the I/O
''lhe r\1ulli(s
srcup did not inve dese features,butdrywef an intaralpait oldrc sysrcm.
a
ll Evolltion ot th6 ao3a5Archlrctur.
-Er- "..,-p
EF@AE
Fr$r e t a, 8 O2a6 capr ocesn t i, te{a.e.
Competitive Pressu?es
Ilctwccn ihc inhr)du(lionofthe 8086and thc u0286,Mo()fok dcvelopcdwhat
l)ccxmerbe strongestcompetitionto Inrel'sdominanceoithe nricroprocessor
mxrkct,thc 68000f^mily.Severalfcaturcsoirhe Mokxol,rmicroproccssors were at-
tradivc k) thc dcvelopmentcommuniry.Thc 611000 familyincorporates
a 32$ir in-
tcrnalrcAi$terfile for dataand.dcLrcssing.
This albws a largeapplicatbnaddrss
spnccwithoutthe limitationof64 Ktsscgmcnls.This 32-bitcapabilityalsomakesit
casyk) port opcratingsyslems(suchas UNIX) andminicomplrlcrapplicatbnsk)
thc (}!1000familyprocessors.
Motorolaalsoboastedabolrllhc 'l)rthogonality"of the 6a000instrucrionset.Unlike
the 8086and the 80286,wifi thcir spc(ial-purposeregjsters, the 68000allowedpro-
grammersto speciiyanyregisicrfo. a giveninskuction.Althoughail68000micro-
processorshad 32-bit register filcs, thc first two CPUS(68000and 68010)were
limited b 24+it addresses ancLa 16 bir meDDryinierface.In 1985,however,
Molorolabegansampling the 68020,which had a full 32,bit addressbus and a 32iit
clxtabus.Althoughlntel hadmostof thc busincssmicfocomputermarket,makersof
scientificandengineeringworkstationsalmo$ unanimouslychoseMotorolaCPUS
for theirproducts.
had to be able to run. At the sametime, they neededa product that would address
the shortcomingsof the 8086family architecture,which gave Motorola an edge in
scientific and engineering markets.The resulting product, the 80386,addresses
these issuesby operating in a nunber of modes.At boot time, it operatesin real
mode like the 80286and is nothing more than a very fast 8086.It uses16-bit
registersand the 8086segmentationscheme,and it is subjectto the 1MB memory
limitation.
But the 80386can also be ser'itchedkr protected mode. In proteted mode, each
segmentis marked by a bit that designateswhether the segmentis a protected-
mode segmentcontaining 16-bir 80286code or a 32-bit protected-mode segment.
Programsresiding in ,2-bit segmentscan use the extended addressspace(sgments
larger than 64 KB) and additional features,including array indexing, orthogonal use
of the register set, and special debugging capabilitiesnot found in previous
Ths 80387
The most rccent mlcroprocessorline from Intel also boastsa new coprocessor,the
80387 The interface between the CPUand the coprocessoris the sameone defined
for rhe 80286and the 80287 The 80386can be coupied with the 80287to provide a
lower-cost floating-point environment.The 80387provides a significant perfor-
mance improvementover its predecessor,executing floating-point benchmarks
about five times faster
8O386Family Extensions
htel has indicated that the 80386product line will continue to evolve. The next
generationprocessorwill be called the 80486and will include capabilities beyond
those of the 803a6.However Intel hascommitted to broadening support for the
lo
i: Eyolution ol th. AO3a6Arclril.cto6
80386as well. Intel recently introduced the 80386SXand the 80387SX,which are
fully compatible with the 80386/80387but support only a 16-bitexrernal data bus
and a 24'bit external addressbus. Intel plans to introduce other processorsthat use
ihe 80386native mode instruction set but that do not supporl compatibility features
such as real mode or V86 mode.
Summary
As you can seefrom the follo!,r'ingtable, the 80386technology has significantly
ad nced beyond thar of its prcdcessorsihowever,the road to 32-bit computing
fl"asnot necessarilystraight and naffow. The 80386hasbeen shapedby a number of
forces,the idalsof thc designers, the limits of compatibility(somestemmingfrom
the early days of the 8080),threats from the comperition (both real and perceived),
and other facrorssuch as Pascal.Multis, and UNIX. Now that I've shown the
origins of the 80386,the remaindcr of the book will show what the 80386is and
Relath"e Peiformance
&ts6/e7 80286/247 N3A6/387
Integet 1.0 2.'7
noartagpold, 1.0 1.7 10.0
11
2
THE AOSA 6
ARCHITECTI,R E
A Data-Prccessing Factory
In recent years,however,this machinelik cycle led to limitations on lhe amountof
work that could be accomplished.A modern microprocessorsuch as the 80386
might be more successfullycomParedwith a factory than with an engine. Ai the
heartofthis data-processing factory,the computational enginerem4ins,but it is suF
roun.ledby a bevy otsuppor(ingdepar(mcnls.
Figure 2-1on the following page illustrates our imaginary widgel factory lt is com_
posed of three departrnents:Shipping and Re.eivinS,Materials,and Manuiacturing
The Shipping and Receivingdepartment deaLswith the world outside the factory' It
orders truckloads of raw materialsfrom suppliers and passesthem to the Materials
daparhne . The goods are sorted here and warehouseduntii needed The Manu-
facturing depar[nent, the "engine" of the factory, forgesthe finished widgets from
the raw materialsand routes them to Shipping and Recelving,where ftey arc sent to
the outside world.
The efficiency of this model lies in the parallel nature of the different activities At
rhe samerimeasrhe Mareri.rlrdepa.henr reque$sthe raw Soodsn(e\\ary |o
build widgets, Manufacturingbuilds the current supply of widgets, and Shipping and
Receivingdealswith the outside world, buys unfinished goods,and ships the newlv
finished widgets.
Conventionalcomputersreceive two lassesof data: inslruclions and operands
The instructions tell the computer which operations to perform on the operands
t3
tHt aog6Boo!(
Similar to the operation of our imaginary factory, the 80386can work on more than
one instruction simultaneously.In the jargon of the computer industry, this is called
PiPetining.
shipplng
& Wictgets
leceiving
Shipping M
& E
Rcelvlng M
o
R
Ahhough simple, this picture of the flow of information through the 80386is fairly
accurate.The three departunentsin the example coffespond to six logical units in
the 80386,as shown in Figure 2-3.Eachunit operatesin parallel with the other
units. later sectionsof this chapter describethe operation of each unit.
14
2! lh. a0306 lrchlt.crur.
1 6M l l z
l5
v
tHE ao5a5 BOOK
5-tti6cks io:Teiiocks
80386 ilicroarchitecture
Figure 2-5" shows a block diagram of lhe internal operating units of the 80386.
Althoughthe progfammersesthe 80386asa sinSleentily,il is instructivcto see
how th 80386achievesthe division of labor that ontributes to its speed.
80386ovryiew
full 32-bitafchitectuf
e
32-bi,r
32,bi,r
t6
* th gO3A5lrchil.clure
Execution unit
The execution unil is the part ot'lhe CPt.llhal d()cs computrlions.lt performs 3ny
shifts, additions, muhiplications, and so on that arc ncccssa.yio accomplish an in-
strr.rtion. The register set is contained inside the execution u.it. Thc unit also con-
tains a logic component called ir barrel shifter, which can pcrfbrm multiple-bit shifts
in a single clock cycle. The exelution unituses this capabiliry nol only in slrift in-
sructions butin acceleratingmultiplications and in generaringindcxed acldresses.
The execution unit xlso tclls thc bus intcrfacc unit whcfl it has data that needs to be
sent to the memory or I/o bus.
17
tHE 00346 BOOK
Segnentation unit
The segmentationunit translatessegmentedaddressesinto linear addresses.Seg-
ment banslation time is almost entirely hidden by the parallelism of the 80386.At
the most, one clock is required to complete the addresstranslation.The typical case
is zero clocks. The segmentationunit contains a cachethat holds descriptorrable in-
formation for each of the six segmentregisters.This unit is described further in
Chapter3.
Pagingunit
The paging unit takesthe linear addressesgeneratedby the segmentationunit and
convertsthem to phtsical addresses.If paging is disabled,the linear addressesof
the segmentationunit become the physical addresses.\X/henpaging is enabled, the
linear addressspaceof the 80386is divided into 4096-byteblocks cralledpages,Each
page can be mapped into an entirely different physicaiaddress.Chapter6 discusses
the pagingprocessin detail.
The 80386microprocessorusesa page table to translateevery linear addressto a
physicaladdress.The paging unit contains an associativecachecalled the transla-
tion lookasidebuffer (TLB). whlch contalns the entries (new addresses)for the 32
most recently used pages.If a page table entry is not found in the TLB, a 32-bit
memory read cycle fetchesthe entry from RAM. Under typical operating conditions,
lessthan 2 percent of all memory referencesrequire the 80386!o look outside the
TLB for a page table enrry.
The time required !o pefform the translation variesbetwen 0 and 5 clocks. Thanks
to the TlB, the typicaldelayis only y, clock.
la
* lhe aO306 Archit ctu..
low-order bit of the operand is designaredas bit 0, the high-order bit in the 1ow
oder byte is bit Z and thc low order bit of the ncxr byte is bit 8. Figure 2-6 shoss
the bits in a registerand in mcmory. If the operand residesin memory, negarivebit
offscts can also be used. Bit -1 is rhe high-order bit of the byte immdiarelypreced-
ing the memory address.
l1
EAX
RlqjDre2-6. Bt sttlnas.
Bytes
The byte is thc basicunit of addressability on the 80386irharis, address3 refersto
the third byte in mcmory,not the third dword.A byte is an 8-bitquantityrhatcan
be interpreted as cithcr ^ signedor an unsignedvalue. l'igurc 2-7 shows the layour
of a byte and fic range of i?lues thar it can specify.
7 0
Signedvaluc128 <r< 127
UnsigncdvnluoS r < 255
t9
,
tHE ao3a6BOOK
Words
words, as pfeviously deflned, arc l6-bit quantities. Figure 2 8 shows the range of
vilues that can be stored in a word. Vhen a worci is written k) mcmory. it is stored
in two bytes. The low order bylc is written to the specified address, and the hlgh
or.ler byte is written to the next consc'cutivememory iocation.
signedvaluc-32768<r< 32767
Unsigncdvalue0 < r < 65535
Dwolds
l)worclsarc 32-bi!quantitiesl-ikel)yicsand words,they cnnbc siSncdor unsigned.
Thc cxlrr bits allow represenlxtion olinlcgral valuesgrenterlhln 2 billion. ligure
2-9illLrskatcs thc rangeof valuc$fbr dw()ds:lnd the way thcy arc $k)rcdin
mcmory.likc words,cLwords are s(xed in mcmorylow-oftlerbylc first lf thc low-
o.dcr bylc is sloredat address,r, thc high4rdcr bylc is storednt {dclress,t + 3.
^+3 ^+2
Ouadwords
Qu3dwods are 64 bir numcric quantities. No instructions rcference quadwotd
memofy opcrands. However,the 32-bit Multiply instruction generatesa 64 bir valuc,
with the high-order bits in registerEDX and the loworcler bits in regirter DAX.
Converscly,the Divide instruction acceptsa 64 bit dividend stored in the same
register format. Storinga quadword in memory rcquires two MOV instructions.
m
2: th aO3A6Archit.crure
D c D D c c n n x l r T 9 3 2
. ' - m
m - @O
n c o+
, o,,,..'.
;i; ; '.' ,-. J''
0011 3
0100 I
0 1 0 s1 l - 9 { 2 " ,
o l l 9 : f f i -i ; i r l *
0111 7
1000 lJ A.l.lrcss
- d+1
r00t 9 ^+2
1010
r )tnvil
llll
Rlg$re z-rc. ACDstoage.
Ilec se ASCII and nCD provide ways to encode numeric valuesand do nor have a
fixed length, they can be used to implement variable-precisionnumbers.1he 80386
supportsASCIIand BCDarithmeticvh fie DcimalA.ljusrandASCIiA.Ljusrin
structions.Chapter.idiscusses ASCIIxnclBCDarithmetic.
21
lllE 00345BooK
The 8036 register sel is partitioned into five classes:the geneml registers,whjch
applications use fbr daia storageand computaiion. seqmenttegbters, whi'.h alfecr
memory addressing;protection rcgisters,whichhelP suppo.t the operating qftemi
control regtste5, llhiah tivrdify the behavior of the pro.esso\ ^nd .lebug and test
/eg,ste,'qwhich are used as their name implies.
Gene?alregisters
The generalfegisters are named EAx, EBX,EO(, EDX, ESI,EDI, EBP,and ESP,as
shown in Figure 2-11.As a rule, any instruction can use any general registerexept
ESqeither as an operand or as a pointer to an operand in memory. ExcePtionsare
notedin Chapter4 in the discussionof the instructionset
l 1or5 ,3
Il3"\ AX CS
t A H I A L s5
llBx Bx DS
B H I B L E5
licx cx FS
r c H r c l GS
tsDX DX
I D H I D I
EBP
DP
ESI
r s l
EDI
D1
ESP
t s P
Generalregisters
22
2r flr. aOSaGArchlt.crur.
3l 1
E!*:iii!!:::i:,:
:::a:r|::ta:At::
i::@1:l::1rr:
Fitf:t i iii* iil
ionror,
EE'::l::::l::li::l
i::::l:!9iil:lii
I 15I
5!
senera' reEDreF
GeneralreSisters
I sozaercgisrer
! s03s6registers
extensions
Flgtte 2-12.386/286rcBlsterc.
EFLAGS regist.r
A breakdown of the EFLAGSrcgister looks like this:
Vu-Vlrtuar 8086 mode: \qhen this bit is set, it indicatesthat the currently exe-
cuiing instruction streamis &86 code. The implications of virtual 8086mode are
coveredin Chapter7. Applications cannol changethe vM (virtual machine) bit, and
instructiofls that modify EFLAGSleavethe \44 bit unchanged.only the task-switch
operation or an interrupy'interrupt return can alter fie VM bit.
zt
rHE 40365BOOK
B.F-Resunefag This bit controls whcthcr a debr. fault cln be generated dur
ing the executi()n of an instruction. Ii(hcn an exception occurs during progra'r cx-
ecution, the 80186 pushes the cu.rcnl CS,ElP, and RFLAGSregisrersonto thc slack
and transfers control to the proper exccption handler.The stack image ofthe
EILAGS register has the RF'bit set b l. whcn the exception hrndlcr returns to the
inteffuptecl i.struction, the RF bit is on, anclthe 80386 pfevents a cLebugfault f.om
being generaied. Any otherfaults (such as Prge faults or protection faults) occur as
uslral. fhe debw cxccption has the higbesr priority of all 80386exccptionsi il
thercfore, an insrrucrion caL$es muhiplc faulrs,the first one Processcdis the debug
cxception. Vhcn conrrol returns t() thc inlcfftpted instructi()n,thc lt-t bit is set. and
the instruction is completed without rcriggeriqg the clebugfalrlt. 1he 80386clcars
the ltF bii upon completion ot'thc imcrrupied inst[tciion. (sec ChaPter5 fbr 2
discnssion of cxccptions rnd support fdr debugging.)
A p()cedurc s currem pfivilcgc lcvcl (Cl' l) nnrsrbc cquxl !o or morc Privilegcd thxn
rhc IOI)L to cxccutc xny ofrhc litk)wing insh\rcrnrrsr IN. lNs, OU'l OllTS, CLI, or
Sl l. ,{ p()cudurc tlul can cxc{r.rlelhcsc instnrti(nrs is sLridio havc //O/rirltu8?
OF-OoerJloroJl4g. whcn xn rfilhn]clic intcger inslrlrcli()n is cxccutcd, thc OI
bir is sct ifthc rcslrlt is too large or t(x) smdll to fit io the destinalion register or
nlcrno.y ^ddfess. Bcc,rusclhe OF flag is scl rcl.rtive to inlcgcf insl.uclions, thc
80386 presunrcsth.rt lhc .lestinatbn fcgister is one bit smxllc. in size to rlk)w fi).
litc sign bil Thc ti)lbwing lnstructions ilh$trate some exx'nplcs
H o vA L , 1 2 7 A L * 7 F H ,l a r q e s t 8 - b i t
s i g n e dj n t e g e r 0 F : 0
ADO
AL,2 r e s u l t , A L - - 8 1 H( l ? 7 )
s h o u l db e A x : - 0 0 8 1( 1 2 9 ) , 0 F - I
Note that the Ol' bir is ignored if Lrflsignedarithmclic is intended R)r cxirnPle,
acl.ling 127 and 2 in fegistcr Al generates a vxlicl, unsigned reslrh ol 129
U lh. ao:ta5 Archlt.cruE
DF-I re.dor.flag The direction flag bit modifies the behavior of the string in-
structions:MOVS,STOS,LODS,CMPS,SCAS,INS, and OUTS.Vhen DF is 0, rhe
string instructions operate on incrementally higher addresses.Vhen DF is 1, rhe
memory addressesare decrementedjand the operand addressesbecome progres-
sively lower. The Sm instruction sets the direction flag bit, and the CLD instruction
clearst}le bit.
IF*Inrct rpt etalieflag Vhen ihis bir is ser,the 80386respondsto external
hardware intefiupts. When the bit is reset, inrerupts are disabled,and the 80386ig-
nores the hardw?re inre(upt pin. Note that this bir does nor affect the NMI inter-
rupt. The processoralflays respondsio fauhs (exceptions) and software interrupts
regardlessof the setting of the IF bit. rx/hen IF is 0, irnerupts xe sai tobe masked.
The STI instruction stslF to 1,and the CLI instruction clearsIF to 0. The interrupt
enableflag is also rnodified when an IRET is executed.A POPFinsrruction modifies
the interrupt enableflag only if the procedure executing the instrucrion hasI/O
privilege.
|E-TfapJlI4g: The trap flag bir assisrsin debugging programs on the 80386.
'Vhen the TF bit is st, an intefiupt 1 occurs immediately after lhe nexr insrruction
executes.The trap flag is usually set by a debuggerithe debug capablliriesof rhe
80386are covered in Chapter5.
sF-stAnlr4g The sign flag bit changeswhen arifimeric or logical instructions
are executed.The siSnflag bit rceivesthe lue of the high-order bir of the result
and, when set to 1, indicatesrhat the result of the instrrrction is nesative,
I'tovEox,-1 : Slgn flag unchangedby M0V
A00tox, 3 : EDX-- 2, SFnov 0
IiEG EOX : EDX-- -2, SF nor I
ZF-Zerafla& The zeroflag bit is serwhenarithmeticinstructionsgenemrea 0
result,
r 1 0 vA L , 0 ; Zeroflag unchangdby 0V
; AL unchanged,
ZF no|{I
A,F-Auxtttat! canlJ aE, The auxiliary caffy flag bit indicatesthat a carry out ot
the low-order nibble of the AL register occufied in an arithmetic instruction. This
bit is used by the ASCII and BCD instructions. It allows implementation of multiple-
digit precision decimal arithmetic. The following e{ample assumesan ASCII encod-
ing oFthecharacters 4 and 7
: AL - 34H,AF unchanged by t{ov
400AL, '7' ; AL - 68H,AF nox I
: A s c I I A d l u s t ,A L- - 1 , A H- A H+ I
E
PF-Parttf fa& The parity flag bit is sct to 1 when an arithmetic insklrclion
resulrs in x value with an even numbcr of I bils. lbr exall1ple, if you issued the fol-
lowing instructions, the resuhing parity flag bit wot d be 0.
t 4 0 vA H , 9 1 H : AH- 100100018, by Mov
Pr unchansed
ADD AH, O5H : AH- 100101108,
PFno|{I
CF-Crrry fla.g, The crr.y flag bi1 is sct whcn the result of an arithmctic opera-
tion is too largeor smallfor the deslinationregisterormenory adclfcss. ft is similar
in operalionto the oF bit but indicitesan unsigncdoverflowoftbe destination.
r'r0v
AL, 127 j A L - - 7 F H , C F u r c l r a n g e db y l 4 0 v
AOOAL, 2 ; AL -- 81H, CF no|{0
AOOAL, AL ; A L - - 0 2 H , C F n o | {I ( r e s u l t i s 1 0 2 t 1 )
t ' t o vA L , 3 C F u n c h a n g e db y f i 0 v
SI]B AL,4 A L - - F F N ,c F n o l l 1 ( b o r f o v b
i it)
Sogment regi$tets
Thc scgmentregistershold the l".rluesthat dll{t which portionsofmcmory a pro-
gramuses.lbur segmen!rcgistcrsarc uscdunderspecificcondilbns,ancltwo are
availahlcas pointersto frequendyuscdarcasoi mcmory.The CS,DS,SS,and tsS
rcgis@rs were inheritedf,om thc 802U6and performthe samefunctbns asthey did
in rll.uCPU1$o. dinor.rlre8rir.r., fS rnd GS.rrc ncu to the 803U6.
Associared with thc scgmcntrcgisiersis a descriptorcache,which hoklsthe starling
addressof the mcmoryscgmcntand otherrelaledinformatk)n.chapter3 detailsthe
relntionshipbctwccn scgmcnlsand memoryaddresses in thc 110386.
The descriPior
cachefor thc scgmcntrcgistcrsis not accessibleb the programmcrionly the 16$it
'egistcrp()rlioncanbe rcpesseddireclly.Figure2-13illlrstfa(esthe segmentregis-
tersand thc intcrnalclcscriptor
cache.
26
2 th6 ao:ta6archll.ctur.
Base
! Programmeraccesible CDTR F::r
IDTR
n Not accesible
Control registers
The ontrol registersregulatethe paging and numeric cop(ressor operation of the
80386.A generaldescription of rhe registersfollowsi refer to lhe specific chapters
on paging and coprocessorsfor more detailed information. A programmer can only
read or modify control registersby instructions of the fbfln MOV CR , /eg where
,?g standsfor one of the general registers.A procedure must bc running at the
highest privilege level to exe.ute these instructions.
2l
fl{E 40305 BOOX
CRO-Control rgister o
The foilowingillustrationshowsthe contentsof controlregister0. TheLMSVand
SMSWinstructionsallow accessto the low-order16bits of CRoasthe machine
MP-Math lrrcsent; The operating systemsets this bit to 1 al boot time if a math
coprocessor(either the 80287or 80387)is present.The MP bit affects the operation
of the rvAIT instrwtion, as described in chapter 8.
PB-Pntaect ena.bla. Settingthe PEbit placesthe processorinto protected mode.
Typically, this is done once, at initialization. Unlike the eadier 80286,the 80386
makes it possibleto switch the CPUback into real mode after entering protected
mode. Someimplementationsof the OS/2 operating systemuse this technique io
allow real-mode MS-DOSprograms to run concurrently with protected-modeOS/2
n
2! th. ao3a6 archli.ctu..
CRI-Control iogister I
Controlregister1 is not usedin the 80386andis reservedfor futureIntel processors.
CR2-Control reglster 2
Whena pagefaultoccurs,the CR2registeris loadedwith the linearaddressthat
causedthe exception.Referto Chapter6 for moredetailson pagingin the 80386.
CB3-Control registor 3
The 80386paginghardwarealsouseslhis registerIt containsthe linearaddressof
the startingpoint of the pagedirectory.The implementation
of pagingis covered
fully in Chapter6.
GoprocessorSupport
The 80386can operale wiih either the 80287or 80387numeric data processor
(NDP).Because rhesespecul-purposechipsoperatein parallelwiththc80386.they
^re calledcoprocessors.'lhe 80287is a slower chip with a 16-bit interface,original ly
designedfor usewith the 80286.Floating-point performancewith the 80287is ap"
proximately 320,000whetstoneswhn running at 10MHz. The 32-bit 80387offers
higher performance.This processoris software compatiblewith the 80287and can
executeabout 1,8'00,000 whetstoneswhen running at 16MHz. Appendix F notesthe
differencesbetween the 80287and 80387 Referencesto the 80387in the following
text also refer to the 80287unlessotherwise noted.
In addition 1othe raw performance advantageof hardware support for floating-
point arithmetic, the NDPSintroduce another levi of parailelism into the system.
As soon as lhe 80386passesan instruction to the 80387,it begins operating on the
next instruction regardlessof how long the 80387takesto complete its opemtion. Of
course,if the 8036 encountersanother floating-point inslrution, it must wait for
the coprocessorto completethe curent operation before the 80386can give it
A
tHE eo3a6BOOK
To use a \alue computed by the 80387and written Io memory, you must ensurethat
the 80387has completed the write operation. The F,firAlTinstruction ensuressyn-
chronizationbetweenthe 80386and 80387
If a coprocessoris absent,the 80386allows an operating q'stem to emulate one and
remain invisible to thc applicarion. For derails on coprocessoremulation, seethe
discussionof the EM bit in control register0 of the 80386earlier in this chapter.
15
f--l__l*o.ain,.g..
3l
f--_lu.on'n,"g",
6 3 ---_-l 0
| , , tong,n,"g",
d r 7d r 6d l ed t { d r , . . . . d 4 , d 1 d 2 d r 4 BCD inteSer
Sign l-\ponent
31 30 23 22
I'T-*-l---___-.]'r'on'""r
63 62 52 5l
30
a Th ao3l5 Archit@l(b
Floating.pointnumbers
The 80387supportsthree floating-point formats. This allows a programmerlo make
compromisesbetween the amount of mmory required and the Fecision of the
results-The srorl /eal format lets programmersspecify numbersof about six deci-
mal digits of accuracy.This format is also known ^s sinele-Wcklon bec usea
short real number fits into a single 32-bil machine word . Iong reak, also U,^oa"nas
double-precision, rcVesent floating-point nlunbers of up to 15decinul digits of ac-
curacy. Holding a long real mnnber requires a double machine word (64 bits). The
rhird format is called tenp (temporary) real ot extended-preclslan.'lemp re l nnm-
bers are 80 bits and representabout 19decimal digits of precision.
3l
fltE ao3a6BooK
,1il1,[[
;[|i,ff]:
Dccimalpoint
37 2101dccimal
Birary poini
6.5625decimal
Sinsledisit befo.c l
F
"',..
""': .-",1L
,Longrcr l.l Sirnttlcand | - IracrionrMSBimpliuJ)
l
Slgnificand
rempreatITTT --Tl F.acrion
dirctlyrepresented
Flg0.ie2-16.FIM nS-potntfuctions.
Single
- t sosr..,sz2x2rctp-127)
Abslutevalue
The bias for the short real exponent is 127.The signifiond includes the "implied 1"
bit and dlows a precision of about six decimal digits. Representativevalues range
from 11.18x 10-33to i3.40 x 1033.
32
2: lh. AO3a6ArchltcruE
LongrcaI
"implied 1"
The bias for rhe long real exponent is 1023.The significand includes the
bit and allows a preision of about 15decimal digits. Representativevaluesrange
from 12.23 x 10-303to 11.80x 10303.
TefiP tcdt:
Extended
so s63
value- s0.sr...s63
Absolurc x 2(cxp-16r,)
The bias for the temp real exponent is 16383.The significand representsthe frac-
tional portion of the value (with no implied bits) and allows a precision of about 19
dcimal digits. Representativevaluesrange from t3.30 x 10-4e32to !7.2 x li4e32
sttec-r^rlaa nA-Irornt ta&res.' In addition to intuitive valuessuch as 3 14159and
6.03x 1013, the 80387represcnts luesthat ariseunderunusualconditions.These
"not a number"')
val\es ue . lle<l lnfinlttes, denornals, ^nd NaNs<NaNstandsfor
Infinity, positive or negative,is representedby a value whose exponent field is all ls
andwhosefractionis 1.0B.Notethat in shortand longrealnumbers,1.0Bis rePre-
sentedby a sig:nificandofall 0$,whereasin lemp real numbers,the significand is a
binary10000000...0B.
Denormals are valuesthal are too small to representin the standard (or normalized)
fashion. Denormals are representedby a !?lue with an exponent field of0 and any
nonzero lue in rhe significand. A floating-point number with t$th an exponent of
0 and a significand of 0 represents0.0.
NaNsare invalid representationsof floating-point numbers.They are identified by
an exponer{ field of all ls and a significand other than the one representinginfinity
The two kinds of NaNsare the slgnaling NaN and the quiel NaN.A signaling NaN
hasa fraction of the form l.ojc.tx. . .rB, where , representsany bit value. The 80387
generatesan e{ception whenever a signaling NaN is used.The 8037 never creates
33
fl|E aoi[6 toox
a signaling NaN,but a Fogrammer can use one to indicate some erfor condirion
such as an uninitialized floating-point variable.The quiet NaN hasa fractional for,
mat of l.Lrro(rrB. Recallthat the leading 1 is not implied in the significand of short
and long reals but must be present in temp reals.The 80387generatesa quiet NaN
insteadof a numeric rcsult whenever a floating-point instruction causesan in\,?lid
operation. Any instruction that receiveseither tt?e of NaN as an operand genemtes
a NaN ai a result. The following table lists specialvaluesused by the 80387
The "x" indicatesthat it makes no difference whether the bit is 0 or l. The .1" before
the decimal in the fraction is physically prsentonly in temporary real format. It is
implied in the short and the long real fomats. Denormalsare recognizedin rhe
short and the lonS format by the 0 exponent value.
BCDIntogor
The other new data type that the 80387supporrsis a packed decimal integer of 18
digits siored in 10 consecuth byte6 of memory. The high-order bit of the high-
order byte is interpreted as a sign bit in the sameway as floating-point numbers.
The rest of the hlSh-order byte is unused.The remaining bytes each conrain two
BCDdicits.
7211 64 0
0 dd
3{
2: rlt. ao306 lrchit*l{r.
0
t
2
3
4 FIP
5 !cs
6 FOO
7 tos
Iloatlng-pointreSisters Errorpoinlels
35
fl|E ao:p6 !o0l(
sT(0)
E;,ol
o sT(o) 0 sT(o)
1 ST(r) 1 ST(r)
2 2
3 3
4 4
5 5
6 6
7 7
0 0 sT(l)
I I ST(2)
2 2
3 3
stp,"rl
5 ; FMITTP
6 o
1 7 ST(0)
0 sT(o)
I ST(r) sT(o)
2
J FSUBRPST(1),ST trov;*q r l
4 I r'AJ'r con$ |
5
36
2: lho ao3a6 lrchlbcrur.
0 0
I ST(0) I
2 2
3 3
4 4
5 5
6 6
'I
he 80387 registcr iddressd by sT(t) varies llccor(Ling to the valuc of the ToP field
in the statuswod rcister.The following section (Lscribesthe ottrer fields in the
statusworc1reSrslcr
,-arrsJ. l'his bit is 1 whcn thc 80387is exccutingan insructbn or whcn an un-
maskeclcxceptbn(bits 0 5) is indicnted.lxccutc lhe instructionINS'ISV AX,
wirich copiesrhc slxtuswod registerlo lhc AX registeroithci303861o lestthis bi!
ca, C2, cb Co-condltton coles.' The 80387setsthesc bi$ when a fk)aling-
point comparc,tesl,orexaminc inslructionis exctu|cd.'Ihe variouscombinalions
thatcurarc discussed in chaPter8.
undcr thc rclcvantinstructi()ns
11oP-Top'o:f-stack: This iield indicaleswhich of the 80387naclrine registers
functions as the &)p of stack.\(/hen a new valuc is Pushedonio thc register stack,
(hc valueof ToP is dccremented by 1.vhcn ^ valueis poppedfrom the stack,ToP
is incrementecl by l. The resultsof thc incrcme or decrementaretruncatecl!o
Ihreebits to allow addressir\gofeight fk)aling-poinlregisters.
ES-Er''olr sutmnarJr The 80387scrsthis bit to I whencver a floating poinl in-
struction generatesan unmaskeclcxccplion. The exceplion indicatorsare birs 0-5
The exccption masksthemselvesarc located in the control word register.
SF- Stack Jault The 80387sets this bit to 1 if an instruction causcsa stack over
flow by pushing loo many operands or a siack underflow by popping the stack
when there arc no more values.This field does not exist jn lhc 80282 so floating-
point code that must run on eithef coprocessorshouldnot rely on having the bir' A
stackfaultalsoresultsin an invalidopcrationexception.
37
tHE to3e6 EOOK
3a
2r lh. 40306 lrchti*hrr.
l2 a 7
x x x x RC PC x x PM UM ZM DM IM
Bt 12-O (r4Fnry con rnt on A02A7): Bit 12is ignored on the 80387 On the
80282 this bit selectseither affine or projective closure.Affine closure allows the
use of both positive and negativeinfinity. In projective closure,very large or very
small numbers overflow to a single unsignedinfinity. The 80387only supports
affine closure,
Rc-Roundtsg cott el: This field specifieshow the 80387handlesvaluesthat it
cannot representexiKdy, The RCfield can be set to one of the following modes:
oo-Round 0owardnearest(choose even number lf equidistano
o1-Round toward neSativeinfinity
10-Round toward positive infinity
ll-Round toward zero (truncate)
To seehow the rounding control affectsthe results ofa computation, assumethat
the 80387can representonly the integers-5 through +5. Figure 2-19on the follow-
ing page showsthe results of rounding the values21/t,7'/r,-11/t,a d-zth ineach
rounding mode.
Pc-PrccTator, cont ol: The PCfleld tells the 80387which floating-point format
to usewhen generatingthe results of add, subtract,multiply, divide, and squareroot
operations,This field can hold one of the following valuesl
00-Single-precision (32-bit)
o1-Reseffed for future coprocessors
10-Double-precision (64-bit)
ll-Extended-precision (80-bit)
Instructions other than those affectedby the Pc field generateextended-precision
results or have a precision specified by the operand.
39
THE A03A6 EOOK
f i - + - -
- , , - 2 - 1 0 1
<-j <-i
Blgofrc2-19,Roundtw contral
PM, AM, OI4 ZM, DIL IM-Mask bltst The remaining bit$ in the control word
regisier are the mask bits for the exeption conditions and correspond to bits 0-5 of
the statusword rcltistcr.The maskbitsarel
Prcisionmask(PM)
Underflow mask (UM)
Overflow mask (OM)
Zero divide mask (ZM)
Denormal operand mask (DM)
Invalidoperationmask(IM)
n
2! lh. ao0a6 aEhlroctur.
1 t5
F
F( o
0 ros
7 0 7 0
l r l 0 r l r l ?? l ?
FFll(op,i.M,
Fl.'lT{' b},es)
Firsrbrre Scco.d blte
41
IHE ao3a6 BOOK
42
MEMORY
ARGHITECTURE=
SEGMENTATTON
32lines,2" possibleaddresses
80386
H\X/
c1
c2
ca
0
c1
4
3: Itomory Archilectur.: S.gmontrtlon
t0v A L , C Sl:0 l
k) fatchlhe firsl byte from thc codescgmcnl.
Previursgcncrntionsofthc l.l0il6lzn ily :rlsodcalt\\,i!h scgmcnrodlncnl()fyihow-
cvci thcseprcccssors lnnircdthc sizeol a scgnrcnrro 64 Kll, wl)ich w:lsofrcn t]r((h
too snull. A singlescgmcn!in 1hc80386crn l)c 1|"5 lxrgcxs,i cB.
An opcr^tingsystcmcicsigner canch(x)sclo simuhlc I lincaf mcnxr.ymodcl(.lso
calledx/at nbdcl) on rhe 80386by crcatingone vc.y hrgc codc sc3menranclonc
very largedalasq]menland luving .rll progrltmsuscrhc sanrevahEsf.r CS.rndDs.
Tbis is a conmon lcchrique when porLingsysremsrharluvc fLnr()n Iinerr:rd.Lrcss
machines. The UNIX operatingsysrcDr-wirh its VAXhcritxge-is rypjcallyilnple
mcntedon linearmcrnorymachincs.
Virtual Addressing
Ixcept when operating in .cal mode, rhe 80lM) is ! rirtual memory .tracess<)t.
\vhcn an instructionrequcsrsrhe contentsofa menory location.rbe insrudion
rclcrs to dle location not by an actual b.rdwarc memory addressblrr by x /l,.rral
,rddress. The vitual addressis really a namefor a rnemorylocarion.The processor
translatcs thc bcation namcim()an appropriatcphysicallocarion.Thc operaring
systemmustDaintaintire propcrmappingberwccnvirruatand physicalmemory.
This conccpt is not as convolured as ir mighr sound.For examplc, supposethat
someonesaysl() me,liPutfiis .cport on the boss'sdesk.,,In my parriclrlar
45
tHEao3a6BOOK
'l
he cPU translatesa vjrtual addrcs$to a singlc 32-bir nltnhet calle.l ^ linear ad-
lr".$ Figure3-3 showsan examplcoiaddresstranslation.Tbis lincarrcldrcssgoes
out on the sysrcnblrsLrnlcss thc pagir\gfcalureis enablecl. Pagingis anotherlevcl
of acldresstr^nslationand is dis.ussccL
fully in Ch.pter 6.
4 CI'] Mc'nory
46
3r flmoly&ciltectu..: Sqm.d.tton
,( OB Mcmory
33EC405[l
47
rHE Ao('a6tOOl(
Segnent descriptors
At this point, )ou probably visualize a descriptoras something like the itern in
Figure 3-5. Indeed, all the data in this figure is in an 80386descriptoq however
becauseof spaceand compatibility constraints,the rgal thing is not quite so pretty.
Figure 3-6 showsthe actual format of an 80386segmentdescriptor
Etglo,rc3-5. vtsuauzeddescrtotor
80386
48 32 ) l lo
Base I s
Llmit Limit
c 1 6 .. 1 9 DPT T}PE 0..23 0..15
2 4. . 3 7 L I 0
-
A"*r"
rights
80286
63 4a 47 3r t6 15
I
limit
P DPI : Tvpe 0..23 0..15
I 0
A.**
ign6
ilo
3: I.m.ry Archh+ture:3.gn. .rto
80286descripro.
as storeo n memory
15
limit
rights 1 6. . 2 3
80385descdptor
Limit
0..15 0..15 I
II
GD
Limit
16. . 1,9
J
L
.ights 16. 23 High
49
tHE ao3a5EOOK
Iegallnsrrlrcdorr Reason
MOV !AX, I0000Hl last byte rsd is 3H
MOV llAX,lolFCIll tast bytc readis 0fI|8
MOv Ar, [0FF!rI k$ byle rcad is oFIFH
MOV IoFFEH,AX last byle Mitten is oFM
50
3r lo.rt Archtt.ctw.! S.Cfrrr.tbn
Access ,.tghts: The accessrights trxlrion of the derriptor hasthe following formatl
7 6 5 4 3 2 \ 0
The P bit srandsfor "present.',Ir is set ro 1 when the segmentindicated by the selec_
ior is presentin phtsical memory. In a virtual memory system,rhe operating sysrem
can move the contenrsof somesegmentsto disk if physicalmemory is full. it tiren
marks the descriptor as not present by resetting rhe p bir to O.If an application loads
a selectorirto a segmenrregister and the descriptorassociaredwilh the selectorhas
P * 0, the not present inteffupt (11decimal) is gederared.
The operaring systemthen looks for a free areaof physicalmmory copies the con_
tents of the segmenrfrom disk back into memory, upclatesthe d9scriptorwirh the
new baseaddress,sets P to 1,afld restartsthe inteftupted instruction"
The DP^Ifield conrainsthe privilege level of the descriptor.The privilege level
rangsfrom 0 (mosr pdvileged) through 3 (ea$t privileged). A task can;cess seg_
ments of equal or lesserprivilege, A task can only read data from or store data in;
segmentsof equal or lesserprivilege. A rask can call only code segmentsof th; same
privileSe:]pwever accessto segmen(sof higher prMlege may b4ranred
indtrecrly
v|arne durdoprotectionmechanism, A taskcan neverinvokea codesegmentof
lower privilege.
The prMlege level of a t^*., calkdrhe curreht prtuttege leuel <Cpl), is the p|.:lihfJge
le.r'elof fte currendy exe(urin8 code segment.Trpicaliy, the most secureporuionJ
or rne operatjngsysremrun ar level0, oher systemsofiwarem ight run at a less
applicarionstypically run at level 3. SeeChe;rer 5 for a descrip_
fivileffd
tion of rhell:el_anq
80386privilege mechanism.
The s (for segment)bit is als,q,s ser to 1 for a memory segmen!..when s is equal to
0, a descriptordescdbesan object other than a memory segment.These obj;ts are
described in the chapteron the 80386protecrion mechaniJm,Chapter5,
Th TYPE field indicatesrhe types of operations allowed on the seAment.Valid
cypsare:
0 Rsd-only daraseSmenr
r Read/wfitedatasegmenr
3 Read/writex?and-downdarasegmenr
4 Execute-onlycodesegmenr
5 Execure^eadable codseament
O Execute-only,,conforming,,
codesesmeD!
7 LKut/readabte,,onformina,codesegment
The tt?e indicator defines the accessrules applied to a segmenr.The CSregister
cannot be loadedwith a selectorof a segmentof rlpe aata 1O3;. No prograir can
5t
tHE AO3A5
aOOK
As more data is pushed onto the stack,the stack pointer (ESP)nears0. If too much
data is pushed onto the slack, the program nftemptsto decrementESPbeyond 0,
resultingin a stackfauh.At this point,the operatingsystemhasno choicebul to ter-
mnate the program,
Placingthe stack in an expand <lownsegmentrather than in a normal dala se8:ment,
however wiLl changethe way mcmory is addressedinside the segment.
52
3! l|ffy lr.hit.crw.! s.gm.nt.fion
'*'-l In* | |I
2048
FF!FFFTFH
ISS
L \,_ lo
Nornal dataseSment Exparddown segment
Expard'down segment
FTFFIFFTH
,-{
3@6
Old limlt
5:l
rHE 00306 BOOK
Descriptor tables
All the derriptors are grouped together in dcscriptor tables.The two sysremde-
scriptor tablesare the Global Descriplor Table (GDT) and the Inrcrupt Descripior
Table (IDT). The IDT contains flo scgmcntdes.riptors, so ir is not dis.usscd here.
A full descriptionof the IDr and othcr facetsof the 80386p.otectionmcchanismis
givenin Chapter5.
An operating q'stem can also implement various local DescriptorTables(LDTS).
Segmentdescriptorsare found either in the GDT or in the cuffently active lDT. The
selectorused to identify the descriptordetermines yhich table to use. The location
oithe tablesin memoryis cleterminedby the GDTR,IDTR,and LDTRregisters.
Soloctors
A segmcnt,as wc hxve seen,is da*:ribe.l by ^ descriptorthar has been J?/ecrcl by a
selcctor.A sciccloris madeofthree c'omponents, asshownin the following
illustration.
15
INDIiX TI RPI
RPI = 3 (lowest)
To use a selcctor,hardware must first break it into three fields, INDEX, TI, and RPL.
Figure 3-10jllustrates how hardware separatesa selocrorinro its comfx)neors.
54
3! X6.ry Archhetur.! S.!6trt.tlon
GDT LD13
Blgnrc 3-1O.Selector.anponents.
Systemmmory
FI
T t--._l
1 |
*o'o,
|
^
llooKBS
|
a I
t t l I l I
55
fllE ao:86 BOOX
osit: ,.r""I[;_-]
B
c
1oo
KBI
El$trc 3-12.Initial tasbslaadedlnto nemar!.
56
* l|.n.ryArchlt.crurcls.ln.nt ttotr
Disk
Descriploruble
57
tl ao3a6900x
Pe?formanee considerations
As the previous cxamplc shows,virtual memory doesn't createRAM out of thin air;
it.usessecondary slolage,usually disk, to supplementthe p/lnrdry (RAM) storage
and give thc appcarance of moreprimarystoragethanexistsin the system.Th
cost of keeping up appearancesis the amounl of time it takesto move data between
primary and secondarystorage.The mofe time the systemhas !o spend swapping,
the lesstime it an spend executing th(r applications.On extreme occasions,a sys-
tem can be so overextndedthat it spendsall its time swapping segmentsin and
out. This pAthologicalsitr rion is <?'lledthtushing.
An opemting syslemdesignercan improve the performanceof a virtual memory
system.On the 80386,for example,code segmentsare immutable, Becausethe con-
tents of a code segmentdo no! change,it doesn't have to be swapped out, You can
recreatethe contentsfrom the originalexecutable imageofthe program,Only
swapping in requires accessto secondarymemory. The operating system,therefore,
can swap code segmentstwice as fas! as it can swap data segments,Actually, if 'ou
recall the contents of a descriptor,you will rememberthat certain kinds of data seg-
ments can be marked as read-only, Like code segments,read-only data segmentsdo
not have to be written !o secondarystoragewhen swappedout.
Another trick that designerscan use also relies on knowledge about code segments,
Thc technique of sggmert shallng lels lwo or fiore taskssharethe samecode, This
is primarily effective in multiuser systems.In the previous example,assumethat
tasksA, B, C, and D representusersrunning applications,Supposethat usersA 4nd
C are running the sameapplicatioo, perhapsa spreadsheet,Now usersA and C are
operating on different data and require separatedata segments,They are, however,
executing the samecode. Figure 3-15showshow all four applications can fit in
physicalmemory in this situation. The usersmaintain separatedescriptorsfor their
code and daia, but the baseaddressesfor the code segmentsofA and C point !o the
5a
3! fl6tmy irchllclN! S.lhdtt.tl.n
1OO
KB lOOKB
Sgment 5KB
l:.llll
rtiti 20 KB
20KB
f_-lrre 80 KB
iiiiriirliiii300K3
3OOKB
i:iiiiiiiiii
1OO
KB lOOKB
15KB
100KB freemmory
B'{,ufe 3-16.Memorylra4nen attan.
Why bother?
Becausvirtual memory is plaguedwith porential performanceFoblems and adds
to the complqxity of operating systemsby forcing rhem to deal with fragmentation
and with identifing shareablesegme s, ),ou mighr be tempredto ask, ,.Isit wo(h
the effort?" In most cases,rhe answeris yes.
One clear advantageof virrual memory is that a user doesn,thave ro spend monev
for enra memorys'mplyro geran ,ppl'calion|o rlrn. Any rppli( arionwill run in
existing memory it will simply run more slowly if it hasro be swappedour. lefs
saythat I have a systemwith 2 MB of physicalmemory and thar 90 percenrof my
59
fl{E ao:F6 BOOK
60.
+ lr.mory lrchh.ctur.! S.gm. .rbn
The 80386does not provide many inskuctions for handliqg these iffegularly sized
items, nor do many Fogramming languages.Consequently,they are awkward to
manipulate and they causemore work for the programmer.
Finally, you must deal with the problem of fragmentation. Becausesegmentscome
in odd sizes,the operating systemmusl work harder to anange physical memory
spacein which to load applications.
Summary
As you have seen,segmentationis a mixed blessing.On one hand, it pro/ides a
method for implementing virtual memory, it provides a mechanismfor implement-
ing a secureoperating ryrstemvia prMlege levels, and the segmentlimits assistpro-
grammersin tracking bugs that arise from in\alid pointers or array boundary erro$.
on the other hand, segmentationgives rise to unwieldy 48-bit pointers, extracts a
performance penally, and can causefragmentationwhen used to implment virtual
6l
THE 8(,386
TNSTRUGTION
SET
Built into evry siored progmm computer is a set of commandsthat causethe CPU
to read from a loation in memory, interplet the contnts as an instruction (that is,
as a command to perform somc funclion), execute the function, and start the cycle
over again. Becausethis sequenceis often implemented in microcode, it is com-
fi\only rcfetred !. as the ,nlcrccycle.
In one of the earliest sooredprogram computers,the EDVAC,each machine instruc-
tion was broken doEn into five tieldsr A bit pattern in one field designatedthe
operation 1obe performed, two fields designatedinput oPerands,one field spci-
fied where the resuit was to be stored,and the final field speified the location of
the next inslruction. Computerdesignerssoon learnedthat if they placed one in-
struction alter another they could eliminate the ficld that specified the addressof
the next instruction- A register called the program colurter or instruction pointer
was used 1opoint to the ner:t instruction and was incremented to pojd 10the nen
one as soon as each instruction was fetched.
a:l
t1|E ao:F6 BOOK
This method has never been modified, and the 80386microcycle can be expressed
algorithmicallylike this:
Instruction Format
Instructions are siored in memory in rhe sameway that characters,floating-point
numbers,inleSers,or any other type of data is siored in memory.The yalue 0F5H,
for example,is the encoding for the CMC(complemenrcarry flag) insrruction. An
80386instruction can range from I bytc to 16byres.
In general,the formatofan 80386instructionlookslik this;
The opcode is 1 or 2 bytes.The mod r/m and s-i-b bytes specify the operands and
memory addressingmodes.The displ (displacement)field is part of the memory
addressand can be 1, 2, or 4 bytes.The dara field specifies alt immediare operand
valueand canalsobe 1,2, or 4 bytes.
Not all fields are presenrin all insrructions. The CMCinstrucrion, as shown pre-
viously, consistsof only a single opcode byte. The instrucrion:
XCHG
EAX,EBX
consistsof only the opode and mod r/m fields. All fields are presenrin the
A D DI E S P + S ] [ E S I *147] ,
Appendix D specifiesthe bit patterns used to encode instrucrions, and Appendix E
containsa table that lets you decodebit pafterns inro the original assemblylanguage
64
4! th. ao3a6 h.lrucriotr sot
Instruction Operands
The inslructions stored in memory command the CPU lo manipulate one or more
operands.'lbe ao3t36tnstruction opcrands can be spcified in onc of five vavs:
TI\ey c^nbe implicit, register, i/nmediate, I/O, $ memoUr refeqlce opeftnds
lmplicit operands
An operanclis implicit iflhc instructiot itsclf specifiesir' TIle CLI nlsrruction'for
, u m p l < . ' ' t , r J r e s o n r h . l l ' r rI n l h ( F l l A L , S r e g i ' r e r ' l r , c p r o g r J m r n ( r d o e s n ' I
haveto spclify anythingbcyondthe insrruction.Thc stackis an nrpLicitoperandin
a numberofinstructions,fof example,I'USH,POP,CALI, andIRET Howevet
becausethc stackresidcsin memory,I will discus\stackoperandsin the sectionon
memofyrcferenceoperands.The fitk)wing instructbnshaveimplicitoperands
Registet operands
An ifftruction wilh a rcTislef()Pcrandperfbnnsan actionon dle valucthat is sk)rccl
in onc ofthc 803116 intern^lrcsistcrs(shownin ligLue4-1on lhe folk)wingpxgc)
Specifyrcgistcropcranclsl)y usinlllhc nameofthc registcrin the oPcrandficld of
the inslructbn.Notetha!not ^ll rcgisters^re lcgaloperandsfor allinstrlations' Ihc
jn
llcncmlrcgislcrs(llAX, Cl, nnd so on) are mostcommonlyused dalamaniPula-
iion in\tru(rron\V\riJ.1nor,li 'r (xrrnnlc, rn(rqm(nlIh( \rrnrenlrI 'f r :egmr'nt
regislcror Lrse^ cont|olor dehugrcgjslerk) st()rca memoryaddress
'l illustrxterypicalinstrudionsusingrcgjsteroPerands
hc followingcLamPLes
65
tHE 803A6 BOOX
31 1615 7 il 0 15
EAX AX cs
IAn,,cr ss
EtsX BX DS
l Bu, BL TS
ECX CX FS
cH,cl GS
EDX DX
DH, DL
E]JI
AP
I]SI
SI J1
L]DI EFI"{GS
DI EIP
ui" '.
Gcncrul fcgislers
Immediate opetandg
An immediateoperandis spcriiiedwhen a valucis parroflhe instructionitsell
ConsiderdreinstrucrbnADD liAX, 3.In additionro the registeroperandEAX,rhe
numeric!"lue 3 is codcdin the instrlKthn and is storedin the codesegmcnrwith
thc bit pattemthatrcprcscntsADD. Orhercxamplcsoiinsrructionsrhatuseim-
mcdiatcoperandsincludc:
l/O operands
External devicesthat Fansfbr data from the computer to anorherenvironment are
cailed input/output (I/O) devices.The 80386communicrreswith rhesedevicesin
two erays.The devicc can accessa portion oi 80386memory to read valuesfrom or
write valuesto memory addresses.The device can aiso have irs own address(or sei
of addresses).The 80386supports 65,536I/o device addresses,called /oro,.rri
I/O communication is done in 8-bir or 16 bit quantiries.'1he accumulatoris always
the sourceor the destinationof the I/O insrruction,and rhe I/O port is specified
65
ar th. actgo ltrrtructl.n s.t
load the AL registerwith the first byte of the stack segment,AlthouSh the segmnt
prefix byte comesbefore the instruction in the code stream,the prefix is usually
written as part of the memory oPeland fof readability. The previous example is nor-
mally $ritten:
Hov A L , S St:o l
Diroct addre3sing
The simplestform of memory referenceis called llrcct addressinS,wherettie n-
struction itself includes the location of the operand. The location is specified as a
16-bit or 32-bit olfset in the curent segment.This offset is also known as the
d.tptacement.Tbe r^tle on the following page showsthree examPlesof direct ad-
dressing.The brackets differentiate data values (no brackets) and memory ad-
67
]NC DWORD PTRI17HI dd I to dre 32 bir valuc at offset 17
rvrov At, t1,\33D4Hl Copy the menory byte !o .cgistcr ,{L
SHT BYIIJI''IR [lFTH],3 shift the memory byte left 3 bts
COUNT OD ? i A l l o c a t e 3 2 b i t s r i t h r a m eC o U N T
INC COUNT : I n c f e m e n vt a r i a b l e
I Icrc xrc someadcliti()nale&xDplcs
ofinstrucri.)nsrhairNesymbolicaclclrcssjng.
COL]NT DD , Rcsove32-l)i!valuc,initialvxllre10
ll,Ac D\(/ RcscNea sinAleword
NANI]] DI] 20 DUP(?) Rescrvc20.onseculivchytcs
Dlic COIJNT Subtracr1 liom fie valuc rl COtrNT
MOV At, NAME Copy first hytc of NAME
MOV AI,, NAMI]III Copy secoddbytc of N,\ME ro AL
OR FI,AG.,IOOOH scl onc bit in rbe sfc(ificd word
Ba3ed addressing
In basedaddrcssing,a regisrcr holds the addre\s of an opcrand. The regisrcr con
tainingthe memoryaddressis calledthe bascrcgister,and you canuseanyofrhe
scvengeneralrc,listersasa b^scrcgisterVhcn y()uuseESpor Drlpas a bascresis
rcr rtredddre$r. J*I-ed ro be Jn,,tf:erfromrh( {r. k \eqmenrtssrrdrne,I ha;
lrornrhc(liu.esrn;n|lD5,.You.pe,ilyr",edaddres,inshlpljcrng,l-,.regis
namein brackets,asthe followingexamplcsiltusrrare.
6a
4: th. ao3a6 h.lrlctlotr 5.t
C O R N EP
RO I N T < >
LEA- E S I ,C O R N E R c e t a d d r e s so f v a f i a b l e
E A X t, E Slr. X Fetch the x conponent
INC IESI].Y I n c r e m e n tt h e y c o n p o n e n t
69
tHE 0034oBoo(
s u n- 0 :
for (i - 0; I < V-fiAx!l++)
s u n+ - v l i ] * v l i l :
Assuming that the size of an ioter is 32 biis, two separatevaluesare required to
progressthrough the array: the index riable dand the offset in memory ofvtll.
For example,when I is 3, the addressofv[3] is the addressofv plus 12(4 x 3)
byt$. Every time I is used as an index inoothe array, it must be multiplied by the
slze of the aftay element.The assemblycode to executethe above loop mrght look
like this:
XOR E C X ,E C X ; C l e a r E C X( c o u n t e f )t o 0
t'10v sul4,Ecx ; C o p y0 t o S U t l
Llr CllP E C X ,V - t l A X ; Is counter> v-l{Ax?
.JGE DOIIE : T e s- 9 0 0 n
II,IUL s q u a r et
ADD su , EAX Comput the sun
I lic ECX
dt{P LI Loopbackto the top
00E:
The highlighted code shoe/sthe conversionfrcm array inde,,<to memory offset and
the addressingof the selecteditem.
The 80386provides a speclaloptimization for affays whose elementsare 1, 2, 4, or 8
bytes. The 80386adiusis the index to produce a memory offset. This adjustrnentis
called scalirngand is indicated in assemblylanguageby placing a multiply operation
in the bracketsthat enclosethe index register The above e)mpl becomes:
x0R E C X ,E C X i C l e a rE C X( c o u n t e r t)o 0
lt0v s u, E C X i Copy0 to Sljll
ct!P EC)(, Is counter > l/-l,lAx?
,tGI DOIIE
70
4r th. 40365h.tru.tlon s.t
The secondversion of the program does not require rhe index value to be copied
and multiplied, so the program runs faster.Also, the instruction:
r'10v EAx,vI Ecx*4]
takesno lor\ger to execute than the instruclion:
ri40v EAx,l/[ EAx]
vrhen EBPis used as a scaldindex regisler, it does not force the memoly reference
relative to the stack segmentas it does when used as a baseregister.When an in-
strrction specifies both a tvdsercgister and an index register and one of them is
EBq EBPis assumedto be the basercgister unlessa salefaclor is present. If n sale
factor exists, it is assumedto be thc index regisler. The following list showsfour
xampls:
MOV EAX,tEtsP+8]
{ESII Array is on stack beginning at EBI' + 8
INC I/ORD PTR[EBX+tsAX.2] 16-bit vstor based at EBx, with index
MOV EDx, PTIEAX.Sl[ESI].Y Array of "point" da(a slrDctures
71
rHE 60306 BOOK
T ,'f,
-
1
-
AA+& A6&A
Queu- firsr in, firsrou!
B'{l0,te4-2. LIFO,IIIO.
72
4! th. ao3a6 h.ttlctlon 3.1
execution addressonto the stack.This allows the subroutine to return to the pre-
vious point of execution.
The most commonly used instruction that changesthe EIP register is CALL.The
CALLinstruction hasone operand, the addressof a routine to be excuted.The
value of EIP (whih points to the instruction immediately following the CAIL) is
pushed onto the stack, and EIP is set to the addressspecified by the CALI operand.
The RET(or "return') instruclion pops the urrent top of stack into the EIP register,
returning control to the instruction after the initial CAIL.
A routine passesinformation to another routine by storing valueson the stack
beforc exeuting a CALLinstruction. The standardway this information is strlrc-
tured is called thetdrr? of the calld routin or the call stack.FiSure4-3 illustrates
a subrourinecall and showshoer'the stack frame is structured.
38 38
34
30 30
2C 2C
28 2A
24 24
20 20
1C 1C
18 1E
PUSHr
38 38
34 34
30 30
zc 2C
28 28
24
20 20
1C 1C
18 18
n srrk framefor'\ubr"
73
Programscm push and pop 16-bit valuesby specifying registersAX, BX, SI, and so
on, or by specifying 16'bit memory references.It is more efficient, however,to push
the contentsof the 32 bit register(for example,EAX for AX) andto disregardtbe
high-order bits. Use dre MOVSXor MOVZX instructions to copy memory operands
to a register and extend them to 32 bits before they are pushed onto the stack.The
reasonfor doing this relatesto how the 80386interfaceswith memory. Ifthe physi
cal nEmory addressis a nultipLeof,i, thatis, if the addressis on a dwod boundary,
then a single memory referencecycie can fetch as many as 4 bytes.If the physical
memory addressis offset from the dword boundary, then at leasttwo additional
clock cyclesare requiredb feadorto wdte a 32 bitvalue.
'l
herciore,afterexeutinga 16-bitpush,aLlsubsequent 32$it st.rckreferences
dcgradein performanceby at least30 percenl lhe 80386generates 32-bitrefer
cnceswhen the 16-bitsegmentregisters(CS,SS,DS,tjs, FS,andGS)nre pushedor
popped,so pcrformanccdcgrxdntionis nor an issucin this case.
Instruction Gategories
The operationsth^t 80386instrr.(rionspcform varywidely,reflcctingboth thc
wide mngeofthc Drachine's capabilitier^nd irs compalibilitywith previouspro-
cessors.
In this secrion,Idividethe instructionsetink) ^ nurrrbcrof rclatcdcrlcgo-
desan.lidcntify thc rrost itnportantinslrucrionsof e^chcaicgory.
Aiithmetic
Arilhmelicinstruclionspcrformsignedanclunsigncclintelleroperalionson
opcrandsof8, 16,ancl32 birs virh fcw cxccplions,theseinstrLrrionshaverhc
0PC0Dtdest, src
Generally,arithmeticinstructionsoperateon sourceand destinationoper4ndsand
storethe resuhin the locationspeciiiedbythe destinationoperand.The dcslinati{)n
operand can be a memory referenceor a register,and the sourceoperand can bc
memory,a register,or an immediatedatavalue.Boththe sourceandthe destinaiion
operandscannotbe mernoryreferences, howeverThe instructionsthatfit dris lbf
ADD
ADC
sua
SBB
CN'P
74
4: lhc o3a6 h.tilctlo. s.t
Thcsc instructions alTd the CR OF, PF, SF,and ZF bits of the EFLAGS rcgistcr dc-
pencling on the rcsulrs of thc operation.
INC
DDC
I)IV
lDlv
MIJI,
IMTJI, signcdnn'ltiply
1 6b i t $
32I)its IiAX
64 bits EDX,EAX
IMUL .v.
'lhe
DIV lDlY and MUL instruclions lciv Lhestatusflags in undefined stales.The
IMUL instruction modifies Cl' and OF, lcaving SF,ZF, AF, and PF undefined.
75
tHE 0396 BOOX
Decinal arithmetic
Sjxinsr.ucrionshelp implementdecimalmath routines.Thc standardintegerin
structionsperformcomputations, andthe fbllowinginstructionsadjustthe fesult
becausc the operands are not integersbut BCD encodings.The followiqg instruc
tions havccither rhe AI- or the AX accumulator asan nnDlicitoperand:
ASCIIadju$ afteraddition
AscII adjust b--foredivision
nSCll adjus!aftcrmultiply
ASCIIadjuslrfter strhr.ction
D,\,\ Dc.imrl rdjuslxfteracLlition
DAS Decinr.ladjlst alier subtraction
Logical
The followinginstructions^rcc lled loglcalbec usethey mxkc no scmantic
xssumplions xhoutthcir opcrandsithatis,fiey do not regad the opcrandsas in_
rcgers,BCD(ligits,characterstrings,anclso on Thc inslructionsare snjcdy
tsoolcan,or llit-by bit, operations.Firstis a selof clyadiciunctionssimilarto thc
nrithmcricinstructionsl
Insttltcttorl EtPkuaton
ANI)
OR
XOR DxclusiveOlt
TIS'I l,crformsrn AND bur modiiiesonlythe EFl,IGsrc3ist$
havcihc lbrm:
A seriesofinstruclionsopcftucson bil strings.Theseinstructi()ns
0PC0DE
dest, trde)(
where desl sclectsa bit srring, eirher in memory or in a regi$let and irder identifies
the parrjcularbit in rhe bit strjngtharis the subjectof the operaUoo.The irdet
valueis eilhcr conlaincdin a regisleror specifiedasan immediatevaluc.If destis a
memory bcation, rrden is trcated as a signedinteger and can iake on any value
from -2G through +2G. Instruclions that operate on bit strings are BT, BTC.BTR,
ANdBTS.
76
a: th. 0306 lrsti@tlon S.t
Iustmctlon B4rlaaatl@
m Bit test (sve the value of the seleded bit in cF)
mc Bit test and complement(savebit, then omplemenr deri bi!)
BTR Bit test and set (Mve bir, then clear A?srbit to 0)
BTS Bit lest and set (saveb]t, then ser dasrbir to 1)
Index--26
2ABH 2ACH
Dest
Two instructions search bit strings, These instructions have the fa,rml
where src indicatesthe locatbn of a bit string. The Aestoperand must be a register
that receivesthe index of the first nonzero bir. The dest opcrand can be only a 16-
bit or 32-bit register and indicateriwhether the src operand is a l6-bit or 32-bir quan-
tity. Figure 4-5 shows how these insiructions work.
BSFEAX,EAX EAX
3r
-To T o 0 1 0 0 I I 1. . .00I00 1o!_o-lBitscanfbrward
+ Start
EAX
3
BSREAX,E{X
101. 001001000 l B i ts c i nr e v e s e
- t
30
The final logical inslrutions are shift and rotate instructions. Figure 4-6 on the fol-
lowirrg page illustrates what shift and rotate instructions do.
77
IHE O3A BOOK
t6.rscioi BxtrthffiUorl
SHL Shift lcft loSical
SHR Shiftrigh! Iogical
SN.L
SAR
ROI,
ROR
RCL Rotatethrough.ary lcft
RCR Rotatehrough cary right
In the above instructions. the sourcc and the destination are concatenatedand
shifted, and the result is t.uncatcd and stored in the destination operand. Figure 4-7
illustrates double shifl instructions.
7A
4! lh. aott6 lrtEctld s.r
Rl8.ore4-7. Doubleshtfts.
Data transfet
Probablythe most frequently used instructions are in the dara iransfer categgry.To
the assemblyprogrammr,a single instruction appearsro do almost all the work.
Actually, the MOV mnemonic is encoded into one of severalopcodes,depending
on the operands involved. The generalform of the MOV instruction is:
MoV dest, src
Either the /es, or the s,"coperand can be a memory referenc,but not both. Both
operands can be registers,and the src operand can be an immediate value for most
choicesof dart This instruction is not restricted to operating on the general regis-
ters, The MOV instruction is the only instruction you can use to read or modify th
control registers(CRo-CR3)and the debug and test registers(DRo'DR7,TR6:TR7).
You can also use the MOV insFuction to load and store the segmentregistersDS,SS,
ES,FS,and GS.
Not all possiblecombinations of s/c and lest are leSal80386instructions. The
restrictions are covered in Chapter8.
Here are four additional data transfer instructions:
?9
tHE aoBa6aooK
Insttuctld E4naiatlon
SE]A deil Setto 1 if above(unsignedx >y) /Ct'= 0& zF= 0
SITAE dalr Setto 1 if aboveor equal / ct = 0
SETB d6r Se!to if below (unsignedx < y) / CF = I
SETBE d6t 5e! () ii below o. equal / Cl' = 1 | zF - I
SETC desr sel ro ilGry / Cf = I
SETE ../eix set Io
SETG de.v st to if greater (siSnedx > y) / 5F = OI & z! = 0
SETGE dat Sct to I if Sreateror equal / SI = OF
sETl Asr setto 1 if lessGignedx < y) / sFl- oI
SETLE .le! setto I ifless orequ /$l"OFandZF-1
sllTNA 4r, setto I if not above(SETBE)
SETNA! des, set to I if no! above or equal (SETB)
SETNB des, Setto I if nol below (SETAX)
SETNBE desr Selto 1 if .ot below or equal (SETA)
SBTNC dest SctI() 1 ii no carry / C! - 0
sEtNE det st to 1 if not equal / zt' - 0
SETNG d6r sitto 1 if not 8.eater (SETLE)
' SETNGEderr Setto 1 if not greater or equal (SETI)
SETNL dert Settol if not less(SEIGE)
SETNLE ,Je!t Setto 1 if not lessor cqual / SF- OF & ZF " 0
SETNO derr setto 1 lf no ovcrflow/ oF - 0
SETNP larr Setto 1 lf no parity/ PI - 0
SETNS le$ Setto 1 if no sign / SF- 0
sETNz lesr Selto l itnoto/ zF - 0
SETO dest 5(r IU ]di o\ ertlow/ OF _ I
SETP tl6t set !o 1 ii parity / PI - 1
SIiTHj ,tesr Setto 1 il parity even / PF - I
SETPO dat xr ro I ifparity odd PF- 0
SETS dest Sct to ifsign / SI - l
ff'lz dest Setto i f O / Z l - |
Stack
The stack instructions siore and retrieve data from lhe stack.The PUSHinslruction
writes its opemnd to the stack,and the POPjnstruclion removesthe top-of-stack
elementand storesit in the location specified by its operand.
The PUSHADand POPADinslructions require no operands and saveor restoreall
the generalregistersto the stack. Figure 4'8 showsthe stack after a PUSHADhas
been executed.Although PUSHADsto(esthe value of the ESPregister,POPADdoes
not reload ESPfrom the savedimage.The new ESPvalue is always the old ESPvalue
plus the number of bytes required to store the generairegister context.
ao
4! thc AO3A5h.ttuciiotr 5.i
BeforePUSHAD
ESP
Control transtcl
Control transfer inshllctions affect the flow of excution, Norrr,allv, an instruction is
fetchcdfrom the addresshcld in rhe EIP regis(er.rnd thcn EIPiJiin(rementedby
rhe sizeofthe instrucrion\r rharit poinrsto the nexrinsrrucri(,n. Thc n(; opcode
is fetched,and the cyclecontinucs.
The 80386supportshranchinstrutions, which alrerEIB and subrourinecall in-
structioN, which savethe old EIP and then modify ir. The sofrware interrupt in-
struction is siftilar to the subroutine call cxcept that 4n interrupt nunbet is
specified for EIP rather than a new value. The addressof the desrination routine is
then determined by a gate in the IDT. Figure 4-9 show$howJMP and CALI instruc-
tions affect the flow of excrution.
at
tHE aooaa gooK
= =
ollset Jump lbove (unsiSnedx > y) / cF 0 & zi- 0
oflset Junp rlFve or cqual / cF = 0
JAT =
JB oflsct Jump below (ursigned x < y) / cI 1
arrret Juop b e l o w o r e q o . l /C f = I Z F -1
JLIi
JC ollsel Ju pifcarry/cF=t
.laxz ollset Jutupifcx " 0
JECXZ o[sel JumpiLECX= 0
JD onset
-
Jc aJJset Jump grcatcr (signcd x > y) / Sr = Ol' & ZI 0
anflt Jump gre.ltef or cqunl/S! - Ol'
JCE =
JL oJlset Jumples,(signcdx < y) /sf lF ol'& zI 0
Jl,li 9lset Jump lcst or cqurl/ sl-l- olr
JNA oliet Jumpnot abovc(JIIE)
JNAD ollset lu,np not above(Jtcqual(Jll)
JNIJ olfscl Ju p not below UA].l)
JNI]I] oIJscl Jumpnot blowor cqual(JA)
JNC ollset Junrpno caffy/ cl - 0
JNrl olJKl J'.'nlPnot cqual / Z! - 0
JNC otrsel Jumpnol grcrler s! l- OI& ZF = 1
JNGIi allsel Jump nd greatcror cqurl (J!)
JNL aJJsel Jumpnol less(Jcll)
JNI,I] ol/el Jump not lessor cqual (JG)
JNO ol|sel Jump no ovcrtlow / o! - 0
JNP ,'Isct Junp no parily / I'F - cr
JNS o"lfsct Jumpno siln / SF= 0
JNZ oIIset
JO otiet Ju'np if ovcrflow / oF = r
JP allsel Junp ifplrity / r! - r
JPE alJsel Jumpprrity even/ l'li = 1
JPO ollser Jumppadryodd / PF= 0
Js oxset Jumpifsign / SI = 1
Jz ofs"l J u m pi f 0 / Z F= I
Three other conditional branch instructions are re loop instructions lI)op instruc-
rions de$cmenl re ECXrcgister and brunch if the conditbns outlined in lhe fol-
a2
4: th. ao3a6 lGttuction 5t
String
Srringinstructions handle large blocks of memory with ease.A string instruction
can move ll block from one location in memory to another,compare one block with
another,or searcha slring for a specific \alue. Striqq instructlons use specific regis
ters for storing operands.DS and tsSIalwayspoint to the sor.rcememory block. ES
ancitjDl point to the destination.Thesepointersare incremented(or decremented)
by thc sizcofthc opcrand(1,2, or 4 byies)everytime thc siringinstruction
Pointer manipulation
Poinle.manipulationinstructionsloada 48-bitpointerinto any pairofthe segment
'I
and gcner.rlrcgislcrs. he fofmat of these instructions is:
feg, nen
where ,crcshnds for the segmentregister (SS,DS,ES,FS,or GS), rcg is any gcncral
register,ancl ,lem is a memory operand.
The LEA (load effective address)instruction computes 32-bit addresses.LEA loadsa
32-bit registerwiih thc addressdefined by the memory operand, which is unusual
becauseother instrrlclions operate on the value stored at the memory operancl
a3
tHC AO3aGBOO|(
locarion.The folk)wing example shows irow to use the LEA insrucrion k) cpmpure
V E C T o RD D 2 0 D U P( ? ) : Array of 20 e'lenents
MoV E A X ,9 ; Array index
LEA E A x ,y E C T 0 R I E A X ;+ 4G1e t p o i n t e r t o 9 t h a r r a y e l e n e n t
PUSU EAX : . P u s hp o j n t e f o n s t a c k
CALL HYSUBR ; l n v o k es u b f o u t i n e
Becausethe LBA instrLrctnrncsscntially pertbrms only additions and shifls on ihc
values ofthe displacemcnt rnd lhc base and index registers,ir can perform simplc
nNltiplicatbns fasterthan the hlrdware mLrltiplyinstmctions can. l'or a value storcd
in x generul fegister (such as EAX in thc srmplc opemtions), these operations can
M l^x,lEAX.zl M u l t i p l yb y 2 ( i n d c x )
l,li,\ !dx, LEAX+EAX.2l MulriplybyJ (base+ rmexr
LIjA liAX, ili^X.4i Muhifly hy 4 (indcx)
LUA rAX, [iAx+liA&4] Mulridy l)y 5 (brse + indcx)
LlA EAX,lEAx"rll ^4ultiplyby u (index)
LliA IAX, lDr\X+EAx"ilj Mtrhiplyby 9 (b,rse+ index)
Input/Output
BecauseI/O ports irle usually conncrrcd b systemdevices,it is imporram ro protecl
againsrindis.riminxteaccessto them.S.'curcsystemroutinesrharrun wirh I/O
privilege (CPL<IOPI) may execuremy l/O inslruction. A lessprivileged task may
execule an l/O insrfuction; however,a gcnc.al p()re.tion faulr (inrernrpt 13)will
occurunlessthe operatingsystemhasgrantedthc iaskpermissionro accessfie
spe.ific port(s). The operating slstem grants permission by setrin8 the appropriate
biLsin the l/O permissionbitmapof the task'sISS.
a4
a: the 603aG
In.r.uctlons.r
Bodr the input and output instnrctions have three forms. The simplestform is:
IN acc, port
OUT part, acc
whcrc dcc is onc of the accumulatofregisterc(AL, AX, or EAX) and po,.t is a value
from 0 to oFFH.Thcseinskuclbns canbe usedto xddressonly the first 256IlO ad-
drcsscs,and the 803ii6suppots asmanyas65j536IlO ports.To access the enrire
range,uscthc followingi()rmofdle instructionsr
I a.c, DX
oUT D X ,a c c
In the aboveinstructions,the I/O rcldressis containcdin thc I)X rcgis[er.
Srdnginsl.uctionsare the thnll tlpe of I/O instnrctions.INS(input srring)rakcsin-
pul from the porl specifiedby DX and storesthe resultat ES:tsDI,
adjustingEDl rc-
coding to the directionflag bit. OUTS(ouDut strind readsthe valueat DS:tjSlrnd
writesit to the port specifiedbyDX.INS and OUTScxn bc prefixcdby thc Rlil, in-
slrucrion,which clusesthe I/O iostructionto repeatuntil tsCXis d$rcnentcd to 0.
Prefix
Irrcfix inslruclionsprc(edeother80386instructions.l,refixesmodify the actionof
thc instructionslhcy preccde.You canapplymorc dnn onc prcfix to an instruction
Thc mostcommonLyusedprclixcsarc the rcpca!prcfixcs,dis(r"rsscd prcvn)usly
with the stringinstruction$.
Il a rcpca!prcfix is applic.lk) any instructbnoiher
thena stringinstructbn,an undcfincdopcodcfault(interrupt6) occurs.The folbw-
ing tablclistsihc rcpcatprefix inslructbnr.
a5
tHE ao3g6BOOK
For example,the instruction MoV EAx, l42rX copies the dword at oflset 42H of the
data segmentinto EAX. vhen the insEuction is prefixed with Ssr,the dword is rcad
from the stack segment.Most assemblerslet you specify the prefix before the in-
struction or as part of the instruction. For examplel
55:
l{0v EAX,t42Hl
l'lov EAX,SS:t42Nl
The only memory referenceinstructions that cannot be prefixed by a segmedtover-
ride are SCAS,STOS,and INS.These are string instructions that operate on memory
at ESrlEDIl.Vhen a prefix instruction is applied !o any other string instruction, it
overridesthe DS:[ESI]pointer only. The MOVSand CMPSstring instructions have
both a source(EsI) and a destination (EDI) pointer aod are allowed a single prefix
instructionthatoveffidesthe DSTIESII pointer.
You can apply the LocK prefix to any of the following instrrrctionswhen reading or
modifying a memory l ationl
A D CA , D DA. I I DB
. T , B T C , 8 T RB, T S ,D T C I, I I C ,I { E GN, O TO
. R,
S 8 B ,S U BT, C N GX,O R
The LocK prefix assertsthe hardware signal LOCK\, which ensuresexclusiveac-
cessto a memory location in a multiprocessorenvironment.
The assemblerusually inserts two additional prefix instructions, but Intel does not
give them mnemonics.I call them OPSIZ(operand size prefix) and ADRSIZ(ad-
dresssize prefix).
OPSIZtogglesthe operand word size of the processorfor the next instruction. Nor-
mally, the machine word size is 32 bits. Prefixlng a 32-bit instruction with OPSIZ
convertsit to a l6-bit instnrction. Similarly, erhen code is run in 8086-compatibleor
80286-compatiblemode, the default machine word size is 16bits; appMng the
OPSIZprefix converrsa 16-bit instruction to a 32-bit instnrction.
In real mode, virtual 8086 mode)and 80286-compatiblemode, the byte 40H is inter-
preted as INC AX, but in native (32-bit) mode, it is interpreted as INC EAX. To in-
cremenl the AX register in native mode, ],ou must prefirathe instfl.rctionbyte with
the OPSIZinstruction. The assemblerdoes all the wo(k, however.Iflou enter the
instruction INC AX in a native mode code segment,the assemblergeneratesthe
bytes 66H and 40H. The following table illustrates the bltes that the assembler
a6
4: th. ao3a6 h.truction S.t
System
Applicationprogramsdo not c'xecutesystcDinslructi()ns. In somccasesisystemin-
skuctionsqnnot be cxcrutcdunlcsslhc prcccs$hasa high privilegelevel The fol-
N'torcdctailcclinformationxboutthese
lowingtabllisls sysrcminstfr.rctions.
instructionsis given in Chapter8.
Instntcltot Et ol4i4ll6
a7
THEA036BOOK
Miscellaneous
A few instructionsdon'tfit inlo anycatego,y. Forexample,the NOPinstructbn per-
rormsno oPerat'on.
l he 'ifAlT instruciion reststhe lurdware pin callectREADY\ . If the READY\ pin is
not active, the CPUwaits until it becomesactive. If the 80386is waitins, it continues
to respond to hardware interrupts; howeve! iI returns to ihe VAIT after the inter
rupt completes. The 80287and 80387hold READY\inactivewhile they perform
floating-point operations. You shouldexecute a WAll instruction before you use
the result of a floating point computation to ensurethat the coprocessorhas fin
ishedexecution.
Floating.Point Extensions
As cliscussed
in Chxpter2, the 80387NDP extendsthe instructionsetofthe tl03ll6
by providinghxrdwarcsLrppoflfbr fk)xting-pointoperarions.tlnlike thc 803ii6,thc
80387prr)grxnrmingmodelis a stxck<nientedmodelratherthanthe two-operand
rcgister/mcmorymodcl.Mosrarithmeticinstrucrionsc^n be spccifiedin thrcc
wnys,with no opcrancls,with a singlcoperand,or wilh Iwo opcrands.Frollowing
afc somccxnmplcsrh.rtilllrstrnterhe floating-poinrnddilioninsrructions.
IIADD
IIADD ST(3)
IADD IEI}P+6I sintjlc-nrcnnny opc nd
ti\DD sT(2), ST
t e m p < - p o p ()
ST <- ST <function> temp
Vhen a single operand is specified, the top of stnck is nnplicitly fie first operand,
so the instructbn becomes:
S T < " S T < f u n c t j o n >o p
'whcn
two opcrands afe specified, borh operands must be 80387 registers,and one
nrust be the k)p of stack. You can store the result of the operation in eidrer register,
which you dsignateby making it the first operand.
aa
4: Th.603A6 h.truction Sot
causesthe top of siack and ST(3)to be multiplied and storesthe result j. ST(3).
Then rhe top of stack is discarded,leaving the newly createdrJlue ai ST(2).
IN,ruc,loi Brpl4notld
Arithmetic
The following table lists the arithmetic operations that tbe 8,03117
pcrforms. Scc
Chapter8 for a description of the types of ope, anclstha! each instruction supporls.
Iwtntcrlott Bt bl4lrtlon
F2xMl C o m p u t2
es r l w h c r e 1 < S T < 1
IAAS Trke absolurevilue of ST
FADD top(rl Md rwo floating poi numbers
FADDP op1, op2 Md opl ^nd op2, pop sr^.k
FIADD Add 16 or 32 bit integer to ST
FCHS Changerhe sign of sT
lcoM o, cotnpare sT with op Geaisteror metndy)
a9
ICOMP oP compare sT wi$ op and pop
ICOMPP ComparesT with sT(l), pop bo$
FICOM CotnpareSTwith 16 or 32 bit integer
Comparewith ioteSerand pop
lucoM oP Comparcauowing quier NaNs
IUcoMP oP LikeFCOMP
IUCoMPP oP Like FCOMPP
l:cos
FDIV tolrtn
FDI\,? op1,op2 Dt\I<Ie aql by aP2, pop
DivideSTby 16-or 32-bitintecr
fDIvR lo4sn Reve$edivide (op2 by opr)
IDIVRP op1,ap2 Reversedivide oP2 by oP7 add pop
Divide integer by ST
IMUL taf@l Florling-point nultiply
IMtJtP ot1, op2 Multiply oP1,by o?2 and pop stak
TIMUL MultiplysT by 16.or 32,bitinteSer
IPA1AN tuctangent of ST(1)/ST
FPREM P tid remalnder of sT/sT(l)
FPREMl Computcpartialrcmaindc|rouiEEspec
FPTAN computc unScnt oi ST,push(l .0)
IIIiNDINT
I.SCAI,D MultiplysT by 2s(1)
ISIN Compute slne oi ST
FSINCOS temp - sT, sT - sin(temp), push(cos(enp))
FSQRT 'lhke
th squarroot of sT
FSUI] I1N,J IrloatinA.poinlsubfaction
fSUrlP oPI, oP2 subtrac! op2 from op, and pop
IISUA Subhact16- or 32ltt tnteger iiom ST
ISUBR IOP(O]
ISIIBRI ap1, op2 Subtrad opl from or2 and pop srack
!ISUI]R Subtnc! STfrom 16- or 32-bir iffe8er
III ST CompareSTagdnst 0.0
Exnmine sT and set condition codes
FXTRACT Decompose5T to er?oneff and signlficand, ST - exponent,
Contrcl
Control instructions saveor alter fie state of fte NDP.Somehave a special "no
wait" form, indicated by lhe letter N as the secondcharacterof the mnemonic. The
"no wait" instructions executewithour the implicir WAIT that occurs berween two
f loating-point instruclions.
90
4! lh. 903A6 h.t.ucilon 3.t
31, 16 L '
0 c\( 0
I 0 s'{/ 4
I 0 T'!( 8
I FIP 12
I
HiSh
0
FOO
FCS 16
20
FCS 24
The following table lists the 80387's conttol instructions and their functions.
9t
IHE aoi|a6looK
31
c\{ 0
S\q 4
T\q 8
FIP 12
0 FCS 16
FOO 20
0 FCS 24
sT(o)o I 2A
sT(o)v 6,
sT(l)o I sT(0)64 D 36
sr(l),,, 4? 40
sr(l) $ .,,
sr(2)d n 48
sT(z)D 61
sT(3)d,5 IST(2/,,, 56
sT(3)re.o 60
sT(3)r n
68
sT(4J, d 72
sT(5)ou sT(4) & 19
sT(5)rr. l 80
84
sT(6)o..r 88
92
sr(7) o 1 ST(6)er p 96
sTo) rs ar 100
sr(7)r" ,. 104
92
5
THE ao3a6
PROTECTTON
MEGHANISM
Selectors
The central feature of the 80386protection mechanismis the sefucto,1 RathertMn
directly accessingany part of the system,a program dealswith a selector which
grants accessto a systemobject. Asseialed with each obiect is information about it,
for example,the object's bcation, size,and rype, and any restrictions on its use.
This information is not stored in the selectorfor two reasons.The selectorwould be
very large,and passingit frcm routine io routine would take a lot of compuler time
More importantly, keepiog the object inJormation in a separatelocation prevents an
unscrupulousor errant program from corrupting the information.
9:'
tHE A03A6BOOX
A selector is like a sealcd envelope. Inside the envelope is imporram data that musr
be kept secure. Like a mcsscnger pefmiued only to see envelopes and pass rhen k)
olhef messengers, a program can skne and retrieve selectors and pass rhem ro orhcr
rouLines.Only the operating sysrcm has accessto the data inside fie cnvclopc,
lich on the 80386 is callc(l ^ .le$riptor.
Descriptors
Aptly naDred,descriptorsdcscdbcx systemobjectin detail.Memorysegments, .rsii-
lustfalcdin chaptef3, are one kind of stsrcm(,bject.OrhersysremobjectsinclMe
tablcstharsuppo the protectionmcchanism,spccixlsegme.rsrhatsrorethe pro-
cessorst.lte,and eccesscontrolobjectscallcdgales.
I)cs.riplo$ are groupeclindcsdipL(' tables.Byenmining a sclcctorthc 80386
hadwxre determineswhich dcx-riptoris associated with thc scicc()rllnd wilh thc
(i)icct ro which the ctescrDtor
poinls.One item rharrhe descriptorin.lic.rtcsjs thc
privilegelevelofthc objccl.This valueis storedin the DPLiicld ol thc dcscriptor
whcn a prcgramrequestsirccc$s to x systenobjectwith a sclcltor,one ofthc lit-
Privilege
'lhc 80386processor
supportsfour lcvclsofincreasingprivilege,mmbered 3,2, r,
and 0. I,rivilegelevel0 is thc mostprivilegedlevel.
The priviLege levelofrhc sclcctorin the CSregisteridcnrificsdrc precedenceof rhe
currently executing rourinc and is called rhe crlrent pririleEie lercl (aPL). Fot rcli-
ability,onlythe mostruslworlhyand crash-resistanr codein rhc opcraringsystem
shouldrun at fie mosrprivilcgedlevel(CPL= 0).Applicationsrharmighi fail or
compromise lhe intcgrity of fie systemshouldrun ar rhc lowest prioriiy (CPt = 3).
BecauscLhenumberof programsfiat canrun at high privilegelevelsdiminishes
ncarlcvel0 and be. selevel0 codc is likely to existonly in the coreof rhe opcrar-
ing sysrcm,the classicillustrationof rhe privilegesystemis one of concenrricrings,
asshownin ligure 5 :l
94
5r th. aoaoa PEl.crid il.ch.nls
Flgt|trct-1. Ptluttegettngs
The concentric ring image is so well integrated intcr the understandingof Privilege
_in rjng 0 or_inringJ -rnother
ihu, o,o".rrn...r_ott.n-.peak ofcode thatruns (that is
*ru'of slvinc that tne cpi of rhe procedure is 0 or 3 Every syrlcm ohicq
evervrhlngr;ferred to by a dcscriPtor) is arsocialcdwilh a privilcgelc\ el anLl
'lesides"in a
Particularring.
The word pll! /k8e connotesriEhrsor advantages not normallygranted On lhc
80386,procedure.s runninSin the innermostringscanaccess daraobiectsin lhe
accessoD-
outerrings(which haveLcssprivileger'bol outcrring proccdurescannot
lrom crasn-
iectswiti qrearerprjvilegeln additronto preventthe oPcralingsyslem
i* a* ,""u"a -a., p.iedures (annotcdll olher Procedures thatmiShtbe lcss
reiiable (procedures in outer rings)
segmcnt, residing,in
Forexample.a procedurerunningin nng I m ay accessa drta ^
a 'egmcnr qho'e privilegc ievelrs u
rinq 2 or ijne 3 but it preventedfrom accessrng
A rine I proiedure.however'cannorinvokea suhrouLine residingin ring 2 or flng ''
nor cln it call one in rine 0. Figure5-2 on rhe following oage rllusl"xreslhi'
U\IX syt-
An ooeri(inq rystemdoesnor needro 'uppon all four privilegelevcl'
2 suppon 5 th-ce
i.t,, f", "*i^pf.. ryei."llv i'nplemenronlv rwo levelc 0 Jnd J Os
ring and
i*"i", rr'. "pi'"ii"g .v"tem code runs in ring 0, applications run in 3'
specialroutines that need accessto I/O devicesrun in ring 2'
95
THEAOSA6BOOK
A Data
tr code(prosramt
+ teSal acccss
----> Illcgal ac.ess
Inte;level corrmunication
s a $ecuritymcaslrfc,concenrricringsof privilegework wcll, but the possibilirycx-
iststhat an applicatfunfunnin{;in rinS3 might needscrviccfrom rhe operaringsys-
rcm.l he opcratingsystcm,howcvcr,rhoughomnipoicntin ring 0, is not acccssiblc
to the lrpplicntk)nThe applicatbn,in effecr,mighr say,"Oh mostgreatanclworrhy
ofopclating syslcms,plasegranl me,dly humbleand()bcdienlservant,additional
RAM for my srack,"bul becauscofthc accessrestrictionsit hasno way ofcalling on
lhc operrting systcn.
Variouscuhufcs have esiablisheda priesthood whose job is to act as intermccliator,
but lhe Intcl clcsignenginccrs apparendydcspaired ot firting somclhing rhat c!m-
plicxrcdin() only 250,000rrxnsisbrs,so they rcs(xledro somethingsimplcr.tr,s
calle.lr ga!c.
Gato6
A gate is a systcmobjecr (rhar is, it has its own dc{-riptor) rhat poinrs to a prcccdure
in a code scgmcni, but the garc hasa privitege levcl separatefrom thar ofrhe code
segmenl Figure 5-3 showshow this changesthe legal subroutine call parh.
.Agaie allows execulc only accessro a rourine in an inncr ring fiom a lessprivilcgcd
procedure.The resrricrion on outward calls, however,rcmains in force. The 803S6
supportsfour types of gates c.tll, intelrupL trap, ^nd ras&.Call gatesare invokcd
via the standardsubrourinecall insrruction. Inreffupr garcsand rrap gatesare in,
vokcd by the INT instrucrion or by hardware interruprs. Taskgatesare invoked by
JMP.CALL,or INT instructions or by barcLwareinterrupts.
96
5: the ao3A5 Pbt6cllon l|*h!ni.n
O crte
n codc (prcsnms)
+ Itsgal access
-_--) t l l c S a l! . . e s s
Flgurc5-3.aaUl)dthrthtauuhAdks
97
tHE 003a6 EOOK
SS:ESP
Descriptor tables
As mentioned earlier, rhe descriptorsfor the memory segments,TSSS,gares,and
other systemobjects are grouped inro descripbr tabtes.The three tr?es of descrip_
tor tablesare: the inteffupt descriprortable (rDT), rhe globat descriprorrable (cDi),
and the local descriplor tablesoDTs).
The IDT contains descriptorsthat relate ro hardware and software intetuDts. A sDe,
ci.rlregis'erIDTR.conrJinsrhe tinearbaseaddres.,rndsize(timn, ot rhe tDT.Tire
IDT is discussedin derail later in this chaprerin the section ..Interruptsand
Exceptions."
9a
& th. ao5a6 Plot.ctld f..h.nl.m
31 15
0 B3ck link
usP0 4
0 ss0 8
ESPl 12
0 551
't6
ISP2 20
SS2 24
cR3 2A
EIP
EFLAGS 3b
EAX 40
ECX
EDX 48
EBX
ESP
EBP
BSI 64
EDI
0 ES 72
0
0 5S 80
0 DS 84
0 !s 88
0 GS 92
0 LDTR
0 t T 100
104
TSS
limit
F|{]ote 5-5. Tatkstatesegnent(TSS)
The cDT is the primary descriptortable. The GDTR registercontains the lineff
baseaddressand limit of lhe GDT. Important descriptorsthat the operating system
usesresidein the GDT. An operating systemcan be built using only the Gm and
the IDT. The LDTS,however,provide an additional 1a)rof Protection and are help-
ful in building reliable systems.
The following illustration showsthe mechanismused to identify a descriptorgiven
a 16-bit selector.The selectoris composedof three fields: the index, the table indica-
tor (TI), and the requestedprivilege level (RPL).
99
t5
T R
I
Application passesfte rina 0 selecror(which is illegal fo! n to use) to lhe .ing 0 routine.
The dng 0 rounne gains acess to the ring segment and writes it !o disk.
too
5: th.AO3A6 Protetion cch.nl$
system can detect that the requested privilege level (RPL) of the selector is less than
(nurerically higher than) the DPL of the desired segmcnt and rcfusc to complete
the operation. Figure 5 7 shows the behavbr of a sccLrre opcrating system in this
ARPLadj!'stsselcctor
tor
iHE 60345 AOOK
0
I
2
100
101
LQ2
103
BrlgElJf,e
5-4. Opetuting slstem usine onu the GDT.
702
+ lh. AO3a6 Ftotetlon tl.chari$
,,' AddressspaceB
Descriptor Formats
Figurc 5-10on the following page illustraies the three forms of a des.riptor' The iol-
lo$ing Jre lhe dercrinlorryPe\:orogrammemorvsegment..sy.rem'egmcnl. and
gates.Progrnm memory segmentdescriplorswere introduccd in Chapter3 System
rgmcnr descriptoF.l.rribe LDT: an.l l5\\. Like prugf.r'nmemoryrgmenr de-
scriptors,systemsegmcntdescripto$ dcscribe regions of memory and have a base
and a limil However,you cannot lod a descriptorfor an IDT or a TSSinto a seg-
ment regisier and read or write the contenls as data. For an oPeratjng systemto up
date an LDT or a TSS,it must create a memory segmentdescriplor wjth lhe same
baseaddrcssand limit, calledan allas Programssuchasdebuggers, which let vou
modify yoLr program's code segments,must also create aliasesbecausecode seg
ments are not writable under the 8036 protection rules
t03
tHE ao3a6 BOOX
63 48 47 31 r6 1 5
"*'"ld
I'i sl _
0t '-
Limit
63 48 3L 16
otr:,,., sl
rYP" 0 Offset
1,,DPI il
Blg!,f,e5-1O,Galeraldes.rtptorforrrar slstem,menory,and gatedescrtptors.
lo4
5! lh. 00366 l''ot.ctlor f.ch.nlm
Multitasking
I have previously shown how the 80386usescall gatesto implement interlevel sub-
routine alls. Interrupt and trap gatesare discussedlater in this chaPter.The follow_
ing sectionsshow how the 80386can use the remaining s)stem objcts (TSSS,LDTS,
and task gates)to implement robust multitasking operating systems.
Simply defined, a task is "a sequenceof related actions leading to the accomplish-
ment of some goal," In a computer,ihe resourcesrequired to accomplishthe goal
are usually included in the definition ofa task-that is, the amount of memory, CPU
time,disk space,and so on.
The tetm multl\^skiflg tefers !o the ability of a computer to execute more than one
task simultaneously,The 80386cannot executemor than one instruction streamat
once, but it can executeone instrrrction stream,$witch to another,execute it, switch
!o a thirdj execute it, switch back to the original, and so on. Becausethe CPUexe-
cutes so rapidly, all tasksappear !o executesimultanecl.rgly,Concutrenc! ^n!l
mul tipr ogranxnlng are synonymsfor multitasking.
An executing task is called a p,"ocess.
Thus, some people refer to multltasking as
multiprocessing,Others, however,use the word t tlttp,'ocessrl,8to refer to systems
in which multiple CPUSor processorsare running simultaneously.To avoid confu-
sion, I do not use the term multiprocessins,and I rfer to computerswith more than
one CPUas multiprocessorsystem9,
Assumethat each task in a computer is implementedby a single programi therefore,
multiple programs must shareth cPU. various strategiesexist for sluring the CPU,
but !o discussand compare these strategiesis beyond the scopeof this book. At
some level, each systemmust turn over conFol of the CPUfrom one task to another.
The first task might be in the middle of a computation when control is wrested
from it ard passed10another taski when the first task resumes,it must be able to
continue processingas though nothing had happened.All the registersthat the lask
was using must be restoredto their original lues when that task regins control.
The 80386hardware supports this kind of task switching via the TSS Figure 5-l1 oo
the following page depicts the $emory layout ofthe TSS.EachTSShas only one de-
scriptor,which defines its basememory addressand limit. FiSure5-11showsthe TSS
descriptorformat immediately below the TSS.To allow accessto the TSSby differ-
ent privilege levels or via interupts, you must use task gates.
l05
fl{E ao:|a6 BOOX
63
"r;",1c
r.,it'1.1P
l"lt ot4;
47
T)?e
a2 31 15
limit
aL 16 15
Backlink
f"sP0
sso: 8
:LsP1. L2
arir: I6
2A
ss2 24
cR.} 2A
EIP 32
EFI,\CS 36
EAX 40
ECX 14
EDX 4a
EI]X
ESP 56
EBP @
ESI 64
EDI 6ll
0 ES 72
0 cs 76
ss 80
0 DS a4
0 FS 88
0 GS .)2
0 IDTR i 96
b ,
loP blunrD 0 l Tl 100
t04
TS5
l06
* th. AO3a6PElcctio fl.ch.ntm
Thc selectorin the task register (TR) idctuifies rhe current task. Usually, rhis registef
is loadedonce at initlalizalionlime andthen is managedby thc iaskswitchopera
tion.I-oadingTR doesn(n causea taskswitch;it doesidentifythe rctiveTSS,
lask switching
Four events can czusc a task switch on the 803861
. The current task executesa FAR CALI orJMP instruction inwhich the sclector
points to a TSSdescripk).
. Thecurfc lask execuresa FARCALLoTJMP instruction, andthe selectorPoints
to a lask gaie.
t07
. Thc curent task executesan IRET instruction to rcturn to ihc pr.wious task. An
IRET causcsa task switcb onLyif the NT (nested task) bit ofthe EFLAGSrcgisrer
is set to 1.
. An inteffupt or exception occurs, and the IDT enny for the vector is a task gate.
For any task swirch, ihe f(tlowing events take place:
L I I r \ ( r r \ k s i r c h r s n o r . J u . c ( l h y a h J n J $ r r c n r ( T u t l . a n e : \ ,e p r i o n .o r J n
IRFjT, the descriptor privilcgc rules are checked. The DPL of the descriptor
(TsS o. task gate) must be nLunericallylessrhan rhe cuffent task's CPLand the
2. The present bii and limit of the descriptor for the current (outgoing)'l SSis
checked to ensure IlnI thc TSS is present and can hold xt leasr 104 bytcs of s1.Ic
information. Ifso,Ihe currcnt machine state is savediothcrwisc an cxccption
Thc rask switched (TS) bit in CRois scr to 1, and rhe current privilege level for
the incomingtaskis takenfrom the RPLfield ofthe CSselectorin the TSS.
to0
5! rhe ao3a Protectlon echanrim
t09
Lifti!
1,10
l/oP 100
tto
5: th A03A6 Prot*tior X*henisn
Nunber CLrss
0 Fnult Diviclceno.
I FaLrk
or(ap Dcbug!!$nrtcrrupi
2 NonnMskahlc intcmupt
lntcrruplon overllow(lN'l O)
5 Aray bound.ry violatbn ( ROIlN D)
6
coproccssornor avNilablc
copro.esor segme.rovenun
l0
ll
12
IJ Generalprotcctionviolation
14
15
16
) , 13 7
Interrupr or r.ap S)stcm dependem
tll
trE @aa6BooK
One classof error is more severethan an abort. If the proces"soris unable to con-
tinue processingan exception, it shutsdown. In a protected-mode environment,the
systemshould shut down only if a hardwarefailure occurs.To prevent shutdown,
the vectors tlut handle the double fault (interrupt 8) and in lid TSS(inlerrupt 10)
conditions should be separatetasks,and IDT entries 8 and 10should be task gates-
This approach allows the 80386to load a new machine state from which to handle
the exceptions.If this is not done, the exception handler might be running in the
sameenvironment that causedthe failures and oisht not be able to continue
processmg.
03 4A 16 15
otrsel,, Offset
I, )PLl: Type
112
5: tho aoiB6 Prol.crbn f,*hani.n
ufL{Gs
0 cs
EIP
ESP
EFLAOS
ESPar initializarion 0 cs
pointed to other stack EII)
ESP
with tmnsition
Intmrptor exception
to ncw stacksegment.
F g$e 5.11,tnterrupt stetckunhout and ulth prltllege trunsnb
31 1 61 5 2 1 0
T E
I x
tl3
tHE ao306BOOK
Thc setector index and TI fields are taken from the selecto. of the segment associ
iled with the excepiion. lnstead ofan RPr ficld, howevet thc effor code has an I bit
and an EX bit. Thc I bit is set to 1 when the index refers to an IDT index, and the TI
bit is ignored. vhcn I = 0, the TI bit indicates whether the selector is from rhe GDT
or l-rom the cumem lDT. lf the EX bit is set to l, the fault was caused by an event
outside the executing proSram.
'lhc'Debugging"scctionlaterinthischaptcroverslhclriSgeringandhandling
dcbugt|aps in dctail.
Inter?upt2-NMl (intetruptl
ll)l vectof2 is rc$crycdior the haftlwnrcNMI condition.N()cxccptionstrap
thr(Nghvcctor2.
tl4
5: th. ao3a6 Plol*tlo. xeh.nls
A task gate c?o be$ handle the double fault vector,although a securering 0 segment
usually works. You shoulduse the method best suited for placing the systemin a
tt5
THEAO3A5
BOOK
known state b.tausc lhc proccssorshuts down ifa third fault occurs while the
is trying l() stari thc i crrupr S exception Mndler.
u03i:16
The shutdo*n srateis simjlarro the hah sLarc.only a proccss.rrreset of NMI (if the
NMI vector is valid) can bdng the pfocessor our of shurd()wn A special shutdown
signal is placed on the bus so that external hardwa.e can detLtt thc shutdown.
An error c()dc of0 is pushed onlo thc stack when a double fault exception occurs.
CS.le$riptor hxs S = 0
cs de$riptor not execuriblc CSindcx
Cs conforming,DPl.> CPL
CS ooronbtrDing, DPL I= CPI-of CS inclet
DPI < RPI
tt6
!! ln. 0oag6 Prcr.crion f,&h.ni.n
The CPLvalue is taken from the RPLof the incoming CSseiector If one of the
memory segmentdescriptorsis marked no! present,a not presentfault or stack fault
occurs rather than the invalid TSSfault. The TSSload stopsat the point of the fa lt,
and the other exception handler must ensurethat the remaining segmentregisters
get loaded.
117
it{E ao3a6BooK
If the slack far t is rriggered by loading SSwith a not presentselectoror if the fault
occurs during gated transition between privilege rings, an error code indicating the
offending selectoris pushed onto the stack.Loading sSwith invalid descriptors(out
of range, segmentnot writable, and so on) results in a generalprotection fault rather
thana stackfault.
Vhen the effor code is 0, this usually meansthat a given stack segmentis too small.
If the operating systemsupports expand-do$'n segments,it can expand the stack of
the faulting npplication.The savedCS:EIPpoints to the Paultinginstruction, which
can alwaysbe restartedrhovr'ever,the samecaveatthat applies to task switches and
not presentexceptions also applies to stack faults. Seethe final paragraphunder
"Interrupt ll*Not present(fault)[ec]" for more details.
31
U P
s R
tta
+ th. ao3a6 trclccllo ll.ch. .m
The three low-order bits of the error code provide more information about why the
addressin cR2 causedthe fault. The P bit is set to l if the fault was a page protection
. fault sther than a page not presentfault. The w/R bit is set to I if the faulting in
struction was attempting !o write to memory. The bit is cleared to 0 if the fault oc-
curred during a read. Finally, the U/s bit is set to 1 if the faulting instruction was
executing in user mode and is cleared to 0 if the instruction $"s a suPefvisorin_
struction. (User mde and supervisormode are discussedin Chapter7)
Beauseof the large flunber of divergentmemory accessestMt occur during a task
svr'itch,operating systemdesigne$ shouldensurethat important task tables(the
CDT, application TSS,and application LDT) are residentin memory before excut-
ing the task switch. The siluations that arise if page faults occur durinS a task switch
are not impossibleto deal with, but sysremdesignis simpler ifyou avoid them.
Intorrupt l5
This vector is reservedfor future Intel processors.
Intorruptr I 7-31
Thse vectorsare reserved for future Intel processors.
Intelruptr 32-45
These veclors are availablefor use by an operating system.The systemcan install
interrupt, trap, or task gatesin any IDT slol coffesponding to one of these intenupts
The interrupt handlerscan be invoked by software INT ', instrrctions or by hard'
warethatsignalsthe 80386via lhe INTR pjn.
lor exalllple, if a page fault anct a ctebug fault arc triggcred on the sxme instruction,
the page fault takes prior ity, and the dcbug faull is masked. However, s'hen the
page fault handlef completcs ils operilion and resiartsthe faulting instruction. lhe
debug far. t is retriggered.
other interupt masking conclitionsoccur when:
. An NMI is triggered. Fudher NMIS arc nuskcrl uniil thc ncxt IRE I instruction
Debugging
'li:rdir
nnrxlly, ,) kx)p(Ecssors hrvc ncvcf()m.ilNtcd mLrh tu solving Lhc prcblcrr
oldclruggiot:. Dchugging on ll1icft)proccss(xshas bcen accorDplishcdwith brcak-
pojnt inslru(Iiors rnd wirh thc abiliry k) singlc stcl (cxcrlrlc one inslruclion rl x
tnnc); but fbr (lifficult pr(trlenrs, prr)grannrcrs l)ave hxd k) (urn li) in-circuir cmula-
tors or hxrdwrrc-nssistcd dehuggcfs.
As D icfocomputcr'syslcrnsbccomc more sophisticrted, hrrdwrfc's xbilily k) clcter
rninc whrt is going on insiclc the Cl'Ll diminishes. FofexaDrple,assLnnctha! r pm'
gftrmmcr wrnts ro bc notificd llnt.r particulardata strucnre hlls heen nx)dilicd.
Bccrlrsc of prgjng, re srructLre might not be in rcntiguous menxny. Thc opcr.rl-
ing systcln s vinurl rremory capebility allows itto rnove the progrrm out from
undcr lhc cyc of drc .lcblrggjng har.LwaLe,.ndlhus the prograrn'slinear ancl synr
bolic nd.Lrcsses bear no relation to the generaredhardware adcl esscs.
Fodunalcly, thc 803M) dssignc.s rccognizccl drcsc problcms rnd added features to
the p()ccssofllut syslcm soflwlrc can use to aid in debugging. F'orirmechanjsnrs
tfigger clsbug i crfuprs u.dcfdiltrcnt conditions: trap flag, rask switch trap,
breakpoi.t fegisters,and soflwafe breekponlr.
Trap flag
Setting dreTF bit i. lbe DIiLAGSrcllistcr causcse singlc-stepfault (inteffupt 1) to
occur beforc the next instruction. The 80386clears the T!'bit before invoking thc
handler pointed b by lD l (l). xldDugh the saved image of EFLAGS on the stack has
Ihe trap flag set.
12l)
!! th.ao3a6trct Grbn lt*h.nl.n
Brorkpolnt rggbtorr
The debug registers(DRo-DR7) implement four addressbreakpoints.vhen the
registersare correctly initialized, each identifies a linear address.If the processol
accessesthat address,a debuggerfault (interrupt 1) occurs The debug regist$ are
describedin detailin "Prosrammingthedebugrgisters"in this chapter'
Soltware b.oakpolnt
The single-byte INT 3 (0CCH) instruction triSgersthis interruPt By replacing the
first byte of an instrrction with an INT 3, a debuggel can causea breakpoint to oc-
cur when the execution streamreachesthe INT 3 Becausethe software interrupts
are classifiedas traps, the savedcS and EIP on lhe stack point to the byte immed!
ately after INT 3. To restart the program, the debuggermust replacethe ocCH value
with th first byte of the original instrution, decrementEIP so that it Pojnts to the
stari of the instruction, and execute an IRET to return from the interupt handler'
This method of implementing breakpoints is much clumsier than using the debug
registersbecauseit requires creating a sritable allas for a code segment'saviogthe
original instruction byte, replacing the instnrction with an INT 3, and undoing the
abovewhen the breakpoint hasbeen triggered Hoer'ever,becausethe debug regis-
ters allow only foul active brakpoints at once, a reasonabletradeoff is to use debug
registersfor data spacebrsdkpoints and INT 3 for code spacebreakpoints
J1
tsreakpolnt address0
Brerkpointaddress2
Breakpointaddress3 DR3
RESERWD DR4
RESER\'ED DR5
B B B B B aB
0 T sD 3 2 1 0 DR6
LEN R\( ulN R\J( LEN R't( LEN R\( G L G L G L G L G L
3 3 2 2 1 1 0 0 0 E E 3 3 2 2 I 1 0 0 DR7
31 16 \5 8 7
Ft$re 5-15.Debugreetsters.
Bits 80*83 are ser Io I if dre breakpoint in DR0-DR3was malched during execu-
tion, evco if the breakpoint was ,ot enabled and did not causethe debug fault.
vhen Intel ICE 386 hardware is used,the debug registersare reservedfor the in-
circuil emulator.The BD bit is set to 1, and any attempt to place (MOV) a value in
one of the debug registerstriggers an interrupt 1.
The debug iderrupt handler must clear the contents of register DR6.The CPUsets
bits,but bits canbe clearedonly programmatic?lly.
122
3! Th.aOOa5 Prct ction lrlch.trl'd
enablea
DR7is the debug conrol register Placing an addressin DRo-DR3!r'ill not
set, as must the breakpojnt length and
trreal-point. The"enablebi(s) in DR7must be
ralues"re
The LE\l? fieldslet you rpecilv r he leng h of brerkPoinr'1 l he lengh
encoded as followsr
Oo-Byte / breakpoint legal at any address
O1-vord (2 bytes) / breakpoint must be on even address
1o-Reserved for fulure use
ll-Dword (4 bytes) / breakpoint addressmust be on dvr'ordboundary
breakpoint
The R/v, field lets 'r)u specify the tlpe of memory acccssthat triggers
'?. This field is encoded as shown belowl
o0-Execution breakPoint
ol-Memory write breakPoint
lo-Reserved for firture use
ll-Memory read or write breakpoint
'When also
R/v is set to O0B,an execution breakpoint' the correspondingLEN field
th breakpoint ad-
must be set to OOBAn execution breakpoint is triggered only if
part of the in-
dress is set to the first byte of the instruction. If any Prefix bytes are
byte
struction, the breakpoint must be set to the addrcssat the first Prefix
lf neither
The L, and C, bits allow breakpoints to be locally or globally enabled
is disabled and does not triSger an inteF
the ! nor the G bit is set, the breakpoint
DR6 is set if the breakpoint condition is met
rupt, although the correspondingbit in
clearslhe L
ItonLv the L bit is se(,the breakpojnris localLyenabledA taskswjtch
uring locally en' D|ed
bils ihe syslemshouldmark fte T bit in the TSSof the tasl<
Thn' the L
U."rto.f"i" .. ,tt"t an itteffupt 1 occurs when the task is reaciivated
bits cin be reset.
only by
If the G bit is set, the brakpoint is globally enabled and can be disabled
clearing C to O.Settiogboth the L and G bits equals setting the G bit
it enables
ResisterDR7contains two other bits, tE and GE When elther bit is set'
80386 processor slows
ttEexa"t rnat.tt cona;tion when exact match is nabled,the
triggered the break-
to ensure that the intenupt 1 faull reports the instruction that
becau<e of
ooinr it r-eana ce are o, rhe 8038omighr geraheadof thc debu8uni(
andrheCSandEIPonthe inrerrug hrn
ir,. i",.,rrr p"r"rr"rl'-;n rheptocessoi
a...".f. - 'Sh, p"ln, one or lwo Insrrrntions beyondthe one I hJr.ltiggercllthe, ,
'hould he enabled I ne
frulr.The pe;formancelossis not \ignili\anr' anLlLE'rnd GE
diflerence betvreen the two bits is tbat LE is cleared after a task switch' as are the
L4 bits.
1Zt
fllE ao:F6 BOOK
Df,o! 0004C020H
DR7:tEo - 1,R\vo- 118,LENo-118 MOVAL,[23]
DRo!0004C020H
DR7:lEo - 1,R\vo- 11B,LENo-118 INCDVORDPTRI01El
DR0!0004C020H
DRT|!E0- 0,R\vo- 114,LENo'11B INCD'{9ORD
PTRlolEl Breakpointnot
DRo:O0O3A0O0H
DR7:!E0 - 1,Rvo - 008,LlNo - 008 CSr0000
MovAr,37H
DBo! 0003A001H
DRTILEo- 1,Rwo- 008,IEN0 - 008 CS:0000
MOvAI, 37H
12i|
MEMORY
AFICHITECTURE:
Physical
Segmentatiooand paging are similar: A name and an offset are translatedto an ad-
dress.This mapping is the essenceofvirtual memory. However,segme ation ancl
mapping are also different- Assumethat any virtual addressfrom ttle Previousex-
ample consistsof a two-digit number and thal the digit in the ro's place is the fiame
name,rather than a letter, as in Figure 6-1.A virtual memory translation would re-
sembleFigure 6-2 on the following Page.In this examPle,virtual address27 is
translatedto ohvsicrl address17
t||E 036 BOOX
\d8.E|Jtre
6-2. vi/t al aid6 trandation off&cn'sze eteneflts.
18
e .m.ry ^rcht*lut.! P.dng
3r 12 1 1
Pageframeaddress31.. .12 0 l) 0 0
? R
( P
The bits marked Oare reservedfor use by future Intel Processors,The field marked
.4a?ll can be used by systemprogrammersto mark pagcsthat are sharedamong
tasks,to hold usaginformation, or to store other paging data. The Pagefram acl'
dressbecomesth high-order bits of the physicaladdrss The 80386setsthe D
(dirty) bit to l when a write operation occurs vr'ithin the specified Page The cPU
sets the A (accessed)bit to l when any memory access(read,write, or fetch) occurs
within the page.
The U/S and R/v bits are part of paging'sProiection mechanism They are dis-
"PageProtection" sction
cussedin this chapter's
vhen the P (present) bit is set to 1)the page is Presentin memory lf P - 0, the Page
is assumedto be swapped to disk, and any attemPtto accessthe page resuLtsin a
page fault (interrupt 13).Y.rhenP - 0, all other bits in th Pagetable (31-D are ir-
ielivant to the aa6 and can be used by the systemprogrammer Frequently,a
swappd page'slocation on disk is storedin those bits when the page is not preseot
127
Thc'$olutionto this spaceproblem,swappjngour the pagctable,ca.not be imple-
mcntcd aith a simple, one level p3getablc. For example,if a program tdes ro access
adctrcssr., the page table cmry (PTE) for jlj lnusr be brought inio mcmofy. Because
the pigc tableis itselfpagcd,the PTEfor pTE(jr) 'rust be broughrimo memory
first. Swappiqgcontinucsuntil the initial pageof rhe pagerablcis swappedin.
A bettersolutbn. the one nnplcrrcnredby rhe 80386.is a two levelpxgetablc.rn
this schemc,thc virtualnamecomponentofrhe vi ual address(the high-odcr 20
lrits)is split into two parts.Thc high order10birsare usedas an index inro a 1)dge
director! A paue.lnecbty entry (PDE)points to a scaled down page rablc thar <)n
tains1024entric's. The l0 bits lelt ove. in dre virrualaddressselectthe pagcrablc
cnties from thc pagetable.Figurc6-3 illusrratesthe two,levetpagestructurc
'this
stfLlcturesolvesthc p()blem of swappingour rhe pagetablebecausethe initi:rl
lookLrpgoesdrroughthc paSedirectory.Thc pagedirectory,wi$ 102432 bit en
trics,lakcsup only 4 Ktsand is permancnrlysr)redin memory.Eachpagetat)leatso
txkcsup 1rKB (fits right imo x pagel)and hxs 1024pagetablccntrics.
RegisterCll3 containsthe physicaiiddfessofrhe pagedirccrofyfor a task.Clt3is
thc only 80386rcTisicrtbat containsa physicalmemoryrddress.A pagedirectory
cntry lus the samcform^t asx pagetableenrryexceprthat rhc t) bil is unusedand
thc A bil is setb I whcneverone ofthc paSctablespoinrcdk) by lhc pagedifc{k)ry
[-.l t til
t
I- t s
:
- ro23
lndcxf lPasctii'ne
124
6! [.mry Archil.ctuF: P.gine
A detailed oxamPle
Fique 6 4 showsa linear addressthat is tmnslated to a physicaladdressvia paging'
As;ume that an instruction refers to the lingrr address13A49F01HThe frame name
(13A49H)is split inlo a directory index (04EH) and a pagetable index (249H) The
page directory is at the addressspecified bv register CR3'location 1C000HThe
page direcrory elementnumber O4EHis selected.It containsthe lue 3A7A2rrc'H,
where ,rjcn representsthe Pagestatusbits lf the presentbit is set,the Pagetable
begins al location 3A7A2000H,and page table entry number 249His selected ln the
anample,this entry containsthe value 2c115rrtH, where ,rn rcpresentsme con-
tents of the statusbits. The offset of the linear addressis aPPendedto the page
frame to yield a physicaladdressof 2C115F01H
- 0 0 0 1 0 0 1 1 1 0 1 0 0 1 0 0 t 0 0 1 1 r 1 1 0B0 0 0 0 0 1
L n e a ra d J r e s < l J A 4 g F O t H
__----T------ ------- ------
102)
Pagetable
2C115101H
cR3
125
tHE 40386 BOOK
Page Faults
If a page descriptoris marked not present(p = 0), a page fault (interrupt 14) occurs.
When this happens,reSisterCR2storesthe linear addfessthat causedthe fault,
anCan error code is pushed onto the stack,Pagefaults can also be causedby viola-
tions of lhe page protection rules, described in the nexr section. Chaprer5 conrains
additional information about pagefaufts in the section called "Interrupts
and Bxceptions,"
Page p?otoction
The format ofa paSedircctory entry and of a page table enrry includesbirs marked
U/S and R/W The U/S bit specifieswhether a paSeis a user page (U/S . 1) or a
supervisor page (U/S - 0). A supervisorpage cannot be used by any procedure run-
ning with a CPLof 3. However,a Focedure wirh a CPI of 0, l, or 2 can accessa
supervisor page. User pagesare accessiblereardlessof the CPL.If a page direcrory
entry is marked with U/S - 0, only a supervisorprocedure can acce$ pagesin the
pag table pointed !o by that directory entry, regardlessof the U/S seiting in rhe in-
dividual pagetable entries.
For a user level program (CPL- 3), accessto individual pagescan be restricted fur-
ther with the R/r / bit, A user level program can read or can executeany user level
pagesbut can wdte to a page only lf rhe R/!7 bit is st to 1 in the pagedirectory and
in the page table enlries. A supervisor level program can read or can wdte pages
regardlessof the sttingsof the R/\i, birs. The rules are summarizedby these
fbrmulas:
- (cpl< 3)l GDE(U/S)= 1& pTE(u/s) = l)
read-access(addr)
wdterccess(addr) = (CPL< 3) | (read,access(addr)& PDE(R/\O = 1 &
PTE(R/V) = 1)
Vhen a user level processloads a selector,issuesa software interrupt, or generates
an accessto the GDT, LD! TSS,or IDT to load a descriptor,systemtable readsand
$'rites are treated as supervisor level accesses.Pushingvaluesonto an inner-ring
l3tt
6: fmo.y Archh.clur.: Pasinq
srack segme is also treated as a supervisor l*el access lf the systcm tables had Io
be stofed in Lrserlevel pages, Ihcy would be lcss secure dran if storcd in supervis.rr
!'or examPle,sc*ments do not nced to fit int() I single page or into a mr. tiPlc ol ,?
pagesr2 Pxgc can contain porlions of more than one segmcnt,or vice versa l bw"
ever, men)ry managementis casicr if alLsegmcntsare nruhiPlcsof 4096 lrvlcs You
can mark xll scgmcnt hrits xs Pagc granular ((l = I jn the segncnt clescript(x ). rn.l
each segmenl limit field willcontain the ntrmbcr of pagesrcquiredto hold thc seg
Multitasking
Opsralingsyslcmdcsignerscanchooscio suPporteilhcra singlcmctnorymap (onc
forcrch lxsk)()f muiripiemenxxy maps(onc fbr dtc systemnnd one for caclr.iPPli
clltion).A singlcvirrualrnemofyspaceis dre siNpLcstaPproachihowevcr,anvsys-
tcln thal supponsmultipievirtual8086-modctasksnedsa cliifcrcnlsetof Pxgc
txblcsforeachvs6lask.In vu6 mode,eachiaskaccesses 0 ro I MB
lincar.rddresscs
Thcrc mustbe a scparxlephysicaladdress spaccfor eachlincaradcLressspacc
Figlrrc6-5 on 1hefbllowingpagcsbowshow Vu6taskscanbc mappedto phvsical
t3l
THE AOi|a6 BOOK
4MB
lMB
640KB 3MB
2MB
0
1MB
V86task1
1MB
0
640 KB
[ach pair of affows indicates
a set of PagemaPpings.
Unearaddress
Et$ilrre6-5,.!4awtng186 tatkttophys(al r4enaL.
8 MB virtual addressspace
007Fr000H
00040000H
00002000H
00001000H
00000000H
Page table 0
Illegal addresses Swapped pages
132
& ia.mory Archlt*tur.! F.chg
i o r t i ' o ; i < 1 0 2 4 ;i + )
atll-bllI+ctlll
The code for the program residesin one page,and each array (a, b, and c) residesin
a separxtepase. If the operating systemprovided a wo(king set of three pagespr
application, this program would run slowly becausetwo pageswould have 1obe
swapped to disk for every,6/ loop iteration. Figure 6-7 iLlustratesthe swap.
only 3 pagesln
mmorystmultaDeously.
.1 musrbe swappedout
andd swapped ln, then
d swappdout and,4
in 1024times.
Most operating systemsprovide working sets much larger than three pagesper
application, but applicationswith large memory requirementsmiSht seesimilar
results.If J,ouwrite an application that requires a large amountof memory, ycu
might improve its performance by changingthe ptogtalJ\'s locality oJreferefice.
The previous program ftagment needs accessto many pagesfor every rycte
through the loop. If this program were running under the operaling systemde
scribed previously,you could increaseits performanceby changingthe data struc-
ture so that at, bt, and ci reside in the samepage.
133
fl{E 40365 BgOX
int a, b, c:
) blockl1024l;
.
f o r ( l - 0 r J < 1 0 2 4 :j + l )
b l o c k t i l . a- b l o c k t i l . b+ b l o c k t i l . c ;
The program now runs with only two page ss?ps, as shoNn in Figure 6-8.
134
7
THRE E
TN ON E
In earlierchapters
I alludcdto the 80386'sabilitylo run sofrwarcwrittenfor pre-
viouslntel microprocessors.This chapterexplorcsthis ability and disclrsscs
how ro
makethc mosrofit.
The 80386provides an almost ideal upgradc parh from previous gcncralions of Inrel
processors. In realmodc,the 80386canrun U086-family programs.Il canswirch
inlo protected modc and execuie 80286soirw"re. The native modc of rhe 80386ex-
pandsfie prolecrcd-mode capabilities
with 32-bitoperationsand climin4resthc 64
KB segmentrcstrictionsof lhe 80286.Virrual8086modealsolcts you run real-mode
prcgramsin protecledmodeithis is advanlageous becauscthcrc aremanymore
real-modeapplic4tionsavailablethanprotected-mode applications.
Beal Mode
When the 80386is poweredup orreinirializdvia rhe hardwareRESET\line, rhe
CPUis in real(reafaddress)mode.Io rcal mode,all of rhe CPU'Sprotccrionfcarures
are disabled, paging is not supporred,anclprogram addressescorrcspond |o physi-
cal memory addresscs.The addressspaceis limited to 1 MB of physicalmemory.
Realmodeis compatiblewirh rhe 8086,rhe 8088,the 80186, rhe 80188,and rhe real
modeof the 80286.Minor differencesbetweenfealmodeon rhe 80386and orhel
processorsare listed in Appendix F.
Vhen lhe 80386is reset,the regisrersare initialized ro rhe valuesshown in the tablc
on the fbllowingpage.
135
t||E 003a5 BooK
DH 3 3 for 80386
DI Identiies revision number ofCPU
ETTAGS 2
IDl'R 0 (base), 3IFH (linit)
Memory addresslng
The 80386'suse of shadowregisters(segmentdescriptorcaches)provides a key to
underslanding real-mode memory addressing.Each80386segmentreSislerthat
holds a seleclorhas an invisible component called a shadowregister.In protected
mode, evefy time a selectoris loaded into a segmentregister,lhe contents of the de-
scriplor indicated by the selectorare loaded into the shadowportion. In real mode,
lhe shadow register is loadedwith a computed lue rather than with a value
extracted from a descriptor.Figure 7-l illustrates the shadowregisters,
'when
the 80386is resct, fie shadowfegistersfor segmentsother lhan CSare loaded
with a base addressvalue of 0 and e limi! of oFFFFH.wth attributes set to 16-bit ad-
dressin$16-bitinstructionsetiread,write,andexecuteabilityiand privilegelevel0.
The CSshadowregistersare set with the samelimit and accessbits as the other
shadowregisters,but have a baseaddressofFFilFooooH. Except for the registers
listedin the abovetable,80386registersareundefincd.
1
CS
SS
DS
ES lrogmminer accessible
I
rc
GS n No! accessible
Invnible' descriprorcache
t36
7: rhree h On.
1 t\,tR
0140 MB+6'1KB
00001400 2CB
Baseaddtcss
137
THE 603A5 BOOX
To unde.srand how the D bit works, examine the 8086 instruction set Most 8086in-
strutions executewith either a byte operand or a word operand The byte/word
indicator is encoded in one bit in the instruction. For example,the oPcodefbr negaF
ing a byte operand is 111101108, and the opcode for negatinga word operand is
111101118.
RJlherrhaninvenrnew op-odesfot J2-birI dwordI operands.80J86de{isners
changedthe meaning of the opcode bit that signifies a lord operand \I/hen exe_
cuting in a native-mode (32-bit) segment,where the D bit in the segmentdescriptor
is set to 1, executinSopcode 111101108 meansnegate,/te and 111101118 mans
negate drrorl The instructions refer to bytes and rather
drrv'ords than to bytes and
woKls. Vhen the D bit of a descriptoris set to 0, however,the opcodes retain their
or'Sinalmeanings.
The D bit also affectsaddresscomputation for memory operands and the slack.
when D = 0, coffespondinS to the 8086,the 16-bit registersare used in calculating
segmentoffsetq as in MOV AL, [SI+8].r /hen D - 1, correspondingto the 32-bit
native mode of the 80386,the sameopcode bits causethe memory addressto be
calculatecl and the instructionbecornes
usingthe 32-bitregisters, MOVAL, [ESl+8].
\(/hen D - Oin stack segmentdescriptors,PUSHand POP instructions access16_bit
operands,Vhen D = 1, 32-bitPushesand popsare executed.
The OPSIZand ADRSIZprefixes qan oveftide the cuffent D bit setting for an in-
struction.Thus;32-bitnative-modeinstructionscanb Preflxedto use16'bit
operands,and 16-bit c.()decan be prefixed to access32-bit opelands and 32_bitad-
dressingmodes.The new 80386addressingfeatures(such as indexino are not
availablein segmentsthat have the D bit se! to Ounlessthe ADRSIZprefix is used.
You need not speciiy the prefix instfuctionsi use extended_addressing mode' and
the assemblerwillinsertthe prefix.
'when
using extended addrssingin real mode, observe the 64 KB segmentsize
limitation. In real mode, addrssoffsets greater than 65535return an inteffupt 13
Interrupt processlng
Intenupt handling is different in real mode than it is in protected mode. As in Pro-
tected mode, the IDTR contains the baseaddrssand limit of the interrupt table For
8086compatibility, lhe baseis initialized to physicaladdress0 with a limit of 3FFH.
In rsal mode, however,the interrupt table does not hold descriptorsieach interrupl
hasa 32-bit selectonoffsetaddressthat points to the routioe to be invoked when an
interrup! occurs.Thus, each enlry is 4 bytes rather than 8 bytes Figure 7-3 illus-
trates the real-mode interrupt vector table
Processingof an interupt in real mode is similar to that in protected mode except
for the use of vectors insteadof descriptors.A software or hardware interrupt causes
the 16-bit FLAGSregisterto be pushed onto the stack, followed by the current cs
and IP The IF and TF flags are cleared to 0, disabling interrupts and single-
stePpmg.
t3a
Physical memory
3t 16 75 0
Vector255
F1gorc1-3,Real-mode
interlupt tEctortaqe.
The pointer from the interrupt table is loadedinto CSand Iq and processingcon-
tinues at the new location, Automatic task switching and interrupt gatesare not
presentbecauseno descriptortablesexist in real mode, The vector in the inteftupt
table specifiesa new excurion addressonly.
Real.mode yestrictions
You canuseall the instructionsaddedto the Iniel80386archirecturesincethe in-
troduction of the 8086,with the exception ofl
IAR
ILDT
ISL
LTR
SIDT
STR
VERR
VERW
Realmode does not support the ways that these instructions accessprotected-mode
selectorsand descriptors,Executing one of these instructions returns nn undefined
opcode fault (interrupt 6).
You can exeuteall other 80386instrutions. Real-modeprograms can accessany
80386register,including the control, debug, and resrregisters.
Realmode does not support paging. Settingthe PG bit in register CR3ro enablepag-
ing causesa protection fauit.
Appendix F outlines the differencesamongthe operarionsof rhe U086,rhe 80286in
real mode, and rhe 80386.
139
fltt aoaa6 300x
Prctected Mode
Sttingthe lo*order bit of CROto 1 switchesthe processo into protected mode.
The processorwill run in protected mode even if no setup is done. Thai is, it will
nul unril the first inteffupt, FARprogram transfer,or segmentregister load. At this
point, the processorneeds to accessa descriptortable. Becausethe 80386depends
on descriptor tables,the systemwill shut down if the descriptor tableshave not been
initialized.
Protected-modeinitialization requires you to set up a global descriptortable and in-
terrupt dcscriptor tablesand to createa task statesegmentfor the first process.The
initial descripior tablesmay be stored in ROM,but lhey must be copied to RAM
before stting the GDTR and ImR to point to them becausethe 80386needs to
writc to the descriptorsns well as read from them.
-.4
Figure .how<a simpleinilialGDI. thi{ CDT wouldhe.ufficientro run ddi-
tional startup code. You could also build the operating systemimagc in real mode
and then switch inooprotected mode. An advantageof$witching into protcctcd
mode as soon as possibleafter reset is that the 80386hardwarc can hclp trap startup
bugs early in the codc devebpment cycl.
In Figure7-4,GDT(o)is uft)sedbecausea selectorwlue ofo is treatedas a special
case,a NULI pointer Thus,any descriptorat GDT(o)will neverbe used.CDT(I)
points to rhe GDT as a witabie data segment,allowing the operating systemto add,
delete,and changedescriptorsas needed.GDT(2) points to the IDT as a writable
data segmentfor the samereason.cDT(3) defines the TSs for the startup task,
cDT(4) defines lhe task'sdata segment,and cDT(5) defines the task'scode seg-
ments.which are in ROM.
0
I
2
3
4
5
140
7: threeinone
Before enabling p.otected mode, the GDTR mlrsLbe loaded wirh rhe address.rn.l
l i n i ro l l , eC D T .T l - , I D T : l u u d . o n . J r ng J r , - , 1- r t , , . r r r J . J J e . i , r r i p \ J n )
far. ts that occur dlring staflup. The ID IIT is initi.rlizccl ro p( )inr ro rhe IDT, and t R is
loxded with the selcck)f ofcDT(3). The PM bii is ihen scr in the CROregisterto en-
xble protected modc. Next. a FARjunp instrrrrion loads rhe CS registerwirh a v. i.l
prctected mode descript()r Finally, the stack s.gmcnl, srack poinrer, and data seg-
nrentregistersareloadcd'rheinitializationwillbuildrheresrofrheoperaringsys-
tem, enable prging, .rnd start application pfograms.
8(,286 compatibility
Protectedmode80286cocleexecuteson the u03il6if rhe lburrhwold of eachdc-
scriptofis initializedk) 0. I)cscfiptorsare 64 bits ()n thc u0286.xs on the 80386,but
rhe lastl6 bits .uc unLrscd. Il1the 80386,thc cxlm bits spccifyrhe high ordcr.,fthe
lraseaddressan.lthc lhit fielclsnnd containthc (l xnd I) controlbirs.'lhcscncw
ficldsshouldbe sct k) 0, resrricrinS segr'entlimits 1064 KII and a(ivaring thc 16])it
insin'ctionsct (which is (\rmFrible *,ith thc 80211(t.
'lhe
u02il6xnd the 80386operatesimilxrly;the few clilfcrenccsin opcr.xri( con,
cern pcrlormxnccxnclnewly xnplenrcntc.lli ||fcsxnd insirrrrrcns.t hc i10386
rllows thc IOCK prcfix to pfececlcthe followinginsr[cLionsonly whcn (l)cy
nodily mcmory:
AI)C INC
AI)I) Nll(;
A Nl ) NO'I"
lJt- OR
BTC 51]I]
B'l]t sua
BTS xci IG
DEC XOR
Illcgaluseofthe LOCKprcfix rcsul!sin a protecrionlluh on rhc 80386.Addi-
rionally,the 80286locksrll of physicalmemoryduringrlrc insLruction;
on rhe
80386,the lockedareais thc mcmoryfegionwith rhe sarrcsraring xdclfessand
lsngthrs the operandofrhc lockedinstruction.
The machincstitus word (MSV) is thc low o cr 16bits of regisrerCR0.Thc MS\v
is initjalizedto 0F|F0H on the 80286,bur ir is inirixlizedb 0 on rhe 80386.Rcgisicrs
that are spccified as undeiined ar reset mighr havc diferenr valuesthan drcy do on
rheU0286.
A! reser,fie baseaddfessofthe CSregistcris diffcre.r on rhe 80386thanir is on rhe
U0286. The CSregisteris setto the last16byrcsofaddressspaceon both processo.s,
but the 80286supporrsonly 2,i bit addresses;rc 80J86slll)porLs32 bir addresses.
141
tHE ao3a5BOO|<
If you must rcturn to real mode, follow this pro.cdurc: lfpaging is cnabled, turn it
offby b.anching to a routine whose linear and physical addressesarc the same'
cleadng rhe PG bjt in CRO,xnd moving 0 inLocR3 io flush the TrB
'lhe
attrjbute bits in cach segmentclcscriptormrisl bc sct to valLEscrtnPrtible with
rcaL-modeopcrxtn)n (that is, they mLrslbe byte granulxr scSmentswilh a ]nnit of
oliFf!'H, ancl the 1l xnd D bits must bc 0) CS must bc m.rrked executablc,ilnd SS,
l)S, ljs, !S, and GS shoukLbe "'dtablc scgments.(Changc thc CS selectorby issuing
.r lAlt jLnnpofcxll instruction.)
Disablc intcrrupts, ancl loxd tlre IDl lt with .l bxse addtssot' 0 xncl x litnit of 3FII I
Clcxr rl)c Ptj bit oiihc CROrcgister !o return to real mode, xnd exc{ute a FAn iu'Ip
to llush thc 803u6 instrl|clbn queLrexnd iniiializc CS to x vxlid rcrl-modc bxse
Oncc yoo loa.llhe stack poinlcr (ss rsl' ) nd (hc olhcrscl]drc registe.s,prcgrams
cxn contintrc proccssingjn '!^lnrxlc
142
7: thre. Ir OE
143
tHE A03A6BOOK
CLI
INT
IRET
LOCK
POPF
PUSHF
STI
Ifthc systemruns with an IOPLof 3, the V86-modetaskwill executethe instruc-
Iions above withoul triggering the gcneral proteclion fault. This createsa problem
becauscthcscinstruclionsmodify the inteffuptflag.Although80386performance
may be higherrr'henIOpL = 3, this operatingmo.lc is not recommended. Allowing
a V86-modc task to disablc interrupts col cl result in a data loss or a systemshur'
down. Forexample,rhe followingtwoline asscmblyprogramlocksthe systemand
requircsa complctcpowcr cycleb bring thc syslcmback on line:
cli
11r Jn'p 1l
1.4
7! thre. in on.
A tradeoff exists between performance and prorr:crion.If you a ow all rasksto issue
I/O instructions, more rhan one rask might accessa device simultaneously.How
ever,if you trap all I/O instructions, prcgrams might run slowly_A compromise is
to mark I/O addressspaceas inaccessibleuntil rhe firsr fault occurs.By trapping the
first I/O instruction to a given port, the operaring systemcm derermine wherher
another task is using the device. If not, the permission bits for the faultine raskcan
be modified to granr accessto ihe specific device, and the rask can resumeprocess_
ing it iull ipeed. Il someolher ra:k is acce\\in8rhe de\ i. e. rhe ldultinraskc.rnbe
suspended or terminared.
Memory-mappeddevicesmust be controlled rhrough paging hardware.pagesthar
correspondto device addressescan be rtarked ,.not presenf'to causea fault, or
they can be mapped to other devicesor memory locations for subseaucntDrocess_
ing.(The larrerrscilecri\e lor Jisplry devrcu!.,
145
tHE AO3e5 BOOK
'!/henever
an interrupt occurs while the processoris executing a V86-modetask,
control movesto a ring 0 code segment.Control may rransferdirectly to ring 0r or it
may transfer to the generalprotection fault handler (which must be in ring 0). The
ring 0 stack is slightly different when control comesfrom a V86 task than when it
comesfrom a protected mode procedure.All segmedtregistersare pushed onlo the
ring 0 stack when an inteffupt or trap occurs in a vll6 task. Figure 7-6 illustrates the
differences in the stacks.Note that an effor code will also be pushed for certain ex-
lTr
I Ral"mo
t46
THE
ao3a6/ao387
TNSTRUGTION
SET
REFERENGE
'lhis
chapterof 7h? 8O3a6Eook provides a referencefor thc 80386and 80387in-
slructionsets.The instructions^re in alphabetical
order,with floating-pointinstruc-
tions following the 80386instruction pages.
The experienced user can find information with a quick glance at the first part of an
inskuctionr a lessexperienceduscr can refer to the detailed descriptions and
Operators
The following referencepagesuse these ope*tors:
147
THEao:ra6'OOX
l4a
6: flE ao56/4o367 ltrltrucrbn 3t R.lcr6.c
AAA 8086/80186/80286/80386
ASCII Adiust After Addtdon (8)
Syntax
Ope.atlon
i f ( A F ( ( A L & 0 F H )> 9 ) ) t h e n
ALE(AL+6)&OFH
endif
Legal Form
Dercrlptlon
This instrlrctionensurestharan ASCIIor BCDaddirionresultsin a lid BCDdigit.
After executinganADD orADC instructiontharleavesa singleBCDoTASCIIdigjt
in registerAL, execureAAA ro produce a lid BCD result,
If the value in AL producesa decimal overflow, rhe BCD digit is forced inlo lhe legal
range (0-9), and AH is incremented.The high-order nibble is zmed so that AL
contains only the resulting single BCD digit, and the AF and CFflags are set !o 1.
If no overflow occurs, rhe AF and CF flags are reset to 0.
Flagg
OFDF IF 1T SF T AI PF CT
Fault!
None,
Examplo
tl0v AL, '5' I B i n a r y3 5 n
ADD AL, '7' ; A d d b i n a r y 3 7 Hy j e l d i n q 6 C N
^aa : A L e 0 2 H , A H e A H+ 1 , d e c i n a t c a r r y s e r
0R AL, 30H : c o n v r tr e s u l t j n s d i s j t t o A s c l r . 2 .
149
fltE ao3a6 Bool(
AAD 8086/80186/80286/80386
ASCII Adiust Before Dtvision (16)
Synlax
Operation
A L e A H * 1 0 + A L
Log.l Forrn
Deacriplion
This instruction supports BCD division. Before execution, the AL regbter should
contain a single, unpacked BCD digit. The AH register shouldhold the next higher-
orderBCD digit.Afrerexecutingthe AAD instruction,Ax containsthe binaryequiv-
alent ofthe two BCD digits. You can then issuethe divide instruction, which leaves
a binary result,
Flag.
OFDF IF TF SF A AF PF CT
x ?
F.ults
None.
Erample
f10v A H ,' 4 ' I H i g h - o r d edf1 g 1 t
flov AL, '2' I L o w - o r d edr1 9 1 t( A x - A S C I 4 I 2)
AND AX, 0F0FH ; convertto unpacked 8cD
AAD ; Ax e 2AH(42 declnal)
llov BL, 6 ; Djvisor for 4216
DIV BL : A L e 7 ( q u o t i e n t )A, He o ( r e n a j n d e r )
0R AL,30H I C o n v e rrt s u l t t o A S C I I' 7 '
150
A: th. lOSaAraO3aT lEtdctlon 3t R6t.Enc.
AAM 8086/80186/80286/80386
ASCII Adiust Aftr Multtpltcatton (8)
Syntax
Operation
legal Form
Do.c.lptlon
TheAAM instructionconvertsthe resultofa sinSle-digitBCDmultiplication(a
value 0-81) in the AX registerto two unpacked BCD digits, the high"order digit in
AH and the low-order digit in AL.
Flag.
OFDF II TF SF zF A.F [F CT
Frult!
None,
Erlmplo
t'tov aL,4 I tlultlplaid
i40v AH,I : l4ulti pl l er
I.IIJL AH : A Xe 2 0 H ,3 2 d e c i m a l
: 4 H 6 3 , 4 1t s 2
0R AX. 3030H '32'
; C o n v e r tt o A S C I I
t5l
tHE 603t8 EOOK
AAS 8086/80186/802a6/80386
AscII Adiust Alter subtractloo (8)
Syntax
Opgratlon
l f ( A F | ( A La 0 F H )> 9 ) t h e n
ALe(AL_6)&oFH
Logal Form
Dorcrlptlon
This instructionensuresthat an ASCIIor BCDsubtractionresultsin a validBCD
digit. After executing a slJB or SBBinstruction that leavesa single BCD or ASCII
digit in registerAL, executeAASto produce a valid BCD result
If lhe value in Al producesa decimal boffow, the BCD diSit is forced into the Iegal
range (O-9) and AH is decremented.The high-ordef nibble is zeroed so that AL
containsonly lhe resulting single BcD di8it, and the AF and CF flags are set !o 1
If no boffow occurs,the AF and CFfiags are resetto 0.
Flag!
OT DT IA TF 3F ZF AI PF CF
? ?
Fault.
None.
Example
t40v AL,'5' 35H
SUB AL, '7' S u b t r a c t 3 7 Hy j e l d i n s o F E H
A L e 0 8 H ,c a r r y s e t i n d i c a t j n s " b o r r o { '
OR AL,3OH C o n v e r tr e s u l t b a c k t o A S C I I ' 8 '
152
g: lh.8ooa6/aoea7 h.tr@tlon Sel n.t.r.nc.
ADC 8086/80186/80286/80386
Addwtth carry (8/r6p/32)
Syntax
Operation
destedest+src+CF
Legal Form.
4eJt
ADC reg, ldata
Aoc nen, ldata
ADC reg, res
ADC reg,
AI)C reg
Dcacrlptlon
This instruction adds the contentsof the dest and s/c operands,incrementsthe
resuh by 1 if the carry flag is set, and storesthe result in thc location specilied by
dert The operands must be of the samesize.If the operands afe signedintegers,
the OF flag indicatesan invalid result. If the operands are unsigned,the CF flag in-
dicatesa carry out of the destination.
Flr9.
OFDF IB TF SI U AT PF CF
x
Faulig
PM RTT VEE6
12 #SS(0)
13 #CP(0) rNT 13 #GP(o)
14 #PF(e)
Erample
: S u b r o u t l n et o a d d t l l l o 6 4 _ b i t integers
tllTER 0, 0 ; Create stack frane
r10V E A X ,I E B P + 8 ] ; Get lo!-order of first valu
tiov E D X ,I E B P + I 2 l : Get high'order of first value
ADD E A X ,[ E g P + 1 6 ] : Add low-ofder bits, senerating carry
ADC E D X ,I E B P + 2 0 ] ; Add hish-order bits Bith previous carry
LEAVE : U n d os t a c k f r a n e
RET : Return tlith val'1e 1n EoX:EAX
l5:,
rHE 0306 SOOK
ADD 8086/aor86/802E6/EO3E6
Integer Addltlon (8/76p/32t
Syntax
Oporation
destedest+sr.
Lsgal Forms
dest sr"c
A00 reg. idata
A00 nen, idata
AoD reg, reg
A00 reg,
400 reg
Do!c.lptlon
This instruction adds the contentsof the /est and s/c operands and storesthe result
in the locationspecifiedby dArl.The operandsmustbe ofthe samesize.Ifthe
opefandsar signedintegers,the OF flag indicatesan in lid resuit.If the operands
are unsigned,the CF flag indicales a carry out of the destination, If the operands are
unpakdBCD digits,theAF flag indicatesa decimalcarry.
Fl.g!
OF DT IF TT SF ZF AF PF CF
Faults
RM I&R6
12 *SS(0)
13 *GP(o) rNT 13 #GP(o)
14 *PI(e)
Example
aDD AL, [4211A] ;8-bjt addition
400 AX, 34 r 1 6 - b i t i n n r ' e d i a t ev a l u e a d d i t i o n
400 ESI, IEBP+8] : 3 2 - b i t m e m o r ay d d i t i o n t o r e s i s t e r
154
ar ThG 40306/00367 lr.trucllon St nl.rerc6
AND 8086/80186/80286/Eo386
Boolean AND (8/r6p/32)
Synt.x
Oporatlon
deit6dest&src
C Fe 0
0 F e 0
Logal Formt
dest stc
AllD reg, idata
allD idata
Al{0 res, reg
ANo reg,
AND mem, feg
Deacrlptlon
on thelest ands/coPemnds
pelformsa bit-by-bxANDoperation
Thisinstruction
andstores TheANDoperation
theresultin thedestoperand, is defin!{lasfollowsl
0&0-0
0&1=0
1&0-0
1&1=1
Flag.
OF DF IF TT ST IrB AF PF CF
0
Faultr
Prl RM tAOa6
12 *SS(0)
13 *GP(o) rNT 13 #GP(o_)
14 #PF(ec)
Example
AND AL,oFH ; z e r oh i s h - o r d enr i b b l eo f A L
ANo EBx.EcX ; comPute EBxe EBx& Ecx
AllD 7FH: l{askoff hish-orderbit of nenorvoperand
BYTEPIRIEBP+6],
155
tHE AOSAA
BOOK
ARPL 80286,/80386
Adiust RPL Fteld of Selector (r6)
Syntax
Operation
i f ( d e s t . R P L< s r c . R P L )t h e n
dest,RPL e s r c .R P L
Z F e l
Z Fe 0
Logal Forma
dest src
ARPL reg, reg
ARPL reg
Dgrcrlptlon
Systemsoftware usesthis inslruction to modify a sel!'ctofs requestedprivilege level
(RPD field. Both the desl and s/c operands mus! be valid selectors.
Ii thc Rl,L of the ,rert operand is numerically lessrhan the RPLof the src, that is, if
the desl sclecior is morc privileSd,the dssl selector'sRPI is lowerd to match that
of the s,"c,and lhc ZF flag is setto 1.Ifthe /esi selectoris lessprivileged(numeri
cally higher) than the src, the ZF flag is cleared to 0, and the lert operand is not
modified.
operntjng systemroutines that are passedselctorsfrom applications shouid use
ARPLto ensurethar fie calling routine has not passeda selectorwith a higher privi-
lege than the npplicatiofl is allowed. Use the calling routine's CSregister as the s/c
Flags
OFDF IF TF SF A AF PF CF
156
ar lh. 0ota6/aGa7 hdl*tlon S'r Bdd'nc'
Faults
PM &t4 lw66
6 tNT6 #UDo
12 *SS(0)
13 #CP(o)
14 #PF(e)
Example
MoV A)(, IEBP+121 ; Get paraneteroff the stack
AR?L AX,[EBP+21 ; A d J u stto c 6 l l e f ' s R P L( p f e v l o u C
s P L )b v
i using CSof return addfess0n st!ck
,JNZ bad-param I granchlf caller passeda badselector
157
txE oo35Boox
BOUND 80186/80286/803E6
Check Array Boundades (r6p/jz\
Syntax
Operation
l f ( ( d e s t< s r c l 0 l ) I ( d e s t> s r c t l l ) ) t h e n
Illt 5
Logal Form
Derc?lptlon
This insruction comparesthe d?stoperand,which mustbe a registercontaininga
signed integer,with rwo values,a bwer bound sroredat the addressspecified by srq
and an upper bound stored in th following location. The bounds can be 16-bil or
32-bitvalues.
If the dai value is lessthan the lower bound or greater rhan the upper bound, an in-
terrupt5 occurs.The returnaddresspushedonrothe stackbyrhe exceprionis the
slartingaddressof the BOUND instlucrionrhatcausedthe inrerrupt,
Flaga
OTDF IF fi SF ZF AF PT CF
Fault3
PM R-n 'AO{t6
5 INT5 INT5 lNTj
6" iuDO rNT6 #uDo
12 $SS(0)
13 *GP(0) INT 13 *cP(0)
150
a: lh. AO36/aO3A7In.truction Srt Befercn@
Examplg
V C L I I I I T SI
DD 1, 20 ; B o u n d sf o r 2 0 - e l e m e n ta r r a y
V C D D 2 0 D U P( ? ) ; A r r a y s t o r a g ea r e a
r59
rHCeo306toox
BSF 80386
Btt Scar Forward (16p/32)
Syntax
Operation
1f (src- 0) the
ZFeI
Z F+ 0
lempe 0
| { h l l e ( b i t ( s f c , t e n p )- 0 )
tempFtemp+1
logal Forms
src
8SF reg, reg
BSF reg, nem
Do.crlptlon
This instruction scansthe s/c oprand and writes the bit position of rhe first I bir in
src to the destination register.If rhe src operand is 0, rhe ZF flag is ser to 1, and the
instructionendswith rhe der, registerinan undefinedstare.
If the src operand is not 0, each bit is e{amined, beginning with bit 0, unril a 1 bit is
found. The bit position of the first l bir (indeD is stored in the /?st register.
Flag!
OFDF IF TT SF ? AI PT CF
Fault.
PM RM IA86
12 #SS(0)
13 *CP(0) rNT 13 #CP(o)
r60
a! fl!. ooata/ooaAt h.rrstion a.r id@E.
Example
XoR ECX,ECX ; Indexinto sectornap
L1r BSF EAX,SECToRSIECX*41
; Scana dword
,lrlz GoT-0llE : Bfanchif any blts set
IllC ECI : Goon to next dltord
CHP Ecx, TABLE-SIzE : Donesearchjng?
,JL Ll ; l{0, scannext table entry
,JHP 110-SECT0RS ! l{o bits set in entlre table
GOT-OIE:
t6t
rHE 00086300K
BSR 80386
Blt Scan Rvise A6p/32)
Syntax
Operatlon
i f ( d e s t l n I A x , B X , C X , D X ,S L 0 t , B P , S P ] ) t h e n
staftbit e 15
staftbit e 3l
1f (src - 0) then
Z F e l
dest + ???
ZF6 0
tenp e startbit
llhlle (b1t(irc, tenp)- 0)
tenp F tenp 1
La99l Formi
d.<f .fi
Dogcrlptlon
This instruction scansrhe src operand in reverse,searchingfor a 1 bir beginning at
the high order of the src operand. If the s/c operand is 0, lhe ZF flag is set to 1, and
the instruction ends with rhe desrregister in an undefined srate.
If the srE operand is not 0, each bit is examined, beginning with rhe high-order bit
(either 15for word operands or 31for byte operands),until a I bit is found. The bit
position (index) of the first 1 bit is sroredin the dest register.
Flags
OFDF IF TF ST T AF PF CT
162
a! rh. 6036/00467 h.rt@tlon S.i h.td{@
Faultt
PM RM VEN6
12 *SS(0)
13 *cP(o) rNT 13 #GP(o)
Examplg
l{ov ECX,SElt_ttA)(-1; lndex of last entry in
' semaphore table
Llr 8sR i Scanfor non-zeroblts
EAX,SMAPH0REIECX*41
Jllz found-lt : Branch lf valld lndex
LOOP LI : Decfnent CX, loop back
i Get here
; lf entiretable is zefo
t63
TI{E AO:|a5 BOOK
BT 80386
Blt Test (r6p/32)
Syntax
Operation
C Fe B l T ( d e s l , i r d e x )
Legal Forms
BT reg, idata
BT men, idata
BT reg, reg
BT neri, reg
Dolc.lpilon
This instruclionteststhe bit spccificdby rhe operandsand placeslhe valueof the
bit into th(j carryflag.
Tl\e index opet^ndholds a bit index into the bit string specified by desr,which can
be a 16-bitor 32-bitregisreror a memorylocarion,The stateofthe bi! is copiedinto
lhe carryflag.
Ifthc mrlsn operandis an immdiatedatavalue,ir canrangefrom Othrough31.If
thc lr?der is held in a register,il can take on any integral value. Someassembler$
might lcl you specify immediate lrdarc valuesgrearerthan 31.If so, they modify rhe
effcttiv rddress by an appropriate value so that the lrdef can be scaledback to
betwccn0 and 31.
BT does not acccprbyte operandq so do not use it with memory-mappedI/O
devicesbecauscrhe instruction causeseither the 16-bitword or the 32-bit word con-
taining rhc sclcrted bit to be read.This could affecr more than onc I/O device regis-
ter You should use a single-byteMOV instrution to red the I/O regisier and then
test the contcnts of the register
Fla96
OF DF zn AI PT CF
164
a! th. 00006/00007rn.t{crrd !.t R.rmm.
Fault!
PM RM \ry)46
12 #SS(0)
13 #GP(o) rM13 #GP(o)
14 *PI(ec) #Pl(ec)
Examplo
llov EAX,192 i Bit lndex
BT SEI4APHoRES,
EAX I Test senaphorenumber 192
,lc sefLset ; Branchif th bit {as set
t65
l'|E ao3a6 BOOK
BTC 80386
Blt Test and Complemenl (16p/32)
Syntax
Operation
CFe 9ll(dest, index)
BIT\dest, lndex) -81\(dest, index)
Legal Foam6
dest index
8TC reS,
BTC
BTC reg,
BIC
Doacripiion
This instructioncopiesthe bit specificdby the opcrandsinto Cl then complements
the originalvalucofthe bit in the &J_,opcrand.
Thc infur operanclholclsa bit indcx into the bit slringspecifieclbydcrt,which can
bc n 16-bitor 32-bitrcgistcrora memorylocation.Thc statcofthc bir is copiedink)
thc cnrryflag, and the bir ofthc dert operandis complemented.
lf ll\e lnrler openlndis ^n immediateda|avaluc,it can rangefrom 0 rhrough31.If
the tt?denis held in a register,it can take on any inte!ruI value. Someassemblers
might le! yolr spccifyimmediateirder valuesgreatcrthan31.lf$o, theyrnodifythe
effective adclfcssby an appropriate value so that the lrdan can be scaledback to
between0 and 31.
BTC does not accept byte operands,so do not use it wirh memory-mappedI/O
devicesbecnusethc instuction causeseither the 16-bi1word or rhe 32-bir word con-
tainingthe selectedbitto be read.Tbis couldaffecrmorethanonc I/O d.,viceregis
rer. You should use a singlc-byle MOV insrruction to read rhe I/O rcgisrcr and rhen
lcst the contentsof the rcqistc.,
Flags
OF DF I CF
166
a: lh.6Ot06/AOi'a? hdncrld a.t R6t s@
Faulta
PA RM |ma6
12 iFs(0)
13 #GP(o) rNT13 *cP(o)
14 sPF(ec) #P(ec)
Exanrplo
I'loVZt EAX,8YTEPTRt01A2Hl; Radnenorybyte tnto 32-bjt regtster
8TC EAX,2 ; Test and complenent blt number
2
tlov t04A2Hl,AL , lrite nodlfled byte backto nenory
JC b lt s e t r Bronchlf the blt l{as st
167
tHE AO3a6 BOOK
BTR 80386
Blt Test and Reset (r6p/32')
Syntax
Opgi.tion
CF+ B\I(dest, lndex)
BIT(desr, trder) e 0
Logal Forms
dest lndex
BTR TEg, I data
BTR nen, I data
STR reS, fe9
8TR fe9
Do.crlptlon
This instructioncopisthe bit spccifiedby the operandsinlo CF,then clcarsthe
originalbit in deslro 0.
the lndex opera dholds a bit index into the bit string speified by /est, which can
bc a 16-bitor 32-bitregisteror a memorylocation,The stateof the bit is copiedinto
lhc cafty flag, and the bit of the lest operand is cleafed !o 0.
If the irder. opcrand is an immediate data value, it can range from 0 through 31.If
the lndex is hel<lin ^ rcgisler,it can be any integer Someassemble$might let lrcu
specify immediatc lrden valucsgrealer than 31.If so, they modify the effective ad-
dressby an Appropriatevalue so that the lr?r/er can be scaledback to between 0
^n<l31.
BTR does not acceptbyte operandq so do not use it with memory-mappedI/O
dcvicesbecausethe instruction cau6seither the 16-bitword or the 32-bit wofd con-
laining the selectedbit to be read.This could affect more than one I/O device regis-
ter You should use a single-byte MOV instruction to read the I/O fegister and then
test the contents of the register.
\0hcn usinga ttTR insrru(rionro imllemenra srenalinglunclion in a multiprocessor
environment, the LOCK instruction prefix shouldimmediately preedeany BTR in-
struction thar mdifies sharedm(]mory.
Flags
O F D F I F T F S F A
l6a
a! ih. 00396/90i[7 ltr.tlEtion s.t Rclcrcnc.
Faults
PM Rtr VAOA6
12 *SS(0)
13 *CP(o) rNT 13 #GP(o)
14 *PF(ec) "r'F(<)
Example
BTR I { Y - F L A G7, ; z e r o t h e h i 9 h _ o r d e r b i t o f b y t e r ' 4 YF L A G
JNC N o TS E T ; 8lt {as already reset
t59
tHE go3a6 BOOK
BTS 80386
Blt Test and set (16p/32)
Syntax
Oper.tion
CFe Bll(dest, index)
BlIldest, index) |
Logal Forms
dest index
B-fS res, idata
BTS men, idata
8TS reg, feg
BTS feg
Do.c.lptlon
This instructioncopiesthe specifiedbit into CF,then $etsthc originalbit in
'lhe
lndex opefan<ll1!.1<lsa bir index inio thc bi! string specified by d?st,which can
bc a l6Jlir or 32-hitregistcror a mcmorybc4tion.Thc stateoflhc bit is copicdinto
lhc carryflag, and the bil oilhe /esl opcrandis serlo 1.
Ifthe irlerc operandis an immediatedata lue,iI canrangefrom 0 through31.If
the irrrlexis held in a register,it canbe any integerSomeassemblers might let you
spc{ify immediate t/r/"r, valusgreater than 31.If so, they modify the effectivc ad-
dressby an appropriate valu so that the Irdre can be scaledback to betwecn 0
and 31.
BTSdocs not accept bytc opcrandsi so do not usc it with memory-mappcd I/O
dcviccs bc.:use the instruction causeseither the l6-bii word or the 32 bit word con-
lainingthc sclectedbi! to bc rcad.This col d aff&t morc thanone I/O deviceregis-
tcr. You should use a singlc-byrc MOV inslructbn to fcad the I/O register and then
tcsl thc contcnts of ih rEiisler.
When using a BTSinstruction tlr implement a semaphorefunction in a
multiprocessorenvironmenl, the I-OCKinstruction prefix should immediately
precede any BTSinstruction that modifies sharedmemory.
Flags
OFDF IF TF SF A AF PF CF
170
a: th aO3a6/aOiF7 In.lructlon S.r B.ts.nc.
Faults
PM R-tr taoa6
12 *SS(0)
13 *GP(o) rNT 13 +GP(0)
Exanple
8TS | ' 1 Y _ F L A 7G , ; Set the hish-order bit of byre |IY,FLAG
JC llASSET ; Blt {as already set
171
fl|E ao3a BOOX
CALL 8086/80186/aO286/80386
Far Procedure Call (32p/48)
Syntax
Oporatlon
push(cs)
push(EIP)
C 5 :E I P e d e s t
legal Form.
Dolcrlptlon
The far procedure call savesthe current code segmentselectotand the addressof
the next instruction(EIP)on rhe stack.Controlthentransfersto the desrination
specified by the operand. The operand can be an immedlate selctor:offsetvalue or
the addressofa 48-bi(FARpoinrerin memory.
The selectorcan point to another code segment,a call gate, a task gate,or a task
state segment.If the seleclorpoints to a gareor TSS,the offset portion of the CALL
is ignored. If the selectorpoints to a code segment,conrrol transfersto the specified
offsetwithin thatsegmenr.
All flags are affected by a task switch.
Flagr
OFDT IF TF SF A AF PF CF
Faulta
PM RU V8IR6
10 #TS(0)
10 #Ts(sel) #TS(sel)
11 +N?(sel) #NP(seD
12 *SS(0)
12 #ss(ss)
13 +GP(o) INT 13 #cP(0)
+GP(CS) INT 13 iGP(o)
172
a: th.4o366/ao3a7 herruclior set Rel.renc.
Example
CALL t6A3:0000 ; 0irect call
CALL F l l 0 R DP T Rl 0 0 5 A H l : indi fect call
173
tHE 0346 BOOX
CALL 8086/80186/80286/80386
Near Procedure Call (16p/32)
Syntax
Ope.atlon
push(EIP)
Logal Forns
Do3crlptlon
This instructionpushesthe addressof thc nxt instruction(EIP)ontothe stack.The
instructionpointeris then setto the valuespccifidby the operand.
Ifthe operandis an immediatevalue,the new instructionpointeris relativeto the
currentposition.Ifthe oprandis a memoryaddressor a reltister,the subroutine
addressis takenindirectlyfrom the operand.
Flag!
OFDF IT TI SF A AF PF CF
F.ultr
PM RM V6O66
12 #S(0)
13 #GPio) INT 13 *cP(o)
Exarnplo
CALL SQRT ; Cal] dj rect
LEA E B X ,F l l T A B L E ; G e t p o j n t e f t o a d d r e s st a b t e
I'lov EAX,3 ; Selectthird function
CALL [EBX+EAX*4] ; Call it
174
ar th. lo36/Oa0t In.truction 3.t not.H6
cBw 8086/80186/E0286/80386
Convent B:nteto Word (8)
Syntax
cB14
Operatlon
i f B I T ( A L ,7 ) t h e n
Lggal Form
cBI
Dolcrlptlon
This instructionsign-extends
the bytein Al to AX.
Flagt
OT DF IT TI SF ZF A.I PF CiF
Faulta
None,
Exarf|plo
llov AL, TIIIY : Reada byte lnto AL
CBI{ I Convertto l6-bit signedlnteger
ADD BX, AX
t75
rHE AOa6aaOOX
cDo 80386
convert Doubleword to Quadword (32)
Syntar
CDQ
Oporatlon
1 f ( 8 1 T ( E A X , 3 -1 )1 ) t h e n
E D Xe 0 F F F F F F F F H
EDXe0
endif
Logal Fortfl
c0Q
Dolorlptlon
This instruction sign-ntendsthe 32-bit EAX register to a &-blt dword. Ir ls most
frequently used before the inteSerdivide instruction, which operateson a 64-bit
dMdend,
Fl!g!
OF DF II TF ST ZA AI PF CA
F ult.
None,
Exqmplg
I'loV EAx, I400Hl I Copydividendto EAx
C00 ; Extendto 64 bjts
IDM|0RD PIR t20Hl ; Dlvide
176
A! th. aoita6l8o:|a7 lFtruction 5.t n.f]dc.
cLc 8086/80186/80286/80386
Clear Caffy Flag ()
Syntax
cLc
Oporatlon
C F e 0
Logal Form
cLc
Deccrlptlon
This imtruction clearsthe caffy flag in the EFIAGSregister to 0.
Flatr
OF DT IF TF SF A A.A PF CF
0
Faulta
None.
Ex.nplo
NO-ERROR:
cLc ; C l e a rc a r r y
RET ; Returnfrom subroutinel{jth success
; i n d j c a t e db y C F
177
fltE ao46 300K
CLD 8086/80186/80286/80386
Clear Dhectlon Flag ()
Syntar
cL0
Oporatlon
0 F e 0
Logal Forrn
CLD
Delcrlptlon
This instructloncleaBthe dirctionflag in the EFLAGS
regisrerto 0. WhenDFis 0,
anystringinstnrctionsincrementth indexregisters(ESIor EDI).
Fllgr
OF DF If TF SF ZF AT PF CF
0
Fault!
None,
Examplo
llov ECX,STR-LEIi I Strlng novecount
CLD I C l e a rd i r e c t j o nf l a g
REPIIoVSB ; Copylhe stfing
t?8
Ar th. AO3A6/8Oa8t IndrEilon a.t R.i.he
cLl a086/80$6/80286/80386
Clear Interupt Flag ()
Syntar
CLI
Opergtion
IF e 0
Log.l Form
CLI
D9!crlptlon
This instnrction clearsthe interrupt bit in rhe EFLAGSfegister to 0, disabltng hard-
ware inteffupts (eJrceptNMI). The procedure s.ecuringthe CLI instruction musr be
of equal or higher privilege than the cuffent IOPL, that is, CPL< IOPL, or a general
Protection fault occurs,
Flrgr
OTDF IF TF SF Z A? PF CI
0
Faulta
PM RM IM86
13 *CP(o) #cP(0)
Ex!mplo
CLI I D l s a b l el n t e r r u p t s
t'10V
AL, SEI1APH0RE r cet menorJvalue
DECAL i oecrenentcounter
JZ done ; Skip if value l{as 0
l { o v 5 E | { A P H o RAEL, : Update
D0litl
SII : E n a b l ei n t e r r u p t
174
tHE ao3a6BOOX
CLTS 80286/80386
Clear Task Switchd Btt ()
Syntax
CLTS
Operation
8 I T ( C R 0 3, ) e 0
Legal Form
CLTS
D.!crlptlon
This instruction clearsthe task switched (TS) bit in the CRoregbter to 0. The TS bit
allows the 80386to efficiently managethe 80387 t*/henever a task switch occuls on
the 80386,the CPUsersrhe TS bit to 1. If the TS bit is I when a coprocessorescape
(ESC)or \VAIT instruction executes,a coprocessornot availablefault (int 7) occurs,
The fault handler can clear the TS bit, savethe NDP state,load the NDP statefor the
cuftent task, and retum to the insruction that faulted, Switchlng between tasksthat
do not use the 80387will not causethe fault, and you can avoid the overheadof sav-
ing and restoring lhe NDP srate.
Only procedures running at a cPL of 0 c4n execute CITS without causinga general
Flrgt
OT DF IF TF SF ZF AT PF CF
F.ult.
RM
13 *GP(o) #cPt0)
Exa|nple
CLTS C l e a r t a s k s w i t c h e db i t
C A L LS | A P N D PS T A T E S a v e / r e s t o f e m a t h c o p r o c e s s o rs t a t e
lao
a! nr. O316/aOOa?h.tiuctid 3.t tlLmc.
CMG 8086/80186/80286/80386
Complement the Caffy Flag o
Syntax
cl'tc
Oporatlon
C F - C F
Legal Form
cl'1c
Dglcrlptlon
Thecarrybit of the EFLAGS registeris complemenrdi thatis, if the iditial valueof
thecarryblt is 0, ir is set!o 1.Ifthe initial lueis l, rheflagis cleared ro0 asa
resultof the iNtrlrction.
Flagr
OF DF IF TF SI ZI A.A PF CF
F ult!
None.
Examplg
8T lAX, I , T e s t . b j t , s a v e1 n C F
JC EXIT ; B i t N r s s t - - w e , r ed o n e
J P TRY_AGAI ; t{ot feady yet
tXITI
CtlC i Return, CF clear
RET
lal
tHE aoita6 BooK
CMP 8086/80186/80286/80386
Colnpare Integers (846P/32)
Syntax
Operation
Logal Forms
desl
cl,P feg, r-data
cllP nen, r-data
CMP feg, reg
CMP feg,
CflP res
D..c.iption
This instrucrion subtraclsthe contents of op2 from op, and discardsthe resuit. Only
thc EFLACSregisteris affected.The followingtableillustrateshow the flagsare set
basdon fie operanclvalues.
Flags
OF DI IT TF SF ZF A.F PF CF
Faults
PM ruI 'ts0A6
12 #SS(0.)
13 *CP(0) rNT 13 +CP(o)
1a2
ar lhe 0346/4036? h.rrs otr A.r B.td.nc.
Examples
CllP AL, [42114] ;8-bit compare
CliP AX, tBX+31 | 15-bit feal/virtual node
CllP C X , t E B P + 8 1 [ E A X * 2 ;] 1 6 - b i t p r o t e c t e d n o d e
CriP ESL T ; 3 2 - b l t c o n p a r ew i t h s i g n - e x t e n d eodp e r a n d
ta3
tHE aoiF6 BOOX
CMPS 8086/80186/80286/80386
Compare Striflg (a46p/32)
Syntax
c PS
Operation
{ h e n o p c o d ei s ( C r i P S BC
, l l P S UC
, P S D )s e t o p s j z e l l , 2, 4)
N U L Le D S T I E S I I E S I: E 0 1 ]
i f ( D F- 0 ) t h e n
ESIeESI+opsize
EDIeEDI+opsize
Logal Forrn3
CI{PSB ; Compare
strlng byte
Cl{PSl,l ; Compare
strins wofd
Cl.iPSD ; Conpare
strlng doubleword
Do.crlptlon
This instructionsubtractsthe memoryoperandpoi ed to by DS:ESIfrom the
operandat ES:EDIand dis.ads the result,asin the CMPlnstruction.The sizeof
the operand is either a byte, word, or doubleword, depending on the opcode used.
The flagsare setasthe comparisondictates,and the contentsofESI and EDI are
modified, either incrementedby the size of the operand, or decremented,depend-
ing on the setting of the DF bit in the EFIAGSregister ESIand EDI are incremented
whcn DF = 0.
You can precede the CMPSinsrruction with either the REPEor REPNEprefix to re-
peatdly compare operandswhile the ZF bit remains I (REPE)or 0 (REPNE).Regis-
ter ECXholdsthe maximumcomparecount.
You can also apply a segmentoverride prefix to the CMPSinstruction to override
the DS seg.mentof lhe DS:IESIIoperand. You cannot ovenide the ESsegment
assumptionfor the EDI operand.
Fl.ge
OF DF IF TF SF ZF AF PF CF
144
Ar th. aO:|aA/6Olt6? lndrFtlon S.t i.tr.m.
Fault!
PM RM IE]A6
12 *S(0)
13 #CP(o) rNT 13 #cP(o)
14 #PF(ec) #pr(ec)
Ex.mple
LEA E S I ,s t a n d a r d ; D S : E SpIo i n t st o d e f a u t t
LES lDL [EBP+12] ; ESTEDI loadedfrom stack frme
t0V ECX,31 i C o u n it s a c o n s t a n t
CLD : Ensuredlrection flag set coarectly
R E P EC I I P S B ; Compare byte stfi ng
Jl{E not-eq ; Branch l f s t r l n g sn o t e q u a l
ta5
fltE ao3a6aoor
cwD 80E6/E0185/80286/80386
CjonYert Word to Doubleword (16)
Syntax
Cl'lD
Opsration
, 5- 1 ) ) t h e n
if (BlT(Ax1
DXe oFFFFH
D X e 0
Lggal Forfi
cl,{D
Deacription
This insiruclionsign-extends the word inAXtothe DXrAXregisterpair.The
prefeded 16-bit to 32-bii conversioninstruclion is C\flDE, C\flD is usd by the 8086
and 80286,which do not have 32-bit registers.
Flag!
OF DF IT TF SF A A.F PF CF
Faultg
None.
Examplo
1.10V A X ,d i v i s o r ; G e tl 6 _ b i t d i v i s o l
Cl,{D ; txtend to DX|AX
DMX : l 6 - b J ! d l v l s io n
taa
ar Th. ao3a66o3a7 harructior 5.t notdencc
CWDE 40346
convert word to Doubleword trxtended (16)
Syntax
Cl,l0E
Operation
i f ( B I T ( E A Xl 5, ) - 1 ) t h e n
E A Xe E A xl F F F F 0 0 0 0 H
EAXeEAX&0000FFF|-H
Legal Form
C!IDE
Deacrlptlon
Thisinstruction thc16-bi!valueinAXto a full32 bitsin theEAX
sign-xtcnds
register,
Flagl
OF DF IF TF SF ZIF AF P8 CF
F.ult.
None.
Erampls
tt0v A X ,s h o r t - j n t : G e t1 6 - b i t s i s n e dv a l u e
NfG AX. : Convrtto nesativenumber
CNoE t R e t u r n3 2 _ b i tf e s u l t
147
fltE ao:F6 BOOX
DAA 8086/80186/80286/80386
Dectfral Adiust AL Aftef Addttlon (8)
Synt.x
Operation
i f ( A F | ( A L & 0 F H )> 9 ) t h e n
i f ( C F ( A L ) 9 F N ) )t h e n
ALeAL+60H
C F e l
C F e 0
Logll Form
Delcrlptlon
This instructionensuresthatAL containsa vallddecimalresulrafreran addirionof
two packed BCD values.
Flegr
OF DF IF TF ST ZF AF PB CF
x
Faultt
None,
Example
0Y AL,72H ; 7 2 i n p a c k e dd e c l n a l
AOD AL, 19I] ; Y i e l d s 8 B Hi n A L
; Adjusts AL to 9IH
laa
a: th. eo35/aooa7h3ttuctid S.t hcl.rc.e
DAS 8086/80186/80286/80386
Declrnal Adlust AL After Subtmctlon (8)
Syntax
0As
Operatlon
i f ' ( A F | ( ( A L & 0 r - H ) )> 9 ) t h e n
i f ( C F | ( A L ) 9 F N ) )t h e n
ALeAL-60H
C F e l
Logll Form
DAS
Dolcrlptlon
This instructionensuresthatAI containsa validdecimalrsultaftera subtractionof
two packedBCDvalus.
Flagr
OF DT IF TF SF 2tr AT PF CT
F.ult3
None.
Exarnple
HOY AL, 42H ; 4 2 i n p a c k e dd e c i m a l
SUB AL, 13H ; Y i e l d s 2 F t l1 n A L
DAS ; A d J u s t sA L t o Z 9 H
ra0
rHEtdtS6 toox
DEC 8086/80186/80286/80386
Dcrement G/r6P/32)
Syntax
DECopl
Opor.tion
Lcg.l Form!
0p1
DEC re{t
DEC men
Do!crlptlon
This instruction subtractsrhe value 1 from opt DECis frequently used to decrement
indexes arld rhereforedoes not affect the cafiy flag (CF).In other respects,ir is
equi lerit to the instrirction:
su8 opl, 1
Fhlr
OF DF IT TI 3FU AI PF CF
x
F ulta
PLt nM V8lXt6
12 #S{0) #ss(o)
13 #GP(0) INT13 ircP(o)
14 #PF(ec)
Examplo
DEC ESI ; Decrement
contentsof ESI
190
ar lho aoiF6/4o307 h.rlGtion s.t i.feenc.
Dtv 8086/e0186/80286/80386
Ursigned Dfu'tston (8/16p/32)
Syntax
Dll opl
Opgi.tion
loNlacc)+acc/apj
h j g h ( a c . ) e a c c n o d u l oo p l
Logal Formg
DiV feS
DIV
De.crlptlon
This instructiondividesthe valuein the accumulak)r rgisteror registerpair by op1,
storing the quotient in the low-order portion of the accumulalorand the reflraindcr
in th high-orderportion.The followingtablc illustratesthe registersused4sac-
cumulators,depending on the size of op.l,
Flage
OT DF IF TF SF ZT AF PF CF
Faults
PL' RM VN86
O INTO INTO INTO
12 #SS(0)
13 #GP(o) rNT 13 +GP(o)
t9t
tltE 004a6 Eoox
Example
rlov EAx,di vidend
CI,{DE ; C o n v e r t 3 2 , b j t o p e r a n dt o 6 4 b r ' t s
oMBX ; 32-bit divroe
r40V quotient, EAX : Saveresulr
MoY renainder, EoX
192
ar rh. ao3a5/603a7herruction scl B.ld.nc.
ENTER 80186/80286/80386
B ter Neq'Stack Frame o
Syntax
E N T E lRo c d l r , r e s t i r g
Ope.ation
, e s t t r g e n a x ( n e s t i n g ,3 l )
p u s h( E B P )
I E M P1 - E S P
if (restirg > 0) then
nestlngenesting-l
,hlle (resting > 0)
E B P E B P 4
p ! s h ( s s : t E B P)I
nestingenesting'I
E B Pe t e n p
E S P e E S P -l o c d l s
Logal Fotms
locals nestlng
ENTER idata, ldata
De3cripiion
Theform
setsup theslackframcusedby highlevellmguages.
Thisinstf.rction
ENTER n,0is equivalenl to lhe instrr.rctions:
PUSH EBP
I4OVEBP,ESP
S U BE S P n
,
This savesthe prcvious frame pointer (EBP),seISlhe framc io the cunent stack lop
(ESP),and al krales spacefor local variables Parameterspassedb the procedurc
a.e addressedas positive offsels from EBII and local variablesare acldressedas
negativeoffsets from EBP.
vhen the secondoperand is greater than 0 (which happensonly in languagesthat
allow nesting of procedure definitions), the pointers to previous stack framesare
pushed otuo the stack to allow addressingof stack-residentvariableswhose s.opes
are outside the curent stack frame.
193
rr{E 8036tooK
Fla93
OFDI IF IT SF 3 AT PF CF
Faultr
Pltt RIt tw6
12 *SS(0)
14 #PF(ec) "PF(ec)
Ex.rnple
EIITER 4, 0 ; Create stlck frafie {jth
: spacefor a d*ord local
t9a
A: rhe 00O06/gOiF7 lNrruGrion S.t tLlcme
HLT 8086/80186/80286/80386
Halt o
Syht.x
HLT
Logal Form
HLT
Degcrlptlon
This instruction stopsall fu(her processingon the 80386.No other instructions will
executeuntil the processoris reset or an interrupt occufs. An NMI interrupt always
brings the processorout of the halt state.The IF flag must be 1 for any other hard-
ware interrupt to be acknowledged.After processingthe interupt, execution con-
tinues with the instnrction immediately following HllT.
You must execute at a CPLof 0 to issuea HLT instructioni otherwise, a genelal pro-
tection fault occurs,
Flag!
OFDT F TF SF ZF AF PF CF
F!ultr
PM &M V8IR6
13 *GP(o) *cP(0)
Examplo
sTt
LI I HLT : I d l e , p r o c e s s i n go n l y i f t e r r u p t s
,Jl'lP 11
195
rHE ao3a6 BOOK
tDtv 8086/80186/80286/80386
lnteger (Stgned) Division (ah6p/32)
Syntar
Oporalion
los(acc)eacc/apj
h i q h ( r . . ) F a c c n o d u l oo p 1
Log.l Forma
I 0 lV reg
Desctiption
Thisinstructiondividesthevaluein theaccumulator rcgistcror rcgislcrpairbyopl,
$i(xingthequotienxin thelow-order portionoftlle accumlrlatoranclthercmainclcr
portion.Thefollowingtable
in lhc high-(xdcr illustratcs
lhercgistcrs uscdrs ac-
cumulalffs,depeoding on dresizeof opl.
Flags
OT DF IF TF SF ZF AF PT CF
?
Faults
RM VaOa6
O INTO INT O IN'I'O
12 'SS(0)
13 "CP(o) rNT 13 +GP(o)
196
a3 the A03A6/6030? In.ltucrlm 3t R.lc6ncc
Examplo
Mov E A X ,I E S P + 1 4 ] : Get dividend
CDo : Convertto 64 bits
1DIV ECX
197
tl|E ao3a6 Eoox
IMUL 8086/80186/80286/80386
Integer (Sigred) Multiplicatton <a/ftp/32)
Syntax
l t 4 U Lo p 1 , l o p ? , L o p j l l
Opgration
dest e mu'ltipllef* multlplicand
Loggl Forms
opl opz op3
I UL res : acce acc * reg
IHUL mem i acce acc * nen
IHUL res, res i opl opl * op2
IiIUL reg, nem I opl e opl . op2
IIUL feg, idata I opl <- opl * opz
IIIUL feg, reg, Jdata i op.l<- op2 + ap3
I UL reg. men, idata I opl op2 * ap3
Dercriptlon
This instructionmultipliessigned,two's complemcnlintcgcrs.Thc flagsare leli in
an unknownstatcc'xceptfor OF and CF,which areclearedto 0 if thc rcsultofthc
multiplicatbnis the samesize(byte, wod, or dworcl)asthe mulriplicand.
In the singleoperandform of&c instruction,the resuhis placedinAX if opl is a
byte, DX$X if op] is a word, and EDX:EAX if op-lis a dword.
In the formsofIMUI lhat use2 or 3 operands,the operandsmustall bc the same
Flag.
OT DT IF TF ST ZF AF PT CF
Faults
PM B.LI 'EA6
12 *SS(0)
13 *GP(o) tN r'13 *GP(o)
Example
IIIUL ECX : E D X : E AeX E A X* E C x
IIIUL AL,CH,7 : A L - C H * 7
l9a
a: th. ao3aoEosaTh.l.uctlotr 5.r Bdr.nc.
IN 8086/80186/802E6/80386
Input from I/O Port (a/ftP/32)
Synlax
Op6ration
Lagal Forrna
Ill
Ili DX
Deccrlptlon
This instruction readsa byte, word, or dword into the specified accumulatorfrom
the designatedI/O port. If rcu use an immediate data value in the instructioni
'ou can addressonly the first 256ports. If the port is specified in the DX register,
you can accessany of the 65536ports.
IN is a privileged instruction, A procedure that attempts!o eraecutean input instruc-
tion must satisfy one of two conditions to avoid a general protection fault.
If the procedure that executesan IN inslruction hasI/o privilege (that is, if its CPL
is numerically lessthan or equal to th IOPI fild in the EFLAGSregister), the input
instruction executsimmediately.
If the procedure does not have I/O privilege, the I/O permission bitmap for the cur-
rent task is checked.lf the bit(s) coffesponding to the I/O port(s) is cleared lo 0, the
input instruction executes.If rhe bit(s) is set to 1, or the pon(s) is outside the range
of the bitmap, a generalprotection fault occu$, Sechapter 5 for more details on
this feature.
If the IN instruction is encounteredwhile in V86 mode, only the I/O permission bit-
map is tested.The IOPL value is not a factor in validating accssto the port.
Fl.gr
OFDFITTFSTA
Faults
13 #GP(o) #cP(o)
199
Exar|iple
IN AX,72N : I n P u ta 1 6 b i t v a l u e
; f r o n P o r t s 7 2 H a r t d1 3 1
l,lov DX, crt port
iN AL, DX ; I r p u t a b Y t e v a lu e
200
4 th. AO:|a5/aOaa?hdrFtlotr l.t R.td.nc.
tNc 8086/80186/EO2E6/80386
(an6p/32')
Syntax
INC op1
Operation
Legal Form!
IliC reg
I lic
Doac.lptlon
This instruction adds the value 1 to opl. This insrructlon is ofren used to increment
indexes and therefore does not affect the carry flag (CF).In orher respects,it is
equi lentto the instruction:
ADD opl, 1
Flag!
OF DT IT TF ST ZF AI PF CF
Fgultt
PDT RM IWI'6
12 *SS(0)
13 #GP(o) rNT 13 #GP(o)
14 #PI(c) #Pl(ec)
Eramplg
IllC ESI ; Increnent contents of ESI
m1
THE A03A6 BOOK
tNs EOra6/a0286/80Ja6
lnput Strtng from I/O Port (a/$p/3zl
Syntax
INS
Opgration
w h e no p c o d jes ( l N S B ,I N S l i ,I N S 0 ) ,s e t o p s i z e ( r , 2 , 4 )
E S : I E 0 I ]e p o r t (D X )
lf (0F- 0) then
EDIeEDI+opslze
E D IF E o l o p s iz e
Legal Forns
IrtlsB i Inputstring byte
lllsl,l r Inputstring word
II1SD : Inplt strjng double*ord
Do3cription
This instruction4llowsthe locationspecifiedby IiS:lEDIltoreceivcdatainput fiom
the I/O porr containcdin the DX register.An 8-bit operation(INSII)acljusts
thc ad-
dressin EDI by 1,a 16-bitoperation(INS\q) adjustsEDI by 2, ,lnd a 32-bitoperxtion
ONSD)adjustsEDI by 4. l he memoryoffsctin tlDI is incrementdiflhe D!'bit is 0
or is dccfcmcntedif DF is 1.
Like$e lN instructbn,thc INSinstructionis privilcgcd.The executingProccdure
mus! have a CPLequal to or nLrmcricallylessthan the IOPL, or accessto thc port
specifiedin DX mustbc granredby the I/O permissionbit11lpin theTSS.
You c1nusethe REPprefixwith the INS inslruction.Usingtheprcfix causesregis-
ter ECXto be interpreled as an instruction count.
A segmentoveffideprefix doesnot affectthe INS inslruction.The destinalionseg-
ment is alwaysES.
Flags
OF DF IF TF ST ZF AF PF CF
202
a: the aooa6/aot0? ltr.tructlon 5.i i.Ltdc.
Faulta
PM RM 'W86
13 #GP(0) INT 13 *CP(o)
14 #PF(e) #PF(e)
Exaniple
LEA EDL nelll-va'l ; Set up destination pointer
I'loV DX, 370H ; Set uP Port address
CLD
I llsD ; Input 32-blt value to ner-val
I IiSD ; Input value to ne[-va] + 4
20it
flfi ao5a5 BooK
INT 8086/80186/80286/80386
Software Interupt ()
Synt.x
IllT vecto.
Oporation
p u s h (E F L A G S )
push(cs)
p u s hE ( IP )
T F e 0
1 f ( I 0 T ( v e c t o r ) . T Y P E- I I I T E R R I J P T _ G A
t hI e
En)
IFe 0
C S : E I Pe d e s t ln a t l o n (I D l ( y e c t o r ) )
Legal Fonn
IllT idata
Dclcrlptlon
This instruction savesthe current flags and execution location on the stack,and the
,vclol operand indicatesthe Im eftry that is selected.The gate from rhe IDT de-
termines the new execution location,
If the processorencountersthe INT instrrrcrion while in V86 mode, the 803M
switchesto the dng 0 stack (SS0|ESPo) taken from the V86 task state segmenrbefore
processingthe inteffupt. Bcausethe processoris running ln rlng 0, the IDT entry
must have a DPL of 0i otherwise, a generalprotection fault occurs,
The INT 3 instruction is usually encodedas a single byte (oCCH) and used as a
breakpoint instnrction for debuggers.
Flag!
OFDT IF TT SF A AI PF CF
x 0
m4
0: lh6 ao3a6/ao3a7h.truction sot nelGtonco
Faults
PM Rtt twa6
10 *Ts{sel)
rI #NP(sel)
12 #SS(0)
13 #GP(o) rNT 13 *GP(0)
Example
INT 42 ; M a k ea s y s t e n ' d e p e r d e 0
nSt call
205
fltE 6036 BOOK
INTO 8086/80186/80286/80386
Interrupt on Overflow o
Stmtax
IIlTO
Operation
if (0F) then
IIlT 4
Logal Form
INTO
DoEc ptlon
This instruction executesan INT 4 instruction if the overflow bit (OF) in the
EFLAGSregisteris 1. Seethe INT instructionfor furtherdetails.
Flags
OF DF IF 1T SF ZF AF PF CF
0
Fault!
PM RM IA0{}6
10 *Ts(sel)
11 #NP(sel)
12 $SS(0)
13 #Cl (0) INT 13 jFGP(0)
14 #PF(ec)
E .mple
ADD ECX,VECToRIEDI*41
i Arithnetlc operatlon
IllT0 : C h e c kf o r o v e r f l o N
206
5: th. 0Oa6/Oo367 h.t ucrlo S.t R.l.6n6
IRET 8086/80186/80286/80386
Inteffupt Return ()
Syhtax
IRET
Operation
if 0,lT- I) then
lask-return (TSS.
back_]lnk)
p o p( E r P )
p o p( c s )
pop ( EFLAGS )
Legal Fotm
IRET
Doacrlptlon
Thisinstruction a rerurnfromaninterruptor,if theNT(nested
signals task)biris
set to 1,a task switch from the current task to the one that invoked it,
Vhen the new value of EFLAGSis popped from rhe stackj the IOPL btts are mod!
fied only if the CPLis 0.
Chapter5 dlscussestransitions acrossprotection rings 4nd task switching,
If the IRET instructlon executeswhile the processoris ln V86 mode, a general pro-
tection fault occurs.It is the responsibiltryof rhe fault handler !o emulate the real-
mode IRET for the V86 rask.
Flag.
OFDFIT fi SFU AX PF CF
x
Faultg
PM RM '&E6
11
12 #ss(o)
13 #GP(o) rNT13 +GF(o)
14 *PF(<) +PF(<)
Exarlipla
I RET
m7
fl|E aoa6looK
Jcc 8086/80186/80286/80386
Jlrfl.p lI Cot dltlofi
()
Syntar
Operation
E I P E I P+ s i g r e x t e n d ( o f f s e t )
204
a: lh. 80346/A03A7 h.lru.tid S.t R.ld.nc.
Descrlption
TheJcc instructions te$ rhe conditions described for each mnemonic. If rhe condi
tion holds true, the processorbranchesto the specified location. If the condition is
false, execution continues with the instruction following the junp.
More than one mnemonic exists for the samecondition. This lets you writc rhe resl
in a manner most appropriatefof the condition. For example,after OR EAX, EAX
you $/ould useJZ, and after CMPEAX,ESIlou would useJE; both mnemonicstest
lor ZF = 1.
Flag.
OF DF IF TF SF ZT AF PF CF
Faults
Plt' RIt \eA6
13 #GP(o)
Examplo
DEC AL : D e c r e n e n tA L
,JZ rached_zero : Bfanchif zero
209
tHE eo366 BOOX
JMP 8086/80186/80286/80386
NearJump ()
Syntax
Opor.tlon
ElP e dst
Logal Formr
Delcrlptlon
This instruction loads a new value into the instnrction pointer (EIP). Subsequentin-
structions are fetched beginning at the new location
'$ifheny)u
use the immediate form of the instruction, the data lue is an offset
from the current EIP The other forms are indirect bmnches,that is, ihe new lue
of EIP is taken from the operand register or memory location.
Flalr
OF DF IF TF SI ZF AF PF CF
F9ult.
PM NlI WR6
12 *SS(0)
13 *CP(o) rNT 13 *GP(o)
14 #PF(ec) #pr(ec)
Eramplo
Jl'lP nell-label 0 i r e c t . r e l a t i v b r a n c h
,lilP ECX
.JHP D I I O RP
DT RI E B P + l 2 ] Branchto ioutine shose
a d d r e s iss o n s t a c k
2to
& lhc 0{Fa6EO3aZ h.tr*tlotr a.t B.l4.nc.
JiIP 8086/80186/80286/80386
FarJump ()
Syntax
Opgration
C S : E l Pe d e s t
Logal Form3
,lllP ldata : C S T E I eP d a t a
JtlP : C S T E I Pe I m e m l
De3crlptlon
A far jump instruction modifies both CSand EIP, In the immediate form of the in-
struction, a rlew 48-bit poinrer is specified. In th indirect form, rhe mem operand
points to a 48-bit selecloroffser poinrer.
The new CSselectorcan be a code segmentselecoor(where the branch is to the
specifled offset within rhe code segment),or the selectorcan be a call gate,task
gate, or task state segment.In this case,the offser portion of theJMP is ignored, and
the new lue of EIP is taken from the gate or the incoming TSS,If the iump causes
a task srwitch,all flags are subjectto changeas EFLACSreloadsfrom the new rask,s
TSS.Chapter5 discussesthe task switch operation and rhe use of gates.
Flag!
OPDF II TF SF za AT PF CF
Faultt
4M VNtt6
10 #'Is(sel)
11 #NP(sel)
12 *Sqo)
13 *GP(o) rNT 13 *cP(o)
14 #PF(ec)
Eramplo
JllP 21A7:0002IIF3H : 0i fect branch
JliP Fl'loRD
P T Rn e { t a s k : S r a n c hl n d l r e c t
217
tltE 00.t04 Boox
LAHF 8086/80186/802E6/80386
Irad AH wtth Flags (8)
Syntax
Opgretlon
AHFEFLAGS&oFFN
L9991Forrr
Dolcrlptlon
This instruction copies the low-order byte of the EFIAGSregister into AH. After the
instruction executes,the AH register has the follo!r'ing contents:
Fl!9r
OF DF IF ZF AI PP
Frult!
None.
Examplo
SHR AH,6
AtIO AH,1 i AHnolrcontalrs !he zF flag
212
A! the 8O366/AOOa?h.iructlm 5.t i.frdc.
LAR 80286/80386
I-oad Access Rtghts (rcp/32)
Syntax
Oporatlon
l f ( c h e c k , a c c e s s ( s e l e c tt )h)e n
Z F L
d e s t e a c c e s s - f lg h t s( d e s c r ip t o r ( s e l e c t ) ) & O O F ? F F O O H
Z F e 0
endif
Logal Forma
dest select
LAR res, res
LAR reg,
Dolc.lpilon
This instruction allows a program to determine whether a givn selectoris acces-
sible to it without causinga protection fault.
If the selecloperand containsa !"llid 80386selectorthat is accessibleto the execur-
ing procedure and the selectorr'?e is one defined below, the zero flag (ZF) is set to
1,and the accessrights field of the descriplor indicatd by the selectoris loaded inlo
the destination register
If the destination register is a 15-bit register,the high-order 8 bits of the regisrer
contain the accessrights fild of the descriptor
TYPB
If the destination is a 32-bit rgister,bits 8-15 contain the accessrights, and birs 20-
23 contain the accessextension bits found in bvte 6 of the descrioror.
31 23 20 16 15 8 7 0
213
rHEaoaaaBooK
Flags
OFDF IF TF SF T AF PF CF
F.ults
Ptt nI tex6
INT6 #UDO
12 #SS(0)
i3 #cP(o) rNT 13 *GP(o)
Exrmplg
i Verlfy that varlableX contalrsthe selectorof a call gate
i t h a ! c a n b e l e g a l l y i n v o k e db y t h e e x e c u t l n gr o u t i n e .
LAR AX, X ; L o a da c c e s sr l g h t s
JllZ no-access i g r a n c h1 f c a n ' t a c c s s
SHR AX, I : llove accessrights to lol1 ofder
Al,lD AX, IFH : S a v eo f l y S b i t a n d T Y P E
CllP AX, oCH : Test for 385 ca'll gate
,Jt ls,gate ; E r a n c hi f a c c e s s l b l e g a t e
214
a3 th. aoa6/803at Inlrructiotr l.t i.lr.nc.
LEA 8086/80186/80286/80386
Irad Effecttv Address (r6p/32)
Syntax
Operation
d e r t e a d d r e s s( s r c )
Logal Form!
dest
LEA reS,
Dglcrlptlon
This instrrrction loads the addressspecified by the memory operand into the dcsti
nation register,No memory acesscycle takesplace,
You can also use LEA to perform simple multiplication or addition asdiscussedin
Chapter4.
Flag!
OF Df IF TF SF Z1 A.F PF CF
Faulta
RM rA0a6
6 +UDO INT6 *UDO
' Thc undefined opcode fauh only ccurs when the w opednd ls enco<lcdas a regisler
Example3
LEA E S I , V E C T o R I E B X t 4:I L o a d a d d r e s so f a r r a y l e m e f t
LEA EDI, IEAX][ECX] : A d d c o n t e n t s o f E A x a n d E C X ,s t o r e i n E D I
215
tHE ao3e5 BOOK
LEAVE 80186/80286/80386
Iav Cuffent Stack Frame ()
Syntax
LEAVE
Operatlon
ftovsP, EBP
P O PE B P
Lcgal Forn
LEAVE
Do.crlptlon
LEAVEis the counterpartof the ENTERinstrrctlon.ENTERis executedimmediately
aftera procedurecallto setup a newstackframe,LEAVEis elxecuted
befofea RET
inskuctionto releasethe returningprocedure'sstackframe,
Fl.gt
OFDF IF TT SF A AT PF CF
Fault!
PM Rtl W)e6
12 #Sq0)
lj 13 *G0(0)
Exatfrplo
EI1TER 4,4 ; F j . s t i n s t r u c t l o no f p f o c e d u r
; Procedur
contents
216
0! lh. 00306/0ot0t h.rrucdon sot Rer.H6
LGDT 80286/80386
I-oad GDT Regtster ()
Stmt.x
LGDT
op
Opelation
GDTR.I
imit e topl
GDTR.baseelop+21
Legal Form
LGDT
Do.crlptlon
This instruction loads the GDTR registerspeclfying the addressand limit of the
global desriptortable (CDT). The operand must point to a data structure in
memory whose first 16bits contain the limit of the global descriplor table and
s/hose dext 32 bits cor{ain the linear baseaddressof the GDT.
Loading the GDTR does nol invalidate th cuffently active descriptors'hovr'ever,
subsequentreferences!o seleclorsload descriptorsfrom the new GDT.
A procedure must have a CPI of 0 to issuethe LCDT insruction.
Flag!
OF DT IF II SF ZF AF PF CF
Faulti
P M N V W
6 +I,DO INT 6 #UDO
12 *SS(0)
13 *CP(o) rNT 13 +GP(o)
14 *PF(ec)
' The undefi.ed opcode fauli only oc.uis when the instrution is enoded wnh a reglster v.lue for op
Eranple
LGDT lnitial_table
217
tHE ao3s6 BOOX
LIDT 80286/80386
Load IDT Reglster o
Syntax
LIDT op
Operation
I D T R i. m
I it e I o p ]
loTR.baseFtop+21
Logal Form
LIDT NEM
Doacription
This instr\rcrionloadsthe IDTR registerandspecifiesthe addrssandlimit of the
interrupt descriptortable (IDT). The oprand must point to a data structure in
memory whosc first 16bits contain the limit of the interrupt descriptortable and
whose next 32 bits contain the ljnar baseaddressof the IDT.
After loading lhe IDTR, any soflware or hardq?re interrupts, faults, or traps will
causean accessto the new IDT,
A procedure must have a CPLof 0 to issuethe LIDT instruction.
Fl.g3
OT DF IF fi SF ZF A.F PT CF
Faultt
PM RM VW86
6 *UDO INT6 "UDO
12 rSS(0)
13 *CP(o) rNT13 *GP(o)
14 *PI(c)
' The u.defined opcode lauh o.ly occurs when the opoperand is en o<ledds! tgistei
Erarnple
LIDT n e wi n t t a b l e : L o a dI D T r e q i s t e r
214
a: lhc ao3a6/00347 h.truction s.t R.frdco
LLDT 80286/80386
Load LDT Register (16)
Syntrx
LLDT OP
Opgration
L o T Re o p
L9gal Fofins
LLoT feg
LLoT mem
Do3crlptlon
This in$truction loads a selecto!into the IDTR registerand specifiesa new local de-
scriptor table (lDT). The operand to ILDT must contain a l".rlid local descfiplor table
selectoror the lue 0.
Active descriptorsthat refer to th previousLDT are not invalidated; howevef, subse-
quent selectorreferencesload descriptorsfrom the new LDT,
If the IDTR is loadedwith lhe value 0, all IDT seleclorreferencesthat causea
memory referenceresult in a generalprotection fault,
The executing procedure must havea CPLof 0 to issuethe LIDT instruction.
Fl.g.
OIDF IF TF AF T AP PF CF
Fault.
Ptl lext6
6 INT6 sLDo
11 JrNP(sel)
12 irss(o)
13 *GP(o)
13 #GPGel)
14 #PF(ec)
Exarnple
LL0T task-8.I dtr
219
tHE to305 aooK
LMSW 80286/80386
Load Machine Status Word (16)
Synt.x
Ll'lsI op
Opsration
c R oe ( c R o& F F F F 0 0 0 0l oHp)
legal Forma
Ltlsl{ reg
Ltlsli mem
Deicrlptlon
This instruction loads the low-order 16 bits of the CRoregister Use it only when
running 80286operating ststem code. On 32-bit systems,use the instruction Mov
CRo,re& Note that you can use LMS\Wto enter protected mode but not to leAveit
and that you can use MoV cRo, reg to both enter and leare protected mode.
A procedur must be running in ring 0 to executeIlvlSW:
Flagr
OF DF II TI ST ZF AF PF CF
F.ultr
PM RM \,86
12 #SS(0)
13 *GP(o) rNT 13 #GP(o)
14 *PI(ec) *PF(ec)
Exampla
Lflsli i nit_state
220
a! Th. aoaa6/ao3a7In*ruction f6t nGtorc.e
LOCK ao86/80186/aO286/803a6
Assert Hardvare LOCK\ Stsnal Prefix ()
Syntax
LOCK
Legal Fo ns
LOCK
Deectiption
The LOCKinst,uclionprclx supportsmultipr(x,ess)r har.lwareconfigrations.You
can use the hardwarc LOCK\ signalto ensufe exclusivc accessto a particular mem-
ory byte,word, or clfford.The LOCKinstructionis vali.lonly ifit precedesan in-
structionin the lisr below.If you useit in combinalionwith anotherinstructbnor
in an unsupportedform of one of the iistedinstructions,an unclefinedoPcodclault
llT OR
Dls SRB
R'IR sUu
BTC xoR
xcHc DDC
XCHG INC
ADD NI]C
,\l)c NOT
AND
The IOCK\ siqnalis asserted for the dumlion of dre instructi()n,incluciingthe limc
required for a rcad-modify-write cyclc. Thc XCHG instructbn docs not require thu
IOCK pfefix bc.causethe LOCK\ signal is alwaysassertedduring a memory XCHG
when writing software for multiprocessorsystems,cnsure that locked accessibr
particular memory addresscsalwaysoccurs to opcmnds of ihe samesizc ln other
words, if you use the dword ar physicaladdress100,alwaysget accessto iI as a
d$,,ordand never as a bytc or word. Locking is not guaranteedto operate coffectly
unlcssvou observethis restriction
Flags
OF DF TF TF SF CF
221
tHE AOaA6BOOK
Faults
PM RM TAOA6
6 #[JDO rNT6 +UDo
Example
LOCK
BTS senaphore,3
222
A: lh. go3a6/ao3a7 In.trucllon sot Rol.renc.
LODS 8086/80186/80286/80J86
Irad Strirg (a/ftp/32')
Syntax
LODS
Operation
N h e no p c o d i s ( L 0 D S BL, o D S N1, 0 0 5 0 )s e t o p s i z ee ( 1 , 2 , 4 )
acc e DS:Itsl]
i f ( D F- 0 ) t h e n
ESIeESI+opslze
Logal Form.
L00SB ; Loadstrlng byte
L00Sf ; Loadstrlns *ord
LoDSD I Loadstrlns doublewofd
Delcrlptlon
This insruction loadsthe byte, wod, or dword at DSTESI into the accumulator.If the
DF bir in the EFLAGS registeris 0)ESIis incremented
by the sjzeof the operand
(1,2, or 4 bytes).IfDF is 1,ESIis decremented,
BecauseLODSis one of the 80386string inslructions, you can precede it with thc
REPprefixi howevn the resulting instruction is useless,as it continLlouslyover-
writes the contentsof the accumulator
You can precede the LODSiAstruction with a segmentoverride preiix. ln such a
case,the operand is taken from the specificd segmnt.
Flage
OIDF IF TT SF T AI PI CF
Faults
PM RM V8IB6
12 *SS(0)
13 #GKo) rNT 13 +CP(o)
223
tHE A0B86 BOOX
Examplo
LEA E B X ,A _ t o _ E ; A d d r e s so f t r a n s l a t i o r t a b t e
t10v Esr, |EEP+121 : source dooress
LES EDI, IEBP+16] i Destjnatr'on
Ll: LoDSB ; Fetch byte from source
0R AL, AL ; Test byte for zefo
JZ 00NE , B r a n c hi f z e r o
XLATB ; Translate the byre
5T058 ; S a v et r a r s l a t e d v e r s t o n
,tftP L1
DONE:
24
a! tn. 0o3aa/0046?In.tructid s.t R.t.rcm.
LOOPcc 8086/80186/8o286/8o386
Decrmrxt ECX and Branch o
Syntax
Operation
E C X e E C X - I
i f ( c c & ( E C Xt - 0 ) ) t h e n
EIPeEIP+offset
endif
Legal Form!
L00P ,ffset
L00Pz offset
L00Pt{Zoffset
L00PE offset
L00PE offset
Dglcrlptlon
These insffuctions support a dcrementand branch ope{ation, For all variants other
than LOOB the decfement and branch ls combined with a test on the ZF bit. A loop
counter is assumedin registerEcx, The instrrction decremntsthe register,and if
the lue of ECXis 0, no branch is taken. No flags are set as a result of the decre-
Ifthe lue ofEC)( is not 0, the branchis takenunlessthe conditionin the LOOPcc
forms is noi true.
Flrg!
OF DF IF TF SF ZI AF PT CF
F.ulta
RM tAO86
13 *GP(o) rNT13 *GP(o)
225
rHE 60366 000X
Ex.mple
; I n i t i a l i z e a r r a yo f t e m pf e a t s t 0 1 O
FL01 t Push1.0 onto Dp stack
LEA ESL array ; StartinE addressof array
{tlov ECX,sjze ; Loadtoop counter
l1: FLD S T ( 1 ) ,S T ; D u p t t c a t 1e . 0 v a t u eo n t { D ps t a c k
FSTP IESII ; Store 1.0. pop t{opstack
L00P 11 ; Contlnue{h e ECXnot 0
F S T P S T ( 0 ) ,S T I D o n e - - p ot ap s t t . O c o n s t a not f f
; N0Pstack
2fr
8: tho aO.?45/AO3a7h.truction S.t iotdence
Lseg 8086/80186/80286/80386
Irad Segment Register (16p/32)
Stmtax
Operatlon
dest e ls.cl
segeisrc+41
Legal Fo?nrr
LoS feg,
LES reg,
LFS reg,
LGS r9, heh
LSS reg, nem
Dercrlptlon
The src addressspecifiesa 48-bit pointer (32-bit in real mode or V86 mode) consist-
ing of a 32-bit offset followed by a 16-bit seletor.The 32-bit offset is loaded into the
dest registerand the selectoris loaded into the segmentregisterspecified by the in-
struction mnemonic.The 80386protection mehanismvalidatesthc descriptor
associatedwith the selector.
Use only rhe ESPreSisterwith the Lseginstruction.
Flag!
OFDT II TF ST T AI PF CF
Faultt
PM BTT 'A0A6
12 #SS(0)
13 *CP(o) rNT 13 #GP(0)
14 *PF(ec) #PF(ec)
Exarnplo!
LES ESL BIGPTR ; L o a d a d d r e s so f a r r a y e l e m e n t [ E B X ]
LSS E S P ,o L D S T A C K ; L o a da n e w s t a c k p o i n t e r
227
tHE 004a6 BooK
LSL 80286/80386
Ioad Segment Llmit (r6p/32)
Syntax
Operatlon
i f ( a c c e s s - 0 X ( s e l e c) t t)h e n
d e s t e d e s c rp l t ( s e ? e c t ) . iIm i t
Z F e I
Z F F 0
Legal Form.
dest sele.!
LSL reg, reg
LSL reE,
Delcriplion
Ifthe serec,operand is accessibleto the executing program as a valid selectorundel
the protectionrules,this instructionloadsthe dat registerwith the segmenrlimir
from the clescriptorindicaredby selctand setsZF to 1.
If the operand is not acccssibleor the descriptorassociatc{ with selscidoes not con-
tain a limjt field,ZF is setto 0.
The value sbred in the ,/cst regisreris alwaysrhe offset of the last adclressablebyre
in the segmenl(page granular limits are converredto byte granular limits). There-
fore, do not use a 16-bit register as lhe dest operand, as the resulting value might be
too large.
Flaga
OF DF IT IT SF T A.F PF CF
Faultc
PM RM 'A$6
INT6 #UDo
12 *SS(0)
13 #GP(o)
14 *PF(s)
228
a: th. ao36/4o387 In.ttuc d soi R.tc6ne
Exafiple
LSL E A x ,t B P + l z l ; Getlinit of selectoron stack
229
?rE 40306goox
LTR 80286/80386
Ioad Task Reglster (16)
Syntax
Oporation
Logal Forma
LTR res
LIR
DgEcrlptlon
This instruction loads the task registerwith rhe selectorspecified by the oprand.
The TSSdes.riptor for the seletoris marked "busy." lDading the task register does
not causea task switch,
If the pfocedure thar executesthe ITR insirurion is not running with a CPLof 0, a
general protection fault occurs.
Fl!g!
OFDT IF TF ST ? AF PF CF
Fault.
PM tm86
INT6 *UD(.)
10 *NP(sel)
12 *SS(0)
13 *CP(o)
13 *GPGel)
14 #PF(ec)
Eranplo
LTR AX ; L o a dt a s k . e g is t e l
230
0: lho 0Gta6/aO:F7 h.truction 3.t R.ld.nc.
MOV 8086/80186/80286/80386
Move Data (e/a6p/32)
Syhtax
Oporation
Logrl Forn!
Dslcrlptlon
This instruction copies the contentsof the s/c ope|and into dest,
Flrg.
OTDF IF IT 3F A AI PF CF
Frult!
Ptv Nt ve86
12 #SS(0)
13 JfGP(0) INT 13 #GP(o)
1{ *PF(ec) #PF(ec)
Exanpler
l{0V AL, IECXI ; Get byte from nemofy
t10V ESI, l82H ; L o a dE S I l , { i t h d a t a v a l u e
1.10V Bx. Dx ; 16-bit movc
l10\/ AH, 7FH ; L o a dA H y i t h 8 - b i t d a t a
81
rHE AOa6BOOK
MOV 8086/80186/80286/80386
MoYe Slector (16)
Syntax
Oporation
Logal Forms
dest src
l40V sreg, reS
l40y sreg, iren
liov reg, sreg
li0v mem, sreg
Do3crlptlon
This instruction copies the contentsof the e operand into the dest operand. If the
destoperand is a segmentregister,the inskuction loadsthe descriptorassociated
with the seleclor into the 80386shadowregisters.Privilege checks and tests for de-
scripror legality are made unlessrhe selectorvalue is 0. A prorecrion faul! occurs if 0
is loaded into the SSregister
when the SSregister is loaded,all hardsare interrupts (including NMI) are maskd
until afterthc ncxt in$tructionxecutes,
to allow loadingof th ESPrgister.
Flagr
OF DT IF TF SF ZT AF PF CF
Faults
RM V8'R6
10 rFNl,(sel)
12 #SS(0)
13 #Cl,(0) rNT 13 #GP(o)
14 *PF(ccJ
Examples
HoV DS, AX ; L o a dn e { d a t a s e q n e n t
r40V ES, heapses ; LoadES reglstel
Hov save ss, SS : Store copy of SS resister
zt2
6: th. AO$6lBOaa7 h.rruc otr S.r Bdr.nc.
MOV 80386
Move speclal (32)
Syntax
Oporaiion
Logal Form.
dest sra
tlov feg, reg
Descrlptlon
Thi$ instruction copies or loadsa speclalCPUregister to or from an 80386general
register.The specialregistersare CRo,CR2,CR3,DRo,DRl, DRz, DR3,DR6,DRZ
TR6,andTR7
A procedure must be running at a CPLofo to executethis instruction,
Flaer
OFDFIFfiSFA AF PF CX
Faultt
PM RM IW'6
13 #CP(o) 'GP(0)
Examplo!
HoY tAx, CRo : SaveCRoin EAX
HoV TR7,ECI i Loadtest register7
zt3
fl{E A03A6 BOOX
MOVS 8086/80186/80286/E0386
Move Sttlng (8n6P/32')
Syntax
t40vs
Operation
v h e r o p c o d ei s 0 4 0 v S BH, o v S ! ,H 0 V S 0s) e t o p s i z ee \ L 2,4)
E s r l E D I l D S I: E S ]I
i f ( D F- 0 ) t h e n
ESIeESI+opsize
EoIeEoI+opsize
ESIeESl-opsize
ESIeESI_opsize
L99al Formt
t4ovsB ; Moveshlng byte
|JoVSl{ : Move
str1ng{ord
l10VSD : l ' 1 o v es t r i n g d o u b l e l l l o r d
Dgrcrlptlon
This inslructioncopiesthe memoryoperandpointedto by DS:ESIto lhe destination
addressspecifiedby ES:EDLThe operandis a byte,word, or doubleword,depend-
ing on the opcodespecified.The EDI and ESIregistersareincrementedby the size
of rhe operandifthe DF bir is 0 of decremenred
ifthe DF bit is 1.
You can apply the REPprefix !o the MOVSinslruction 1orepeat the instruction. You
must placc the value specifying the repeal count in the ECXregister.
A segmentoverride prefix may be applied to the MOVSinstrrcrion. It will override
the DS segmentof the DS:[ESI]operand. You cannot override the Es segment
assumptionfor the EDI operand.
lbr dword-aligned strings,a REPMOVSDtransfersdata quicker than does fie equiv-
alent REPMOVSBor REPMOVSWHowever,if the sourceand desiinalion stdnfls
overlap, only the REPMOVSBoperation works correctly.
Flags
OFDF IF TF SF A AF PF CF
234
& th. aoi|a6rao3aTh.tructt n 3.t lt l.de
Fauli.
PM R'I 'UB6
12 #SS(0)
13 #GP(o) rNT 13 #GP(o)
14 #Pl(ec) #Pr(ec)
Ex.mplo
LEA ESI, copyrlghtisg i Get sourcestrjng
LES EoI, [EBP+12] ; ES:EDIloadedfrom stack frane
llov ECX,31 ; 51zeof soufcstring
CLD ; Ensuredltectlon flag set correctly
REPI.iOVSB i coDybyte string
2;t5
fl aota6 BooK
ilovsx 80386
Move wlth Slgn nxrcnslon (e/r6p/12)
Syntax
t'10VSX
dest, src
Oporation
dest e slgn extend(src)
Legal Form!
Do.crlptlon
This instruction copies an 8-bit operand to a 16-bit or 32"bit destination or a 16-bir
operand to a 32-bit destination and sign-e{tends the sourceopefand !o fit, Signex-
tension is performed by duplicating the hiSh-order bit of the src throughout the up-
per bits ofthe dsloperand.
Fl.g!
OF DF TF TF SF U A.F PT CF
Frult.
PM EM I&)46
12 *SS(01
13 sGP(o) rNT 13 #GP(o)
14 *PF(ec)
Exarnplea
tl0vst TAI AL ; Extendbyte to dwofd
lloVSX EoI. l,{oRD
PTRIESI] ; Extendwofd t0 dword
l'loVSX CX, DL : Extendbyte to {ofd
4$
a: lhe ao3a5/ao3a7 h.ttuction 5.r Rd.r.nc.
MOVZX 80386
Move wlth Zefo E Ftenslon (a/fip/32)
Synlax
l40VZX
dest, sr.
Ope.ation
legal Foams
5rc
HoVZX reS, reg
HoVZX reS, mem
Doscrlptlon
This instructioncopiesan 8-bitoperandtoa 16-hitor 32$i! destinarionor a 16-bir
operandb 4 32-bitdestinationand zero-extends ihc sourccoperanclrc fil. Signex-
tensionis performedby filling the upperbits of thc deslopcflrndwith 0.
Flags
OF DT IT TF SF A A.F PF CF
Faulta
BM W@6
12 *SS(0)
13 *GP(o) rNT 13 *CP(0)
Exanpl6s
l l 0 V Z X E A X .A L ; Exterd byte to dwofd
ll0vZX EDL I'I0RD P T RI E S I ] ; E x t e n dl l o r d t o d l l o r d
I40VZX CX, DL I E x t e n db y t e t o w o f d
237
fltE 40365 BOOK
MUL 8086/80186/802E6/80386
Unstgned Multtpltcattorl (a/16p/32)
Syntax
I'lULsrc
Operation
Legal Forms
llUL reS
MUL
De3crlptlon
This instructionperformsunsignedintegermultiplicationandrequiresonly one
operand, the multiplier. Th multiplicand is the accumulator,and the product is also
stored in the accumulatof.The size of the sr operand determineswhich registe$
will be used,as illu$tfatedin the foilowingtable:
Flat!
OFDFlr TFSTT AN P! CF
? x
Fault.
PM RM '4086
12 *SS(0)
13 #GP(o) rNT 13 *cP(0)
234
Ar Th. aO3A6/6O34?hdructlon S.t iol.rdc6
Example
r'r0v EAX,3
Ii4UL D I 4 O RPDT RI E S I ]
Jc res 64 B r a n c hi f r e s u l t r e q u i r e s6 4 b l t s
r,40v res 32, EAX Elsestofe product
ztg
tHE O35 BOO|(
NEG 8086/80185/80286/80386
Ngat lnteger (an6p/32\
Syntax
EG op
Ope?ation
Logal Forng
llEG reg
IlEG
Dglcrlptlon
This instructionsubtractsits operandfrom 0, whichresullsln a two'scomplement
(integer)negationof the oprand.
Fl.gi
OF DF IF TF SF ZF AI PF CF
x x x
Fault.
P]t n t&R6
12 #SS(01
13 *GP(o) rNT 13 #GP(o)
14 #PF(ec)
Ersmplg
; Conpute absolutevalue
0R EAX,EAX : Test for +/'
,llls SKIP ; J u n p i f n o t s i s n e d( p o s i t l v e )
llEG EAX : l{egatenesativenunber
S KPI :
240
a! lh. o:F6/ao3a7 h.tructld s.t R.l.retrE
NOP 8086/80186/80286/80386
No Olrratlon ()
Syntax
NOP
Logrl Form
NOP
Dercription
This iostructionperformsno functionotherthantakingup spacein the code
segmeft,
Flag!
OFDFIFfiSI? AF FF CF
F!ult!
None.
Exrmplo
lloP : l{othjngoccurs
41
tHE 40365 EOOK
t{oT 8086/80186/80286/80386
Boolean Complement (aA6P/32)
Syntax
NoTop
Opsration
Legal Fo na
NoT reg
NOT
Dg.crlptlon
This instructioninvertsthe stateof eachbit in the opemnd
Fl.gr
OF DI IF TF SF ZF AF PF CF
F.ult.
PM R.M VaO86
12 *SS(0)
13 ,rCP(o) INT 13 *GP(o)
14 #PF(ec) "P(ec)
Exampl
ll0T ECX ; Insert ECX
42
A: flF ao:|a6/ao:|a7 ltr.irrctlon 5.1B.ld.nco
Stht.x
Oporation
destedestlsrc
Lggal Fornt
0R r9, ldata
0R nen, ldata
0R reg, reg
0R reE.
0R nen, reg
Dorcrlptlon
This instruction performs a Boolean OR operarion berween each bit of the src
operand and the destoperand. The result is stored in .&st The trurh rable defining
the OR operation is as follows:
o lo - o
ol1-1
1lo-1
111-l
Flag!
OFDF IF TT SF ? At PF CF
0 0
F.ulta
PM nM tU66
72
13 *GP(o) rNT i3 #cP(o)
14 #PI(ec)
Exampla
0R AL, 80H ; Set high bit of AL
43
tHE OO(|a6BOOK
OUT 8086/80186/80286/80386
Output to Porrt (a/ftp/32)
Syntax
Operation
Legal Forlni
DOrt
oUT data,
OUT OX,
Dgrcrlptlon
This instruction outputs the value in the accumulator!o the specified data p,rrt.
Placing an immediate value in the ,ort operand field lets you addressports 0-255.
You can addressport addresses0-65,535 by storing the port number in the Dx
register.
OUT is a privileged instruction. A procedure executlng an ourput instrrrction must
satisfy one of two conditionsi otherwise, a generalprotection fault occurs,
Ifth procedurethatexecutesan OUT instrrrctionhasI/O privilege(if its CPLis
numerically lessthan or equal to the 1OPI field in the EFLAGSregister),the output
instruction executesimmediateiv.
If the procedure dos n(n have I/O privilege, the I/O permission bitmap for the cur-
rent task is cheked.If the bit(s) corresfnnding to the I/O port(s) is cleared !o 0, the
output iqstruction xecutes.If fie bit(s) is set to 1, or the port(s) is outside the range
of the bitmap, a generalprotection fault occu$. SeeChapter5 for more details on
If the OUT instrucrion is encounteredwhile in V86 mode, only the I/O permission
bitmaprs lested.The IOPLvalueis not a faclor.
Flags
OTDF IT TF SF T AI PF CF
Faulta
PM RM 'A0A6
13 *GP(o) #GP(o)
244
ar th. oll66/8o3a7 In.ttuction s.t Retereco
Example
flov DX, 378N : Set port address
oUT DX, AX i t{rjte t0 ports 378 and 3/9
24tt
fltE ao3a6 BooK
OUTS 80186/80286/80386
Output Strlng (a/ftp/32)
Syhtax
OUTS
Operation
r p c o di es ( 0 u T s B0 ,u 1 s ! 1 , 0 u TsSe0t )o p s j z e ( 1 , 2 , 4 )
| { h eo
p o f t ( D X )e D S : I E S I ]
1 f ( D F- 0 ) t h e f
ESIESI+opsize
ESIeESI-opsize
Legal Form3
oUTSB ; out stfing bYte
oUTSl,l ; o u t s t r l n gw o r d
0UTS0 :0ut strlngdoublewond
Dorcrlptlon
This instruction outputs the byle, word, or doubleword at offset Esl to the port
specified in rcister DX. The ESIregister is adjustedby the size of the memory
operand-incrementedif the DF bit is 0 or decremenled if DF is 1
You can precede the OUTSinstrution with the REPinstructioni however,register
ECx must contain a count of the number of times the OUTS inskuction is to be
executed,
You can apply one of the segmen!override prefixes to the OUTSinstruction, caus-
ing the operand to be takn from the specified segmentrather than the segment
pointed to by DS.
outpur insructions are privileged lnstructions. The protection checks for the ouTS
iostructions are the sameas those for the OUT instruction
Flags
OF DF IF TF SF ZA AI PF CF
26
a: th. 6o365/looa7 In.t u.tton a.t lt.t rac.
F.ult3
PM R,I 'ryN6
12 #SS(0) #s(0)
13 #GP(o) rNT 13 #GP(o)
14 +PI(ec) ,*pF(ec)
Exarnplo
tEA ESL I0_CHI|L_C140
i cet pointer to strjng
l(ov DX,C0|{TRoL[ER i cet I/0 port nLrnber
Itlov ECx,8 | Sjze of I/0 strjng
REP 0UTSD ; output I doubtewofds
47
tHE 603a6 BOOK
.
IIOP 8o86/8o1E6/8o286/80386
Pop segment Reglster (r6)
Syntax
Oporation
s e g e S S I: E S P ]
ESPeESP+4
Logal Fornr
PoP sres
Delcrlptlon
This instruction pops a 32-bit value off the stack and storesthe low-order 16bits in
the spcified segmentregister.RegisterCSis not a valid destination oPerand,but
the othrsegmentregisiers(DS,ES,SS,FS,and GS)arewlid.
The wlue slored in the segmentregistermust be a lid selectoror 0i otherwise, a
protection fault occurs. (RegisterSScannot be loadedwith a 0 ) Note also that a
POPSSinstruction heslimited usefulnessbecauseSSand ESPare required to imple_
ment a stack. Ho\,,ver,if you execute a PoP SS,the 80386inhibits all hardwafe in_
terrupts to enable the loading of ESPand the guarding againstintefiupts while the
stackpointeris invalicl.
If the PoP instflrction is executedby a V86 mode task, only 16 bits are popped off
Flag!
OF DI IT m AF PF CF
Faults
PM RM te)a6
t0 *NP(seD
L2 *ss(0) #ss(0)
t3 #GP(0) tNT 13 #GP(0)
Eramples
POP GS
POP DS
28
a! th. aog6/ao3a?In.trucllon 3.t i.Lm..
POP 8086/80186/80286/80386
Pop Value off Stack (16p/32)
Slmtax
Operatlon
derte ss:IEsP]
if (slzeof (dest) - 16) then
ESPeESP+2
ESPeESP+4
legal Foam!
PoP reg
PoP nen
Dorcilptlon
This instructionpopsthe cuffedtvalueat the lop-of-stack,sloresit in the dest
operand,and adiuststhe stackpointer
Foroptimumperformance, keepthe stackon a doublewordboundary.Pustingand
poppingl6-bit luesmight alterthis alignment.Forthis reason,it is preferableto
sign-extendor zero-extenda 16-bitopefand!o 32bits beforepushingor poppingit,
W'henyou executePOPin V86mode,the srackwill generallybe usedonly for 16-
bit lues.This doesnot degradesystemperformance.Pushirgandpopping16-bit
valuesleadsio problemsonly whenboth 32-bitand16-bitpuslrcsandpopsare
mixedin the samecode,
Flag.
OTDA IA TF SF ? AT PF F
Fault!
PM nM VUA6
12 *SS(0)
13 *cP(o) rNT 13 *CP(o)
14 *PF(ec)
Examplo
POP ECX
249
|E ao0a6 BooK
POPA 80186/80286/80386
Pop All General Registers (16)
Syntax
Oporatlon
OI
SI
POP BP
ADD
POP BX
POP DX
POP CX
POP
Legal Forrn
D.rcrlpllon
This insir!rction pops all 16-bitgeneral fegislers excePtSPfrom the stack Because
thc registersare sbrcd asa 16-byteblock of data,the POPAinstructiondoesnot
aff!'ct dolbleword alignmcn! of the stack.
Flagr
OFDFIF TF SF A AF P8 CF
Faulta
PM ra0a6
\2 *ss(0)
13 INT13 #GP(0)
't4
Exanple
2t;o
a: the ao3a6/ao:w h.rrucrton S.t i.td.tu.
POPAD 80386
Pop AI Grxeral Registers (32)
Syntax
Oper.tlon
EDI
ESI
POP EBP
ADD ESP,4
POP EBX
POP EDX
ECX
POP EAX
Lggal Fotm
POPAO
Dorcilptlon
This instr\rctionpopsall 32-bitgeneralregistersexceptESpfrom the slack.
Flags
OBDF IF IT SF 3 AI PF CF
F!!lta
lwt6
12 irss(o)
L3 INT 13 #cP(0)
14 *PF(ec) *PF(ec)
Examplo
1
tHE aoa6 Boox
POPF 8086/80186/80286/80386
Pop Srack tnto FLAGS (16)
Syntax
Opor.tion
FLAGS
e S S :I E S P
]
ESPeESP+2
Logal Form
POPF
Doacrlptlon
This instruction pops the low-order word of the EFLAGSregister from the stack.
POPFprovids compatibility with previous Intel microprocessors.Use the POPFD
instruction in native-mode programming,
Flagl
OFDF IF TF SF A AF PF CF
x x x
Fault!
PM R.IT IW6
12 #SS(0)
13 tNT 13 *GP(o)
14 *Pltec) *PF(ec)
Examplo
2
: tho ao.?06/003a7 lFtruction 3.t t l.Enc.
POPFD 4o366
Pop Stack tnto EFLAGS G2)
Syntar
Oporation
E F L A GeS S S| [ E S P ]
ESPeESP+4
Lsgal Form
POPFO
Do3crlptloh
This instructionpopsthe top-of-stackinto the EFLAGS regisrerTheVMandp.Fbits
initiallypresentin EFIAGS arenotmodified. Theinteffuprflagi$modifiedonlyif
CPL< IOPIbeforethePOPFD, thatis,ifthe executing
procedure hasI/O privilege.
The IOPLfield is alteredonly if CPL- 0.
Flr93
OT DF II TF SF ZF AT PF CF
x x x
Fault.
PM RM V8O86
12 *S5(0)
't3 *GP(O)
INT 13
$pr(ec)
Er.mple
POPFD
253
tltl @t06 300K
PUSH 8086/80186/80286/a0386
Pushvalue onto stack (a/$p/32')
Syntax
PU5Hop
Operaiion
j f ( s j z e o f ( o p )- 1 6 )
ESPeESP-2
ESPESP-4
s s : [ E S P ]e o p
Log.l Form.
PUSH ldata
PUSH res
PUSH sreg
PUSH men
Do.crlptlon
This instructionpushesthe opemndonrothe stack.The stackpointerisdecre-
mented before the lalue is pushed.Ifthe operand is the ESPregister,the value
stored on the stack is the value thar ESPhad before the instruction was executed.
(Thb instruction is different from the 8086instruction, which pushesthe new
value.)
Note that pushing 16-bit registersand memory operands onto the stack changesthe
stack'smemofy alignment, It is more efficient to sign-extend or zero-extend the
operand to 32 bits and push th dword. The 80386usessegmentregistersto push
an instruction lu onto the stack,
when 'ou execute the PUSHinstruction in v86 mode, segmentregistersare pushed
as 16-bit values.The stack will generally be used only for 16-bitvalues in V86 mode.
This does not affect systempedormance bcausestack misalignment only occurs
when both l6-bit and 32-bit lues are pushed onio the stack.
Flaga
OFDF IF TF SF A Af PF CF
4
ar Tho aO3A6/AO3a7h.l.uctld S.l Felcrsce
Faulls
Rir tao86
12 #SS(0)
13 #GP(o)
Examplea
PUSH 7
IIOVSX E A X ,A X
PUSH EAX
PUSH a r r a y t E SNI 4 l P us h n e n o f y v a l u e
5
tHE AOaaABOOK
PUSHA 80186/80286/80386
Push l6-Btt Gneral Registers (16)
Syntax
PUSHA
Opgratlon
PUSH AX
PUSH CX
PUSH DX
PUSH BX
PUSH tenp
PUSH BP
PUSH SI
PUSH DI
Logal Form
PUSHA
DoEcrlptlon
Thisinsrrucrion
srores on(hestack.Thisinstruc-
a copyof all eight16-bitregisters
tion provides compatibility with 80186and 80286software. Use the PUSHADin-
struction in native-modeenvironments,
Flag!
O F D T I T T ? S F A CF
F.ults
tao86
L2 *ss(0)
la
't4 tNT 13 i*GP(0)
Example
PU5HA
256
ar th.O36/Oildt In.trucrbr E tnd.rc.6
PUSHAD 80386
Push 32-Elt General Reglstefs (32)
Syntax
PUSHAD
Oper.tlon
tempe EsP
PUSH E A X
PU5H E C X
PUSH ED)(
PUSH E B X
PUSN
PUSH E 8 P
PIJSH E S I
PIJSH E D I
Log.l Form
PUSHAI)
Dorcrlptlon
This instruction sioresa copy of 4ll eight general registerson the stack,The l?lue
of ESPthat is savedto the stack is the ESPvalue before e,xecutionol the PUSHAD
instruction,
Flag!
OF DF II TF 38 ZI AF PT cr
Fault!
Prt teB6
t2 rss(0) *oP(0)
13 INT 13
't4
Eramplg
PUSHAD
E7
fl{E ao3a6 BOOX
PUSHF 8086/80186/80286/80386
Push 16-Btt EFLAGS Regster (16)
Syntar
PUSHF
Oporation
E S P- E S P 2
ss:tEsPle FrAGs
Logal Form
PUSHF
D.icrlptlon
This instflrctionpushesthe low-order15bits of the EFLAGSregisterontothe stack.
PUSHF providescompatibilitywith 16-bitprocessors andcaNesmisalignmentof
the stackif usedin nativemode.Or y 32"bitprogramsshouldusePUSHFD,
PUSHF causesa generalprotectionfaultin V86modeif the executingprocedure's
IOPLis numericallylessthan3.
Flrg!
OT DF IF TF ST U A.F PF CF
Fauli!
PM R.tt W)86
12 #SS(0)
L3 +cP(0)
14 #PF(ec) *PI(ec)
Example
P U SFH
254
a! th. ao3a6/aooa7 h3ttuclld Lt hd.Mo
PUSHFD 80386
Push EFI,\GS Reglster (32)
Stmtax
PUSHF0
Opgralion
ESP-ESP.4
S S r t E S P ]e E F L A G S
Logal Form
PUSHFD
Dercrlptlon
Thls idstructiod pushesthe contentsof the EFLAGSregister onlo the srack. PUSHF
will causea generalprotectlon fault in V86 mode lf IOPLis lessthan 3.
Flrgr
OADF IF TF SF zF AI PF CF
Flultr
RM V8o86
12 *SS(0)
1,3 #GP(0)
14 #Pr(ec) #PI(c)
Exanplo
PUSHFD
259
rraE@306Boox
RCL 80M/80186/80286/80386
Rotate Tlrough carry Lft (a/ftp/32)
Syntax
Opgration
t e m pF n a x ( c o r r t , 3 l )
l f ( t e n p- 1 ) t h e n
0 F ( h l g h b J t ( d e s t )t - C F )
0Fts?
v a l u e e c o n c a t e n a t e( c F , d e s l )
l{hlle (tenp !- 0)
x e h j g h b j t ( v a lu e )
v a l u ee ( v a l u e< < 1 ) + x
tempetenp-I
C F F h l g h b l t ( v a lu e )
Legal Fo.mr
dest count
RCL reg, idata
RcL ld6ta
RCL reg, CL
RCL CL
Oe3crlptlon
This instruction concatenatesthe cafiy flag (CF) with the des, operand and rotates
the \alue the specified number of times.A rotation is implemented by shifting the
lue once and transferling the bit shifted off the high end to th los/-order position
The OF bit is defined only if the rotate count i6 1. Th 80386never rotatesa pattern
more than 31 times. Countsgreater than 31are maskedby the bit pattern
0000001FH.
Flage
OF DF IF TF SF ZF AF PF qF
260
a! th. ao36/to3O? t..tfucttor 3.t i.tcEn.
Faults
PM RM I\N6
12 *SS(0)
13 *GP(O) rNT 13 #cP(o)
14 #PF(ec) *Pf(ec)
Example
RCL E A X ,3 ; R o t a t e E A X3 b i t s t e f t
261
THE 60346 BOOK
RCR 8086/80re6/80286/80386
Rotate Tlrough CanyRight @/r6p/32)
Syntax
Opoiation
temp+ nax (courl, 31)
J f ( t e m p- 1 ) t h e n
0 F e ( h l g h b l t ( d e s N I) - h i s h b i t ( d e s t< < 1 ) )
0 F e ?
e f d if
v a l u e e c o n c a t e n a t (ed e s t , c F )
{file (tenp !- 0)
x e v a l u e & 1
v a l u ee ( v a l u e > > 1 )
highbit (value)e x
tempetenp-1
C Fe h l g h b l t ( v a l u e )
Legal Form.
Daac.iption
This instruction concatenatesthe caffy flag (CF) with the des, operand and rotates
the value the specified number of times.A rotation is implementedby shifting the
value once and transfeffing the bit shifted off the low end () the high-order position
The OF bit is defined only if the rotate count is 1. The 80386never rotatesa pattern
more than 31times. Countsgreater than 31are maskedby the bil pattern
000000rFH.
Flags
OFDF IF TF SF A AF PF CF
x2
ar th. ao3a5/acia7 In.ttucll.n S6t Bclcrencc
Faults
PM RII IAIB6
12 #SS(0)
13 #CP(o) rNT 13 *GP(o)
Er.mple
RCR E A X ,3 ; R o t a t e E A X3 b i t s r i g h t
263
fltE ao3a6 BooK
. REP 8086/80186/802E6/8oi86
Repeat Strtng Irreftx ()
Syntax
REP
legal Form!
REP
REPE
REPZ
R EP I l E
R EP I l Z
Do3crlptlon
The repeat prefix may be applied to any string lnstruction (CMPS,INS, lODs,
MOVS,OUTS,SCAS,STOS).'Whenthe prefix is present,the string instructlon exe-
cutes rpeatdlybasedon the cowrt lue in the ECXregister The ZF flag ls also
testedwhen executing CMPSor SCAS,
If EC)(is 0 when a repeated strinS lnstruction is encountered,the string instrucrion
will not be executed.
Referto the individual string instructions in this chapterfor additional information.
Fl.g.
OF DI II TF ST ZF AF PF CF
Faultt
PM RM I&)46
6 *UDO INT6 #UDO
Exgmple
t{ov EAx,0
0Y ECX,tO24/4
REP STOSD ; j n i t i a l i z e I ( 8 o f n e n o r yt o 0
264
0! llr. o:t66Eo3a?hdrEtton l.r n.t@m.
RET 8086/80186/802E6/80386
Near Retum f.om Subroutlne o
Syhtax
RETcourt
Oporatlon
E I P p o p ( ) l
ESPeESP+count
Logal Form.
RET
RET ldata
Dolcrlptlon
This instrirction restoresthe insrruction pointer to the value ir held before the
previous CALL instrrction. The !"lue of EIP that had been savedon rh 6tackis
popped.If the count operand is present,the cornt lue is added ro EsB removing
any operands that were pushed onlo the stackfor the subrourincall,
Fl.g!
OFDF II TT ST U AJ PT CI
Fault!
PM R.LI IM86
12 #SS(0)
13 #GP(o) rNT 13 #cP(o)
14 #P!(ec)
Exarrplo
R E T4
265
tHE ao3a6BOOK
RETF 8086/80186/80286/80386
Far Relur'l from Subroutlne o
Syntax
R E T Fc o u n t
Ope.aiion
E I P e p o p ()
c s e p o p ()
ESPeESP+couri
Logal Fotm.
RETF
RETF JdAtA
Descrlptlon
This variation of the RETinstruction poPsboth a new cS and EIP from the stAck.
The instruction assurnesthat the CSvalu is stored as the low-ofder 16 bits of a
dword on the stack.
Ifthis instructioncausesa privilegeleveltransition,the protectionchecks
described in chapter 5 |ake Place
Fls93
OF DT IF TF 3I ZF PF CF
Faults
RII vN86
10 #M(sel)
12 #SS(0)
13 #CP(o) INT 13 #GP(O)
*pFiec)
Example
RETF : R o t a t e E A X3 b i t s l e f t
28
a. th. lo35/ao3g? lBtructlon S.t i.t*.nc.
ROL 80E6/80186/80286/80386
Rotate kft (an6p/32)
Syntax
Operation
t e m pe m a x ( . a r r t , 3 1 )
if (tenp- 1) then
0F e (hlghbit(dert) l- CF)
0FF ?
{hlle (tenp l- 0)
x e hlghbJt(dest)
dest e (dest << l) + x
tenp e temp I
cF e hishblt (desi)
Legal Formo
caunt
RoL reS, jdata
RoL ldata
RoL res. CL
RoL nem, CL
Do!crlpilon
This instructionroktes the /rt operandthe specifiednumberof times.A rolation
is implemented by shifting the value ence and transferring rhe bir shifted off the
hiShend ro lhc low-orderposrrronofthc value.
The OF bit is defined only if the rorate count is 1.The 80386never rotatesa paltern
more than 31times. Couds grgater than 31are maskedby rhe bit panern
0000001FH.
Flag$
OFDF II TF SF A AF PI. CF
a7
fllE aoaa6 gooK
Faultg
PM RT' 'EI'6
12 *SS(0)
13 *GP(o) rNT13 #G(0)
14 *PF(.) *PF(ec)
Exarnp19
RoL E A X ,3 ; R o t a t e E A X3 b i t s l e f t
2Aa
a: th 40386/o03A? In.ttuction St Rol.Hc.
ROR 8086/80r86/80286/80386
Rotate Rtght G/r6p/32)
Syntar
Operatlon
t e m pe m a x ( c o r r t , 3 1 )
i f ( t e m p- 1 ) t h e n
0 F e ( h i 9 h b l t ( d s t ) ! - h i g h b i t ( d e s N< < 1 ) )
0Fe ?
H h J l e( t e n p l - 0 )
x e v a l u e & 1
v a l u e ( v a l u e) ) 1 )
h 1 9 h b 1 t ( v a l u ee) x
t e m pe t e m p 1
C Ft s h l g h b j t ( v a l u e )
Logal Forma
d.<f .a,,.r
RoR feg, Jd a t a
RoR idata
RoR reg, CL
RoR neit, CL
Do.crlptlon
This instruction rotatesthe /e.rt operand rhe specified number of timcs. A rotation
is implemented by shifting the value once and transfefiing the bit shifted off the low
end to the high-order position of rhe value.
The OF bit is defined only if the rotarecounr is 1. The 80386never rotatesa pauern
more than 31times. Countsgrerter than 31are maskcdby the bit pattern
0000001FrJ.
Flags
OFDT IF TF SF T AF PF CF
259
rHE 00306 BOOX
Feulta
PM RM IAOA6
12 +SS(0)
13 *CP(o) rNT 13 *GP(o)
U #P!(ec) *PF(<)
EramDlo
RoR EAX,3 ; RotateEAx3 blts rJght
270
0! th.6o365/60o0? h.ttucrion aer B.t rm..
SAHF 8086/80186/80286/80386
Stor AE ttr EFLIGS (8)
Syntax
5AHF
ODeratlon
EFLA6S | (AH & 0D5H)
e EFLAGS
Logal Form
SAHF
Do3crlptlon
This instruction loads the conrentsof the AH rgisterinto bits Z 6, 4, 2, and Oof the
EFLAGSregister.
Flrgr
OIDF IT TT SI zF AF P8 CF
x
Fault!
None,
Eramplc
SAHF
zr1
tHE O03A6 60ltx
SAL 8086/80186/80286/80386
Shlft Irfr Arlthmettc (8/r6p/32,
Stmt.x
Opgr.tlon
tempecount&001FH
, h 1 l e ( t e f l pI - 0 )
CFe hjghorder( dest)
dest e dest << I
tempetemp_l
lfcount-lthen
0 F e h i g h o r d e(rd e s t ) l - C F
0 F e ?
Lggll Form!
dest count
SAI feg, ldata
SAL men, ldata
SAL reg, CL
SAL nem, CL
Dgrcrlptlon
This instruction shifts the dest ope:andcount bits to the left, The arithmetic shift
left (SAL)and loSical shift left (SHL)are equivalentinstructions,
The cornt operand must either b an immediate data lue or be stored in register
CL.The 80386masksthe corrt operand with lFH so that the corrt value is never
Sreaterthan 31,
If the corrt operand is 1,the overflow flag is reset to 0 when the high-order bit and
the carry flag have the same lue after the shift. If the high-order bit and CF have
different lues, OF is set !o 1.If cor,rt is greater than 1, oF is undefined.
A left shilt is equivalentto multiplying the d?st operandby 2ctu"t.
Flagt
OF DF IF II SF ZF AI PF CT
272
* th. ao3a6/ao3a7Instruction St R6l.renc
Faults
PM RM WNA6
i2 *ss(o)
13 +GP(o) rNT13 *GP(o)
1.i #l,F(<) *PF(d
Examples
SAL E C X ,7
SAL NORO P T RI E B P + 8 ] C
, L
273
t||E ao3a6 gooK
SAR 8086/80186/80286/80386
Shft Rtght Artthmettc <a/$p/52)
Syntax
Operation
tenpecorrt&00lFH
! l h i l e ( t e m p! - 0 )
s a v ee h i s h o f d e f ( d e s t )
CF-dest&1
dest e dest >> t
h j 9 h o r d r( d e r t ) - s a v e
t e m pe t e m p 1
lfcount-1thei
0 F e 0
0 F e ?
Lggal Forrr!
Dolcrlptlon
This instruction shifts thc dest opeftnd count birs to the right The shift is called
arithmeric becauseit prescrvesfie sign bit of the ,test operand
The corrt operand must be an immediate data value or it must be slored in tegisler
cl. The 80386masksthe corrt operand with 1FHso that the corrt value is never
greater rhan 31.
If corrt is 1, the overflow is feset to 0. If corrt is greaterthan 1, OF is undefined
The arithmetic righl shift is simila. to dividing destby 2'a"' s{cept that negative
valuesare rounded toward negtive jrfinity, rather than toward 0 (that is, -3 shifted
left I rounds to -2, whereas-3 divided by 2r rounds to -l).
Flags
OTDF IF TF SF 2rr AI PF CT
274
A: flF gG?e6/ao3a7 h.trrcilotr a.i nd.r.nc.
Faults
RM ttsO86
12 #SS(0)
13 *cp(o) rNT 13 #cp(o)
14 *PF(ec) #PF(ec)
Exarnplgs
SAR E C X ,7
SAR I'I0RD P T RI E B P + 8 ] ,C L
275
tNE goea6 BooK
SBB 8086/80186/80286/80386
subtractlon wlth Boffow (alftp/32)
Syntax
S B Bd e s t , s r c
Oporation
dest P dest - src - cF
Logal Formr
dest src
Sss reg, i data
s88 i data
s88 reg, reg
s88 reg, nem
s8B nen. feg
Delctlptlon
This instruction subtractsthe trc operand from the desl operand and decrements
the dest operand by 1 if the CF flag is set.The result is stored in dest
Flag.
OFDF IT TT SF 3 AP PF CF
x
Faultl
RM rts046
12 #SS(0)
13 ,fOP(o) rNT 13 *GP(o)
14 #PF(c)
Examplg
: 6 4 - b i t s u b t r a c t l o n o p e r a t i o n E D ) ( T E A- XE B X : E C X
SUB E A X ,E C X ; Lor-order Dlts
SBB E D X ,E B X : H i q h - o r d e rb l t s
z'6
0! th. A036/A03A7 h.rr* .tr 3.t B.l*.nc.
SGAS 8086/80186/802a6/a0386
Scan Strlng (8/r6p/rz)
Syniax
scAs
Opgratlon
r h e n o p c o d el s ( S C A S BS
, C A S I ,S{ ,C A S Ds)e t o p s l : e e ( L 2.4)
ULLacc-ES:[t0I]
i f ( D F- 0 ) t h e n
EoIeEDI+opslze
Lur!LUl . opsrze
Legal Form!
SCASB I Scanstrlng byte
SCASI i Scanstrlng ,ord
SCAS0 i Scanstrjng doublerord
Doacrlptlon
This instruction comparesthe lue in the accumulator(AL, AX, or EAX) with the
operand at ES:IEDII.The flags are set accordingto the compare operarion, and the
BDI reglster ls adlisted by the size of the operand. If the direction flag (DF) is 0,
EDI is incrementedi othrwise, it is decremented,
You can apply the REPEor REPNEprefix to the SC"{Sinstruction. The ECXregister
contalns a repeat count, indicating the maximum number of times the instruction
shouldbe repeated.The instnrction will repeat only whlle the repear condition is
true, that ls, when ZF - 1 for REPE(REPZ)or ZF - 0 irr REPNE(REPNZ).
You cannot use a segmentoveffide prefix with SCAS,The ESregister is alwaysthe
destination of the string io be s.anned.
Flag!
OFDFIl TT SF? AI PF CF
x
277
t{E 80aa6 EOOX
Faults
PM RLI 'AOE6
12 #SS@)
13 #GP(o) INT i3 *GP(o)
14 +PF(ec) #PF(ec)
Example
I S a r c fho r a n a s t r i s k1 n a s t f i n g
LES EDl, LEBP+121 i Strlng pointer on stack
l10l/ ECX,tEBP+2OI : Strlng slze on stack
CLD
; Characterto searchfor
REPIIE SCASB : scan
.JE I.IATCH ; granchlf found
274
! th. AOSaa/lOa87 In.lr*ttff 3.t nd.rcno
se9 8086/80186/80286/80386
Segment Overtde I'refh o
Logal Fo.ma
cs:
DS:
5S:
E5:
F5:
GS:
Delcalptlon
Theinstruction
thatfollowstheseprefixes
takesitsmemoryoperand
fromthespec-
Ified segmentrather rhan from the defaulr seSment.
You cannot override the following string instructionsl
INS
scAs
sTos
Flatr
OADI IF TF SF A AF PI CF
Flult!
None,
Examplo
tl0v EAX,FSTIESII I Rad
fron FSrathrthin 0S
Al)D D S : l E B P ] ,7 | llrlte to DS father than SS
275
fl{E A0365 aOOX
SETcc 80386
Setrye oncorrdt lort (8)
Syntax
S E T c cd e s t
" Oporatlon
1f (cc) then
desl e I
dest e 0
Legal Form!
SETA dest i S e t l f a b o v e ( u n s l g n exd > y ) / c F - 0 t Z F - 0
SETAE dest : S e t i f a b o v eo r e q u a l / C F - 0
SETS dest : S e t l f b e l o l , v( u n s i g n e dx < y ) / C F - 1
SETBE dest , set 1f belotl or equal / CF- 1 | ZF - 1
i 5er rr carry / LF - r
i ) e r r r e q u a r/ . f - r
SETG dest ; s e t l f g r e a t e r ( s J g n e dx > y ) / s F ' 0 F & z F - 0
SETGE dest ; Set lf greater of equal / SF - 0F
SETL dest : S e t l f l e s s ( s j g n e dx < y ) / S F l - 0 F
SETLE dest : S e t l f l e s s o r e q u a l/ S F l - 0 F & Z F - 1
SETIA dest ! set if not obove(SETBE)
S E TA E d e s t i S e t l f n o t a b o v eo f e q u a l ( S E T B )
S E TB dest I Set 1f not below (SEIAE)
SETIBE dest ; set 1f not below of equal (SETA)
SETNC dest ; Set 1f no carry / CF- 0
SETl,lE dest ; Set 1f not equal / ZF - 0
SETNG dest ; Set if not greater (SEILE)
S E T N G Ed e s t ; Set 1f rot greater or equal (SETL)
SETNL dest ; set lf not less (stTGE)
S T TL E d e s t I Set lf not less or equal / sF - 0F & ZF - 0
S E T0 dest I Set lf no overflor / 0F - 0
SETNP dest , Set if no parlty / PF- 0
SETNS dest ; Set if no sjgn / 5F - 0
SETNZ dest ; Set if not 0 / ZF - 0
SETo dest ; Set if overflor / 0F - 1
SErP dest : S e t i f p a r i t y / P F- L
SETPE dest ; St if parity even / PF- I
SETPo dest : Set if parity odd / PF- 0
STS dest ; Set if sisn / sF - I
STz dest ; Set if 0 / zF - 1
2n
l! ttr. O:Lt/tloil6jt In.trcttor &r Bdd.nc.
Doscrlptlon
This instuction sets the dert blte to 1 if the condition described by the opcode is
meq otherwise, the instnrction clearsrhe b''te to 0.
Fl.gg
OT DF IF TF SI U AT PF CF
F.ult!
PM RM IW6
12 #SS(0) #SS(0)
13 *OP(o) rM13 #GP(o)
14 *PF(ec) #PF(c)
Eramplo
SETIIZ AL
I'tovzx EAx, AL
nl
THE A03A6 BOOX
SGDT 80286/80386
Store CDT Reglster o
Syntar(
S G o Td e s l
Operation
d e s t e G D I RL. I r ' 1 I T
dest+2eGoTR.BASE
Legal Form
SGDT
Do.cription
This inslructionwdtesthe limit portionof the GDTRto the lest memoryaddress
,nd writesthe lincarbaseaddressofthc GDTto the dwordat dest+ 2.
Flags
OT DF IF TF SF ZT A.F PF CF
Faultr
PM R.M tW6
6' #uDO rNT6 *UDo
12 *SS(0)
13 *GP(o) INT 13 *GP(o)
' The undefincd opcode fnult onlt oc.us whcn the let opetand is cncoded as x egistei
Example
s00TI300H
l ; S a v eG D T R
2a2
A: th. AO3A5/aOOa?Instrucrion 3d nel.rence
SHL 8o86/80186/80286/80386
Shft I-ft Logtcal (8/r6p/32)
Syntax
Operation
ten'pecourt&001FH
while (tenp !- 0)
C F e h i s h o r d e r( d e s t )
dest e desl << I
tempetemp-l
ifcourt-lthen
0 F e h i g h o f d e r( d e s t ) 1 - C F
Lo99l Form!
dest caunt
SllL reg, i data
SllL nem, idata
SllL feg. CL
StlL men, CL
Dorc.lptlon
This instruction shifts the dest opet^t\d count bits to the lcft. Thc arirh'ncric left
shift (SAL)and logical left shift (SHL)are equivalent instructions.
The corr,t operandmusteitherbe an immcdiatedatavalueor be storedin regisrer
CL.The 80386masksthe cowlt operand wilh lF:H so that the co&rt vallre is nevcr
greaterthan 31.
If the corzr operand is 1,the overflow flag is reset to 0 when rhe high-order bit and
the carry flag have the samevalue after the shift. If the high-order bir and CFhave
different values,OF is settol.lf count is grearerthan 1, OF is undefined.
A left shift is equivalentto muliiplying the dest opc:dandby2n'r-t.
Fla93
OTDF IF IT SF A AF PF qF
n3
tHE eo3a6aoox
Faults
NA tA(t86
12 +SS(0)
13 #GP(0) rNT 13 #GP(o)
14 #PF(ec) *Pr(ec)
Erample3
sHL E C X7,
SHL P T RI E S P + 8 ] ,C L
NORD
244
6i th. 004a6/ao3a7 h.ltuctld S.t F.1.retr6
SHLD ao386
shtft Ifft Double <16p/32)
Syntar
S H L Dd e s t , s r c , c o u n t
Opo.ation
tenp e max(count, 31)
value e concatenate (dest, src)
v a l u ee v a l u e< < t e n p
Log.l Form.
dest src
SHLD reg, feg, idata
SHLD nen, reg, ldata
SHLD res, reg, cL
SHLD reg, cL
Dolcrlptlon
This insiructionconcatenates the s,"coperandto the t&st oPerandand shiftsthe
resultiAg double-sizevalue left. The low-order bits are stored in des,
The corr?i operand is maskedvr'ith 1FHso lhat no shift counts Srealerthan 31 are
used,
Fl!93
OF Df IF TF ST ZT AT PF CF
? x
Fault.
R t ' 8 6
12 #SS(0)
13 JfGP(0) INT 13 *GP(0)
14 #PF(ec)
Examplo
lrov EAx. IESII ; Get lor-ofdef dword
SHLD E A X ,I E S I + 4 1 ,7 ;64'bit shlft
n5
tfiE 40306 000x
SHR 8086/80186/80286/80386
Shtft Rtght Irgtcal (a/L6p/32)
Syntax
Oporetion
temptscourt&001FH
l{hile (tenp !- 9)
CF-dest&1
dest e dest >> I
tempe tenp I
ifcount-1then
0 F e h l s h o r d e r( d e s t )
0 F F ?
logal Form!
dest count
SHR feg, 1 d at a
SHR ldata
SHR feg, CL
SHR CI
Dolcalptlon
This instruction shifts rhe dest ope|3'ndcount bits to the right. The high-ordcr bits
are cleared to 0 as the low-order birs are shifred.
The co&rt operandmusteilherbe an immediatedatawlue or be storedin register
CL.The 80386masksthe corrrt operand with lFH so that the count lue is never
greater than 31.
If the coar?loperand is 1,the overflow flng is set to the high-order bir of the dest
operand. If co&r?,is greater rhan 1, OF is undefioed.
Flags
OFDF IF Tf SF T AT PF q8
M
a: Th. aO3a6/aO0B7ltrstructlon S.r R.td6nce
Faults
PM R l tm86
12 #SS(0)
13 #GP(o) INT 13 *GP(o)
14 *PF(ec) +PF(ec)
Example3
sHR E C X7,
SHR P T R[ E S P + 8 ] ,C L
UORD
a7
fliE aooa6 goox
S}IRD 80386
Shft Rtght Double A6p/32)
Stmtax
S H R Dd e s t , s , " c , c o u n t
Operatlon
tenp max(courr, 31)
v a l u ee c a t { s . c , d s t )
v a l u ee l a l u e > > t e n p
Logal Form.
src count
SHRo feg, res, ldata
SHRD res, idata
SHRD reg, reg, CL
SHRD reS, CL
D9!calptlon
This inshuction concatenatesth srE operand to rhe dest operand and shifts the
resulting double-$izevalue righr. The low-order bits are srffed in r,/es,
The count opefar.d is maskedwith lFH so that no shift counts greater than 31arc
Flagr
OFDF IF TT SF A AI PF CF
Fault3
RM tm86
12 *SS(0)
13 #Gr(0) rNT 13 *cP(o)
14 .fPF(ec)
Eramplo
flov E A X ,[ 0 0 2 A H ] ; Cet lo|{ ordef ororo
SHRD E A x ,l 0 0 2 E h l ; 64-bjt shjft
2aa
a: th. oo3a6/aoaa7h.l.Etl.n s.l R.l.Em.
srDr 80286/80386
()
Store IDT Reglster
Syntar
SIDTdest
Oporation
d E S t E I D T RL. I I II I
dest+2eIDrR.SASE
Logal Fotm
SIDT NCM
Dg.crlpllon
This instructionwritesthe limit Portionof the IDTRto the de$ memoryaddress
andthe linearbaseaddressof the IDT !o the dwod al dest+ 2.
Flrg!
O! DB IF TT S! ZI Af PF CT
Faulta
RM re86
6 #UDO INT6 *UDO
12 #SS(0)
13 *GP(o) INT13 #cP(o)
14 *PF(ec)
. The undefined op.ode fault only occuB whcn the ds, oPerand is encoded as a leSister'
Example
Sl0T int,tab : Get addfessand limlt of I0T
4g
r||E aoaaEoof,
SLDT 80286/80386
Store LDI Regtster (16)
Syntar
S L D Td e s t
Oporation
dest e LDTR
Logal Formt
SLoT r9
SLoT mem
Dalcrlptlon
This instruction storesthe selectorin the LDTRin the desiinarion location.
Fllgr
OFDF TF TF SI T AF P8
Frultt
PM
6 INT 6 #UDO
12 #SS(0)
13 #GP(o)
14 #P!(ec)
Examplo
SLOT DX i Put LDTselector into 0x
290
a! th. ao3o6rao3a? h.ttuclld S.t i.LHc.
sMsw 80286/80386
(16)
Store Machlne Status Vord
Syntax
S H S Ud e s t
Oporation
Lggal Form3
Sllsll reg
sl4sl{ nm
Dolcrlptlon
This instructionstoresthe low'order16bits of registercRo(the80286machine
statuswofd) in the &st oPerand
Thls instrrrctionis providedfor compatibilityonly Usethe Mov CRoinstrlrctionin
nativemodeprogramming
Flagr
OFDF IF IT 3F A AT FF CF
Faultr
PM RU wo86
6
12 #SS(0)
13 #GP(o) INT 13 *GP(o)
14 #PF(c) *PF(c)
ExamDle
slrslltDrl
231
r|tE aoiFa Boot(
sTc 8086/80186/802a6/803a5
Set Carry Flag ()
Syntax
STC
Opo.atlon
C Fs 1
Lagal Form
sTc
Do.crlptlon
This instruction sets the caffy flag (CF) in the EFLAGSreghter ro 1.
Fl!g!
OFDF IF TT SF U AX PF CF
F.ult!
None,
Er!mplo
sTC ; Caffy ftag set t o l
n2
a: th. aoo06lto5a7 b.lructlon S.t B.lmm.
STD / aora6
aoa6 / ao3a6
/ ao2a6
Set Dlrectlon Flag o
Syntax
sr0
Operation
Log.l Forn
STD
Dslcrlptlon
This instruction set6the direction fleg (DF) in the EFLAGSregister to 1. This in'
strirction indicate6reversedirection in the string instructions tg decrementthe ln_
dex registerswhen DF ' 1,
Flagr
OTDTlF TBSFU AF PF C8
Frult!
None,
Exanpla
STD ; Preparefor reversestring operatlon
29:l
rHE 00446 3|,0x
srl 8086/80186/80285/80386
Set Inteffupt Flag ()
Syntar
STI
Operation
IF e I
Logrl Forn
STI
Dolc.lptlon
This instruction sets the Inreffupt flag (IF) in the EFLAGSregister to 1, enabling
hardware interrupts.
Th eyecuting program must have a high enough privilege (CPl < IOPL) io lssuethe
STI command to avoid a generalprotection fauft.
Fl.gr
OIDTIl TFSFzF AT PF CX
F.ult
R.Lt te)66
13 *GP(o)
Exrmpla
CLI I D l s a b l ei n t e r r u p t s
lrov AL, sehaphore : Get mnofyvalue
DEC AL ; Decrenent countef
,12 DollE : Sklp 1f vatuewas0
l10V senaphore.
AL : Update
DONE:
STI jnterrupts
: Reenabte
4A
ar lh. ao56/ao3a7 h.tEctlon 36t BoLrdc.
sTos 8086/EO186/80286/80386
Store Stalng (a/fip/32t
Syntax
sT0s
Operation
{ h e n o p c o d e1 s ( S T 0 S BS, T o S NS, T 0 S D )s, e t o P s i z et s ( 1 , 2 , 4 )
E5:lEoll e accln
lf (0F- 0) then
EDIeEDI+opsize
E D I+ - E D I _ o P s i z e
Log.l Forma
sTosB; storestringbyte
: Store strln9 rord
ST0SI'I
SToSDr Store strjng double{ord
Dsrcrlpllon
This instruction \erites the current contents of the accumulabor(AL, AX, or EAx, de-
pending on the opcod used) !o the memory location pointed to by ES:EDI.It then
increments or decrmentsEDI by the size of the operand, accordingto the DF bit in
the EFLAGSregister.
If you precede the STOSinstruction with the REPprefix, register ECI(must contain
a count of the number of times STOSis to be executed.This fills memory with the
value in the accumulator,
You cannot use a segmentoverride prefix with the STOSinstruction. The destina-
tion segmentwlll alwaysbe selectdby Es.
Fl.g!
OT DF IT T} SF ? A.T PF C!
Faultt
PM RM Iry'86
12 *SS(0)
13 #cP(o) rNT 13 *GP(o)
85
tHE ao3a6 BOOX
Erample
: C l e a r 1 0 0 b y t e s o f m e n o r yb e g i n n l n ga t l o c a t r ' o n0
t10V EoL 0 : B a s ea d d r e s s
flov ECx, 100 / 4 : Count (in dvords)
XoR E A X ,E A X ; C l e a f a c c u n u l a t o tro 0
cL0
REP SToSD ; Zero menory
295
O: lh. 004t6/6o367 h.tructLon S.r hd.EF.
STR ffi2a6/ao3a6
Store Task Reglstr (16)
Syntax
STRdest
Opor.tlon
Logal Forrra
STR feg
STR
De.ctlptlon
This instruction storesthe task register selecoorin d?Jr,
Flag!
OF DT TF TB ST Z} AF PF CT
Flultt
PM tw86
6 INT6 #UDO
12 #SS(0)
13 *GP(o)
14 *PF(ec)
ExanDle
STR CX ; Store current task's selctor
,,t7
tHC AOa6ABOOK
Syntax
SIJBdert, rl"c
Oporatlon
dest r- dest - sac
Logal Formt
dest trc
SUB reg, ldata
5UB men, idata
SUB reg, reg
SUB aeg, nen
SU8 Dem, reg
Dolcrlptlon
This instruction subtractsthe s/c operand from the d4r, operand and storesthe
resuh in desl
Fhgr
OBDT IF TT SI A AX PT CA
x
F!ultr
PM R.U V$46
12 *SS(0)
13 +GP(o) rM 13 #GP(o)
14 *PF(e)
Eramplc
; 64'blt subtractionoperationEDxrEAx EBx:ECX
SUB EAX,ECX : Lor"orderbits
SBB El]X,[81 ; Hish-orderblts rlth possib]eboffor'r
294
ar fir. 6oga6rioita? In.tructlon 5.t F.lddc.
TEST so86/80186/80286/80386
Test Blts (8/r6p/32)
Syntax
Opgration
ULLedest&rrc
Legal Form!
dest
TEST reg, ldata
TEST r data
'IEST reg
reg,
IEST reg,
TEST NEM, TEg
Dolcrlptlon
This instruction performs a bit-by-bi! AND operation on the t/c and dett oPerands
and discardsthe result,The flag bits, however,are set asthey would be after an
AND instruction,
Fl.gt
OF DF IF 1T SF A A.A PF CF
0 x 0
F ulta
PM nM lm86
12 #SS(0)
13 *GP(o) INT 13 #CP(o)
14 *PF(<)
Examplor
TEST AL, (]FH C h e c lkf a n Yb i t s s e t i r
299 ;
rxE 600t6 BooK
VERR ao2a6/ao3a6
vedfy Read Access (r6)
Syntax
V E R sRe l e c t
Opgration
i f ( a c c e s s i b l e ( s e i e c t )&) r e a d a c c e s s ( i e i e c t ) )t h e n
Z Fe I
Z F e 0
Log.l Form!
.FIF.t'
VERR reg
VERR nen
D9!ctlptlon
This instruction sets the ZF bit in EFLACSro 1 if rhe current procedure can load the
sslectoperand into DS, ES,FS,or GSand can read a lue from the memory seg-
ment without causinga privilege violation.
If th selectoris for a descripior that is not a memory segment,jf the memory seg-
ment is not readable,or if the current procedure does not have a hi8h enough privi-
lege levl to Sain accessto rhe segmenr,VERRclears ZF to O.The VERRinstnrction
does not generatea fault for refeffing to a seleclorthat is invalid; however,a fault oc-
curs if the instruction operand is a memory operand and the operand addressis
in lid.
Nole that this instruction does not check the 'presenf bit of the descriptor,nor does
it check accessat the page protection level (U/S and R/V bits of page table enrries).
Fla93
OFDF IT TF ST ? AI PF CT
Faulta
PM RM VA!86
6 INT6 #UDO
12 *SS(0)
13 #GP(o)
14 #P!(ec)
300
6: lh. ao3a6/ao3a?h.trucrbn 3t nd.Hc.
Exanple
VERR i I O R DP T R[ E B P + 8 ] C h e c ks e l e c t o f o n s t a c k
JZ COIITINUE B f a n c hi f 0 K
STC
LEAVT A n dr e t u r n i f s e l e c t o r i s i n v a l i d
RETF
CONTITUE:
30r
rHE 40396 BOOX
VERW 80286/80386
Veflfy Wrlte Access (16)
Stmtax
V E R l ,sle l e c t
Operatlon
i f ( a c c e s s i b l e ( s e r e c) t )a { r 1 t e _ a c c e s s ( s e i e . t )t)h e n
Z F e L
Z Fe 0
Logal Forns
<e1e.r
VERII reg
v!Rt{
Dercilptlon
This instructionsetsthe zF bit in EFLAGS to 1 ifth cuffentprocedurecanloadthe
selec,operand inlo DS,SS,ES,FS,or CS and can wrire a value to the memory seg-
ment without causinSa pridlege violation.
If lhe selectorb for a descriptorthat is not a memory segmenr,if rhe memory seg-
ment is not writable, or if the current procedure does not have a high enough privi-
legelevel to gain accessto the segment,VERV clears ZF !o 0. The VERWjnstrucrion
does not generatea fault for referring io a selectorthar is inr?lidi however,a fauh oc-
curs if the insuuction operand is a memory operand and the operand addressis
in!?lid.
Note that this i$struction does not check the 'preseni bir of the descdpror,nor does
it check accessat the page protection level (U/S and R/W bits of page rable entries).
Flage
OF DF IF TT SI ZP AF PF CF
Fault.
PM R lwtt6
6 INT6 *UDO
12 #SS(0)
13 *GP(o)
n2
ar th.ll(xta6llosaT h.trctlon alr id@m.
Erample
VERI{ l { 0 R 0P T RI E B t + 8 ] : C h e c ks e l e c t o r o n s t a c k
JZ CoNTItil.lE ; B r a n c hi f 0 K
STC ; Set carry flas
LEAVE ; A n df e t u r n i f s e l e c t o r i s j n v a l i d
RET
C OT ] N U E :
303
tHE O<tO6BOOK
watt 8086/80186/80286/80386
Walt Ur{ll Not Busy ()
Synlax
I{AIT
Legal Forrn
Doacrlptlon
This instruction placesthe 80386into an idle srateunril the BUSY\ pin is iMcrive. If
the BUSY\ pin is inactive when the instruction executes,no idle occurs.The BUSY\
pin is usually connected to a numeric coprocessor,You shouldexecutethis instruc-
tion before any 80386instruction that will accessa value stored by the coprocessor.
If both the TS (task switched) bit in regisrerCRoand rhe MP (monitor coprocessor)
bit are set, a coprocessorfaulr occurs.If the ERROR\pin of rhe 80386is active, indi-
cating 4n unmaskedexception on the coprocessor,a math fault occurs,
Fl.gr
OFDF IF TF SF U AI PT CT
Fault!
PM tw86
7 *NMo INTT *NMO
16 *MFo INT 16 *MIO
Examplo
S t o r ef l o a t i n g - p o 1 nr te s utl
I.IA
IT liajt ior copfocessor to flnlsh
PUSH result Pushthe result onto the stack
CALL fp_print
304
0: lhc AOSaG,!OSaTh.trstlotr 3.t R.l.6nc.
XGHG 8086/80186/80286/80386
Exchang (a/MP/32')
Stmtar
XCHGop1, ap2
Opor.tlon
Lcgal Forrnr
api op2
XCHG reg, reg
XCHG reg, men
XCHG reg
Dorcrlptlon
This instructiongwapsthe contentsof two operands,If eitheroperandis a memory
operard,the busLOCK\signalis heldactivedurin8rhereadandwrite memory
Flagr
OF DT IF TF 3F '!F AI PF CI
Frult.
PM nM tm86
12 *SS(0)
13 #GP(o) rNT 13 #GP(o)
14 #Pr(ec) sPI(ec)
Examploa
XC|IG E A X ,E C X I S*ap IAX and ECX
XCll6 AL, IESI+101 i E x c h a n s eA L | / i t h n e n o r y
305
rrrE 90486Boox
XLATB 8086/80186/80286/EO386
Translate Btte ()
Syntax
XLATB
Operatlon
A L e D S I: E B X + A L ]
Legal Form
XLATB
Doscrlptlon
This instruction usesthe lue ofAL as a positive index into a table located at
DS:EBX.lt then storesthe indexed table byte in Al, replacing the original value.
You can apply a segmentoverride prefix to XLATB so that the lable accesslocarion
will be at EBX + AL in the specified segment.
Fhg!
OFDT IT TF SF A AF PF CF
Fault!
PM R]' VNI]6
12 #ss(o)
13 *GP(o) INT 13 i*CP(o)
14 *PF(O *PF(ec)
Eramplo
LEA E B X ,A Z E _ T A B I L o a do f f s e t o f A S C I I t o E B C o I Ct a b l e
LDS ESL SRC ; L o a ds o u f c e s t r l n g p o i n t e f
LES EDI, 0EST_BUFF ; L o a dd e s t j n a t l o ns t r i n g p o i n t e r
CLD ; setDF-o
Ll: 10058 ; cet byte of source stfjng
C5: ; A s s u n et r a n s l a t e t a b l e f e s i d e s i n C S
XLATB ; Translate byte
5T0SB ; Store fesutting character
0R AL, AL ; Test fof liULcharacter
JNZ Lt : L o o pi f n o t N U L
306
a: the gGt06/AO3a7 h.ttuctld 3.t neLH@
Syntax
Oporation
destedest"src
Logal Forrni
Do.crlptlon
This instruction performs a bit-by-bit exclusiveOR operarion on rhe src and dest
operands, storing the result in the &st operand. The XORoperation is defined as
followsl
0^0-0
0^1-1
1^0-1
1^l-0
Flagr
OFDF IF TF SF ? AF PF CI
0 0
Fsultt
PM RM VWJ6
12 #SS{0)
t3 #cp(o) tNT t3 *cp(o)
Exa|nples
XOR AL, OFFH : C h a n g e0 s t o 1 s a n d v i c e v e r s a i n A L
XOR E B X ,E C X ; c o m p u t eE 8 x e E B X^ E C X
307
TtrE ao3a6 Boox
PROCESSORS
Proc6sors that suq@rt
the instructian.
MI\EMOl\tIC
UsedW ,he asseubler,r
rE)r6ent the instruction.
NAME
Name of irtstru.tian.
rhcbpdrix.k lrrheqxxteisFlcoMethsitkislx ptdxrt$rheomprdgr
LEGAI, FORMS
Iegal forms of the
insttuction.
DESCRIPTION
Description of the
instruction. fitem =
memor! olrerand.
EXCEPTIONS
An 'r" in a botc
indicates that the
sPecified etccePtion m4)
begeneratedfor the
b6tru.ction. A "-" in a
box indic&testhot the
sPecifiedexcePtionis not
possibb. SF= Srackfault.
PE = Precision exception.
UE = Underflou
excePtion.OE =
OLelflow excePtian.ZE
= Zero divide exception.
DE = Denorrrwl
exce?tion.IE = lntolid EXAMPLE
oPelqtion etcception. Eqch etaampleshous the
80387 stack beforeand
after execution ofthe
iftstluctton.
304
0: flF 4o3a6i60367 ltr.trucrld s.t hGlsrcne
FABS aoa7/ao2a7/ao3a7
Abolute Value
Legal Form
FABS : If (ST < 0) then 5T e ST * -1
Dloriptlon
This instructionreplacesthe or8inal valueof the elementat rhetop of stackwith irs
Excoptlon.
SF PE I'E OE A DB IB
Exampl.
Before
ST -3.71 ST
FABS
309
fl{E AO3a6 BOOX
FADD aoa7/ao2a7
/ao3a7
Addttion
Lggal Forma
FAoD ; S T ( 1 )e S T+ S T ( 1 ) :p o p ( ) l
FADo nem32 : STe ST+ mem32
FA00 mem64 : ST F ST+ nem64
FADD 5T(r) ; STi- ST+ sT(n)
F A 0 0 S T ,S T ( n ) | S Te S T+ S T ( n )
F A 0 0 S T ( n ) ,S T I S T ( n )e S T ( n )+ S f
F A 0 0 PS T , S T ( n ) I S T S T+ S T ( n ) :p o p ( ) l
F A D D PS T ( n ) ,S T : S T ( n )t s S T ( n )+ S T rp o p ( ) l
Do3crlptlon
This instruction adds the specjfied floating-point operands and optionally pops the
top ofstack,
If you specify a memory opemnd, ir is converredto temp re l (80-bir) former before
it is added to the top of stack.
If you add a floating-point value to infinity, the result is the original irfinity, If you
add two infinities, they must have the samesign, and the result is the sameirfinity,
Ercopllona
SF PE UE OE 2IE DE IE
x x x
Examplo3
ST 4.66
sT(1) o,2l ST 4.47
sT(2) 13.00 sT (1) 1300
FADD
3to
ar rrE aoOaosottT h.rrE$d br nd.He
ST 4.6 ST 4.6
sT(1) o.2L sT(1) 0.21
sT(2) 13.00 sT(2)
FADD ST(2), ST
3tt
tHE A036 BOOK
FBLD aoa7/ao2a7
/ao3a7
BCD Irad
Legal Fortr
FSLD m e m 8 0 ; p u s h ( f l o a t ( n e n 8 0)
Doicrlptlon
This instruction convertsan 80-bx, l9-digit BCD integer to a temp reai and pushesit
onto the stack.If the memory operand is not a !?lid BCD integer,an undefined
value is pushed onto the stack.
Exceptlon.
SF PE UE OE ZIE DE IA
x
Er.mplo
ST 17.00
ST 102.04 sT (1) 102.08
IBLD IESI]
ESIpoints ro 17 BCD.
312
a: tho aO3g6/AOa8?
hniucrtd acr ndcrenco
FBSTP aoa7/ao2a7/ao3a7
BCD Store and Pop
legal Form
FBSTP nem80 | n e n 8 0e B C D ( S T ) p
: op():
Do.c.lptlon
This instruction rounds the top of stack to an integer,storesin memory in BCD f(f-
mat,and then popsthe stack.
Unlike most arithmetic operations, FBST?signalsrhe invalid (I) exception if eittrer
opemnd is a quiet NaN,
Excoptlona
9I PE IJE OE 4 DB IB
Examplo
ST 3.09
sT (1) s]'
FBSTPIOA2FI]
313
tlll00t86 BooK
FCHS ao87/ao2a7/ao3a7
Charge Stgn
Logal Forn
FCHS ;sTesT*_1
Doacription
the signbii of the top of stack.
This insrructioncomplements
Excaptlon.
SF PE I'E OE ZlE DB IE
Exanple
5T -to2a.9
sT (1) 5.2001
ICHS
314
a! lh. 00306/003t? h.rrFtro Lr R.r.retrE
FCLEX aoaT
/ao2a7
/ao3a7
Clear Excptlons
Logal Fornr!
FCLEX : SHeSx&07F00H
FliCtEx I Sl,leSf &07F00H
Dosc Dtlon
This idstruction clearsthe exception flags in the statusword and the busy bit to 0.
The FCLEXform of the instnrction checks for unmaskedexceptionsfrom previous
operations before clearing the statusword. The FNCLEXform clearsthe S\7 bit
without checking.
Excgptlonr
SF PE IJB OE U DD IE
3r5
tHE ao3a6 BOOK
FGOM aoa7/ao2a7/ao3a7
Coltrpafe
Legal Forms
FC0tl c o n P a r es T , s T ( 1 )
Fcoll mem32 c o n P a r e( s T , m e m 3 2 )
Fcoli nrem64 c o m p a r e( s T , m e m 6 4 )
Fcoll ST(n) c o n p a f e( s T , s T ( n ) )
FCoHP mem32 c o n p a r e( S I , n e n 3 2 )
c o r n p a r e( S T , m e m 6 4 )
FC0I4P5T(n) compare ( S T , S T ( f) )
c o n p a r e( s T , s T (1 ) )
Doscription
This instruction performs thc ftl/].cti(mcomparc (op-l,op2) and scrs the 80387con-
dition code accorcLing k) Ihc resuhoffie comparison. Thc 80387$tackis optionaliy
Popped once or twice.
'l
he following ublc showsthe condition code setrirytsthar result from the compare
frnction. FCOMconsidcrs+0.0and-0.0 to b equal.
Con Utton Ca C2 Cl @
apl>at)2 0 0 0
0 0 1
l 0 - 0
citbc..'pis a NaN 1 1 - I
FC0l{ op : F l o a t i n gp o l n t c o m p a r e
FSTSII AX : Store status tofd to AX
; S t o r eA Ni n t o f l a s s
You canthenuseanycondilionaljump instruction(Jts,JNE,lA,JAn,JB, orJBE)1o
branch on the result of the compare.You can useJP to test for NaN opcrands.
U.like mostarithmeticoperations,FCOMsignalsthe invalid(I) exceptionif either
operandis a quiet NaN.
Exceptions
SFPEUBOBZDEIE
3t6
a! th. ao3a6Eoa87 lndrEtlon !.t R.l.mo
Examplea
ST 21,.0
sT(1) ST
ST Q) 0.1114 ST
FCOM ST (2)
Before
-21.0
sT(1) 6.0
sT(2) 0.1114 5T 0.1114
!COMPP
317
tHE 004o BooK
FCOS 80387
Coslne
Legal Form
FCoS ; STecos(ST)
Doscrlptlon
This instruction computes the cosine of the \.?lue in radiansat the lop of stack and
replacesSTwith cosine.
The operand processedby Fcos must be a %lue betweeo t 263or the instnrction
does nor execureand conditioo code C2 is set to 1. C2 is cleared io 0 if the lnstruc-
tion is executed.
Exceptlona
SF PB I]B OB ZE DE IB
x
Erlmplo
Before
ST 0.785399
sT (1)
3ta
a! the gctlt6ia(x|aT In.lrFtlotr 8.t i.t.6n4
FDECSTP aoa7/ao287/ao3a7
Decrernent Stack Polnter
Logal Fo?m
FDECSTP : ToPe (ToP_ 1) & 07H
Do.c.lption
This instructionallows'nu to manipulatethe 80387stackpointe. IssuingFDECSTP
is equivalentto pushinga newvalueontolhe stack,but no valr.re
is supplied.The
tagregistersarenot modified,
Ercoptlonr
SF PB IJD OB 4 DE IE
Ex.mplo
Befofe
ST
ST 4.201 sT(1) 8.201
sT (1 999.9 sT(2) 999.9
FDECSTP
3t9
fl{E ao5a6 BOOI(
FDIV aoa7/ao2a7/ao3a7
DtYtsion
Legal Forma
F DI V S T ( 1 )e S T ( 1 )/ S T rp o p ( ) ;
F DI V men32 ST ST / nen32
FDIV men64 ST ST / hem64
F DV I sT(n) sTF sT / Sl(n)
F DV I S T ,S T ( n ) S Te S T/ S T ( n )
FDIV s T ( n ) ,s T s T ( n )e s T ( n )/ s T
FDIVP S T ,S T ( n ) S Te S T/ S T ( n ) !p o p o ;
FDIVP S T ( n ) ,5 T S T ( n )e S T ( n )/ S T : p o pO ;
Dolcriptlon
This inskuction executesa divide operation with rhe above operands.If )iou
specify a rnemory operand, ir b converted!o temp real (80-bit) format befcre the
division is performd.A stack pop operarion is performed if specified by the
opcode.
Division by infinity results in 0. Infinity divided by a feal nunber results in infinity,
Infinity divided by infiniry is not a valid operation.
Exceptlon!
SFPBUEOBUD!IE
x x
Examplog
ST 4.0
sT(1) 0.4
sTc) 5.0
!Dry
3N
0! lha 0o006lgxtaz hdt@tlor 3.t lt t.mo
ST 4.0 4.0
sT(1) 0.4 sT(1) 0.4
sT(2) sT(2) t.2,
FDIV ST(2), ST
321
IHE aoa6 BooK
FDIVR aoa7/ao2a7/ao3a7
Dlvblon Reve$d
Logal Forn!
FoM S T ( 1 )e S T/ S 1 ( 1 ) ;p o p o :
FDM rnen32 STe nen32/ ST
FDIVR mem64 5T e nen64/ ST
FoIVR ST(n) 5Te Sl(n) / ST
FDM SI, ST(n) S Te S T ( n )/ ST
FDM S T ( n ) ,S T S T ( n ) S T/ ST{n)
FoMP 5T,ST(n) S Te S T ( n )/ S T ;p o p o ;
FDMP sT(i), ST S T ( n )+ , 5 T / S T ( n ) rp o p( ) ;
Deacrlptlon
This instruction executesa divide operation wirh rhe above operands.This instruc-
tion is equivaleft to mIY but the divisor and dividend operands are exchanged.If
'ou specify a memory operand, it is convertedto temp real (80-bit) format before
the division is performed. A srackpop operation ts performed if specified by the
opcode.
DMsion by infiniry results in 0. Infinlty divided by a real number results in infinity,
Infinity divided by infinity is not a valid opr4tion,
Erceptlonr
SF PB I]B OB 2'E DE IE
x x
Eramplo!
ST 4.0
sT(1) 0.4 ST 10.0
sT(2) 50 sT (1)
FDI\,'R
322
a! lh. ao35/aoca7In.ttuction 361R.t r*c.
ST 4.0 ST 4.0
ST(1) 0.4 sT(1) 0.4
sT (2) sTc) 0.8
FDn? ST(2),ST
323
tltE 8035 BOOX
FFREE aoa7/ao2a7/ao3a7
Free NDP Register
Logal Forn
FFREE ST(n) ; Tl,l(i) e UIiUSED
Deacrlptlon
This instruction marks th specified stack element as unusedby setting the tag
s/ord ior th corresponding80387register.The stack pointer is not modified, no( is
fie actual content of the NDP register
Exceptlons
SFPEUEOE4DEIE
Examplo
ST 190000.3 ST 190000.3
sT(1) sT(1)
sT(2) 0.001 sT(2) 0.001
!FR!E ST(1)
324
a! th. 60lE6rao:ta7 h.tretlon l.t R.lm|E
FIADD aoa7/ao2a7/ao3a7
Intgei Addldon
Legal Form!
FIA00 nem16 : ST F ST+ float{nen16)
FlA00 nen32 ; SI e ST+ float(nen32)
Delcrlptlon
This instnrction converts the !flo's complementlnteger at the specified addressto
temp real foimat and adds it to the top of stack.Other than the difference in
operand ttpe, this instnrction is equhdent to FADD.
Excoptlon3
SF PE I,E OB U DE IE
Exrmplo
Before
t7,6 ST
sro) 0,333 sT (1) 0.333
3
tHE AO35 BOOK
FICOM aoaT/ao2a7/ao3a7
Integer Compare
Leg6l Forr s
FlColl men16 | compare(ST, neml6)
Flcoll nen32 | conpare(ST, mem32)
FICOI{Pnen16 : conpare(ST, neml6);pop()l
FIco P mem32 : conpare(ST, nem32):pop() |
Do3crlption
The two's complementinteger is converted !o iemp real format and comParedwith
th top of stack. If the opcode is FlcoMq the stack is popped after the comparison
The condition codes are set in the samemanner as those for FCoM
Excgptlon.
SFPEUEOB4DEIE
x
Examplo
Befo!
ST 6.0
sT(1) 13792.29731 ST t37922973r
326
& th. aot8a/lota? h.truc$.r &t Ad.h.o
FIDIV aoa7/ao2a7/ao3a7
Integef Dlvlslol
Logal Forrng
FIDII/ mem16 r STe ST / rest(nen16)
FIoIV nem32 : ST ST / real(mem32)
Dgacrlptlon
This instruction fetches the t$/o's cornplementinteger from memory, converts it to
temp real furmat, and usesit as a divisor of the top of stack.The results generared
by thi6 instruction ar the sameas rhosegeneratedby $e FDMnstrucrion.
Excoptlon!
SFPEUBOEADEIB
x
Errmpla
ST 1.0 ST -0.25
sTo) 2.2 sT (1) 2.2
327
tfiE 503A6 IOOK
FIDIVR aoa7/ao2a7/ao3a7
Integer Dft'lslon Reversd
Lcgal Fotma
FIDIVR nenl6 ; s T e r e a l ( m e m l 6 )/ S T
FIDM m e n 3 2 : S T t s r e a l ( m e m 3 2 /) S T
Do.criplion
This instruction convertsthe two's complementinteger at th sPecifiedmemory
locarion to temp real format and divides it by the top of stack The results generated
bv this insrruction are lhe sameas those generatedby the FDIVRinstruction
Excoption!
3F PB UE OB Zts DE IE
x
Examplo
Before
-4.0
sT(1) 2.2
nDM D\{/ORDPTR[EBP+161
Memorypointer is integer-4.
324
a! lh. aoat/4o367 Inatructtd a.t a.i.r6c.
FILD aoa7/ao2a7/ao3a7
Integer Irad
Legal Forma
FILD meml6 ; push (float (meml6)
FILD mem32 ; push (float (mem32)
FILD mem64 : push (float (mem64)
Descrlptlon
This instruction converts a two's complementinteger to temp real format and
pushesthe vaiue onto rhe 80387stack.
Excoptlohr
SI PB I]E OE Z DE II
Eramplo
Befo.e
666.0
ST 1.2@ sT(1) 1 209
329
tHE 603A6 aOOK
FIIIUL aoa7/80247/ao3a7
Integer Multlpllcatlon
legal Form3
FIIIUL meml5 ; STe S T * r e a l ( m e m 1) 6
FIITUL nem32 ; ST t s S I * r e a l ( m e n 3)2
Delcfiptlon
This instruction converts the t\to's complementintegpr at the specified memory
location to temp real format and multiplies it by the oopof stack.The results of this
instruction are identical to those obtained by FMUL.
Exceptlona
SF PE I.'E OE Z DB IE
Examplg
Befo.e
ST 0.16
't7.9
STO)
Memorypointer is integer-4.
330
* th. 0oata/6o367 l..r.Erlo a.t tr.Lm..
FINCSTP aoa7/ao2a7/ao3a7
Increment stack Poldter
Log.l Form
F I n C S T P ; T o P F ( T o P + 1 )& 0 7 H
D9lcription
This instruction incrementsthe TOP field in the 80387sratusword. The conrentsof
the 80387register previously at the top of stack and th regisrer'sassociaredtag
word are not affected.
Exceptlont
SFPBUEOBADBIB
Exanple
ST 72 32
sT (1)
331
fl{E A0366 SOOX
Ftl{tT aoaT/ao2a7/ao3a7
Inlttallze NDP
Lggal Form!
FINIT ; Cl{e 037FH: TI e oFFFFH
S}l sl'{& 4700Hr
FNINIT ; C l i e 0 3 7 F H rS l , le s l , J& 4 7 0 0 H :T I e o F F F F l i
Drcrlptlon
This instruction sets the 80387machine stateto its default value All registersare
marked unused, all exceptions are masked,rcunding conhol is set to nearest,and
the operating ntode is set to double-precision
The FINIT instruction testsfor any unmaskedexceptiod in the 80387before clear-
ing the NDP state,unlike FNINI! which does not codsequently, the first floating-
poin! instruction of an application shouldbe FNINIT
Erccptlona
SF PB I,E OE A DB IE
332
Ai th. AO3A6/0Oaa7h.truc{on a.t iGtcftre
FIST aoaT/ao2a7/ao3a7
Integef Stofe
Legal Forfts
FIST menl6 m e n l 6e i nt( ST)
FIST ne'n32 n e m 3 2e i nt(ST)
FISTP mernl6 m e n 1 6e i n t ( s T ) : p o p () :
FISTP n'en'32 m e m 3e2 i n t ( S T ) , p o p () ;
m e m 6e4 i n t ( S T ) : p o p () |
Deacrlptlon
This instruction rounds the currenr top of stack to an integr accordingto tlle on-
trol bits and storesthe lue in the specified operand. Ifthe opcode js FIS'P, the
stack is popped afrer the store operatlon. Note that rhe sign of a floating-point 0 is
iost upon conversionto th two's complementinteger format,
Two differences exist between FIST and FISTP.The FISTPinstrucrion, which Dops
rhe sLac k afrerrhe sroreoperarion.canitore a 6{ -bit inreser:FISTcannor Thc FIIT
instrlrction generatesan in lid operarion exceprion if the bp of stack is a quier
NaNi FISI? does nor.
Excoptlon!
SF PE !'E OE U DE IE
x *
Ex.nplo
ST 32.1 ST 32.1
sT(1) 456.78 sT(r) 456.78
IIST DTi{ORDPTR[EBP+421
Integer 32 stored into memory.
333
lHE got96 BOOK
FISUB aoaT/ao2a7/ao3a7
Integer Subtractlon
Logal Forms
F1SUB meml6 : 5T e ST real(nen16)
FISUB nen32 : sT e Sr - real(mem32)
Descriptlon
This instruction convertsthe two's complementinteger at the specified memory
location to temp real format and subtractsit from the lop of stack.The rsults of this
instruction ar identical to those obtained by FSIJB.
Excgptlons
SF PE UE OE ZTE DB IB
x
Er.mplo
Before
ST a6.99 ST 33.99
sT (1) 0.0 sT (1) 0.0
FISIJBWORD PTRIA72HI
334
ar th. 6oad6/ao3a7 Instructlon 3.t h.tdE.
FISUBR aoa7/ao2a7/ao3a7
InGger Subt.actlon Rversed
Legal Forms
F I S U B R m e m 1 6 : S T e r e a l ( m e m 1 6 -) S T
F I S U B R n e m 3 2 : S T e r e a l ( m e m 3 2 -) S T
Delcrlptlon
This instruction converts the two's complemntinregerat rhe specified memory
location to temp real format and subrractsthe top of slack from ir. The results of this
insrucrion are identical to those obtained by FSUBR.
Erceptlons
SF PB IJE OE ZE DE IE
x
Example
Before
ST 36.99 ST -3399
sT (1) 0.5 sT (1) 0.6
'WORD
FISUBR PTR[A72H1
Memory pointer is integer 3.
335
tflE 60(|a6Eoor
FLD 8087/80?87180387
Irad Real
Logal Forms
FLD nen32 : push(mem32)
FLD nem64 ; push(mem64 )
FLD neng0 : Push(men8o)
FLD ST(n) : p u s h ( S T () n )
Do3ctiption
This instruction pushesa copy of the specified operand onto the 80387stack.If you
specify a 32-bit or 64-bil floating-point memory operand, it is converted to temP
real format before being stored,
lf the operand is a single- or double-precisionvalue, the 80387might generatea
denormal exception. A dnormal exception is not Seneratedby a value already in
temp real format.
Exccptlon!
SB PE IJE OE U DE IB
x
Examplg
ST
ST t9a62.o ST(1) t9362.0
sT (1) 7.11 sT (2) 7.11
336
a! th. aooa6/00307Instruclion a.r Bd4cnc.
FLDconst aoa7/ao2a7/ao3a7
ut dlconstan
Logal Forn!
FL01 ; push(I.0)
FLDL2E ; push(I og2(e))
FLoL2T ; push(los2(10))
FLoLG2 : push(1os10(2))
FLDLN2 : p u s hI (n (2 ))
FLDPI ; p u s hP( I)
FLDZ ; push(+o.o)
Deacrlptlon
This instruction pushesthe consranr lue spccified by the opcodc ono the stack.
The function ln standsfor log base e
Ercoptlona
Sf PE I]B OE ZA DB ID
Exanrplo
ST 3.141196...
ST 4.0 sT (1)
FLDPI
337
fl{E a0388 BOOK
FLDCTI' aoa7/ao2a7/ao3a7
Load cnntrol Word
Legal Form
FLDCl,l meml6 ; Cl'l e nenl6
Descriptlon
This instruction loads a new value for lhe control word from memory. FLDcv can
unmask previously maskedexceptions,triSgering an unmaskedexception
Excepilons
SFPBI]EOEADElB
x
334
a! th. O366/aO3A7h.t ucitd 3!t R.t@nc.
FLDENV EO87/80287/80387
Load Envhonment
Logal Fo.m
FLDENV ,renp ; NDPe memp
Doacrlptlon
This instruction loads the 28-byte blok pointed ro by memp into t}te envhonmedt
registersof the 80387 The memory operand contains a new control word, sratus
word, tag word, and effor block. The memory format for the environment is shown
in Figure8-1,
3r 16 15 q Blte offset
0
4
8
Eror offser(EIP) L2
Inor sleclor(cs) 16
20
32-bit format
Byteoffset
0
2
4
6
lPro r 8
l0
9P,cui t2
16-bitformat
Blgl'fe A-L a0387entronmen'
Excoptlon3
SF PE IJE OE ZB DE IE
339
fllE ao3a6 aooK
FMUL aoa7/ao2a7/ao387
Multtpltcatton
Legal Forms
FI'{UL S I ( 1 ) e S T ( 1 )+ S T ; p o p ( ) ;
FHUL nen32 SIeST*mem32
FI1UL nen64 STeST*mem64
Fl'lUL s T ( n) ST6ST*ST(n)
Fri4UL S T , S T ( n) STeST*ST(f)
FtlUL ST(n),ST ST(n)eST(n)*ST
FI'1ULP ST, ST(n) sT e sT * sT(r): pop():
F|IULP S T ( n) , S T s T ( n ) e S T ( n )+ S T ; p o p o ;
Doscrlptlon
This instructbn ,nultipliesthe specifiedoperandsand storesthem asindicated
above. If't'ou specify 32-bit or 64-bit memory operands,they are converted to temp
real format before the multiplication takes place.If the opcode specifies,the stack is
poPpedafterthe oPeration.
Multiplyinganyvalueotherthan0 by infinity resultsin infinity.Multiplying0 by in-
finity is an in\"alidoperation.
Ercaptlon!
SFPBUEOE4DEIE
Examplo3
ST 2.O
sT(1) 0.0:t 5T o.o2
sT(2) 7.6 sT (1) 7.6
FMIJI,
tro
0! th. to36/a(l!t7 rBttucito a.l i.t.Erc.
2.0 ST o.o2
sT(1) 0.0r sT(1) 0.01
sT(2) sT(2)
FMU! ST(1)
34t
tHE ao3a6 BOOr
FNOP aoaT/ao2a7
/ao3a7
No Operatlorx
Legal Form
FNOP
Doscrlptlon
FNOPis an alias for the FSTST,ST instruction. It does nothing.
ExcoDtlon3
ST PE UE OB 2ts DI IB
Er.mplg
Before
ST 3.3 ST 3.3
sT (1) r9.6 sTo) 19.6
rNOP
g2
a: th. O306/aO3A7h3ttuctlon aet BcLmc.
FPATAN aoa7
/ao2a7
/ao3a7
Partlal Arctangent
Legal Fo.m
FPATAI1 : S T ( 1 )e a t a n ( S I ( 1 )/ S T ) ; p o p ( ) ;
De3crlptlon
This instruction computesthe arcrangenrin radiansof ST(1)+ ST The mnemonic
"partial arctangent"is inherited from
earlier NDPS,which placed restricrionson the
\,?luesof ST and ST(1).These \dues are not testricted on the 80387
Exceptlons
SF PE UE OE ZB DE IE
Ex!mplo
ST 2.0 0.4616...
sT (1) 1.0 sT(1) 1,0
FPATAN
34:l
tl|E ao:[6 EOOX
Legal Form
FPREI'I ; s T e r e n a i n d e(r5 T / S T ( 1 ) )
Doscription
This idstfl.rctionusesrepeatedsubtractionsto compute the remainder of STdMded
by sT(l). Becausethis operation could require 4 large number of itetations (during
which time the NDP would be inaccessible),the 80387halts after producing a par-
tial remainder.The 80387reducesthe value in STby a faclor of up lo 2@in a single
If the remainder is a partial value (that is, the operation does not complete), the C2
statusbit b set to 1. If the remainder is lessthan the lue of ST(1),the operation is
complete and bit C2 is cleared to 0. By testing the lue of C2, the FPREMinstruc-
tion may be executedrepatedlyuntil the remainder operation yields an exact
result.Mditionally, when the insta.rctionis complete (C2 - 0), the three leastsig-
nificant bits of the quotint of ST+ ST(1)can be computed by the following formula:
e!C0x4+C3x2+Cl
where C0, Cl, and C3 are ihe remaining statw bits.
The FPREMinstruction reducesoperands for the kanscndentalfunctions of the
-1
80387to legal lues. For e,xamPle,the operand to F2xMl must b < ST< 1'
FPREMproduces an exact result, and th Precisioncontrol and rounding control bits
are ignored durinS execution,
The FPREMIinstruction producesthe IEEE-754standardPartial remainder lalue'
which may be different from FPREMwhen ther are two integersequally close to
ST+ ST(1).FPREMrounds toward 0, and FPREM1choosesthe even value
Excoptlona
SFPBUBOEADEIE
344
0! lh. aooa6/ao3Ott[tructton 3.t h.f.r.rc.
Example
ST 6 ST 2
sT (1) 4 ST(D 4
FPREM
C2-0
3t5
IHI OO3a6BOOX
FPREMI 80367
IEEE Parttal Rematnder
Logal Fonr
FPREIiI ; S T e r e n a i n d e r( S T + S T ( I ) )
Dacrlption
This instruction usesrepeated subtractionsto compute the remainder of STdivided
by ST(1).Becauserhis operation could require a large number of iterations (during
which time the NDP would be inaccessible),the 80387halts afier produ.ing a par_
tial remainder The 80387will reduce the value in STby a factor of up to 2s in a
singleiteration.
If the remainder is a partial v3lue (that is, the operation is not complete),the c2
statusbit is 6ct to 1. If the remainder is lessthan the value of ST(1),the operation is
completeand bit C2 is cleard to 0. By testjng the value of C2,the FPREM1instruc_
tion may be executedrepeatedly until the rmainder operations yield an exact
result. Additionally, when the instruction is complete (C2 ' 0), the three lea$ sig_
nificant bits of the q.Dlient of ST+ ST(1)can be computed by the following formulal
e=C0x4+C3x2+C1
whefe C0,Cl, and C3are the remainingstatusbits.
The FPREM1instruction reducesoperands for the transcendentalfunctions of the
80387to legalvalues.For o<ample,the operand to Fr(Ivfl must be -1< ST< 1.
FPREM1always producesan xact result, and th Precisioncontrol and rounding
control bits are iSnoredduring xecution,
The FPREM1instruction producesthe IEEE-754standardPartial remainder value,
which may be different from FPREMwhen there are two inteSersequally close to
ST+ sT(l). FPREMalwaysrounds toward 0, and FPREMIalsr'ayschooseslhe even
ExcaDtion3
SF PE UE OE ZTE DE IE
346
! lh.00iro6/0o3tt rDrruc on3ttd.E*.
Exaanplc
Before
ST 6.0 ST 2.0
sT (1) 4.0 sT (1) 4.0
IPREMl c2-0
347
rHE aota6 loo(
FPTAN aoa7
/ao2a7
/ao3a7
Partlal Tangent
Lggal Form
FPTAII : S T e t a n ( S T ) :p u s h ( 1 . 0 ) :
Description
This instruction computesthe tangentof the top of stack and arrangesthe NDP
stackswh that:
Exceptlonr
SF PE UE OE 2I DE IE
x x
Examplo
Before
ST 1.0
ST 0.78549... sT(1) 1.0
sT (1) b.2 sT(2)
FPTAN
344
& th. ao3a6/4o307 lntlrFtlotr l.t i.lmm.
FRNDINT aoa7
/ao2a7
/ao3a7
Round to Integer
Log.l Forn
FRllDIl{T ; STe lnt(ST)
Drcriptlon
at the top of stack to an integer basedon the set-
This instnrction rounds the %%%%%%%%%%%%%%%%lue
tings of the round orltrol (RC)field in the control word. see chapter 2 for a discus-
sion of the 80387rounding modes.
Exceptlon3
ST PE UE OE 'IE DE IE
x
Ex!mplc
ST 1.06 ST 1,0
sT (1) 601 sT (1) 60.1
FRNDIM
349
it! aota6 BooK
FBSTOR aoaT
/ao2a7
/ao3a7
Restore NDP State
Lggal Form
FRST0R ,eDp
D6!cription
This instructioo loadsthe entire 80387proces.sorstate from the 108-byteblock of
dat^ begir.ni,..gat rnenp. Use the FSAVEinsrruction to siore rhe NDP state.F8ure
8-2 showsthe format of the state block.
t5 Btte offse!
0
2
4
Instrucrionpoinrero.,1
lPn 8
t0
Ji OP* 12
Sn0) o a
Registef sT(o)v 6 18
sroo r I sr(0)61, 22
sT(1)16.'
sr(l)- * 30
sT(2)or 34
sT(2)3'. 6l 3A
sT6)o s ST(2)d ,o 42
sT(3)16.
r
sT(3)4 '
sT(4)o.r
sT(4)' 6
sT6)0. rr ST(4)& D 62
70
sT(6)0 i
sr(6b 6l 7a
sT(7)oE I sT(6/... a2
STOL .i7 86
STO)o r t9
16'bit fornat (real & v86 modes)
350
6r licocla6iaoat? h.ttuctlols0t i.t dc.
fic'rJKB'}2. cohhnued
a1 15
0
4
8
Enor ofist (ElP) t2
16
20
24
sT(0)or 2a
Regisler
sT(r)b.
E I Srto\.- 36
5T(1)16. {7 40
ST(1)4 rs
ST(2)or 4A
STQ)] ..63
STe). s I sT(z)ar 56
ST(3)rso o0
sT(3) it
sT(4)r, 61 72
sr(5)o.x I s!11&..L 70
5T(5)rr..o 80
ST(5)a..re 84
sr(6)0. ,1 88
5T(6), 63
sro)^ ( | sT(6)6{, 96
100
104
32-bl!forma!
New unmaskedexceptions miSht be triggered bec?usea new statuswold and con'
trol *!rd are loaded.
Ercaptlona
ST PB I]B OE Its DE IE
351
IHE AOiE6 EOOK
FSAVE aoaT/ao2a7/40387
Save NDP State
Legal Forms
FSAVE Derrp ; menpe NoP
FNSAVErerp I mempe rilDP
Description
This instructionstorcsthe completeprocesrorstareof the 80387in memorybegin-
ningar locationmempFigure8 3 showsrheformatof the stareblock.
L5
0
2
4
6
8
10
31 oPr,srql \2
sr(o)or 14
llJ
sTo),," sr(0)i" 22
26
sT(1)4d ,q 30
sr(2)o l 34
ST(2)r' A 38
Sll3)u r( | S T ( 2 ) L.i D 42
sr(3),6'
ST(3)4 rq 50
sr(4)on 54
sT(4)r,e 58
sr{5)or I sT(4)&- 62
sT6)i6,
sT(5)$., 70
sT(6)0 l
ST(6h or 7A
sT(7)o D ST(O6a.' 82
sT(7)j6 t 86
sr(7\s 7e 90
16 bil forma! (real & v86 modet
352
a! lho ao3a6Eo3t? ltr.tructlo. 3.r Rdd.nc.
3\ 16 1 5
0
4
a
Errcr ofTsel(EIP) t2
i6
20
24
sT(0)o 31 2a
sr(oh 6. 32
sT(l\.6 | sT(o)d 19 3o
sT(1),6.4? 40
sr(t)s..'!
sT(2)d{ 48
sT(2)e:.o:
sTa3)^ ,. 5T(2)d i
STG)16 o 60
STc)as D
sT(4)d 1 68
ST(4)r .6' 72
sT(5)o ,t I ST(4)d D
sT(5)rr.I 80
a4
sT(oo 4 88
sr(6):rr: 92
sT(7)oF I S16to,, 9b
STO)rao 100
sT(7)ls.., 104
32-bitlormat
After the FSAVEis completed,the NDP stateis set to the initialized state,as if an
FNINIT instruction had been executed,
The FSAVEform of the instruction testsfor any unmaskedexcePtionsbefor execut-
ing the save,while FSAVEdoes not. If you use FSAVE,pending e\ceptions are re-
instatedwhen the statblock is loadedby an FRSTORinstruction. FSAVEis not
executeduntil previous floating-point instructions complete
Exceptiona
Sf PE T',E OE A DE IE
353
rrrE 0006gooK
FSCALE aoa7/ao2a7/ao3a7
Scale by 2"
Lg.l Form
FSCALE : sT e sT * 2inr(sr(r))
Degcription
This instruction scalesthe rop of stackvalue by the povr'erof 2 in ST(1).If the value
in ST(l) is not an integer,ir is ',chopped" before being used as an exponent. chop-
ping generatesthe nearestlnteger smaller than the orlginal value.
The 80387does not perform a muftiply operation, but it usesthe identity (.t x 2n)
(1.0x 2n) - r x 2n+mand adds the integral porrion of ST(1)to rhe exponent of ST.
Excgptlon!
SF PE IJE OE U DE IE
x x x
Exgmplo
Bfore
ST 1,0 ST
sT(1) 3.01 ST
sT(2) 92.6 ST
FSCAI
354
& th. O@46/aO3t7 tn itucllon 3.r R.lf,.nc'
FSETPM ao2a7/ao3a7
st Protectd Mode
Logal Form
FSITPI'1
Dtcrlptlon
This instnrction performs no operatlon on the 80387 It is required on the 80287to
signalthat the ciu i6 entering protected mode and is supporrcd for compatibility
only.
Excoptlon!
STFEUDOEUDEIE
355
IHE ao3a6 BOOX
FSIN 80387
Slne
Legal Forrh
FSIII ; S Tt s s i n ( S T ) ,
Doscription
This instructioncomputesthe sinc ofthe rop of stackand sroresrhe resultin ST.
Thc value in STis assumedto be in radians.
The input operandto FSINmustbe a valuesuchthat I ST | < 263,or no operation
takes placc and the C2 condjtion code is ser ro 1. If the operand is a legal value, C2 is
clearedto 0.
Excoptlona
SFPEUEOEUDEIE
Exgmplo
ST 3.14159.. ST 0.0
sT (1) 88.6 sT (1) 88.6
FSIN
354
a! th. 6035/10367 h.ldcil{ a.t nd.@@
FStI{COS 40347
Slne add Coslne
Legrl Fo.m
FSINCoS i t e m pe S T , S T e s l n ( t e n p )
: p u s h (c o s( t e m p ))
Deacription
This insrruction computesboth the sine and cosine of the top of stack,although the
lues might be lessprecise than those generatedby FSINand FCoS.The lue in
ST is assumedto be in radians.
The input operand !o FslNcos must be a value 6uchthat I sT < 263or no open-
tion takes place and the C2 condition code is set to 1. If the operand is a legal lue,
c2 is cleared to 0, the top of stack is the cosine lue, and ST(1)containsthe sine.
Excoptlon!
SF PE I'B OE A DE IE
x
Examplo
Before
ST -1,0
ST 3,14159... sT(1) 0.0
sT (1) 88.6 sT(2) 88.6
ISINCOS
357
tHE 036 BOOi
FSORT aoaT/ao2a7
/ao3a7
Squar Root
Legal Form
FSQRT ; S Te s q r t ( S T )
Dorcrlpllon
This instruction replacesthe top of stack with the squarercot of the original lue.
Taking the squareroot of a negative lue results in an invalid operation, except
that the squareroot of oegativezero (-0.0) is defined as-0-0. The squareroot of in,
finity (positive) is defined to be infiniry.
Ercoptlon!
SFPBUEOEUDEIE
ExanrDlo
Bfore
354
4 lho 00306/00'A7 h.rrsflotr l.t B.lmm.
FST aoa7/40247/ao3a7
Store Floatlng Polnt
Log.l Formg
FST men32 men32e ST
FST nen64 ST
FST ST(n) S T ( n )6 ST
FSTP mem32 men32e S T ; p o p)(i
FSTP men64 S T ; p o p( ) ;
FSTP mem80 nn80F s I ; p o p)(;
FSTP ST(n) S T ( n )e S l I p o p)(r
Dercrlptlon
Thls instruction storesrhe top of stack in the designateddestination. If the opcode
is FSTP,the stack top is popped (discarded)after the siore operation. If the destina-
tion is a 32-bit or 64-bit real memory operand, the top of stack is rounded according
to the rounding control (RC)bits of the control word.
Note that the FSTPform of thls insFuction can store a remDreal (80-bit) value.while
the FSTform cannot.
Excoptlona
ST PE IJB OB 4
E amplg
Before
ST 69.0
sT (1) 98.6
359
tltE 003.4 BooK
Legal Fo?irs
FSTCI menl6 ; meml6F Cl{
F N S T C }m
i e m 1 6 : h e m 1 6e C f
Do.c.lptlon
This instruction storesthe contents of the control rord (Cw) register in memory.
The FSTCV form of the instnrction checks for unmaskedoceptions before the
control word is stored,while FNSTC\/ does not,
Ercottloni
SF PB I]B OB 4 DE IE
360
a! th. SOOaOI8OaAT
h.trFtlon S.t n.t.Me
FSTENV aoa7/ao2a7/ao3a7
Store EN'troonrent
Legal Forns
F S T E I I V, ? e r ? p n e n p e e n v ( N D P )
FllSTEllV r ? e r ? p n e m p e n v ( N D P )
Descrlptlon
This instruction storesthe contents of the 80387eflvironment registers(CV, S\v,
T.Ii(/,and error pointers) in memory beginning at ,rarp Figure 8-4 outlines the for-
ma! of the 28-byte environment block.
31 16 1t I Blte offset
0
4
6
Errcroffset(EIP) L2
Eror selectof(Cs) 16
2A
24
32-bit formal
rt B'1eollset
0
2
Instruction Dointern x 6
IP,. 8
10
o&c rl t2
16-bitfomat
F gate A-4.80387entlrcnnenL
Exceptlo|rg
SF PB tJE OE U DE TE
351
rdE 00aoaEooK
FSTSW aoa7/ao2a7/ao3a7
store status word
Legal Forns
FSTSI,{ AX AX F Sli
FSTSI'I meml6 menl6 Sll
FllSTSl,l AX AX Sl'l
F STSI'J meml6 neml5 Sl'J
Dercrlptlon
This instruction storesthe conients of the 80387statusword in memory or in the
AX register of the 80386.The FSTSVform of the instruction checks for unmasked
exceptionsbefore the control word is slored, while FNSTS1V does not.
ErccDtlonr
SF PB IJE OE 2T DB If,
362
6! lh. ao3aGEGtaT h.trrtlon S.l Ad@rc.
FSUB aoaT/ao2a7/ao3a7
Subtractlon
Legal Form3
FSUB ; S T ( 1 )e 5 T - S T ( l ) j p o p ( ) :
FSUB men32 : Sr e 5T - nen32
FSUB nen64 ; ST 5T - nem64
FsUB ST(n) i S T S T - S T ( n )
FSUB ST, ST(n) : S T S T - S T ( n )
FSUB ST(n),ST : S T ( n )e S T ( n ) 5 T
FSUBP ST, ST(n) : S Te S T - 5 T ( n ) : p o p ( ) :
FSUBP ST(n), ST : S T ( n )e 5 T ( n ) - 5 T ; p o p ( ) ;
Do.crlptlon
This instruction subtractsthe specifled operands and storesthe result on the 80387
stack 4s shown aboj,,e.Optionally, the top of stack is also popped.
If you spe.ify a 32-bit or 64-bit real memory operand, it is convertedto temp real
format before it is subtractedfrom ST.
If any real value is subtractedfrom infinity or infinity is subtractedirom any real
value,the resultis infinity.Subtracting two infinitiesoflhe samesignis an invalid
operation.
Exceptlona
SF PE I]E OE ZE DE IE
Examploa
ST 9.81
sT(1) 6.1 ST 1.51
sT(2) 72.O STO) 72.0
FSUB
35:t
tlt! 00406 tooK
ST 9.81 ST 7.61
sT(1) 6,3 sT(r)
sT(2) 7Z.O sT(2) 72.0
FSUBDWORD PTRIESI+4]
Memorypoinreris shortreal 2.2.
3A4
a: fh. ao366/ao3a7 h.l.ucllor 3.i R.l*de
FSUBR aoa7/ao2a7/ao3a7
Subtr"actlon Reversd
Legal Fo.m!
FSUSR I S T ( 1 )e 5 T ( 1 )- S T ; p o p ( ) ;
FSUBR nem32 | STe nen32_ 5T
FSUBR mem64 ; STe men64_ ST
FSUBR Sr(n) ; s T e s T ( n )_ S T
FSUBR sT, sT(n) : sTe Sl(n) - sT
FSUBR S T ( n ) ,S T ; S T ( n )e S T ' S T ( n )
FSUSRP 5T,ST(n) | S Te s T ( n )- s T ; p o p o r
FSUBRP S T ( n ) .S T I S T ( n )e s T - s T ( n ) ;p o p ( ) j
De.crlptlon
This instruction subtractsthe specified operands and storesthe result on the 80387
stack as shown above.This insta.rctionis equivalentto FSUBexcept that the
subtrahendand mlnuend are exchanged.optionally, the lop of stack is also
poPPed.
If you specify a 32-bit or 64-bit real memory operand, it is convertedto temp real
format before it is subtractedfrom ST.
If any real value is subtractedfrom infinity or inJinity is subtractedfrom any fed
value,the resultis infinity. Subtracting two infinitiesof the samesignis an invalicl
Excspilon!
9F PE I,E OE ZB DE IB
x x
Era|nplgg
ST 9.81
sT(1) 6.3 ST -3.t1
sT(2) 72.4 sT (1.) 72.0
FSUBR
345
rHE 60366 aOOK
9.81 ST 7.61
sT(1) 6.3 sT(1) 6.3
sT(2) 72.O sT(2) 72.0
FSUBDWORDPTRIESI+4]
Memory pointer is short real 2.2.
366
a: lh. eo3araoolT lBtruc.llon aet B.f.rdc.
FTST aoa7/ao2a7/eo3a7
Test for Zero
logal Form
FTST ; c o n p a r(eS T , 0 . 0 )
Deacription
This instnrctioncomparesthe top of stackwith 0.0andststhe 80387condition
codesaccordingto the resultsof the comparison,
The followingtableshowsthe conditioncodesettingsthatresultfrom the com-
parisonfunction.FTSTconsiders+0.0and-0.0 to be equal.
C6dttloa C3 Q CI <P
sT>0.0 0 0 - 0
sT< 0.0 0 0 - 1
ST- 0.0 1 0 - 0
S T i s a N a N l l - l
The 80387condition codesare arranSd in the statusword so that C3, C2,and C0
rnap into the samebit posilions asthe ZR PF,and CFbits of the 80386EFLAGS
reSister.Thus, issuing the following idstructions setsth 80386flags as if the com-
parison had been performd on the 80386:
i F l o a t i n g - p o i nc to n p a r e
FSTSll AX I Stofe status vord to AX
SAHF ; Store AHinto flags
You can then use any conditional jurnp instruction (JE,JNE,JA,JAf, JB, orJBE) lo
branch on the result of the comparison.UseJP to test e/hether STis a NaN.
Unlik most arithmetic operations, PTSTwill sl8nalthe In%lid (IE) excePtionif ST
is a quiet NaN.
Exc.ptlont
SF PB IJE OE ZE DI II
87
lt{E ao3a6 aool(
Exarrplo
ST -37.37 ST
sT(r) 1.0 sT(1) 1,0
FTST
a6a
a: flF 0o.te6/ao3a7 lFtruciion l.t R.td.nc.
FUCOtul ao3a7
Unofdered Compare
Logal Forfia
FUC0I4 ; compare (ST, ST(1))
FUCoMnen32 ; compare (Si, nen3z)
FUC0Mmem64 ; compafe (ST, mem64)
F U C 0 l 4S I ( n ) ; c o m p a r(eS T ,S T ( n ) )
FUCotlP : c o n p a r (eS T ,S T ( 1 ) ) :p o p o
FUCoP mem32 i conpar(ST, men32)rpopo:
FUCoP mem64 | compare (ST, men64);pop();
F U CPo ST(n) i c o n p a r (eS T , S T ( n ) ) rp o p ( ) :
FUC0|,4PP ; c o n p a r (eS T , S T ( 1 ) ) ;p o p ( ) rp o p O l
Doac ptlon
This inskuction is identical to FCOMexcept that no exceptions are signalcd ifeirher
oPerand in the comparefunction is a quiet NaN,(the comparisonis unordered).
FUCOMexecutesthe func\ior\ compare (opl, op2) and sets rhe 80387condition
code accordingto the resuhsof the comparison.The tll387 stack is optionally
poppeoonce or rwrce,
The followinS table showsthe condition code settingsthar result frdn the compare
function,FUCOMconsiders+0.0and -0.0to be equal.
Con tttton C3 C2 CI CO
op1> Qp2 0 0 - 0
o?l <oP2 0 0 - I
opt - oP2 1 0 - 0
1 1 - 1
FUColl op ; F l o a t i n s - p o i ncto n p a r e
FSTSN AX : Store status {ord to AX
SAHF : S t o r eA Hi n t o f l a 9 s
You can then use any conditional iump insrrucrion (JE,JNE,JA,JAE,JB, orJBE) ro
branch on the result of the comparison.UseJP to test for unodered comparison.
369
tFt 00406 rooK
Exqeptlon3
SF PE I'E OB A DE IE
Examplo
3m
a: th6 aooa6/0o39?lEtruc{on 5.r i.td.nc.
FWAIT aoa7/ao2a7/ao3a7
Walt Untll Not Busy
Legal Form
Do3criplion
This is the 80386VAIT instnxtion, but many assemblersallow ],ou to encode it as
FWAIT becauseit relatesto the NDp. FWAIT placesthe 80386inlo an idle state until
the BUSY\ pin is inacrive. If rhe BUSY\ pin is inactive when the instruction is exe-
cuted, no idle occurs.The BUSY\ pin on the 80387is held active while the NDP is
performing a floating-point instruction. Executerhis insrruction before any 80386
instruction that will use a value stored by the coprocessor.
Excoptlon!
SFPEUEOBZDDIE
371
tlt! @306 Boox
FXAI,I a0a7/ao2a7/aota7
Examlne Top ofstack
Logal Forrn
: C ce e x a n i n e( S T )
Dgacriptlon
This instruction sets the condition ode bits in the 80387statusword (SW) accord-
ing to the value of the top of stack.The following table indicatesthe settingsthat
can arise basedon different valuesof ST.
c3 c2 cl a1,
Unsupported' 0 0 0
NaN 0 0 I
valid (normrD 0 1 0
lnfinity 0 1 I
0 0
Unused(T\v - enpty) I 0 I
Deno.mal I 1 0
Unused(Tw - mpty) 1 1 1
The s bit in Cl is set to the sign of the value of ST,with 0 indicating a Positive lue
and 1 indicating a negative.
Excoptlonr
SF PB I]E OE A DB
'Unsupported values are sPcclalblt Patreds th.t rere %lid for lhe 8087 ot 80287but are no lo!8et
supponed.lhesein.lude Pseudo-NaN, pseudo.zc.o,pseud,c_inflnity, and unnomals
372
ar rh.lOaOo/AOOat h.tflc d a.r i.t r16
Examplo
Before
ST
sT (1) 44.0 ST(1) 45.0
FXAM
373
. tHE AO3a5 aOOK
FXCH aoa7/ao2a7
/ao3a7
Exchange Stack Blements
Legal Forms
FXCH ; tenp STi ST e ST(1); ST(I) e tenp
FXCH ST(n) ; t e m pe S T r S T e S T ( n ) : S T ( n )e t e m p
Descrlption
This instruction swapsthe contents of the specified stack registers.This allows
valuesto move to the top of stack,which is the standardoperand locatlon for many
80387instructions.
Ercoptlona
SF PE T'E OB 4 DE IE
x
Eramplo
ST ST 1.0
sT(1) 2.0 sT(1) 2.0
sT(2) 10 sT(2) 3.0
FXCH
374
a: flr. ao:Fa/Eo3?h.rruc d 3.r RdFnc.
FXTRACT aoa7/ao2a7/ao3a7
Extract Floatlng-Polnt Components
Logal Form
FXTRACT ; tenpe sI: 5T exponent(temp)
; p u s h ( f r a c toi n ( t e m p) )
Dorcrlptlon
This instruction breaks rhe top of srackinto irs constituent pans, the significand and
the exponent,The exponent is slored as a true, unbiased lue. not as iust the bit
parcrn in the exr onenrFieldol rhe floaLins-poinr represenution.This operarion
leavest}Ie fraction or significand on the rop of stack and the er.ponentat ST(1).The
original value is desrroyed.
If the original top of stack is O,the ewonent pofiion is set to ne9rive irfinity.
Excoptlonr
SF PB I]B OB Z DE ID
Ex.mplo
Before
ST r.59x I
FXIRACT
3?5
flG ao3a6 BOOX
FYL2X aoa7/ao2a7/ao3a7
Compute Y x log2x
Legal Form
FYL2X ; t e m p+ l o s , ( S T ) r p o p ( ) ; S T e S T * t e m p
Doacription
This insiruction pops rhe top of stak,takesthe base2 logarithm, and multiPlies the
rcsult by the ne top of stack.Anothcr way of expressingthe function is:
ST(1)x log2ST
The initial top of siackmustbe a positivevalue,0lhrcugh infinity lfit is not, the
rcsultsofthe opcrationareundefind.
You canalsousethis inslructiontocomputelogarithmswith a baseotherthan2,
relying on the idenlity:
log,, 'c = (log2 x) / <log2n)
Thc following code fragment illustrats this computarion
FLol : 1.0
FLO i ;n, 1.0
FYL2X ; log, n
FLD1 : 1 , 0 , 1 o g ,n
F D I V P S T ( r ) ,S T I l / 1 o 9 ,n
FLo x I x, 1/109n ,
FYL?X : logrx* l/logrn
Exceptlons
ST PE IJ'E OE ZE DB IE
Exarnple
ST 8.0
sT (1) 0.01 ST 0.03
sT (2) 0.333 sT (1) 0.331
FYL2X
376
A! lh. aoata/0o347 harruc o. Sct R.ld.nc.
FYL2XPI aoaT/ao2a1/ao3a7
computerxbg2(x+1)
Legal Forn
FYL2XP1 ; t e n p e l o s , ( S T + l . o ) ;p o p ( ) : S T e S T * t e m p
Doscription
This instruction pops the top of stack,adds 1.0to the value, takesrhe base 2
logarithm, and muhiplies the resulr by the new rop of stack.Another way of ex
Pressingthe instruction is:
ST(1)x log, (ST+ 1.0)
The jnitial top of stack must be within rhe range -l + ',L /2 < X < I -',L /2, or the
resultofthe instructionis undefined.
This instruction is provided so tha! adding 1.0ro rhe top of srack and cxecutirtg
FYL2Xdoesflot resultin a precisionloss.Because the FyL2Xpl funcrionis com-
puteddifferendythanthe FYI2X instruction,a specialrangeresrricrionexists.
FYLDGI iS also uscful in computing the arcsinh,arccosh,And arctanhinvcrse
hyperbolic rrigonometric ftnctions.
Excoptlons
ST PB !'B OE ZE DE TI
x
Exal|tplo
5T 15.0
sT (1) 10.0 ST 40.Q
sT (2) sT (1)
FI].D(P1
377
t||E 60346 BOOK
F2XMI aoaT/ao2a7/ao3a7
compute 2. -l
Legal Form
F2xfll ;srezsr-1
Descriptiolr
This instruction replacesthe current lop of stack (ST) with the value of the function
2sr - 1. Horr'ever,the initial operand lue must be within the range-0 5 < x < +0 5
or the result of the operalion is undefined.
The fiinction 2* - 1,rather than the simpler 2x, is provided on the 80387to ensure
precision when x is near 0 (for e{ample, when computing h}perbolic trigonometric
functions).
'Becausethe range of the FIO'41 instruction is narrow, subroutines!o compute 2n
must use FRNDINT and FSCALEto bring the instruction into a legal range and scale
the result !o I proper \alue.
You can compute the generalfunction ,cvby using the ide iryl
x! = 2t x log2tc
4nd using the FYL2X and F2XM1instructions.
Excoptlont
SF PE I]B OE ZE DB IE
x
Exalfrple
ST 0.01 ST 0.0069
ST(:I) 3.0 sT (1) 3.0
F2XM1
374
APPENDTXES
379
Appendix A
POWERS
OFTWO
0 1 I
I 2 2
2 4
3 ii E
4 16 t0
5 20
6 &
128 u0
8 100
9 512 200
10 LO24 400
11 2044 u00
t2 4096 1000
)3 at92 2000
74 16344 4000
i5 327/.a 8000
16 "": 10000
20 1048576 trr:
32 429196725k 100000000
3at
Appendix B
ASGTI
GHARACTEll
SET
Hreh-(r.le1" Blts
Blts 0000 0001 0010 0011 0100 0101 0lro 0r1r
0000 NUL DlE space 0 @ P p
o00l soH Dcl l ^ a n q
0010 STX DCz I 2 B l t b
o01l ETX DC3 3 C S (
0100 EOT DC4 5 4 D t d r
0101 ENQ NAK 5 E I l e
0110 ACK SYN
0111 BEI I]TB f 6
7 G
F
\
V
( 8
f
1000 BS CAN ( 8 l l x b
lool HT EM ) 9 I Y i y
1010 LF SUts , J Z j
loll VT tsSC i K l k l
1100 F! r's < L \ 1 1
1101 CR GS - = M l n l
1110 so Rs > N
t1t1 st us ? O o R U B
343
Appendix G
apgoDE
TABLE-
Key to Abbreviations
Operands are identified by a two-character code of the form Zz. The first character,
an uppercaseletter, specifiesthe addressingmethod; the secondcharacter,a lower-
caseletter, specifies the type of operand.
305
Tl|e ao33aBOOK
Jf. The instruction contains a relative offset to be added to the instruction pointer
register-for cxample,JMPshort, LOOP-
tt; The mode r/m byte may refer only to memory-for example,BOUND, lEs,
IDS, I,SS,LFS,IGS.
o.' The instruction has no mod r/m bytei the offset of the operanclis coded as a
word or doubleword (depending on addresssize attribute) in thc instruction. No
base register,index register,or scalingfactor can be applied-for cxample, MoV
(AOH-A3H).
R. The llx)d field ol the mod r/m byte may rcfcr only to a generalregister-for cx-
ample,MoV (oFH20H,oFH 26H).
s, The reg field of thc mod r/m byte selectsa segmentregister-for example,Mov
(8CH,8EH).
?.' Thc rcg ficld ofthe mod r/m bytc sclcctsa testregisler-for examplc)Mov
(oFH24H).
by DSrSI*for example,MOVS,COMPS,OUTS,LODS,SCAS
X, Memoryaddresscd
by ES:DI-for example,MOVS,CMPS,INS,SToS
r. Mcmoryaddressed
346
ADD.ndlt e ODcod. Lbl.
RegisterCodes
'{qhm an opemnd is a register encoded
in the opcode, the regisreris idenrified by
its name,for example,AX, CL, or ESLThe name of the register indicareswhether
the register is 32 bits, 16bits, or 8 birs. A regisreridentifier of rhe folm e)O(is used
when the width of the register depends on the operand size attributq for q,(ample,
eAJ(indicatesthat the AX regisreris used when the operand size atrribute is 16and
tllat the EAX register is used when the operand size attribute is 32.
347
tHE ao3a6 BOOX
N O IJ NI ) ARPI,
l,lJsllAD 1'S: Ol)SlZtil ADltslzE:
ljw,ltw
NOP
xqto XCI IG XCI IG XCHG xct I(i XCIIG XCHG
3aa
ADpddir c: oFcod. LbL.
S 9 A B C D E F
OR OR OR OR OR OR PUSH 2-t'.4e
Eb,Gb Ev,Gv Gb,Eb Gv,Ev AL,Ib CS
SBB SBB SBB SBB SBB SBB PUSH POP
!b,Gb Ev,Ov Gb,Eb Gv,Ev eAX,lv DS DS
SUB SUB sua STJB SUB SUB
Eb,Gb Ev,Gv Gb,Eb Cv,Ev CSI DAS
Al,tb eAX,lv
CMP CMP CMP CMP CMP CMP
a Eb,Gb Ev,Gv Gb,Eb Gv,Ev AL,Ib eAX,Iv DS:
TEST TEST
AI,,Ib eAX,Iv sTosa sTos\(/D LODSB LoDsv/,D SCASB scAsI{,/D
MOV MOV MOV MOV MOV MOV MOY MOV
AX,lv eCX,lv eDX,Iv eBX,Iv eSqIv EBP,IV eSI,Iv Dl,Iv
ENTER RETfar INT INT
LEAVE INTO IRET
lw,lb Iw 3 tb
ESC ESC ESC ESC ESC BSC ESC ESC
D
0 I 2 3 4 5
CAIL JMP JMP JMP IN IN oUT ottr
Jv Jb A!,DX eAX,DX DX,AL DX,eAX
349
tHE ao3a6 BOOK
390
ApPrdir c: Opco.l6 t blc
8 9 A B C D E F
391
tHE aoaa5Boox
80387 Extensions
'Ihe following
lablesshowthe opcodemapro the 803U6insrructk)nsetlbr the 80387
extensions. opernndabbreviations
The for dreset4blcsarcl
fs.' IlTs:livc acldrcss,short rcal (32-bit)
Xr.. Effecriveaddress,longreal(64-bit)
.8 .' Effcrtjvc ^cldress,tenp real (80-bit)
l9r. Hffectiveaddress,word (16+it)
.Ed. Effccrive ad.lress,doublewod (32-bit)
,4.' Effective address,quadword (64-bit)
-E i Effective address,BCD (80-bit)
I.a. Effective acldress(no operand size)
slal).' Stackelementi
Sf.'Stacktop
392
APP.ndirc: Opcod.t hL
FomaC
ESC0
ESCI
39:l
tflE ao:|a6 BOOK
ESC2
ESC3
Cfl{'3d
Group3a: mod-11,nnn-100
m 000 001 010 011 100 101 110 111
GENI) (FDISI) FCLEX FINIT GSEI?I\O
BSC4
394
Atf.r4x e OFo.L Lbla
ESC5
ESC6
ESC7
395
Appendix D
TNSTRUCTTON
FORMATAND
TTMTN(G-
This appendix describesthe 80386instruction se!.A table lists all instructions $/ith
instructionencodingdiagmmsand clockcounts.Detailsof the inslructionencoding
are provided in the following sections,which describe the encodinli structure and
lhe definitionof fieldsoccurringwithin 80386instructions.
397
tHE 60364 BOOX
EATrpe
Displacement or y 6
Baseor indexonly 5
lndex+ displacement 9
Base+ displrcement 9
Base+ index 7or8
Base+ index+ 11or12
394
atr*dlr Dr lr.ituciron Ford.t ed ?lnlng
399
i a
BTE
.UE ;
E F E - = = $ ii
! = = : : : i " :
i -
ts9
Ht 1 {
E
f N , P " - n
3
0
r ' r r - n P
U E l 1 l ' 1 I I t
'!
d
! HE
EEHiiiHiIHEH
i
x EEEEEEEE
ssiH
sgL#
$*ggggggiis
6
E
-
400
: . {
!;
3!g
!
i i ; ; ;
gq
f i : .
E
x ; i - : ! 9 = d
E:$ FIFIFI
t9E|3
flt
I
l'
FFfl[]f]
lsllsl: l: lEl
u
ta
n
5l
3t
I
nn
:l
tpl I
i E tt - l
FFFFF]
tltltltlUUU
tEtEt: t: t:
lEl
I
t:t
tll
l8tt;l
El tll b]F]E]E]E] F]E]E]F]E]E]E]F]
t
: .
g : e .
E g r
"zt t 2 t ? 4
3 '-q n t9r"
i z i t iei " aa. I i : ! i ! i ! E 1E =1=
T !,EK d : i M * E
9 : : 3 : : t
U : : i a l F r F ;
i + i c ! ; i " ' + i 5 +: ! r
g ati 4i e
i ti
= -6 * J ' i l r L ! l oi i; E i + IE
e i 3 I * 3! i n : 5 ; : 4 g : : - . !a : F :
401
" -
EQE " " : 1 i : . : i
' t i
.r: ; t - t - :
? : .- ' -
;
e - - - : - - - - : - : - - - - ^ r -
g9
- . . : . - . - ; 6 : - . " . i -
d
E
5
^ " . - i . - " - : " ^ e ^ " . - i ^
BBEBuBB
r! N N F F
l:l l. l. l:
i
a EtEEtEl
E
E
g gFi,
g
gE;'
:
s;s ; Iis;igs
i
5
402
I
:
8
d l r - o
XQ
5 - - . : - 5 - - - -
E
E
a
5 T ^ - ! ' ! . : . - " -
8
T
d x - " - - s -x . . - - i I
; i i
i
i i i
J FI TIIFIFIFI
F]FIFIFI i FI F] It :
E H [] hli l?l lillil|;
l; i |;l ;l
t t r t t t T F T r F t t f l t l t l | l If l
lll|:llil Elll l=l;l=llJ
iclllI lEl'l l:l. lEl
lel
rl
e E]E]F]E]E]
L]11
E]F]F]E]E]
E]b]E]E]E]
E] t-l
i . > F F
! , i' - . ! -r : 6 : r' "E 6t ; E
* i i
t t r r r i ! i r y i l E : : i ; :' .&i "n "" ' { ;
c ! : : 5i :Ei , r F. E
R j j :5 -s" :d l. 9
9 3i 5 !
; ! i! !; F; : = t i : - E i !
: .b ig
t E E
! 5 i FE { : : 5 ! E i d : d : : : 3v E = ; : " ! l
! s e $ s : ! : . s 9 g $ 5 9 E 4t ra tZ i = t t ; i
403
tsae
@dE
SP
! ! 1 l l ; * '""
5 "!ff;*"-"
e
E
i * r , ir i i k ;i$*"'^
B : : i ! i i :
U
: i i i i i i nN-R ss$*""
tEt :
l-l lsl rn nn
-t
l1
I
t
fll:l |]
t:l
te tE
T
:
t-t
n
t -
t:t
lel
t t -
lcl
tal
l:l
f t] u
:I :I
l Bl 3 l
| | | |
; l l 3 ll c l l E l
5t 13tt:t t:l
!
a
x E E n ll Efl fl fl
E
I
Eg,rE,, x
; E
j
eii,,
i E.i
EU
!
z is' ;i,, iil!
tLO4
E !i
9 ?".
i :E
3',24
s
"65
s i 'I i '
t F
: : : i . f- + i "
- ;
? i
; - :
:
3
" f- { S
EE \ 1 E P
xg
r e t ! s
6 \ < I ! I 1 { { 3
E
a
2I a d F ll -{
B E
s s s ;
lllin EF ; :
lJlll,lll ! i
I
- - : F]E]E]b] i E
- - :
l1l'l l1 lit til til
EUUI FtFtFtFt
FrF
l,|,|,1 l-|-|-l
tF-tiF-ttEl l:l l:l l:l
tEttfl tEl T'I T1 T1 !! d "_",""
E i z i z i lE|fl
tErFr|;llEll;ll;
|;ll
Hfi flll flflflfln liI
l r l : l : l lall:l l:l
EE6:r!e: l =l l, ' l =l l= l l gr cr,: r cr c r t Er i l
I lel l:l lEl tit tEttgl
l:l l:l l:l t - t t - t l - l E]F]
F]F]
E]E]E]E
F]E = F
g ! I , [ - E
q a i
r E E . F
- b- b . b F b - h . R . ,
E.,E-
s i : 4 1 - , i
E c! t l
. ; ! ?
e
C EEE :d -E: E- E
i i ; : : :
; ; 3 ;r Fs a g ! ;
: : : E : i : I s I 3 :E? : i
a ,g !e H# n& 8[ ! s #?* 33 "e 1 3 i 1 AA; 5 &$ i> &t &;"tFE i_
! IF
. E : I=a I
405
3ae < . ; : < r f , f d 5 N -
- t - d
- 3 6 *
.dY
E8
3E
- d F F : - - . . ; -
- - \
i i "
E 6. . ^ . - 5 - - ^ . S . : e 1 f F {
d
t n ^ F : F 5 6
d
Eis*l.l
6-
n
l,l
u
u
E :
5 ;
I E
;
i tl
! ilHtlEliEEEU
t g tt g tt g t c t ; Et .'t Pt :t ;
l:l
i I
l:lhllslsl! L;l:l
l:ll""l rr
:
.!
i
!
r! e
IlEl n
l:l
I
-r
!
nTlnfllillltu
lElsl J lEllE lg lsllJ lsllil l:l
l:l :l :l lE l-
lalfl e lFllE
JrrLL
,
lE l: l:l l:l l:l
ld lElElEl
lEl
r-r-r-r-r-
lEl
r:l
lll EEHH
o
2
:q
?
8 i
f
F ! F
, _-iE
a
: i ; E S re i,
g P
r
-
9
i , ;
4 3 n
.!
E
: "
-
9
. - ;
E e , ,
E i
;
; i i s E : !! ,B!
I j. Ee : i. r"? ? 7 t 1=d l i i
?9 i Fi :
E
9
D
= E+ ! ; ; i i t E: ; ; ei i ! : g f E ,l ii {, { } F
'3 E4- g F 5 E b9 " F F 5 E i: 6 l
z g = . 3 " 9 " " . = ! E 2r ' q : E l X S i ' ;
4{r6
385 { f < S , s r F 6
a z ; * z i . e * ,
e E 6 5 5 5 t 5 5 ; 5
Xg
5 F 5 5 S S
d
E 5 ; f s sf
+ i s i
5
, i F i -i o; : ss ss
et ;p
i 9 9
I
F
g
o E 5 $ G s i
J : , t i ! f i
! !
s s " { gs 3 s s
"E : .E lu n
lilHHH
!;el*l
;l
ill
t-.1 u
lsl
HHHH
:
HHHEEHHH
HEHH
tr' r
a b E ! . q
H : H * P
E i H E t i l l i E
!5 5.*: ir ,HiHI! !HSE
-
9
g E ! F ; l 5l" JErq l f E : 9 x : 9 t .
S 6 . : 5 A ! o !
" ! E EE F Ei E i EEEF
E 4Eg Ei a i i i t 3 : ? ? i i l : 2 ; E r ; z t E
i l P : ; E = i : P [ : ? :
* 9 8 = J - P E E * E = E
f =2 E
r r i E Y r 9 f r g g c P B
407
d
_ e ! i : i i : : i r : ;
E ; ! , c < <
:R
E ; F ? ? ; f : ; ; ; ;
F
z
E 3
.t
!
F R : I 8 !
* a 8 K g F
E a &
x
B A
3
n
l:l
lEl
s
!
t8t t:l t-l u
t:l
E E
i * p
-
l p p
{ - = ; h = = e
" i i i ^ ' ;t t { : : { i i:-i zzi
. t . F ei l + i i ! : : I t E g ; i * !{ i: : E
o
!
0
ti r ;i E i r i : i i l i Ei i ; 6 ; , ;7.\!?=tFsr ir ?i igi i?EiI it {
; i i t F r r i rj :i ? ? ? ?" ,i" ;, "g
5 :
F : ' ; i ! ; ! !; ; t ! Fi r ! s ! i i g [ 3! i 5I 5 :
408
3!e I:
EEE .1
3
E
. 4 < F ; i i
2 2 2 .E rE : E
i qE
! <
L ) "
<
" z " !
< ; 3
1 t
F
E8
t
t ; : A : - ? ? F
F
c c ;
e
6
N R S i i : * s ( H A - P H R E 6 n s nK 8
d :
F
l l li n r : F;]
lEl : I'l E
t1
t:l
l3
Ft Tt Tt Fl
t t t t t t t l
t:t t;t t:t t:l ft
t:l
t:l
t - Lt - | _ t t- l:l
r ; -b !i I
! q 4 t * i 4 E Z z ? z z? E l a a Ev e
3 s s ;
r ,i :
E
g ; t
i;g! i ; i3l d F f i ! ! g
0 l i d i !E ; E i-r!*sq?*:iF:*4{
f
E ! ! ! i i s i i : i F H si :! i ; S E l i F E
i ssE e
sI
e 5 = .
409
3iE i r i l i r ! 1
"du
E * 2 2
E ; ; i i
E :
g g
E - A
, ! ;
E t l Tt T_1 T_t T_l n-] Tln
l:1 :1 la lcl l:l l3l i:'l :l lil l:l
l:t :l t:t tEt t:ttgl
t;t t8t t;t 3l l:l El l:l lEl
t;t -l
l - t - l ft :t t + tt : l
l:l :l t ; tt - t
I tltl
l:l l:
ftl
:l :l
t | t t l
T]]T]]
t t t
Ft t-l
t l
I tat tal :t :l
i
6 t:ttBt :t El :l L8l :l Bl
ftt1 ftfl aa aL1
E t " 6 6
I r " ; : ! I
T 5 E : s ? r; 3 t i F E : F E i ; C 4 | C
4 1 + E E 2_ ! t t $ _ :
-
*
*
E
e
: i L 9
q
F : r
7
i E F
:
) . E
!
i5 E i i ! c q l ; e * : E e A
? i : ! ; i i i i
E E :
1 : t ! :
! E * l ; i P E a Ey F ; . f , E E : { ! : = i = : V - " : =
F a E + 5 ' - E ' "
B
E E: ^ ; : I ; : :t ; ; 9E ; :
5 - " H t t E ! ! E E d ! ! e
4to
EAE
.dE
-
;
t a
:
: = {
!
b < b < b < b < b < b < b .
4 E i i i
E5
E b < b < 6 < b < b r 5 <
E
i i i , i i i i i
E
a
E
E
d
:
I I I i I g
E
: - : : : 1 ? : : i
E tl El-l E[
l:llEl l:1lal l:lli
E t l E r E t l E U [ l T [ tlel
,,:1
,l tglEl 13lJ 13lJ lll" l:"1
]
lJlgl ll Lgl l;LlEl l;llEl l:llE lJlal lJlsl l: lE hllal
F]FI=]FIFIH FIFIFIFIFIFIIIiI F]FIFIF
lJll l,ll=l lJlJ lJl=llel=l lEl
lJ |:llJ l;ll=ll:lll
t E]E]
E]b]E]E]E]E]
E]E] E]EIEE] E]E
EIF]
" i g g
i: g ; i . , * . , t ..u, i 1
E
= iEg: ; 55;ff igi* iE Fi E5: a5l-
q i : ; i ; I ; : ; i i5Ei
5 i
S
z 3 i ; i ; = f i s :F E . : :i':
4tt
3q<
eE5 : )"
E ;E i- it - "z ; i "z ! 3 ! 1 { {
i t
b / a 4 b < b < b b b
E8
b { E - b < b < b b E
n : i n , & a
b : f t b b b b F E e
E E E E E E E E : : I
E
5
;A
F F
HI EtI HTIEIH
j . l l
EHEI
1 1 3E E t i t l t i f l t ;
:t "t tat1ta:| t:l:t t;
lu ntnt ttI
EH
I
i
8
I
I
e !:
It i tt l: l
trt t;l
E] E]
E
l1I el
11l1el
ElEl ElElu
r "
lllEl
lc
Ll blu u
l i F
EE E
! " . = E e i t
d
t l'- i ; (j* ,f : i:
fi 4. 2 < |
:
t ;E
9r
:
e E
e 5 5 S $ S S e e S S S S
0 s s s s S $ s $ s s $
EEHBEBHH
:
*
HEEEHEEEE F " E
ir ?Ei ; * Eg ; f
A gi,rieLsir
Ei:
Fitigi cia
ii,FLEIs
F
J
4
Fr B s g a F FliI rgg
;i i ii t; rlbig
i ;i f; ;i
413
3Q<
'du { < { < < 1 {
4 d r r i a g t K ;
U
' 6 i
I : 9 : t +
rn r-l
t-|1
l 3 ll i l
l:l lFl
t! t:l
ft
I
Ft
tsl
H t ;
! ufl tluu u "
:
E
t: tt
ll ::l l: ll:: l l "|l ,E 'i a
r t'lt- t; . . , lr . ,
= - ; - e - e + S f l
x l L " E E" 1j d1 Z 1
; ; i 3i i i
E
T
E
; e 9. i ;
1 '
iI :
I
:
:
E !,"F+fi;
: 6 : : ; s E o s e
? F ,!r e F f 9 " : i ! 1 * . i [ ; i ; r i i ' ]
F !a E 3
q : gin l ! i i "*- ! " :* f b ! g E E* d { | $
!
E
H 3 *E
E. - - :
? < +- . 5
; -! -! 1 ! E ; ; r t , ; ;l :: $ t i i 5 F ,
414
3!e :
-nts
ir
E
sl
Hi
o
a ; a a d n n a n a
g AE ; i ; ; ; ; ; $ ; ; ; ; i i
, E FR
l : 9 n $ f n f i R H K r g s s R R X f ; 9 X F :
F H
d
g
n = s
g H
; ;
6 " &
3 . 3 * !
*iF: g i 6! - -i i " i,, !a;, ;i :li i ' i ib!
il;; {=i-,i.";;i
i ! : ( s E i * * ? i ? ! : i n1 E $ ? ! { g ! ( (
E E i : Fi z , ; ; 4 l i Et ,i ii i t ; { ' ; { ; }
E ; l F EF F E E I I e t : $: +: A ; ; E q ; : :
i : H } E E ; : : : : : : 8 f r ; ; I t C : : : T T
E E ; ; ; i : F i ii ii ; ; : ; ; s i dd$ ; ;j ; I
H i : : : t :. j : i t ! : ; ! ! i ! 2 i l t i E r 5f 5 1
a
4t5
< F E F
5 > + F , ! z z z : 2 2 > - > i i " - , , 2 z ' z z >
89
c a
E
p
E .a
ss , x f i 5 e { : I s g f l d { F F : F F ^ l t
I
d ti
;, i,
P !
E , A
* l s I
I !geE $ , q e " - !tl -i ll
E
I * (;'$'1
e ! A
l B a i l:l
A i ; ; ; tl
- t u
!i ; a z+' r t g t r : 5 k 3i
" i i , a i t a ? t ?
rt rt
E i t . . 4 4" " A d
!! r * ; r E : ;E! t ! . ; . i F r d E E 8 E ! E
t 3 i ! i i : ? i i { E
: tr3trtAZZ!1
5
9
U
=
E ; " i T I I I E ; !FH ?
:t::?
? ! i
: : $ g E : : : : i ; i i " i
c - . Fi I f { nl d a a BE
E F
e
E ! : ! ; F F ; F F FF- F
: sF
9i : : : :l : : Y :?
4t6
3!E IE
e5E
s
6 " ;
1 1
BR
a : '
E > 2 2 2 2 2 " -
q
6 { A l 9 l d ! ! 6 a
8
I ' I
d S - * : : s : : 6 ,t
EEEHHEEH
HHHHHHHEH
!l !3
r
$
ll
lsl
ld
lEl
B"-'"
lEl
lel
lEl
lEl
lEl
iEl HHHH I E J- E
^ a
3 &
! s 6 : !
E ! * i & i
i,E$sgsEFgggi
! 3
B l r { :
F c I B e F a a i e s 4
417
EiE
.EE
6 9 i " - " i * s c
*U
.3
6 *-A "6 ts
p
E
F F S F i
e e R Fi : ; ; " - i
5 E
f , { = = ; E
t
HEEI
IIEH FI
_ElhlhlElElE
FIFIFIFIFI FIFI
lEl lEl la lEl lEllEl lEl lql
t:t tH til tfl tittfl tH trl
6 E
E :
|;llEl |:l|:llcllal H
lEl EHEEE
EH
.i
F]F]F]F]F]F]F] Ffl F1fl FFlF]F]
t-t t-t t-t t-i t-t t-t t-l l-l l-l l:l l:l l=ll:l l:l l:l
v q
8
b
E
x F]E]EEE]
E]H lit t:t t:t tEt t:ttEtt:t t:l
ElElFl Fl Fltg t9 Fl
6 9
6 9
d
t
fs$'l,i{igisE,
;$s is$,
fu
igEl'u*
$
d
a F 6 E F
E
: O A U E
s
ata
lpp.ndlr D: rBrructl.n Fomri .nd nnhg
lnstruction Encoding
All instruction encodingsare subsetsof the geneml instruction fomat sho.rvnin
Figure D-1. Instnrctions consisrof one or rwo primary opcode byres,possibly an ad-
dressspecifief consisting of the mod r/m byre and scaledindex byre, a djsplace-
menrif requ'red.Jnd an rmmerlia(e drlr ficld it requjred.
Within the primary opcode or opcodes,smaller encoding fields can be defined.
These fields vary accordingto the cla$ of operation. The fields define information
such as direction of the operarion, size of the displacements,register encoding, and
Figure D{ illusrrates some of the fields thar can appear in an instrucrion, such as
the mod field and the r/m field. Severalsmaller fields also appear in cerrain instruc-
tions, sometimeswithin the opcode bytes.The follovr'ingtabl is a complete lisr of
all fields appearing ln the 80386insrruction ser.Detailed tablesfor each field aDDear
laterin this aDoendix.
419
tlrE 0oat6 BooK
Fteldswtd n a03a6Insaucdons
of,Bt s
Specifiesvhether data is b)1e size or li l size (full slze I
is either 16 or 32 bts)
Specifiesdirection of data openrion 1
Specifieswhether an immediaredata field musrbe I
Gneralregisterspecifier 3
Addres, mode specifier (effective addresscan be a
3tor m
Scalefactor for saled indq add.essmode 2
ceneral registerto be used as index registef 3
ceneral rgjsterto be used asbaseregiste. 3
{e82 Segmentrcgister spectfier for CS,SS,DS, ES 2
s.e83 Segmentregisterspecifief fof CS,SS,DS, ES,tS, GS 3
lor conditional i.s!.!tctions, specifiesa condlrton 4
assertedor a condirion neSated
42l'
ApD.ndlx Dl tmttuction Fm.t .nd ttfrtnt
421
tHE g0396 BOOX
000 AX
001 cl, c)(
0t0 DI DX
0tt tsL BX
100 AH
t0l CH tsP
110 DII SI
1t1 BH DI
000 ]]AX
001 CL Ec)(
010 DL EDX
0tl Bl. EDX
r00 ,\ll ljsP
t0l cit uE!
110 DII tssl
111 BII ]]DI
C\J ES
01 cs
lo
11 DS
422
ADF.ndlr D! h.tructlon Fotfr.t.nd tldltr9
000 ES
001 cs
010 ss
011 DS
100 IS
101 GS
110
111
4n
tE ao3a6 tooK
01000 Ds{EAX+dsl
01001 DS,{ECX+d8j
01010 DS,IEDX+d8]
01011 DS,tEBX{d8l
01 100 s'i-b is presen!
01 101 SS;IEBP+d8I
01 110 DS:tESI+d8l
01111 DS,IEDI+d8]
10000 DST[EAX+d321
10001 DST[EO(+d321
10010 DS,tEDx+d3z1
10011 DS{EBX+d32j
10100 s-i-bis present
10101 SSdEBP+d321
10110 DS,lESr+d321
10111. DST[EDI+d321
42/|
Apptrd|l D! In.tructi.n Forn.t.nd timi.s
11000 EAX
11001 CL ECX
11010 DL EDX
ll0tl Bl- EtsX
1 11 0 0 A]I TSP
1 11 0 1 CH EBP
: 1 11 1 0 DH ESI
11111 DH EDI
Encodlng of 32-Btt Addrss Mode (mod r/m Blte and s-l,b Btte present)
00000 Ds:tE^x+(sNledind(x)l
00001 DS,tDcx+(scaled index)l
00010 Ds:lEDx+(scalcd indeDl
00011 DS:IEBX+(sc2lcd indcdl
00 100 Ds,lEsl,+(scrled
index)l
00 101 Ds:1d32+(scnledindcx)l
00110 Ds:IDSI+(sc^led
incicx)J
00111 DStEDI+(s(alcd index)l
10000 DSIEAX+(saledindex)+d321
10001 Ds:IECX+Gcaled index)+d32j
10010 Ds:lEDx+(sc,led indcx)+d321
10011 Ds:DBx+(scaled indei+d321
l0 100 sslEsP+(saled index)+.1321
10101 sslEBP+(saled index)+d32]
10110 Ds:IESI+($aled index)+d321
10111 Ds:IEDI+(s!alcdindex)+d321
NOTni Mod lieLl in mod r/n byrq ss, ind.x, llse fiekls in s i b byte
ss 5.414 Factor
00
01
:10
:l:l x8
425
tHE A0306 BOOX
000 EAX
00:t ECX
0:10 EDX
0:L:l EtsX
100
101 TBP
ll0 tisl
111 FDI
' Vhen index ficld is 100,indiati.g no indq rcgistcr, sslield 'nust equal 00. Il index is 100and ssd@s
not eaual()0. the etTdtive addre$ is undeJind.
01000 D5:lBX+SI+d81
01001 DS:lIlX+Dl+dill
01010 ss,lBP+sr+d8l
01011 sslRP+Dl+d8l
01 100 DS:lsr+d8l
0r 101 D5:lDI+d81
01110 ssttsP+d8l
0 11 1 1 DS:U]X+d8l
10000 DS:t8X+Sr+d161
10001 DSTIBX+Dt+d16il
r0010 ss,lBP+sI+dl6l
l00ll ss,lBP+DI+d161
10100 DS:lSI+d161
l0 101 DS,tDl+d161
10110 SS:tBP+d16il
l0llt Ds,[BX+cl16i]
425
lpp.ndlr O:Irtrrlcrlo rormat.nd llmlne
11000 AX
11001 CL (x
11010 DL llx
11011 BL BX
11100
11101 CH BP
11110 DFI 5l
11111 BH DI
1 10 0 0 EAX
11001 CL uc-x
11010 |'I, ltDx
11011 llL Iillx
ll100 ,\H tisP
ll 101 CH I]I]P
11110 DLI ESI
11111 BII EI)I
427
THE A03A6BOOK
Signexreoddata8 ro fill
16 bit or 32'bndestination
o Ovcriio'w 0000
NO No overflow 0001
1]/NAE Rek)Vnot aboveorcqual 0010
NIJ/ ll Not belovabove or equal 0011
ta/7, [quil/zero 0100
NIANZ Notcqul/not zero 0101
BI]/NA Dclow or equal/not rbove 0110
NI]E/A Not bclowor cqual/abovc 0111
sign 1000
NS Ndsign 1001
P/PE Prrity/prrityeven 1010
NP/PO Not parity/prrityodd 1011
L/^_Gli Lessthan/notgreateror equal 1100
NI,/CE Not lessrhan/8.cateror cqual 1101
I,I]/NG Lcssthm orqual/grcatcr tha. 1110
NIE/G Not lessor equrl/greatcrthrn 1111
000 cR0
010 cR2
011 cR3
Donotuseanyotheren oding.
424
apD.ndx D! In.ltFilotr Fom.t .trd tlnlnt
000 DRO
001 DRl
010 DRZ
011 DII3
ll0 DR6
111 DR7
Do nor useany otheren(odin8
110 TR6
111 TR7
Do not use any other encoding
80387 Extensions
The table beginning on the iollowing pag shows80387extensionsto the 80386
instrrctionset.
4n
Instructlon Encoding/Tlmlng
s r'(0)roi,ncrcr/Qr mefrry
s 110)roron!inrct.r Dcnory
ri(ioMP- (i.niptrexodpop
FCOM}P-coinfrcandpp
430
I!futronEnodtlgr'nntrr*i @!,kl
4it1
bredonn'codrng^tntns, .,,n !./
T I Y .
-
: :: ,rr-zr" . ,t
FP1!\Nr=rxdirLknsed.f sT(0) at]m TU]m---]
I lrm Txmdi--_l
| s. @r T11lffi l
I rm Tnmm----l
rYr.2xtr-s1(l).toFrGT(o)) f !s.io, Tllm I
l_l
-lsmol T,11roor-
f s or TlMtn---
I sc 1I lrL000N I
[D(:\v.Jdd.rrr01*ortt s o0r I H 0 0r 0 ! R / f sJvorsP
|s1(:\u-srk(ndwi)d -"s ri--T@lih^ rri,rcr-N-
-ri 'oi--l-iot ,,, R/,i-f-mp'spl
-Tinooori---
r !sa-o
IsrriNv-sr&envtonfunr - rsr @i--rHooro ra-r srslo;
f s ooi--TNoo mtH rr!m;i-
f Es-n Tioo im-friTilr--r
I s( r0 i00r@ F/N I srsiory l
rrNcslP-r'rcicfunrrx.k|nid.r
---.l
--ismi---Tmw,
FDrCSlr'-D(rc'nenr!!.k|sink' f-rm Tnmd---l
I rs. 1or Tm; $nt-l
Il
I rs. ou Th,oooo
Sh4dul arca\ t .licate inntrcti.ns ttut a/e frat avilahte b t]'057la12117.
NOTIS
a. when loadin8 single-p..rision or doublc-pr(nion 0 from memory,.dd 5 ct..ks.
b. ,^d<lI clocks to rhc linjle wh.n d - L
c ^dd I .lk ro cr.lr ran,jewhcn i - l
.l Add I .locks to rnc ranse wher d = 0.
e rypical - 52 (whcn d - 0,.16-54, r''pi.!t = ,19.)
I Add1cl@k b lhe mngcvhen lt = 1.
j. Thesc tnnings hold for operands in rhc range x <,r/.1 r.oropmnds nol jn this rangc, up
!o 76
-dJr' ,tu ,lo.Ln rhr b. nap wrrdu.c ..\ .prr.nd.
m . 0< s T ( o <
) 6,-@<sT0)< + 6.
n.0 <lsT(o) < (2 - SQRT(2))/2, -* < sTO) < + *
432
Appendix E
TNSTRT.'GTION
DISASSEMBI.Y
TABLE
4:t3
THE A03A6 BOOK
Bttes b,tes
ADD Ib, reglj .0F 8Cdisp32 JLdisp32(JLIJNGE)
ADD Ed, reg32 .0F 8D disp32 JNrdisp32(JNIrcE)
ADD rc8,Eb ,0F8E disp32 Jr! disp32(JrllrN(;)
dDD regl2,!d '0I8Fdhp32 IIX disp32(JNLIj4CE)
ADDAT, dAIAS SETOUb
.0t.1dr!32 ADD EAX,d!u32 sEfNo !b
,06 PI]5II I.]S SETI]Eb(SIJ'I'B/SETNAE)
'07 POP!S 0F 93 ca surNtsEb(SI'iNIJ/SETAE)
08 ca Olt Eb, rc88 sril z ljb (sETzstlft)
Olt Ed, rcg32 0f 95 er SE'I'NZ Ib (SETNZ/5IJ'1NE)
OR fegti, Eb Oti(Xe. sETaritib (SETRE/slil\A)
ORreg32,Ed sETNRli!b (SETNBD/SI11)
0F 98 ca SITSEb
10Ddata32 oR IiAX, drtd32 (Ir 99 ca stlT\'s Eb
"0li PIJSI I CS srlP Eb(sETr,/sli't?E)
sLl)T Dw sli'tNPtb (SETNP/s[tPo)
51lt ]]w SETI, Iib (SITLISETNGI')
0f C\)cr/2 LLryt |w 0l:9t) cr $j rNLBb(sti INL/SETGE)
0l' 00 er/3 L-tR ltw 0I 9li cx sul tE lrb (sril t_,j/sltTNc)
VEItli liw sriINLEEb(sli'r'Nr.D/
oir00 cx/5 VIIRV liw 5'ilcE)
(JIr0l cr'/0 SGl)| lir PIJSII l{i
0l:01ca/l Sll)1 [a POPIS
0l 0l cn/2 I(lltl lln llT lid, rcg32
olj 0l cr/3 l,ll)l na SHLDt:d, rc!32,data8
0f 01 cnl,1 SMSV!w SHLDEd,rqt32,CL
0f 0l crl6 IMS\V Ii$, PLJSLI GS
. 0 F 0 2c . I- R reg32,Lw IOP GS
Lsl-reg32,liw llTS lil, rc832
0! 06 cns SHRDEd,rc832,.lxrl|tl
01j20-/n/r.! lilov cR., reg32 sHltD Ed,rcx32,cl
0I2l -/n/rc8 Mov DFq reg32 lMLiLreg32,ljd
oF 22 /nheg Mov rc832,cRn l.SSre832,!a
OF23 /nheg MOV rcg32,DRn r 0 F8 3 c r BTREd,rell3z
MoVTRn, rc832 LFSrc832,Ea
oir 26 -/nrcg MOVrc932,TRn .Oli 85 ea LGS.eg:12,lia
.0f 80 dhp32 lo dispj2 MOVZX reg32,!b
.0f 8l disp32 JNo.lisp32 MOVZ\ re&32,liw
'0F82 disp32 JB disp32(JB4NA|) BT !d, darail
'0F83 disp32 JNtsdispj2 (JNB/JAIj)
"0F8,1disp32 Jz tisp32 (JZf)E) tsTRFil, data8
,0Fil5disp32 Jh'Z disp32 (JNr7JNE) BTCEd,data8
.0F86disp32 JRBdisp32(JBrlNA) ' 0 F B B e a B'l c Ed, reg32
,oli87dhp32 JNBEdisp32 (JNBI/ ' 0 F B Cc a BSI reg32,!d
JA] '0I BD ea BSRfeg32,Ed
'0! il8.lisp32 JSdisp32 '01B! ea MOVSXrc332,Eb
.0F89dhp32 JNs <lisp32 MOVSXre!32, !v
.0F8Adisp32 JI, disp32(I,PE) ADC Eb,regs
.0FilR disp32 JNl, disp32 (JNPfPo) ADC ljd, reg32
434
App.ndix Er h.rrmtion Dis...mhly Lble
Itrtructton Dts6semblYTable.
Aftes Bttes
ADCreg8,Eb rNctcx
. 1 3e a ADcre832,Id r42 INC EDX
INC EBX
i15 dara32 ADC !AX, dan32 .44 INC ESP
i16 PUSHSS .45 INC EBP
.17 l,ol ss '46 tNc Esl
18ca SDREb, re88 INC EDI
SBBEd, reg32 .18 Drc lrAx
ststsreg8, Eb ,19 DECECX
ststsfe832, Ed DIC EDX
lc dltaS SBDAL, datas DECE]]X
'lDdata32 SaBEAx, data32 '4C DECIiSI
.1ll I'USIIDS tlD DEC1]BI
.1I POPDS DlC 1]SI
AND Eb, reg8 .4f DECIiDI
AND td, rc32 .50 PUSHEAX
22 ca AND rcg8,Eb .51 PUSHECX
,23c) AND re832,Ed r52 PUSHI]DX
AND At, data8 '53 PUSII I]RX
'2t <l^t^32 AND EAX,data32 PUSLII]SP
26 ESI ,55 PUSII EI,}P
27 ,54 I'USII ESI
28ea SUBljb, rcSiJ '51 PUSHEDI
'29e sUB l,n, reg32 POPEAX
SUareA8,Eb "59 POPICX
r2B a^ SUB!eg32,Ed POPIJDX
2c datnli SUll A!, data8 .51] I OP EBX
'2D.lIa32 SUtsEAX,daul2 POPESP
2E CS: '5D POPEtsP
2t DAS 15E PO! DSI
xOR nb, regli "5F POPI]DI
XOREd, re832 I USIIAD
32 e^ XoR fegu, Db i6l POPAT)
,33ea XOR.e932,lld BOUNDre832,[a
34 drns xoR ,{t-, darail 63ea ARPLEw, rc816
'35 dat^32 xOR EAX,dan32 64 FS:
36 SS: 65 cs:
37 OPSIZI
3aea CMPEb, regS 61 ADRSIZ:
cMP Ed, !c932 .@ dat^32 PUSHdata32
CMl, reg8,Eb .69 eadxa32 IMUr re832,Ed,.lara32
CM? reg32,Ed PUSHdataS
3Cdatd CMPAL, dala8 IMUL reg32,Ed, data8
"3Dd^t^32 CMPEAx, dara32 6C INSB
3E DS: .6D INSD
3F 6E oulsB
INC EAX OUTSD
435
fltE ao3a6BooK
Bltes Attes
70 disp8 JOdisps i8B ca MOV re832,Ed
71disp8 INO disp8 8Ccals MOV Ew, veg
72 dispS .lB clisp8(lElJNAll) . 8Dea rEA rcgl2, L
JNB.lisp8(JNts/JAE)8Eets MOV src8, Ew
Jzdispa(JaJD 18Fea
75 disps JNz.lisps(JNzfNE) 90 NOP
JBlldisps(JRli4NA) i9l xcltc EAx, licx
JNBIidisp8(JNBtj/J ) .92 XCI1CEAX,EDX
Jsdisp8 .93 XCTIGEAX, EtsX
7t displ.] JNs.lisp8 '94 XCFIGI]r\X, ESP
Jr clhps(JPIIE) '95 XCHG EA\ IIJP
JNr dispeUNldl,o) "'6 XCHGEAX,I]SI
Tcdisp8 JLdispu(.llrNc!) .97 XCHGEAX,EDI
7l)disp8 JNLdisps(JNL4cx) .98 cBv// clr'DE
JLlidisps(.lLEllNc) 99 c-!(D
JNLEdisp8(JNLE/Jcli) 9]\ oifset32 CArl,offsct32
,\DD Eb,dara8 98 \fr\IT
Olt !b, datx8 '9C PUSTIFD
.9D POP]ID
SBIIlib, &talJ 'E SAIIII
,{ND lib, drta8 9r I,AIII:
SUII[b, dxta8 MOv AI-,ldisp]
XOlt Eb, d2rn8 MOVnAX, klispl
CMI)Eb,darr8 MOVldispl,AL
AI)D Ed,data32 MOVldhpl,llAX
OR ril, datN32 MOV$]l
ADC f.il, drn32 .A5 MOVSD
sDBEd,dxu32 A6 CMPSR
AND Ed,data32 CMPSD
SURDd,data32 'I' lgt
AL, data8
. 8 1 e a / 6 d a r a 3 2 XORtkl, dara3z ' A9.tar^32 1lST EAx, da1a32
'81 c., dNr$2 cMP Ed,.lata32 STOSA
.,\lJ STOSD
OREd,dr1a8 LODSB
ADc Ed,d a8 LODST)
SBRE l, .lara8 scAsu
AND lld, dara{l scAsD
sUB Ed, data8 B0 da!i8 MOV ,\r, dara8
XOREcl,dataS Br d2a8 MOV CL .lata8
CMPEd,datas 82.lara8 Mov DL, data8
'I
IjST Eb, fc88 B3 dataS MOV BL, data8
TtS I Ed,reg32
XCIIC Eb, reg8 85 datas MOVCH,data8
XCHC !d, reg32 ts6dda8 MOV DH, dara8
MOV Eb, .c88 87 dari8 MOV BH, daraS
MOv Ed, reg32 ! Ba data32 MOV EAX, data32
MOV reg8, Eb ' 89 data32 MOV EC( data32
435
ADp.ndlr E! lFlrucilon Dl-..{blt t l|.
Lrstruc"donDtsa3re$bly\^bE continued
BJ'tes ryos
!BA daa32 MOv EDX, data32 .Dle^n SAREd, 1
.BBdaa32 MOv EB)q data3z D2e /O ROI Eb, CL
.Be daa32 Mov EsP,dita3z D2ea/1 ROREb, CL
.BDdata32 MOV EBB data32 D2ea/2 RCLEb, C!
rBEdara32 MOV ESI,dara32 Dze/a RCREb, CL
rBF data32 MOV EDI, data32 DZea/4 sHL Eb,Cl
c0 ealo data8 ROLEb, data8 DZe /5 SHREb,CT
c0 eall dalaS ROREb, daa8 DZean SAREb, CL
C0 e^/2 dxr^8 RCl,Eb, data8 'D3 e^10 RO! Ed, CL
C0 e^/3 d^r^8 RcREb, dataS iD3 e^lr ROREd, CL
COe2/4 d^^A SHr Eb, dataS tD3ea/z RCLEd, CL
co ea./sd^a8 SHREb,data8 RCREd, CI,
c0 et7 datas SAREb, d1ta8 ,D3 ea/4 sHL Ed, Cl,
.CLealod^aA ROl,Ed, dataS .D3 ea/s SHREd,CL
.Cleal1data8 ROREd, dat.8 .D3 e /7 SAREd, CL
.C1e /2 dx^A RcL Ed, data8 D4
.ct e /3 d^taa RCR!d, data8 D5 AAD
'cl e /4 dar^8 SHLEd, dara8 D7 )OAI
'C't e /5 da6A SHREd,data8 D8 ESC0 (NDP)
'Cl eal1 dataa SAREd, data8 D9 ESC1 (NDP)
C2dara16 RETdara16 DA ESC2 (NDP)
RET DB ESC3 (NDP)
LESre832,Ed DC ESC4 NDP)
,C5e IDS reg32,Ed DD ESC5 WDP)
c6 adataS MOv reg8,dataS DE ESC6 NDP)
Mo\ rc932,d^t^32 DF ESC7 NDP)
C8dara16 data8 ENTERdata16,data8 E0dispS LOOPNBdhp8
LEAYE OOOPNE/,I,OOPNZ)
cAdatal6 RETFdatal6 El dtspS LOOPEdtsp8
CB RETT (LOOPE/LOOPZ)
cc INT 3 E2dispS IOOP dtspS
CD d*a8 INTdata8 E3dispS JcxZdtspS
CE INTO E4 dltl8 IN AL, dataS
CF IRET iE data8 tN EAx, dara8
D0 ea/o ROt Eb, 1 E6 dataS OUT dara8,AL
D0 eal1 ROREb, 1 ouT dara8,EAX
D0 ea/z RCLEb, I rE8 e$2 CALLea32
Do ea./3 RCREb, 1 E9disp3z JMPdisp3z
lxe /4 SHLEb, 1 .EA ea48 JMPFARea48
IX ea/5 SHREb, 1 EBdisp8 JMPdisp8
DOekn SAREb, 1 EC IN AL, DX
.D1ea/0 ROLEd, 1 iBD IN EAX,DX
tDl eaJL ROREd, 1 EE OUT DX, AL
'Dl ea!2 RCr Ed, 1 "EF OUT DX, E,{X
.D7 e /a RCREd, 1 !! LOCX
.Dlea./4 SHI,Ed, 1 E2 REPNE/,REPNZ
"DL e2/5 SHREd, 1 F3 RNP/REPE/lREPZ
4i37
rNtrucdon Dlsa$embly Table.
Bttcs Bttcs
HII !8 cLc
!5 cMc
'l
1,9 sTc
LiSl Ib, data8 cLt
16 ea/2 NOT!b !B sTl
NI]GEb CLD
MIiL AL, Eb ID S-I'D
IMUI AL, Eb INC I]b
Dlv ,|.r, tib DECEb
IDIVAI-,I]h INC Ed
-lliSTlid, DECEd
dara32
NOT Ed C,\LL td
NEGEd CALI,Ii\RCA
MUI EAX,Ed JMPEd
IMULEAX,!d "|1. ct5
DIV E X, Ed ?IJSIJKI
I D I VI J A XI,i d
80387Bxtnstons(NDP Escapes)
B!tes ales
lr8 crlo E{DI) Iienlr2 D9 DO IINOP
D8crvl IiMULlteal32 D9rio IICIIS
D8en/2 I'COMRcal32 D9 til FAI]S
D8cd3 ICOMPRcal32 D9 E.4 !'TS1'
D8 a!/4 FSIIBRerl32 D9 E5 IX{M
D8 e/5 FSUBR Real32 D9 D8 II,D]
FDIVIteil32 tD ii9 FLDI,2T
lDM Rcrl32 D9 UA FTDL2E
D8Co+i IADD Sf, ST(i) D9 EI] FLDPI
D8C8+i fN'tu.sT,sT(i) D9 EC I]IDG2
D8 Do+i FCOM STST(D D9 ED IIDN2
D8 D8+i FCOMPST,ST(i) D9 Etr t \.Dz
DI] EO+i FSUtsST,ST(i) D9 FA f2xM1
DUE8+i rsuaR sll sr(D D9 FI !YL2X
l:Drvstl s](i) D9 F2 IPTAN
DS la+i FDIVR ST,5T(O D9 F3
D9.a/O FtD Rerl32 D9 F4 !](,I'RACT
D9 aa/2 FSTReal32 D9 F5 IJPRI]M1
D9 e,/3 FSTPReal32 D9 F6 IDNCSTP
FLDENVEa D9 F/ I]INCSTP
D9 ex/5 FLDCWEw D9IB FPREM
Fli l ENv !a D9 T9 FYI''PI
D9 cal7 l-STCwF-w D9IA FSQRI'
D9 C0+i FLD ST(i) D9IB fslNcos
D9 C8+i FXCHST(i) D9 TC FRNDINT
434
ADpddlt E In.td.tion d..smuy l.hl.
Btttes Bttes
D9 FD FSCALE DD ea,t2 FSTReal64
D9IE FSIN DD ea/3 FSTPReal64
D9 FF FCOS DD s/4 F&STOR!a
IIADD Ind(t DD eal6 FSAVEEa
IlMUl,lntlo DD ean FSTSVEw
DA ealz FICoM Irt16 DDCo+i TIRDE ST(i)
DAe /3 FICOMPlnt16 DD DO+i TSTST(i)
DA.ea/4 FISUDInt16 DD D8+i ISTP ST(i)
DAd/5 t-IsUBRIntlo DD EO+i FUCOMSTC)
FlDIvlntlO DD I]8+i FUCOMPST(i)
DA ean FIDII'R Inr16 DE eto FIADD Int32
DA E9 FUCOMPP DE eall FIMULInt32
DB ealo FILDInt16 DB e^/2 i_ICOMlnt32
DB ea/2 FISTIntl6 DE e^/3 FICOMPIn62
DB ea,/3 !lsTP Int16 DEe /4 IISUB Int32
ILD Real8o DE ea/5 IISUBRInI32
Dtsea/6 ISTP Real8o D Ee J 6 IIDIvlnt32
DB E2 FCLEX DEe /7 IIDIVR Int32
DB E3 FINIT DE Co+i FADDPST(D,ST
DC e^/O FADD Real64 DE C8+i FMULPST(i),ST
DCe /1 FMUI,Real64 D]I D9 FCOMPP
DCe /2 FCoM Real& DE EO+i FSUBRPST(i),ST
Dce /3 FCOMPReal64 DE E8+i rSuBP ST(i),ST
DCe /4 FSIIB Real64 DE FO+i IDI\TP ST(t),ST
DC e^/5 ISL]BRReal54 DE F8+i FDIW ST(I),ST
Dc e^/6 IDIV Real64 DI eal0 llLD Int32
DC ea/7 FDIVRRed64 D\1e.Jz IISTI.t32
DC Co+i txDD sT(t),sT DFe /3 IISTP Int32
DC C8+i FMUTSTO,ST DFe /4 rBlD Bcd80
DC Eo+i FSUBR STO,ST DF eal5 FIID Int64
DC E8+i FSUBST(i),ST DFe /O FaSTPBcd80
DCro+i FIDI\'RST(I),ST FISTPlnt64
DC F8+i !DMT(), ST D! n0 FSTS!(AX
DD ealo IID Realtr
439
Appendix F
aoaG-FAMlLy
PROGESSOR
DTFFERENGES
Real.Modo Differences
Between the 8085 and the 8O386
The 8086 processordoes not generateexceptions6, 8-13, and 16.
Instructions execut more rapidly on fie 80386;in most cases,addressdecode time
441
The count vatue for shift and rotate instrucrions is taken modulo 32 in rhe 80386.
The full valuc(up k) 255)is usedon the 8086,which canresultin long instntction
442
tFC.ndir tr aoo6.Fsily Pr6..er Dlll.EnM
443
tHt oo:t66aoox
u
lndex
A B c
AAA(AscllAdju$AfterAddnion) bkklink. S li.kffeld cacne, pageBble18,129 30
149 bdeaddres CALL(P@ed!rc Calls)17274
AAD(AscII AdjusrBefde of theGDTt c^l gare96+1, ]04.12r
DMsion)l' ofthelDT2T cary flag(cF)26
MM(ASCIlAdjustAfter <nase96enr16-49,r.9e-,9,CBw(convertBtretovbrd)l7t
Multipli.atidD151 L36 CDQ(Coftrt Doublmrd io
AAs(ASCIIAdjustAfte. b6ed addresi.g Quadrurd)t6
subtB.tion)l52 alone58 59 ctr rcgisrer22
abdt (d.eption .las) plu dbplenent 69 CrC(Cler Carryllad 177
cop!@e$orseSnrntovdun plu displ2@ment plus cl,D (cle^r Dnerion rlas) 178
(lNT 9) 116 index71 ctl (cle Inteflupr!l.d 179
deflned111-t2 basepointd (EBP)regGter Cl,reglster22
doublerauh(lNT 8) 115 3-4.59 CLIS(clea.l sk swnchedBID
acce$ed(A) bit 52 ba6eregistes6a-71 r80
a.ce$ itghts51-12,8Z 137 BCDltrkErlons CMC(CotoplenedC2riyFIag)
accumulaor 2,66,75,83 floarlrs-poinr89 181
Aoc (Addwnh ca!9 153 intese'76 anP (comprreI eg*)
ADD(InteAtrA<lditlor)154 BCDnumcic tonat 21,30,34 B2-V
addreses BHrcglstr22 cMPs(conpareSring)184-5
effetireI,83 bias,fl@tlng.pointexponent codesegnc s 5r-t2
pht*al18,45,125-27,129, t-33 compatibilitywith 8046441+3
132,137 big(B) blt 52 @mpatibllltywlth 80286443
sesmnt/offser46-47 binaryfretion! 3rj3,38 @ndirlon<odes
ent! t45-41.\02,125-27 bit lnsrlMriom76-7 8038738-40
addlessin8 rDdes..t@Irstructior blt strlngs18-1176-7 EFLACS rcai$ter23-26,80,82
operands BLreglsre!22 Jd (ltmpif Cond,toDa2
addle$ ffaNlatlon Boolearlm$ualons76 SE'|.. <*rBytc on Cddtiofl,
vhual !o llner 46-47 BOUND(Chek Array 79-O
vinualio physiqll2'-26 B@rdfjeo 158 @rfqming scgmnrstl
afiineclosule39 boundschek fault(lNT t) &.kollsructiors 92
AH register22 111,11,{ .ontrolcgiltcD (CRo-CF3)
alL5segrenrs103,121 BPrca\st* 3-4,22 Z7:29.79,\07,77A,D8
AL .gl6te.22 b..nch instr@tiorua1-8, contrcl0ansferinsrlutions
AND(B@leanAND)155 bekpoinireSi$e6121-24 at-82
architerure berkpoi kap114 controlwod (CW)rcgister39
mlDlion ot l-U BS!(Bit S.anFdMd) 160-61
mic@hiteture 16-18 BSR(Bir &m Beverse) 162 etulation of 28
dtthfttlc lnstr@tios mGnTsr164-65 envionoent91
floadng-poha9-9o BrcGnTe andcomplemot) insr.@do!s8a-92
i,nreg*7415 166-67 iarrcdNtionof6-7
arithneticshifts7a BTR(Bil Teslmd Refo .ucric fdnaG 30-34
ARPL(AdjustRPL)156-57 1@-69 resisr635
day indding. .ta salioA BTS(BnTstmd Seo00-Z coprocsu{jlu faultONT16)
ASCIIinsf(tions 76 BsI df&e Unft(BIU) 17 119
ASCUnmeric lomat a busldk (LcK\) 79,86,130 @pr@sw noraBilablefault
ASCnbbb3S3 burtB)bit106 0NT7)115
auiliary c.rryfLg (AF)25 blsyTSS104,1OA oprdsu sg@t mnun
available(A\a)bit52 ax rSisrer 22 (IM9)116
tl4i
tHE AO3a6 BOO|(
cs segmentrejliste.t, 26,45,tl, dn{dy, pager$le rt8, i27 n exeute o.ty segmdrs t1-52
94,136 31 Dr register22
cutrent priviLge level (cPL) 2 i, dniy (D) bit r27 expmd .losr scginnrt 51 5l
130
5r,9.1-95, dhabrcinter4lr (cLD 25j erFnent, flmting-poi 3t 3l
144-41 cxrende<lPredsion floarin8 point
r\\ teMrd) 1s6 (enPrenD 3r,33,13-39
CWD!(Conve(Wod to DIV (UnsignedDivjsion) t9l dcnsior type (I'r') bft 23
DoublewodExtendcd)
F
IABS (,\breluie VaILE)309
83337(Zti e{.eprion) 38
D FARCALIr a JMI,S107,lrz i21,
D ,^ (l ){ nnxl Adju$ AL llier doublefauli(INT 3) 111,115
double Prsision forut (long
DAs (Dc.imil AdjL$tAL Aftcr FBLD(BcD li,d) Jr2
rBSTP(RCD Skr and Pop) 313
doublsods (dwo(ls) 20
rCI Is (chan8csign)314
drla lrrnslcrinsru.tions79 80 Ds sqhcrt rcgister5,45.3J
rCL!:x (clcar[xcc$iont 315
FCOM(Comprrc, 316-17
SCII2I
IlcD 2t,3,1 FDICSI?(D{f crcnl Stack
E
doubl$ods(lqodt 20 liDlVl (l)ivisionltcvc6cd)
446
lnt r
tutT
tHE AO3A5BOOX
LOP.! (,Np De.rement E()( NoT (B@la. complement) 212 prdision excptio. (PE) bit JB
and Ban.h) 225-26 not PresenllaultONTn) lr7
LA [oad scgnmr Rcghtc, 229 ,\DnsrzS687,rl7,138
LSI-(LGd SesmentLinil) LOCK36, 13O,141
227 28 BCD21,30,34 OP5IZ36, DZ r33
,|44
lndd
w.itabledaa seSndts 51
XCHCGrchange)305
p e4 t26-2a,L)3-.14 I<LAIB(Tra.slaieBfe) 306
l@di.a md sding 79,l01,107,
segmenrs52,t6 60,117 xOR(Bolqn EklusiveOru307
2eo divideexcepd@(zE) bir 38
in vlftu.l .ddrssing 46,60
zrc dividf2ult(INT 0) n4
tl.4tt
ROSS P. l{Et-rSOl{
The manus.ript for this book ras preparedand submitted to Microsoft Pressin
electronic form. Text files were processedand formatted using Microsoftltbrd.
cover dsign by Hornall Anderson Design.works
Interior text design by Darcie S.Furlan
Illustrations by Becky ceisleFJohnson
Princlpal typography by Lisa c. Iversen
Text composition by Microsoft Pressin Garamondwith display in Helvetica
Black, using the Magnacomposition systemand the Linotronic 300 laser
imaaesetter,
ffiyffirlss
ffiffiefistlm
He.eis a clear,comprehensive, andauthoritotive introduction
to the chip
that isthefoundation of today'spopular,high+oweaed micaocomputers.
Wfittenfor everysefious programmer, THE80386 BOOKincludes
scoresof superbassemblylanguage examples alongwitha detailedanalysis
ofthe chipitself.RossNelson,o fofmerlntelprogrammer, mvers:
r the CplJ: its organization,
registers,and80287 6nd80387 math
copTocessors
'the momoryarchitoctuti linearyssegmented addressing,virtual
addTess space,segmentdescriptofs,selectors,sndvirtualmemory
. the instruction 6dts of the 80386 microprocessor andthe 80387
mathcoprocessor
. the 80386 protsction schoms:globoldescriptorandlnterruptdescrip-
tortables;selectorsisegmentandsystemdescdptors; inte.rupts,trops,
andfaults; anddebugsupport
. the implementation ofoviftual mEmorysystem throughpaging
. compatibility withprevious generations of Intelomicroprocessors
Of specislimportance isthe comprehensive, clearlyorganizedinstruction
set rcferencethat willbea valuable
resoufcefor 80386 programmers.
Everyassembiy-language programmef, microprocessor designengineer,
andstudentof computerarchitecture willfindTHE80386 BOOKan
excellentreference.
ISBN I- 551t5-138-1
u.s.A.
u.K.
Austral.
Irecommended]
s24"S5
f22.e5
$37.35 ilillilili
lfii
ilt