第四章 8086指令系統(tǒng)_第1頁
第四章 8086指令系統(tǒng)_第2頁
第四章 8086指令系統(tǒng)_第3頁
第四章 8086指令系統(tǒng)_第4頁
第四章 8086指令系統(tǒng)_第5頁
已閱讀5頁,還剩196頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第四章

8086指令系統(tǒng)4.1匯編語言程序4.28068尋址方式4.38086指令系統(tǒng)4.4程序設(shè)計步驟內(nèi)容與要求:

1.熟悉Intel8086/8088的尋址方式。

2.掌握Intel8086/8088的指令系統(tǒng)。

3.了解Intel80386擴充與增加的指令。4.1匯編語言程序(1)機器語言(MachineLanguage)

機器語言是一種用二進制表示指令和數(shù)據(jù),能被機器直接識別的計算機語言。它的缺點是不直觀,不易理解和記憶,因此編寫、閱讀和修改機器語言程序都比較繁瑣。但機器語言程序是計算機惟一能夠直接理解和執(zhí)行的程序,具有執(zhí)行速度快、占用內(nèi)存少等特點。(2)匯編語言(AssemblyLanguage)

匯編語言是一種采用助記符表示的程序設(shè)計語言,即用助記符來表示指令的操作碼和操作數(shù),用標號或符號代表地址、常量或變量。助記符一般都是英文字的縮寫,以方便人們書寫、閱讀和檢查。實際上,用匯編語言編寫的匯編語言源程序就是機器語言程序的符號表示,匯編語言源程序與其經(jīng)過匯編所產(chǎn)生的目標代碼程序之間有明顯的一一對應(yīng)關(guān)系,故也稱匯編語言為符號語言。例題:1.定義問題:

已知某日的最高溫度和最低溫度,要求計算這一天的平均溫度。2.算法設(shè)計: 最高溫度加最低溫度 將和除以2得到平均溫度3.數(shù)據(jù)結(jié)構(gòu):

DATASEGMENTHI_TEMPDB92H;實際值可能是從傳感器讀取的數(shù)值LO_TEMPDB52H AV_TEMPDB? DATAENDS4.初始化表: MOVAX,DATA MOVDS,AX5.選擇指令:

選擇算法中每一主要動作所需的指令(按功能選擇指令),并決定數(shù)據(jù)在這些指令中的存放形式,最后則按照指令的要求,用MOV或其他的指令把數(shù)據(jù)傳送的正確的位置。

ADDdest,source;查指令表并閱讀細節(jié)ADCdest,source;dest=dest+source+CFDIVsource;DIV02immedmodenotallowed!!6.編寫程序:DATASEGMENTHI_TEMPDB92HLO_TEMPDB52HAV_TEMPDB?DATAENDSCODE SEGMENTMAIN PROC FAR

ASSUMECS:CODE,DS:DATA MOV AX, DATA MOV DS, AX MOV AL, HI_TEMP ADD AL, LO_TEMP MOV AH, 00H ADC AH, 00H MOV BL, 02 DIV BL MOV AV_TEMP,AL

MOV AH, 4CH INT 21HMAIN ENDPCODE ENDS

ENDMAINExample: PURPOSE:ADDS4WORDSOFDATA

STACK SEGMENT

DB 32DUP(?) STACK ENDS DATA SEGMENT data_in DW 234DH,1DE6H,3BC7H,566AH

ORG 10H sum DW ? DATA ENDS CODE SEGMENT MAIN PROC FAR

ASSUMECS:CODE,DS:DATA,SS:STACK MOV AX, DATA MOV DS, AX MOV CX, 04 MOV DI, OFFSETdata_in MOV BX, 00 ADD_LP: ADD BX, [DI] INC DI INC DI DEC CX JNZ ADD_LP MOV SI, OFFSETsum MOV [SI], BX MOV AH, 4CH INT 21H MAIN ENDP CODE ENDS

