Thiết kế và lập trình hệ thống - Chương
✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑1✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳ArithmeticOperations:•Addition•Subtraction•Multiplication•Division•Comparison•Negation•Increment•DecrementLogicOperations:•AND•OR•XOR•NOT•shift•rotate•compare(TEST) ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑2✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳Addition,Increment,Add-with-carryandExchange-and-add(80486):Contentsoftherightmost8bitsoftheFLAGSregistercanchange(+Overflow)forarithmeticandlogicinstructions.Flagsinclude:•Z(resultzero?)•C(carryout?)•A(halfcarryout?)•S(resultpositive?)•P(resulthasevenparity?)•O(overflowoccurred?) ✁✁AL✂✄☎✆✆☎✝✞✟✠✡☛☎☞☞✍✌✎✏✍✑✒✓✔✕✖✗✘✚✙✖✙✖✛✜✖✢✏✌✖✗✣✤✥✦✝✧★✩✧✆✄EDI]☛☎☞☞✍✌✕✖✗✪✌✏✖✕✌✞✫✒✕✕✔✬✭✒✗✯✮ ✁✥ECX✂EBX☛★✫✰✱★✫✰✞★✦✰✂★✦✰✱✑✕✪✗✪✓✒✭★✫✰✮✲ ✁✁ECX✂EBX☛✳✌✖☞✬✑✕✒☞☞✪✓✗✴ ✵✶✪✏✓✷✙✌✮ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑3✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳Subtraction,DecrementandSubtract-with-borrow:Comparison:Changesonlytheflagbits.Oftenfollowedwithaconditionalbranch: ✁✂EAX✂EBX✁✄✥EDI☛✟✷✶✌✕✖✗✪✌✏✖✕✌☎✫✒✕✕✔✬✭✒✗✯✮ ✂✂ECX✂EBX;EAX=EAX-EBX✥✆✝AL✂✎✞✟✠ ✄LABEL1☛✡✷✙✢✪✬✖☛✷✒✭✑✕✶✍✖✭✑☞✮✠✂✄LABEL2☛✪✬✫✰✱✱☎✰✂☎✰✱✌✰✖✭✌✖☎✰✱✫✰✥✆✝✲✥✍✎CX✂DX☛✡✷✙✢✪✬✖☛✷✒✭✑✕✒✶✑✏✖✮ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑4✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳MultiplicationandDivision:IMUL/IDIV:Signedintegermultiplication/division.MUL/DIV:Unsigned.ALalwaysholdsthemultiplicand(orAXorEAX).ResultisplacedinAX(orDXandAXorEDXorEAX).CandObitsareclearedifmostsignificant8bitsofthe16-bitproductarezero(resultofan8-bitmultiplicationisan8-bitresult).Divisionbyzeroandoverflowgenerateerrors.Overflowoccurswhenasmallnumberdividesalargedividend.✆✁ BL✣✆✁ BX☛✟✢✖✜✪✒✭✂✫✰✱✌✰✁✎✂✟✄✌✪✗✓✖☞✑✓✭✔☎✣✆✁ CX✂✆✝✞✟✠✡;EDX|EAX=EAX*ECX✆✁ ECX☛☎✰✱☎☛✁✦☛✄✷✓✌✪✗✓✖☞☎☛✌✰☞☎✰✱☎✰✁✦✰✄✌✪✗✓✖☞☎✁✣✌CL;DX|AX=(DX|AX)/CX✣✁✣✌CX☛☎✟☞☎☛✱☎✰✘✫☛✂✷✓✌✪✗✓✖☞☛✷✑✏✪✖✓✏☛✪✓☎☛✂✕✖✙✒✪✓☞✖✕✪✓☎✟ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑5✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳LogicInstructions:Allowbitstobeset,clearedandcomplemented.CommonlyusedtocontrolI/Odevices.Logicoperationsalwaysclearthecarryandoverflowflags.•AND:0ANDanythingis0.CommonlyusedwithaMASKtoclearbits:•OR:1ORanythingis1.CommonlyusedwithaMASKtosetbits:✲✲✲✲✲✲✲✲ ✢✖✕✒✓☞✁✁✁✁✂✂✂✂✄✒✌☎✁✁✁✁✲✲✲✲✆✖✌✷✭✏AND AL✂BL☛☎☛✱☎☛☎✆✌✦☛✲✲✲✲✲✲✲✲ ✢✖✕✒✓☞✁✁✁✁✂✂✂✂✄✒✌☎✲✲✲✲✂✂✂✂✆✖✌✷✭✏OR EAX✂10☛★☎✰✱★☎✰ ✆✞✞✞✞✞✞✞☎✟ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑6✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳LogicInstructions:•XOR:Truthtable:0110.CommonlyusedwithaMASKtocomplementbits:•TEST:OperatesliketheANDbutdoesn’teffectthedestination.SetstheZflagtothecomplementofthebitbeingtested:Additionaloperationsavailablein80386andup.•BT:Testthebitintheleftoperandspecifiedbytherightoperand.•BTC:Testsandcomplements .•BTR:Testsandclears .•BTS:Testsandsets .PutsbitundertestinCflag.Latterthreechangethebitafterwards.✲✲✲✲✲✲✲✲ ✢✖✕✒✓☞✁✁✁✁✂✂✂✂✄✒✌☎✲✲✲✲✲✲✲✲✆✖✌✷✭✏XOR AH✂CH☛☎✟✱☎✟✰ ✆✫✟ ✄ AL, 4JZ☛☎✦★☛☛✡✷✙✢✏✍✑☛☎✦★☛✪✬✶✪✏✂✪✌✁✖✕✑✮☛✧✖✌✏✌✶✪✏✂✪✓☎☛☎☎✞✞✞✞✞✎✞✞ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑7✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳LogicInstructions:NOT(logicalone’scomplement)NEG(arithmetictwo’scomplement-signofnumberinverted)Shift:CommonlyusedforcontrolofI/Odevices.Alsousedformultiplicationanddivisionbypowersof2.•Logicalshiftsinsert0,usedwithunsignednumbers•Arithmeticrightshiftsinsertsignbit.Doublepercisionshifts(80386andup):✤ EBXNEG✧★✄✩SHL AX✂✎SAR✟✠✂✫☛☛☎✰✪✌✭✑✗✪✜✒✭✭✔✌✁✪✬✏✖☞✭✖✬✏✎✶✪✏✢✑✌✮☛✟✠✪✌✒✕✪✏✁✙✖✏✪✜✒✭✭✔✌✁✪✬✏✖☞✕✪✗✁✏SHDR AX✂BX✂✎✂☛☎✰✌✁✪✬✏✖☞✕✪✗✁✏✶✔✎✂✒✓☞✬✪✭✭✖☞✬✕✑✙☛✏✁✖✭✖✬✏☞✪✏✁✏✁✖✕✪✗✁✏✎✂✶✪✏✌✑✬✦✰✮SHDL AX✂BX✂✎✵ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑8✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳Rotate:Rotatesbitsfromoneendtotheotherorthroughthecarryflag.Commonlyusedtooperateonnumberswiderthan32-bits:BitScanInstruction(80386andup):Scanthroughanoperandsearchingfora1bit.Zeroflagissetifa1bitisfound,positionofbitissavedindestinationregister.ROL SI✂✎✵RCR BL✂CL☛✟✠✕✑✏✒✏✖☞✭✖✬✏✶✔✎✵✢✭✒✜✖✌✮☛✦☛✕✑✏✒✏✖☞✕✪✗✁✏✫☛✢✭✒✜✖✌✏✁✕✑✷✗✁✜✒✕✕✔✮SHL AX✂✎☛ ✕✪✗✪✓✒✭✵ ☎✶✪✏✓✷✙✶✍✖✕✪✓✌✰✂✦✰✒✓☞☎✰✮☛✟✁✪✬✏☎✰✭✖✬✏✎✶✪✓✒✕✔✢✭✒✜✖✮RCL BX✂✎☛✆✑✏✒✏✖✜✒✕✕✔✶✪✏✬✕✑✙✢✕✖✏✪✑✷✌✟✟☛✪✓✏✑☛✭✑☞✑✕☞✖✕✶✪✏✑✬✦✰✮RCL DX✂✎☛✆✑✏✒✏✖✜✒✕✕✔✶✪✏✬✕✑✙✢✕✖✏✪✑✷✌✆✫☛✪✓✌✰✮BSL EBX✂EAXBSR BL✂CL☛★☎✰✌✜✒✓✓✖☞✬✕✑✙✏✁✖✭✖✬✏✮☛✫☛✌✜✒✓✓✖☞✬✕✑✙✏✁✖✕✪✗✁✏✮ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑9✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳StringScanInstructions:SCASB/W/DcomparestheAL/AX/EAXregisterwithabyteblockofmemoryandsetstheflags.OftenusedwithREPEandREPNECMPSB/W/Dcompares2sectionsofmemorydata.ProgramControlInstructions:•ConditionalandUnconditionalJumps•Calls•Returns•InterruptsUnconditionalJumps:•Shortjump:PC-relativeusingtwobytes(+127/-128bytes).ShortjumpsarePC-relative(OffsetaddedtoIP:relocatable).JMP✆★✰✧☛ ✬✬✌✖✏☞✖✏✖✕✙✪✓✖✌✖✓✜✑☞✪✓✗✯✮✆★✰✧ ADD☎✰✂✦✰☛✁ ✁✪✌✕✖☛✷✪✕✖☞✒✬✏✖✕✏✁✖✭✒✶✍✖✭✂ ✂✁✄☎ ✆✝✄✞✠✟✡☛✟☞✝✝✌✎✍☛✏✑✒✏✓✔✄✄✆✝✕✖✁✗✘✙ ✚✞✛✜✢✑10✣✔✎✤✟✌✖✥✜✧✦✥✑✑✢✏✎★✑✥✤✝✩UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳UnconditionalJumps:•Nearjump:PC-relativeusingthreebytes(+/-32K).Sincesegmentsarecyclic,wraparoundallowsanylocationtobereachedwithinthe64Ksegment.Inprotectedmode,nearjumpsrangesare+/-2G.•Farjump:Directjumpusingfivebytes(addressininstr.stream).Twobytesgivetheoffsetandtwobytesgiveanewsegmentaddress.Protectedmode:Thesegmentvaluereferstoadescriptor.(Note:Thelinkerneedstofillinthevaluesinthiscasewhenthepro-gramfilesarecombined.)•IndirectJumps:(EIPvalueinreg/mem,notintheinstructionstream).Usesa16-or32-bitregisterasanoperand.JMP☎✰☛✡✷✙✢✏✍✑✒☞☞✕✖✌✌✗✪✏✖✓✶✔☎✰✮ [...]... ¦ ¡ !" Conditional Jumps: Either short or near jumps on the 80386 and up (only short, otherwise) Test flag bits S, Z, C, P and O For unsigned numbers: %& 8 D ) D 5 £ M £¤ Usually a 16-bit offset is used and CS is left alone Using a FAR PTR directive loads both CS and IP §' ¤ ¥¤ AB ¨ E D JMP A Unconditional Jumps: • Indirect Jumps (register indirect address mode): ¤ 2 ¡¢ 8 7 . BALTIMORE COUNTY1 9 6 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳Addition,Increment,Add-with-carryandExchange-and-add(80486):Contentsoftherightmost8bitsoftheFLAGSregistercanchange(+Overflow)forarithmeticandlogicinstructions.Flagsinclude:•Z(resultzero?)•C(carryout?)•A(halfcarryout?)•S(resultpositive?)•P(resulthasevenparity?)•O(overflowoccurred?) ✁✁AL✂✄☎✆✆☎✝✞✟✠✡☛☎☞☞✍✌✎✏✍✑✒✓✔✕✖✗✘✚✙✖✙✖✛✜✖✢✏✌✖✗✣✤✥✦✝✧★✩✧✆✄EDI]☛☎☞☞✍✌✕✖✗✪✌✏✖✕✌✞✫✒✕✕✔✬✭✒✗✯✮ ✁✥ECX✂EBX☛★✫✰✱★✫✰✞★✦✰✂★✦✰✱✑✕✪✗✪✓✒✭★✫✰✮✲ ✁✁ECX✂EBX☛✳✌✖☞✬✑✕✒☞☞✪✓✗✴. 6✪✎✫✬✠✭✮✯✰✰✭✱✲✮✎✳MultiplicationandDivision:IMUL/IDIV:Signedintegermultiplication/division.MUL/DIV:Unsigned.ALalwaysholdsthemultiplicand(orAXorEAX).ResultisplacedinAX(orDXandAXorEDXorEAX).CandObitsareclearedifmostsignificant8bitsofthe16-bitproductarezero(resultofan8-bitmultiplicationisan8-bitresult).Divisionbyzeroandoverflowgenerateerrors.Overflowoccurswhenasmallnumberdividesalargedividend.✆✁ BL✣✆✁ BX☛✟✢✖✜✪✒✭✂✫✰✱✌✰✁✎✂✟✄✌✪✗✓✖☞✑✓✭✔☎✣✆✁ CX✂✆✝✞✟✠✡;EDX|EAX=EAX*ECX✆✁ ECX☛☎✰✱☎☛✁✦☛✄✷✓✌✪✗✓✖☞☎☛✌✰☞☎✰✱☎✰✁✦✰✄✌✪✗✓✖☞☎✁✣✌CL;DX|AX=(DX|AX)/CX✣✁✣✌CX☛☎✟☞☎☛✱☎✰✘✫☛✂✷✓✌✪✗✓✖☞☛✷✑✏✪✖✓✏☛✪✓☎☛✂✕✖✙✒✪✓☞✖✕✪✓☎✟ ✂✁✄☎