w04AssemblyLanguageBasics.docx
- 文档编号:28203824
- 上传时间:2023-07-09
- 格式:DOCX
- 页数:25
- 大小:332.85KB
w04AssemblyLanguageBasics.docx
《w04AssemblyLanguageBasics.docx》由会员分享,可在线阅读,更多相关《w04AssemblyLanguageBasics.docx(25页珍藏版)》请在冰豆网上搜索。
w04AssemblyLanguageBasics
Module4:
AssemblyLanguageBasics
Module4Overview
Thex86Architecture
Objectives
WhatYouWillLearn
Attheendofthissection,thestudentwillbeableto:
∙DiscussthebasicarchitectureoftheIntel®x86familyofmicroprocessors
∙DiscussRegistersandtheirfunctions
∙DiscussFlagsandhowtheyareused
Intelx86Architecture
Pentiumi-386isa32-bitCISC(ComplexInstructionSetComputer)processor,whereastheAPLHAisRISC-based(ReducedInstructionSetComputer).No16-bitcodeiscoveredinthismodule,howevertheprinciplesaresimilar.
Theprocessorshaveanumberofgeneral-purposeregisters.Registersarespecial-purposememorylocationsthatarelocatedinthemicroprocessoritself.Accessingregistersismuchfasterthanaccessingexternalmemory.Someregistersaregeneralpurpose,whileothershavededicatedfunctions.
General-purposeregistersareusedtoholdvaluesforcomputationandstoreresultsinternally.Registersingeneralarebyte,work,orDWORDaddressable.
Theflagsregisteristheonlybitaddressableregister.Anyinstructionsaffecttheflagsregisterbasedontheresultsofacomputation.Forexample,afterasubtractioninstruction,thezeroflagissetiftheresultiszeroandclearediftheresultisnon-zero.
Assemblyinstructionstake0,1,or2argumentscalloperands.Anoperandcanbeanimmediatevalue,registervalue,orapointertoamemorylocation.ThegeneralsyntaxforIntelAssemblyisoneofthefollowing:
PrefixInstruction[operand1],[operand2];comment
Where:
Prefix
-Anaddresslabelorinstructionmodifier
Instruction
-Instructionbeingexecuted
Operand1
-Typicallythedestinationoperand,butitcanbethesourceoperand(oneargumentinstruction),oritcanbeboththesourceanddestinationoperands(INCinstruction)
Operand2
-Sourceoperandfortwoargumentinstructions
SoforaMOVEAX,[12341234]instruction,thesourceoperandisthevalueataddress12341234andthedestinationoperationistheEAXregister.Thisinstructionwillmovethevaluestoredat[12341234]intotheEAXregister.
IntelCPURegisters
Intel386+processorscontainavarietyof32-bitregisters.Thei-386familyofprocessorsisknownasnon-orthogonal.Thismeanstheregisterandtheinstructionsetarenotcompletelyinterchangeable.Inotherwords,someinstructionscanonlybeusedwithcertainregisters.Forexample,INandOUTinstructionsarehardwiredtotheaccumulator(EAX)register,ECXisusedforcountingforloopinstructions,andEDIandESIareusedforindexing/stringinstructions.Otheraddressingmodescanonlybeappliedtocertainregisters.
Thei-386familyhassixgeneral-purposeregisters–EAX,EBX,ECX,EDX,EDI,ESI.Eachregisterbeginswiththeletter“E"for“Extended”toaddressthefull32-bitsinsteadofjust16.
Addressing
EAX,EBX,ECX,EDXregisterscanbeaddressedinthefollowingways:
AL
lower8bitsoftheEAXregister
AH
highorderbyteoflowerorderwordoftheEAXregister
AX
lower16bitsoftheEAXregister
EAX
thefull32bitsoftheregister
SimilarlysyntaxexistsforEBX,ECX,andEDX.
Note:
TheESIandEDIregistersarenotbyteaddressable.
RegisterTypes
General-PurposeRegisters
EAXisalsoreferredtoastheAccumulator.Thisregisterisusedthemostandcontainstheresultsofmanyinstructions.ItiscommonforcompiledcodethatreturnsavaluetodosousingtheEAXregister.
ECXspecializesincounting.
EBXandEDXaregeneral-purposeregistersandareusedforaddressingmemoryasapointer,usedasoperandsforlogicandarithmeticfunctions,andcontaintheresultsofinstructions.EAXandECXcanalsobeusedforthesefunctions.
IndexingRegisters
EDI,ESIaregeneral-purposeregistersthatspecializeinindexing.StringinstructionsuseEDIasthedestinationpointer,andESIforthesourcepointer.Therefore,inordertocopyablockofmemoryfromoneplacetoanother,ESIwouldbesetuptopointtothesourceblock,andEDIwouldbesetuptopointtothedestinationblock.ECXwouldbeloadedwiththenumberofbytestotransfer,thedirectionflagwouldbesetuptoincrementordecrement,andthenREPMOVSwouldcopythebytes.
StackRegisters
ESPandEBPareprimarilyusedforstackmanipulationandcontrol.ESPisthestackpointerandusedtopointtothecurrentstacklocation.TheEBPregisterisusedtopointtothestackframeforagivenroutine.Atroutineentry,theEBPregisteristypicallysavedonthestackandthensetequaltothevalueofthecurrentstackpointer.TheEBPisthenusedtoreferenceargumentsandlocalvariables.
FlagRegister
Theflagsregisterisacollectionofsingle-bitflags.Manyinstructionsaltertheflagstodescribetheresultoftheinstruction.Theseflagscanthenbetestedbyconditionaljumpinstructions.
InstructionPointer
EIPistheinstructionpointer.Itpointstothenextinstructiontobeexecuted.EIPismodifiedbyRET,RETI,JMP,CALL,andINTinstructions.ThisregisterisperhapsthemostimportantregisterintheCPUsinceitdirectstheCPUtothenextinstructiontoexecute.
Thex86Instructions
Objectives
WhatYouWillLearn
Aftercompletingthissection,thestudentwillbeableto:
∙RecognizethedifferenttypesofAssemblylanguageinstructions,memory,branch,conditional,stack,etc
∙DescribetheAssemblyinstructionsusedtocreatelocalvariables
∙DescribetheAssemblyinstructionstopassargumentsonthestack
Intelx86Instructions
Onthex86processor,instructionsarevariable-sized;sodisassemblingbackwardsisanexerciseinpatternmatching.Todisassemblebackwardsfromanaddress,youshouldstartdisassemblingatapointfurtherbackthanyoureallywanttogo,thenlookforwarduntiltheinstructionsstartmakingsense.Thefirstfewinstructionsmaynotmakeanysensebecauseyoumayhavestarteddisassemblinginthemiddleofaninstruction.Thereisapossibility,unfortunately,thatthedisassemblywillneversynchronizewiththeinstructionstreamandyouwillhavetotrydisassemblingatadifferentstartingpointuntilyoufindastartingpointthatworks.
InstructionNotation
Thegeneralnotationforinstructionsistoputthedestinationregisterontheleftandthesourceontheright.Theremightbesomeexceptionstothisrule.
PrefixInstruction[operand1],[operand2];comment
Arithmeticinstructionsaretypicallytwo-registerwiththesourceanddestinationregisterscombining.Theresultisstoredintothedestination.
Tosavespace,manyoftheinstructionsareexpressedincombinedform;whichmeansthatthefirstparametermustbearegister,butthesecondcanbearegisteroramemoryreferenceoranimmediatevalue.
MOVEAX,177
ADDEAX,EDX
Tosaveevenmorespace,instructionscanalsobeexpressedwiththefirstparameterasaregisteroramemoryreference,andthesecondcanbearegister,memoryreference,orimmediatevalue.
Unlessotherwisenoted,whenthisabbreviationisused,youcannotchoose"memory"forbothsourceanddestination
Operands
Assemblyinstructionseachtakeavariablenumberofargumentscallsoperands.Thisvariesonthetypeofinstructionandvariesfrom0to2operands.
ImplicitOperands
Implicitoperandsarespecifiedbytheinstructionitself.CLIandSTIareexamplesofinstructionsthatdonotspecifyanoperand.TheCLIandSTIsetandcleartheinterruptflagintheEFLAGSregister.CLIandSTIalwaysapplytothatbitonly.
RegisterOperands
Registeroperandsarewhentheinstructionreferencestheregisterforthesourceordestination.Someinstructionsreferenceasingleregister–whichcanbeeither,orboththesourceanddestination.
Example:
CALLEDI;EDIcontainsaddresstocall
MOVEAX,EBX;EAX<-EBXEAX=destinationEBX=source
INCEAX;EAX=EAX+1–EAX=sourceanddestination
ImmediateOperands
Immediateoperandsarepartoftheinstructionitself.Valuetobeusedfollowstheopcodeintheinstruction.Immediateallowstheusertoloadaconstantintoavariableorreferenceahard-codedorfixed-memorylocation.
Example:
MOVEAX,177;loadEAXwith177
MOVCL,0FFH;loadCLwith255
CALL12341234H;callroutineatlocation12341234H
I/OOperands
I/Ooperandsarenotusedinusermodeprograms.I/OinstructionsareusedtoreferencedevicesintheI/Omap.TheseincludemosthardwaredeviceslikePICS,serialports,parallelports,diskcontrollers,etc.
Example:
INAL,04H;readportlocation4intoALregister
OUT04H,AL;writeALregistertoportlocation4
MemoryReferenceOperands
Memoryreferenceoperandsareusedtoreferencememoryviaamultitudeofaddressingmodes.Thesesaddressingmodesincludedirectaddressing,basedaddressing,baseplusdisplacementaddressing,indexplusdisplacementaddressing,andbaseplusdisplacementplusindexaddressing.
DirectAddressing
Directaddressingiswheretheaddressitselfisusedasanoperand.Thisisusedtoaddressfixedlocationsinaprogram–aglobalvariableforinstance.
Example:
MOVAL,[12341234H]
INCDWORDPTR[12341234H]
BasedAddressing
Basedaddressingusesaregistertoholdtheaddress.Thisisusedtode-referenceapointertoavariable.
Example:
MOVAL,[ECX]
DECDWORDPTR[ESI]
BasePlusDisplacementAddressing
BasePlusDisplacementAddressingissimilartobasedaddressingexceptforanadditionaloffsetthatisaddedtothebaseaddress.Thisisusedtoaccessavariablecontainedinastructure.Thebase(register)pointstothestartofthestructureandtheoffsetallowthecorrectelementtobereferenced.Thisisbetterthanthepointerhavingtopointdirectlytotheelementsinceoftenmorethanonestructureisreferencedbyasec
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- w04AssemblyLanguageBasics