END MAIN ;FULLSEGMENTDEFINITION ;-----stacksegment-------- STACK SEGMENT

DB 64DUP(?) STACK ENDS ;-----datasegment-------- DATA SEGMENT ;datadefinitionsareplacedhere DATA ENDS ;-----codesegment-------- CODE SEGMENT MAIN PROCFAR

ASSUMECS:CODE,DS:DATA,SS:STACK MOVAX, DATA MOVDS, AX ------ MOVAH, 4CH INT21H MAIN ENDP CODE ENDS ENDMAIN4.28086尋址方式(Addressingmodes)

指令以不同的方式訪問操作數(shù)(數(shù)據(jù))稱為尋址方式。

8086共有7種尋址方式:(1)寄存器尋址(register)(2)立即尋址(immediate)(3)直接尋址(direct)(4)寄存器間接尋址(registerindirect)(5)基址相對尋址(basedrelative)(6)變址相對尋址(indexedrelative)(7)基址變址相對尋址(basedindexedrelative)操作數(shù)在CPU內(nèi):(1)寄存器尋址(Register): MOVBX,DX MOVES,AX ADDAL,BH

MOVCL,AX(2)立即尋址(immediate): MOVAX,2550H MOVCX,625 MOVBL,40H MOVAX,2550H MOVDS,AX MOVDS,0123H操作數(shù)在CPU外:(3)直接尋址(direct):

MOV

DL,[2400];movecontentsofDS:2400HintoDLPA=DS*10H+2400Example: Findthephysicaladdressofthememorylocationanditscontentsaftertheexecutionofthefollowing,assumingthatDS=1512H. MOV AL,99H MOV [3518],ALSolution: FirstALisinitializedto99H,theninlinetwo,thecontentsofALaremovedtologicaladdressDS:3518whichis1512:3518.ShiftingDSleftandaddingittotheoffsetgivesthephysicaladdressof18638H(15120H+3518H).Thatmeansaftertheexecutionofthesecondinstruction,thememorylocationwithaddress18638Hwillcontainthevalue99H.(4)寄存器間接尋址(registerindirect):MOVAL,[BX]

;movesintoALthe;contentsofthe;memorylocation;pointedtobyDS:BX

MOVCL,[SI]

MOV

[DI],AHExample: AssumethatDS=1120,SI=2498,andAX=17FE.Showthecontentsofmemorylocationsaftertheexecution MOV [SI],AXSolution: ThecontentsofAXaremovedintomemorylocationswithlogicaladdressDS:SIandDS:SI+1;therefore,thephysicaladdressstartsatDS*10H(shiftedleft)+SI=13698.Accordingtothelittleendianconvention,lowaddress13698HcontainsFE,thelowbyte,andhighaddress13699Hwillcontain17,thehighbyte.Strcpy函數(shù):將指針d指向的字符串復(fù)制到指針s指向的位置—寄存器間接尋址法 viod strcpy(char*s,char*d) { while((*s=*d)!=‘\0’){ s++; d++; } }(5)基址相對尋址(basedrelative)MOV

CX,[BX]+10;PA=DS*10H+BX+10

MOVAL,[BP]+5;PA=SS*10H+BP+5

MOVAL,[BP+5]

MOVAL,5[BP](6)變址相對尋址(indexedrelative)

MOVDX,[SI]+5;PA=DS*10H+SI+5

MOVCL,[DI]+20

;PA=DS*10H+DI+20Example:

AssumethatDS=4500,SS=2000,BX=2100,SI=1486,BP=7814,andAX=2512.ShowtheexactphysicalmemorylocationwhereAXisstoredineachofthefollowing.Allvaluesarehex. (a) MOV [BX]+20,AX (b) MOV [SI]+10,AX (c) MOV [DI]+4,AX (d) MOV [BP]+12,AXSolution:

