WINDOWS ASSEMBLY LANGUAGE & SYSTEMS PROGRAMMING 16- and tib-23 low-level programming rof eht CP dnaswodniW 2nd edition bY yrraBreluaK ,rerutceL htidE nawoCytisrevinU ,)EE(.cS.M .gnE.C D&RskooB ,ecnerwaL sasnaK64066 0 thgirypoC ,7991 yrraBreluaK llA sthgir .devreser oN trap fo siht noitacilbup yam eb ,decudorper derots ni alaveirter ,metsys or transmitted in any form or by any means, electronic, mechanical, ,gniyp o,cgontiodhrpocer ro ,esiwreht otuohtiw roirp nettir wnoissimrep fo eht.rehsilbuP nI siht ,koob ynam fo eht snoitangised desu yb srerutcafunam dna srelles othsiugnitsid rieht stcudorp yam eb demialc sa .skramedart euD tnemegdelwonkca si ybereh edamfo lla lag.enloitcetorp swodniW TM si a kramedart fo .tnfooistoarrcoipMroC Disclaimer. Whilst due care has been taken in the preparation of this book, no ytilibisnopser si detpecca rof yna ,ycaruccani ssol ro egamad ot ,atad rolaitneuqesnoc ssol ro .egamad ehT tnetnoc fo eht noinapmoC ksiD si ton deetnaraug ot eb yltcaxesa .debircsed si hndToeihtsiidleb uypb D&R skooB / relliMnameerF ISBN: 087930474X :NOITUBIRTSID USA adanaC srehsilbuP puorGtseW srehsilbuP puorG tseWadanaC P.O. Box 8843 345 dnomhciR teertStseW ,ellivyremE AC26649 etiuS322 Tel:( 800)7 88-3123 ,otnoroT oiratnO M5V 6Yl Fax: (510) 658-1834 adanaC Tel: (416) 504-3900 :xaF 4( )612093-405 KU dnaeporuE aisA lliH-warGcM gnihsilbuP.oC namgnoL eropagniS sregna&reppohS daoR 52 tsriF koL gnaYdaoR daehnediaM Singapore 2262 erihskreB 6LSLQ2 Tel: 65 268 2666 detinUmodgniK Fax6:25 6 780 23 Tel: 0800 810800 or 01628 502500 Fax: 82610 422077 :liam-eku.oc.llih-wargcm@seireuq nitaLaciremA lairotidE & gnitekraM ecitfO DIlanoitanretnI D&RskooB 621 ddlleOifedgadoiRR 1601 West 23rd Street, Suite 200 ,notliW CT 06897 USA ,ecnerwaL SK64066 Tel: )302( 2722-438 Tel: (913) 841-1631 Fax: )302( 5279-267 Fax: (913) 841-2624 :liam-e moc.ifm@sredro :beW moc.skoobdr.www//:ptth Contents Ch. Page Preface xi 1 CPU Architecture 1 elbmaerP .................................................. 1 pu-rewoP ehtCP ........................................... 2 ehT metsySseliF ....................................... 3 rebmuNsmetsyS ........................................... 6 sretsigeR dnayromeM ..................................... 9 CPehtfopaMyromeM .............................. 12 ehT UPC & Support Chips ........................... 12 lanoitnevnoC dna dednetxEyromeM ..................... 14 stnemgeS ............................................ 14 laeRedoM ........................................... 17 SOD laeR edoMgnimmargorP ....................... 18 SOD detcetorP edoMgnimmargorP .................. 18 gnidoCstniartseR .................................... 20 edisnI eht 286/386l486/etc. ............................... 2 1 UPCsretsigeR ....................................... 22 snoitcurtsnI .......................................... 23 laeR dna detcetorPsedoM ............................ 25 yromeMtnemeganaM .................................... 25 noitatnemgeSylnO ................................... 25 wodahSsretsigeR .................................... 26 srotpircseD .......................................... 28 683gnigaP ........................................... 28 68-lautriV ........................................... 29 noitnetnoCseussI ........................................ 3 1 Privileges.. .......................................... 31 O/IegelivirP ......................................... 3 1 ksaTgnihctiwS ...................................... 32 stpurretnI ................................................ 3 3 laeR edoMstpurretnI ................................. 33 detcetorP edoMstpurretnI ............................ 34 elbmatsoP ............................................... 36 I.. 111 iv Ch. Page 2 cisaB ylbmessAegaugnaL 37 elbmaerP ................................................ 37 kcatSsnoitcurtsnI ........................................ 38 refsnarT folortnoC ....................................... 39 lanoitidnoCpmuJ .................................... 43 gnisserddAsedoM ....................................... 44 tnemgeSsretsigeR ................................... 46 gnirtSsnoitcurtsnI ....................................... 47 citemhtirAsnoitcurtsnI ................................... 50 lacigoLsnoitcurtsnI ...................................... 54 edoC dna ataDslebaL .................................... 56 edoCslebaL ......................................... 56 ataDslebaL .......................................... 58 gnisseccAataD ...................................... 5 8 sretnioP .............................................. 59 ,SEL ,SDL dna AELsnoitcurtsnI ..................... 60 lacoLataD ........................................... 62 epyTedirrevO ........................................... 63 serutcurtS ................................................ 65 lebaLsetauqE ........................................ 66 elbmatsoP ............................................... 67 3 gninepO swodniW 69 Preamble ................................................ 69 SOD susrev swodniWgnimmargorP ..................... 70 lanretnIsecnereffiD .................................. 7 1 gnidliuB a swodniWnoitacilppA ......................... 72 yrarbiLsnoitcnuF .................................... 72 ehT scinahceM fo gnilbmessA dnagnikniL ........... 73 ehT kniLpetS ........................................ 74 owT spetS rofsecruoseR ............................. 74 swodniW gnimmargorPscinahceM ....................... 75 stcejbO .............................................. 75 seldnaH .............................................. 76 secnatsnI ............................................. 76 segasseM ............................................ 77 CxatnyS ............................................. 78 egasseMpooL ....................................... 78 kcabllaCsnoitcnuF ................................... 79 ataDsepyT .............................................. 82 V Ch. Page 4 ehT eraBsenoB 85 elbmaerP ................................................ 85 gnitteGdetratS ........................................... 86 slooTderiuqeR ...................................... 86 ecruoSseliF ......................................... 89 ecruoseR dna noitinifeDseliF ........................ 89 egasseMtamroF ..................................... 90 ekaMeliF ............................................ 91 tnempoleveDelcyC .................................. 92 noitacilppAerutcurtS .................................... 94 yranimilerP edoC .................................... 94 putratSedoC ......................................... 96 ){NIAMNIW ......................................... 98 kcabllaCnoitcnuF ................................... 102 5 leveL-hgiHylbmessA 109 elbmaerP ............................................... 109 edulcnIseliF ............................................ 109 tfosorciM susrevdnalroB ........................... 110 notelekSsisylanA ....................................... 111 LEDOM. evitceriD ...................................... 119 etavirP dna labolGataD ................................. 120 MSAM susrev MSATepocS ........................ 121 s’MSAT @@ ....................................... 121 efiL fo citamotuAataD .............................. 122 gnilbmessA dnagnikniL ................................ 123 6MSAM susrevMSAT .................................. 125 SWODNIWreifilauQ ............................... 126 sepytotorP .......................................... 127 kcabllaCngiseD .................................... 129 rehtOseitilibitapmocnI .............................. 130 MSAM gnilbmessA dnagnikniL .................... 13 l 6MSAM margorPgnitsiL ............................... 132 6 margorPngiseD 137 Preamble ............................................... 137 tcejbOgnisserddA ...................................... 138 gnillaC anoitcnuF .................................. 138 ylraEgnidniB ....................................... 41 1 etaLgnidniB ........................................ 142 ++C gnidniB ........................................ 142 vi Ch. Page ylbmessA egaugnaLgnidniB ........................ 145 esU foSIHT ............................................ 145 gnicafretnI htiw++C .................................... 147 gnilipmoC ot MSAP/O ............................. 147 eniL-nIylbmessA ................................... 148 eniLD-nI SO dnast’noD .............................. 149 ehT MSAbutS ...................................... 150 elipmoC dna elbmessAspetS ........................ 511 ehT gnizamA eniL-9margorP ........................... 153 A notelekSmargorP ..................................... 154 sedirrevO ........................................... 156 tratskciK ............................................ 157 egasseMgnildnaH .................................. 157 ehT WODNIWtcejbO .................................. 158 ........................................... 162 NIAMNIW ) kcabllaC ................................................ 165 (=AM ) ............................................... 168 ecnatirehnI .............................................. 171 gnitteG tirehtegoT ...................................... 175 elbmatsoP .............................................. 178 7 PC Hardware 179 elbmaerP ............................................... 179 UPCsub ................................................ 179 lortnoCsuB ......................................... 180 sserddAredoceD .................................... 182 stroPO/I ................................................ 183 O/IsnoitcurtsnI ..................................... 184 draobyeKecafretnI ...................................... 184 ssalC-TA draobyeK troPstnemecnahnE ............. 186 CP noisnapxEsesuB .................................... 187 yrtsudnI dradnatS erutcetihcrA)ASI( ................ 188 larehpireP tcennoC ecafretnI)ICP( ...................... 911 elbmatsoP .............................................. 194 8 ,SOIB ,SOD & Windows Low-Level Services 195 elbmaerP ............................................... 195 SOIB dna SODsecivreS ................................ 197 dradnatS SODstpurretnI ............................ 200 SOD detcetorP edoM ecafretnI)IMPD( ................. 203 hF2-TNIsnoisnetxE ..................................... 205 vii Ch. Page swodniWsnoitcnuF ..................................... 207 Thunking ............................................... 219 cireneGgniknuhT ................................... 219 eroM 59niW”stnemevorpmI“ ........................... 222 eciveD O/IlortnoC .................................. 222 yllacimanyD elbadaoL srevirD ...................... 223 sdaerhT ............................................. 223 yromeM deppaMseliF .............................. 224 elbmatsoP .............................................. 224 9 Direct Hardware Access 225 Preamble ............................................... 225 noitasilaitinI ............................................ 226 gnisserddAstnemgeS ................................... 227 tceriDoediV ............................................ 229 erotseRoediV ....................................... 321 egnahC oediVedoM ................................ 232 A oediV-tceriD edoM-txeTenituoR ................. 232 llaC NEERCSTNIAPER .......................... 234 lanidrOsetanidrooC ................................. 235 oT dna morF txeTedoM ............................ 236 oediV tuptuOseussI ................................. 237 tupnIegasseM ...................................... 238 gnitnemirepxE ...................................... 239 A oediV-tceriD wodniWmargorP ................... 239 I/O Ports ................................................ 244 10 emiT-laeRstnevE 249 Preamble ............................................... 249 TSRs ................................................... 250 gnikooH arotceV ................................... 251 ecivreS enituoR)RSI( ............................... 253 gnitseT ............................................. 255 erawdraHstpurretnI ..................................... 256 TX erawdraHstpurretnI ............................. 256 TA erawdraHstpurretnI ............................. 257 ’swodniW dradnatS edoM erawdraHstpurretnI ...... 258 tpurretnI reldnaHedoC .............................. 260 decnahnE edoM erawdraHstpurretnI ................ 263 tceriD yromeMsseccA .................................. 264 11 Real Mode Access 267