IneachcasePA=segmentregister(shiftedleft)+offsetregister+displacement. (a) DS:BX+20 location47120=(12)and47121=(25) (b) DS:SI+10 location46496=(12)and46497=(25) (c) DS:DI+4 location4D504=(12)and4D505=(25) (d) SS:BP+12 location27826=(12)and27827=(25)#include <stio.h>

#define PROFIT 15

#define MAX_PRICES 10Int cost[]={20,28,15,26,19,27,16,29,39,42};Int prices[10];Int index;Main(){ for(index=0;index<MAX_PRICES;index++) prices[index]=cost[index]+PROFIT}Strcpy函數(shù):將指針d指向的字符串復(fù)制到指針s指向的位置—基址相對尋址法Voidstrcpy(char*s,char*d){ int i; i=0; while((s[i]=d[i]!=‘\0’) i++;}(7)基址變址相對尋址(basedindexedrelative)

MOVCH,[BX][SI]+20

;PA=DS*10H+BX+SI+20(7)基址變址相對尋址(basedindexedrelative)

MOVCL,[BX][DI]+8 ;PA=DS*10H+BX+DI+8 MOVCL,[BX+DI+8] MOVCH,[BX][SI]+20 ;PA=DS*10H+BX+SI+20

MOVAH,[BP][DI]+12

;PA=SS*10H+BP+DI+12 MOVAH,[BP][SI]+29 ;PA=SS*10H+BP+SI+29

MOVAX,[SI][DI]+displacementAddressingModeOperandDefaultSegmentRegisterRegNone(CPU)ImmediateDataNone(CPU)Direct[offset]DSRegisterindirect[BX][SI][DI]DSDSDSBasedrelative[BX]+disp[BP]+dispDSSSIndexedrelative[DI]+disp[SI]+dispDSDSBasedindexedrelative[BX][SI]+disp[BX]+[DI]+disp[BP]+[SI]+disp[BP]+[DI]+dispDSDSSSSS4.38086

指令系統(tǒng)指令(Instruction)和偽指令(Directives)格式:

[標號:]助記符[操作數(shù)][;注釋]

[label:]mnemonic[operands][;comment]指令(Instruction):

4.3.1數(shù)據(jù)傳送類指令

4.3.2算術(shù)運算指令

4.3.3位操作指令

4.3.4串操作指令

4.3.5程序轉(zhuǎn)移指令

4.3.6處理機控制指令4.3.1數(shù)據(jù)傳送類指令

(1)MOV(MOVement) MOVdst,src;copysrctodst MOVCL,55H MOVDL,CL MOVAH,DL MOVAL,AH MOVBH,CL MOVCH,BH

MOVCX,468FH MOVAX,CX MOVDX,AXMOVBX,DXMOVDI,BXMOVSI,DIMOVDS,SIMOVBP,DIMOVAX,58FCHMOVDX,6678HMOVSI,924BHMOVBP,2459HMOVDS,2341HMOVCX,8876HMOVCS,3F47HMOVBH,99H指令格式中的dst表示目的操作數(shù),src表示源操作數(shù)(下同)。指令實現(xiàn)的操作是將源操作數(shù)送給目的操作數(shù)。這種傳送實際上是進行數(shù)據(jù)的“復(fù)制”,源操作數(shù)本身不變。這種雙操作數(shù)指令在匯編語言中的表示方法,總是將目的操作數(shù)寫在前面,源操作數(shù)寫在后面,二者之間用一個逗號隔開。必須注意,不能用一條MOV指令實現(xiàn)以下傳送:①存儲單元之間的傳送。②立即數(shù)至段寄存器的傳送。③段寄存器之間的傳送。(2)LEA(LoadEffectiveAddress) Format:LEAdst,src ;dst=OFFSETsrc Flags:Unchanged. Function:Loadsintothedestination(a16-bitregister)theeffectiveaddressofadirectmemoryoperand.

ORG 0100HDATA DB 34,56,87,90,76,54,13,29 … ;toaccessthesixthelement: LEA SI, DATA+5 ;SI=100H+5 MOV AL, [SI];ifBX=2000H;SI=3500HLEADX,[BX][SI]+100H;DX=2000+3500+100;=5600H(3)PUSH(PUSHwordontostack)Format:PUSHsrc;SP=SP-2Flags:Unchanged.Function:CopiesthesourcewordtothestackanddecrementsSPby2.PUSHAXPUSHDIPUSHDX(4)POP(POPwordoffstack)Format:POPdst;SP=SP+2Flags:Unchanged.Function:CopiesthewordpointedtobythestackpointertotheregisterormemorylocationindicatedbytheoperandandincrementstheSPby2.

POPDXPOPDIPOPAX(5)XCHG(eXCHanGe)Format:XCHGdst,srcFlags:Unchanged功能:將源地址與目的地址中的內(nèi)容互換。MOV AX,5678H ;(AX)=5678HMOV BX,0FFFFH ;(BX)=0FFFFHXCHG AX,BX;(AX)=0FFFFH,(BX)=5678H4.3.2算術(shù)運算指令(1)加1指令I(lǐng)NC(INCrementby1) Format:INCdst ;dst=dst+1 Flags: OF,SF,ZF,AF,PF.Unchanged:CF

INC指令是一個單操作數(shù)指令,操作數(shù)可以是寄存器或存儲器操作數(shù)。 如:INCBX,即(BX)+1→BX。 加1指令可用于對計數(shù)器和地址指針進行調(diào)整。

Example:Adds5bytesofdataSTACK SEGMENT

DW 64 DUP(?)STACK ENDSDATA SEGMENTdata_in DB 25H,12H,15H,1FH,2BHsum DB ?DATA ENDSCODE SEGMENTMAIN PROC

FAR

ASSUMECS:CODE,DS:DATA,SS:STACK

MOV AX, DATA MOV DS, AX

MOV CX, 05 MOV BX, OFFSETdata_in MOV AL, 0AGAIN: ADD AL, [BX] INC BX DEC CX JNZ AGAIN MOV SUM, AL

MOV AH, 4C INT 21HMAIN ENDPCODE ENDS END MAIN

(2)ADD(ADDition)Format:ADDdst,src ;dst=dst+src

Flags: OF,SF,ZF,AF,PF,CF

MOV AL, 25H MOV BL, 34H ADD AL, BL MOVDH, 25H MOVCL, 34H ADD DH, CL MOVDH, 25H ADDDH, 34H例題:;Thisprogramcalculatesthetotalsumof5bytesofdata.

.MODELSMALL .STACK64;--------------

.DATACOUNT EQU 05;equateDATA DB 125,235,197,91,48;definebyte

ORG 0008H;originSUM DW ?;defineword;--------------

.CODEMAIN PROC FAR MOV

AX,@DATA MOV

DS,AX

MOV CX,COUNT ;CXistheloopcounter

MOVSI,OFFSETDATA;SIisthedatapointerMOV AX,00 ;AXwillholdthesum

BACK:

ADD

AL,[SI] ;addthenextbytetoALJNC

OVER ;Ifnocarry,continueINC AH ;elseaccumulatecarryinAHOVER:

INC SI ;incrementdatapointerDEC CX ;decrementloopcounterJNZ BACK ;ifnotfinished,goaddnextbyte

MOV SUM,AX ;storesum

MOV AH,4CH

INT 21H ;gobacktoDOSMAINENDP

END MAIN(3)ADC(AdditionwithCarry)Format:ADC

dst,src ;dst=dst+src+CFFlags:OF,SF,ZF,AF,PF,CF;Thisprogramcalculatesthetotalsumoffivewordsofdata.

.MODELSMALL.STACK64;--------------

.DATACOUNT EQU 05DATA DW 27345,28521,29533,30105,32375;

ORG 0010HSUM DW 2DUP(?) ;duplicate;--------------

.CODEMAIN PROC FAR MOV AX,@DATA MOV DS,AX MOV CX,COUNT ;CXistheloopcounter MOV

SI,OFFSETDATA ;SIisthedatapointer

MOV AX,00 ;AXwillholdthesum

MOV BX,AX ;BXwillholdthecarriesBACK:

ADD

AX,[SI] ;addthenextwordtoAXADC BX,0 ;addcarrytoBXINC

SI ;incrementdatapointertwice

INC SI ;topointtonextwordDEC CX ;decrementloopcounter

JNZ

BACK ;ifnotfinished,continueadding

MOV SUM,AX ;storethesumMOV

SUM+2,BX ;storethecarries

MOVAH,4CH INT

21H ;gobacktoDOSMAINENDP

ENDMAIN;Thisprogramaddsthefollowingtwomultiwordnumbersandsavesthe;result:DATA1=548FB9963CE7HandDATA2=3FCD4FA23B8DH.

.MODELSMALL

.STACK64;--------------

.DATADATA1 DQ548FB9963CE7H ;definequadword

ORG0010HDATA2 DQ3FCD4FA23B8DH

ORG0020HDATA3 DQ?;--------------

.CODEMAIN PROC FAR MOV

AX,@DATA MOV DS,AX

CLC;clearcarrybeforefirstaddition

MOV

SI,OFFSETDATA1;SIispointerforoperand1MOVDI,OFFSETDATA2;DIispointerforoperand2

MOVBX,OFFSETDATA3;BXispointerforthesum

MOVCX,04;CXistheloopcounterBACK:MOVAX,[SI];movethefirstoperandtoAXADCAX,[DI];addthesecondoperandtoAXMOV[BX],AX;storethesumINCSI;pointtonextwordofoperand1INCSIINCDI;pointtonextwordofoperand2INCDIINCBX;pointtonextwordofsumINCBXLOOPBACK;ifnotfinished,continueadding

MOVAH,4CH INT21H;gobacktoDOS MAIN ENDP

ENDMAIN(4)

DEC(Decrement)Format:DEC

dest;dest=dest-1Flags:OF,SF,ZF,AF,PF.Unchanged:CFFunction:Subtract1fromthedestinationoperand.NotethatCFisunchangedevenifavalue0000isdecrementedandbecomesFFFF.(5)SUB(Subtract)Format:SUBdest,source;dest=dest-sourceFlags:OF,SF,ZF,AF,PF,CFFunction:Subtractssourcefromdestinationandputstheresultinthedestination.Setsthecarryandzeroflagaccordingtothefollowing:CFZFdest>source00theresultispositivedest=source01theresultis0dest<source10theresultisnegativein2`scomp ThestepsforsubtractionperformedbytheinternalhardwareoftheCPUareasfollows: 1.Takesthe2`scomplementofthesource 2.Addsthistothedestination 3.Invertsthecarryandchangestheflagsaccordingly;fromthedatasegmentDATA1DB4CHDATA2DB6EHDATA3DB?;fromthecodesegment MOV DH, DATA1 SUB DH, DATA2 JNC NEXT NOT DH INC DHNEXT: MOV DATA3,DH(6)SBB(SubtractwithBorrow)Format:SBBdest,source:dest=dest-CF-sourceFlags:OF,SF,ZF,AF,PF,CFExample:;fromthedatasegmentDATA_ADD62562FAHDATA_BDD412963BHRESULTDD?;fromthecodesegment MOV AX, WORD

PTRDATA_A SUB AX, WORDPTRDATA_B MOV WORD

PTRRESULT, AX MOV AX, WORDPTRDATA_A+2 SBB AX, WORDPTRDATA_B+2 MOV WORD

PTRRESULT+2,AX(7)CMP(CompareOperands)Format:CMPdest,source;setsflagsasif“SUB”Flags:OF,SF,ZF,AF,PF,CFFunction:Comparestwooperandsofthesamesize.Thesourceanddestinationoperandsarenotaltered.

CF ZF dest>source0 0 dest=source0 1 dest<source1 0Example: DATA1 DW 235FH ….. MOV AX,0CCCCH CMP AX,DATA1 JNC OVER SUB AX,AX OVER: INC DATA1 …..Example: ….. MOV BX,7888H MOV CX,9FFFH CMP BX,CX JNC NEXT ADD BX,4000H NEXT: ADD CX,250H …..Example: ….. TEMP DB ? ….. MOV AL,TEMP CMP AL,99 JZ HOT INC BX ….. HOT : HLTExample:;Thefollowingprogramfindsthehighestamong5grades.MODELSMALL.STACK64;--------------

.DATAGRADES DB 69,87,96,45,75

ORG 0008HIGHEST DB ?;--------------

.CODEMAIN PROC FAR MOV AX, @DATA MOV

DS, AX

MOVCX,5;setuploopcounter MOV

BX,OFFSETGRADES;BXpointsto GRADEdata SUBAL,AL;ALholdshighestgradefoundsofarAGAIN:CMPAL,[BX];comparenextgradetohighestJANEXT;jumpifALstillhighestMOVAL,[BX];elseALholdsnewhighestNEXT: INCBX;pointtonextgradeLOOPAGAIN;continuesearchMOVHIGHEST,AL;storehighestgradeMOV AH,4CHINT

21H;gobacktodosMAINENDP

ENDMAINExample:ASCIIlowercasetouppercaseconversion43217650000010100111001011101110000NULDLESP0@P\p0001SOHDC1!1AQaq0010STXDC2“2BRbr0011ETXDC3#3CScs0100EOTDC4$4DTdt0101ENQNAK%5EUeu0110ACKSYN&6FVfv0111BELETB‘7GWgw1000BSCAN(8HXhx1001HTEM)9IYiy1010LFSUB*:JZjz1011VTESC+;K[k{1100FFFS,<L\l|1101CRGS-=M]m}1110SORS.>N^n~1111SIUS/?O_oDEL行0123456789101112131415列01234567.MODELSMALL.STACK64;--------------

.DATADATA1DB 'mYNAMEisjOe'

ORG 0020HDATA2 DB 14DUP(?);--------------

.CODEMAIN PROC FAR MOV AX,@DATA MOV DS,AX MOV SI,OFFSETDATA1;SIpointstooriginaldata MOV BX,OFFSETDATA2;BXpointstouppercasedata MOV CX,14;CXisloopcounterBACK: MOV AL,[SI] ;getnextcharacter CMP AL,61H ;iflessthan'a'JBOVER;thennoneedtoconvert CMP AL,7AH ;ifgreaterthan'z'JAOVER;thennoneedtoconvert AND AL,11011111B ;maskd5toconverttouppercaseOVER: MOV [BX],AL ;storeuppercasecharacter INC SI ;incrementpointertooriginal INC BX ;incrementpointertouppercasedata LOOP BACK ;continueloopingifCX>0 MOV AH,4CH INT

21H ;gobacktodosMAIN ENDP

END MAIN4)十進制數(shù)(BCD)運算指令

1.壓縮BCD碼調(diào)整指令(1)加法的十進制調(diào)整指令DAA(2)減法的十進制調(diào)整指令DAS

2.非壓縮BCD碼調(diào)整指令(1)加法的非壓縮BCD碼調(diào)整指令A(yù)AA(2)減法的非壓縮BCD碼調(diào)整指令A(yù)AS(3)乘法的非壓縮BCD碼調(diào)整指令A(yù)AM(4)除法的非壓縮BCD碼調(diào)整指令A(yù)ADKeyASCII(hex)BinaryBCD(unpacked)030011000000000000131011000100000001232011001000000010333011001100000011434011010000000100535011010100000101636011011000000110737011011100000111838011100000001000939011100100001001ASCII碼ASCIItounpackedBCDconversion;-----------datasegmentASC DB ‘9562481273’

ORG 0010HUNPACK DB 10DUP(?);-----------codesegment MOV CX, 5 MOV BX, OFFSETASC MOV DI, OFFSETUNPACKAGAIN: MOV AX, [BX] AND AX, 0F0FH MOV [DI], AX ADD BX, 2 ADD DI, 2 LOOP AGAIN;-----------datasegmentASC DB ‘9562481273’

ORG 0010HUNPACK DB 10DUP(?);-----------codesegment

MOV CX, 10 SUB BX, BXAGAIN: MOV AL, ASC[BX] AND AL, 0FH MOV UNPACK[BX],AL INC BX LOOP AGAINASCIItopackedBCDconversionKey

ASCII

UnpackedBCD

PackedBCD434000001007370000011101000111(47H)Example:

ORG 0010HVAL_ASC DB ‘47’VAL_BCD DB ?;------------- MOV AX,WORDPTRVAL_ASC AND AX,0F0FH XCHG AH,AL MOV CL,4 ROL AH,CL OR AL,AH MOV VAL_BCD,AL;----------------PackedBCDtoASCIIconversionPackedBCD

UnpackedBCD

ASCII29H02H&09H32H&39H0010100100000010&000010010110010&0111001Example:VAL_BCD DB 29HVAL_ASC DW ?--------------- MOV AL, VAL_BCD MOV AH, AL AND AX, 0F00FH MOV CL, 4 SHR AH, CL OR AX, 3030H XCHG AH, AL MOV VAL_ASC,AX---------------(1)十進制加法的調(diào)整指令DAADAA(DecimalAdjustafterAddition)Format:DAA;NotethatDAAworksonlyonALFlags: Affected:SF,ZF,AF,PF,CF,OFExample:----------DATA1 DB 47HDATA2 DB 25HDATA3 DB ?---------- MOV AL, DATA1 MOV BL, DATA2 ADD AL, BL DAA MOV DATA3,AL---------- Example:1.ConvertfromASCIItopackedBCD2.AddthemultibytepackedBCDandsaveit3.ConvertthepackedBCDresultintoASCII

.MODESMALL

.STACK64

.DATADATA1_ASC DB '0649147816'

ORG 0010HDATA2_ASC DB '0072687188'

ORG 0020HDATA3_BCD DB 5DUP(?)

ORG 0028HDATA4_BCD DB 5DUP(?)

ORG 0030HDATA5_ADD DB 5DUP(?)

ORG 0040HDATA6_ASC DB 10DUP(?)

.CODEMAINPROC

FAR MOVAX,@DATA MOVDS,AX

MOVBX,OFFSETDATA1_ASC MOVDI,OFFSETDATA3_BCD MOVCX,5 CALLCONV_BCD;convertASCIItoBCD MOVBX,OFFSETDATA2_ASC MOVDI,OFFSETDATA4_BCD MOVCX,5 CALLCONV_BCD;convertASCIItoBCD CALLBCD_ADD;addtheBCDoperands MOVSI,OFFSETDATA5_ADD MOVDI,OFFSETDATA6_ASC MOVCX,05 CALLCONV_ASC ;convertresulttoASCII MOV AH,4CH INT 21H;gobacktoDOSMAINENDP;---------------;THISSUBROUTINECONVERTSASCIITOPACKEDBCDCONV_BCDPROCAGAIN:MOV AX,[BX] ;BX=pointerforASCIIdata XCHG AH,AL AND AX,0F0FH ;maskASCII3s PUSH CX ;savethecounter MOV CL,4 ;shiftAHleft4bits SHL AH,CL ;togetreadyforpacking OR AL,AH ;combinetomakepackedBCD MOV[DI],AL ;DI=pointerforBCDdata ADD BX,2 ;pointtonext2ASCIIbytes INC DI ;pointtonextBCDdata POPCX ;restoreloopcounter LOOPAGAIN RETCONV_BCDENDP;THISSUBROUTINEADDSTWOMULTIBYTEPACKEDBCDBCD_ADDPROC MOVBX,OFFSETDATA3_BCD;BX=pointerforoperand1 MOVDI,OFFSETDATA4_BCD;DI=pointerforoperand2 MOVSI,OFFSETDATA5_ADD;SI=pointerforsum MOVCX,05 CLC;CLearCarryflagBACK:MOVAL,[BX]+4;getnextbyteofoperand1 ADCAL,[DI]+4;addnextbyteofoperand2 DAA ;correctforBCDaddition MOV[SI]+4,AL ;savesum DECBX ;pointtonextbyteofoperand1 DECDI ;pointtonextbyteofoperand2 DECSI ;pointtonextbyteofsum LOOPBACK RETBCD_ADDENDP;THISSUBROUTINECONVERTSFROMPACKEDBCDTOASCIICONV_ASCPROCAGAIN2:MOVAL,[SI];SI=pointerforBCDdata MOVAH,AL;duplicatetounpackANDAX,0F00FH;unpackPUSHCX;savecounterMOVCL,04;shiftright4bitstounpackSHRAH,CL;theuppernibbleORAX,3030H;makeitASCIIXCHGAH,AL;swapforASCIIstorageconventionMOV[DI],AX;storeASCIIdataINCSI;pointtonextBCDdataADDDI,2;pointtonextASCIIdataPOPCX;restoreloopcounterLOOPAGAIN2RETCONV_ASCENDP

ENDMAIN(2)加法的非壓縮BCD碼調(diào)整指令A(yù)AAAAA(ASCIIAdjustafterAddition)Format:AAAFlags:AffectedAFandCFFunction:ThisinstructionisusedafteranADDinstructionhasaddedtwodigitsinASCIIcode.ThismakesitpossibletoaddASCIInumberswithoutmaskingofftheuppernibble“3”.TheresultwillbeunpackedBCDinALwithcarryflagsetifneeded.ThisinstructionadjustsonlyontheALregister.AHisincrementedifthecarryflagisset.Example: MOV AL,‘5’ ;AL=35 ADD AL,‘2’ ;addtoAL32theASCIIfor2 AAA ;changes67Hto07H OR AL,30 ;ORALwith30HtogetASCIIExample: --------- SUB AH,AH ;AH=00 MOV AL,‘7’ ;AL=37H MOV BL,‘5’ ;BL=35H ADD AL,BL ;37H+35H=6CHthereforeAL=6C AAA ;changes6CHto02inALandAH=CF=1 OR AX,3030H ;AX=3132 ----------Example:

.MODELSMALL .STACK64 ;--------------

.DATA VALUE1DB'0659478127'

ORG0010H VALUE2DB'0779563678'

ORG0020H RESULT1DB10DUP(?)

ORG0030H RESULT2DB10DUP(?) ;--------------.CODEMAINPROCFAR MOVAX, @DATA MOVDS, AX

CALLASC_ADD;callASCIIadditionsubroutine CALLCONVERT;callconverttoasciisubroutine

MOVAH,4CH INT21H;gobacktoDOSMAINENDP;---------------;THISSUBROUTINEADDSTHEASCIINUMBERSANDMAKESTHE;RESULTUNPACKED.ASC_ADDPROCCLC;clearthecarryMOVCX,10;setuploopcounterMOVBX,9;pointtoLSDBACK:MOVAL,VALUE1[BX];movenextbyteofoperand1ADCAL,VALUE2[BX];addnextbyteofoperand2AAA ;adjusttomakeitunpackedBCDMOVRESULT1[BX],AL;storeBCDsumDECBX;pointtonextbyteLOOPBACKRETASC_ADDENDP;---------------;THISSUBROUTINECONVERTSUNPACKEDBCDTOASCIICONVERTPROCMOVBX,OFFSETRESULT1;BXpointstounpackedBCDdata MOVSI,OFFSETRESULT2;SIpointstoASCIIdataMOVCX,05;CXisloopcounterBACK2:MOVAX,WORDPTR

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論