您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > PIC单片机应用开发典型模块_第八章程序
(1)16×16位定点数加、减法程序LISTp=16f877INCLUDEp16f877.incACCALOEQU20;存放加数或减数低8位ACCAHIEQU21;存放加数或减数高8位ACCBLOEQU23;存放被加数或被减数低8位ACCBHIEQU24;存放被加数或被减数高8位ORG0X0000STARTGOTOMAIN;***双字节减法子程序,入口地址ACCB-ACCA,出口地址ACCB***D_subCALLNEG_A;求ACCA的补码;双字节加法子程序,入口地址ACCB+ACCA,出口地址ACCBD_addMOVFACCALO,0;ACCB和ACCA低半字节相加ADDWFACCBLOBTFSCSTATUS,C;有进位否?INCFACCBHI;有,ACCB高字节加1,再加ACCAHIMOVFACCAHI,0;ACCA、ACCB高半字节相加ADDWFACCBHIRETURN;子程序返回;ACCA取补子程序NEG_ACOMFACCALO;ACCALO取反加1INCFACCALOBTFSCSTATUS,Z;低8位有进位吗?DECFACCAHI;有,ACCAHI减1,再取反COMFACCAHI;否则ACCAHI直接取反RETURN;子程序返回(2)16×16位定点数乘法程序LISTp=16f877INCLUDEp16f877.incACCALOEQU20;存放乘数低8位ACCAHIEQU21;存放乘数高8位ACCBLOEQU23;存放被乘数低8位和乘积第16~23位ACCBHIEQU24;存放被乘数高8位和乘积第24~31位ACCCLOEQU26;存放乘积低8位ACCCHIEQU27;存放乘积高8位ACCDLOEQU28;临时寄存器ACCDHIEQU29;临时寄存器TEMPEQU2A;临时寄存器SIGNEQU2B;存放乘积的符号ORG0X0000STARTGOTOMAINORG0X0100D_mpyCALLS_SIGN;求取乘积的符号,并对负数取补CALLSETUP;调用子程序,将ACCB的值送ACCDINCFTEMPCLRFACCCHI;清ACCCCLRFACCCLOMLOOPBCFSTATUS,C;清进位位RRFACCDHI;ACCD右移RRFACCDLOBTFSCSTATUS,C;判断是否需要相加CALLD_add;加乘数至ACCB,见加法程序BCFSTATUS,C;清进位位RRFACCBHI;右移部分乘积RRFACCBLORRFACCCHIRRFACCCLODECFSZTEMP;乘法完成否?GOTOMLOOP;否,继续求乘积BTFSSSIGN,7;是,确定乘积的符号GOTOOVER;为正,乘法结束COMFACCCLO;为负,乘积取补INCFACCCLOBTFSCSTATUS,ZDECFACCCHICOMFACCCHIBTFSCSTATUS,ZNEG_BDECFACCBLOCOMFACCBLOBTFSCSTATUS,ZDECFACCBHICOMFACCBHIOVERRETURN;子程序返回SETUPMOVLW15;初始化TEMP寄存器MOVWFTEMPMOVFACCBHI,0;ACCB送ACCDMOVWFACCDHIMOVFACCBLO,0MOVWFACCDLOCLRFACCBHI;清ACCBCLRFACCBLORETURN;子程序返回S_SIGNMOVFACCAHI,0;ACCAHI异或ACCBHI,结果送SIGN单元XORWFACCBHI,0MOVWFSIGNBTFSSACCBHI,7;ACCB为负吗?GOTOCHEK_A;否,检查ACCACALLNEG_B;是,求取ACCB绝对值CHEK_ABTFSCACCAHI,7;ACCA为负吗?CALLNEG_A;ACCA为负,求取ACCA绝对值,RETURN;ACCA和ACCB均为正,返回(3)16×16位定点数除法程序LISTp=16f877INCLUDEp16f877.incACCALOEQU20;存放除数低8位ACCAHIEQU21;存放除数高8位ACCBLOEQU22;存放被除数和商的低8位ACCBHIEQU23;存放被除数和商的高8位ACCCLOEQU24;存放余数低8位ACCCHIEQU25;存放余数高8位ACCDLOEQU26;临时寄存器ACCDHIEQU27;临时寄存器TEMPEQU28;临时寄存器SIGNEQU29;存放商的符号ORG0X0000STARTGOTOMAINORG0X0100D_divCALLS_SIGN;确定商的符号,并将负数取补CALLSETUP;初始化TEMP,将被除数移至ACCDINCFTEMPCLRFACCCHI;清余数寄存器CLRFACCCLODLOOPBCFSTATUS,C;清进位位RLFACCDLO;被除数、余数左移1位RLFACCDHIRLFACCCLORLFACCCHIMOVFACCAHI,0;ACCCHI-ACCAHISUBWFACCCHI,0BTFSSSTATUS,Z;ACCCHI=ACCAHI?GOTONOCHKMOVFACCALO,0;是,ACCCLO-ACCALOSUBWFACCCLO,0NOCHKBTFSSSTATUS,C;ACCCACCA?GOTONOGOMOVFACCALO,0;是,余数减除数SUBWFACCCLOBTFSSSTATUS,CDECFACCCHIMOVFACCAHI,0SUBWFACCCHIBSFSTATUS,C;置进位位NOGORLFACCBLO;商左移1位RLFACCBHIDECFSZTEMP;循环完毕?GOTODLOOPBTFSSSIGN,7;是,确定商的符号GOTODIVOVER;为正,除法结束,跳转到结束行COMFACCCLO;为负,商和余数分别取补INCFACCCLOBTFSCSTATUS,ZDECFACCCHICOMFACCCHICALLNEG_B;见乘法程序中间NEG_BDIVOVERRETURN;子程序返回S_SIGNMOVFACCAHI,0;ACCAHI异或ACCBHI,结果送SIGN单元XORWFACCBHI,0MOVWFSIGNBTFSSACCBHI,7;ACCB为负?GOTOCHEK_A;否,检查ACCACOMFACCBLO;是,ACCB取补INCFACCBLOBTFSCSTATUS,ZDECFACCBHICOMFACCBHICHEK_ABTFSCACCAHI,7;ACCA为负?CALLNEG_A;ACCA为负,取补(NEG_A子程序请参见;16×16位定点数乘法子程序NEG_A)RETURN;ACCA和ACCB均为负,返回(4)浮点数加减法程序LISTp=16f877INCLUDEp16f877.incACCALOEQU20;存放加数或减数的尾数ACCAHIEQU21EXPAEQU22;存放加数或减数阶码ACCBLOEQU23;存放被加数或被减数尾数以及和或差ACCBHIEQU24EXPBEQU25;存放被加数或被减数阶码ACCCLOEQU26;临时寄存器ACCCHIEQU27;临时寄存器ACCDLOEQU28;临时寄存器ACCDHIEQU29;临时寄存器TEMPEQU2A;临时寄存器TEMP1EQU30;临时寄存器TIMESEQU31;临时寄存器ORG0X000STARTGOTOMAINORG0X0100F_subCALLNEG_A;求ACCA的补码,将减法转换为补码加法F_addCALLSUBADJ;调子程序判断EXPB和EXPA的大小BTFSCSTATUS,Z;参与运算的两个数阶码相等?GOTOPADD;是,求尾数的和BTFSCSTATUS,C;EXPBEXPA?CALLF_swap;是,ACCB与ACCA互换MOVFEXPA,0;否,求取两者的差值SUBWFEXPBSCLOOPCALLSHFTSR;ACCB右移规格化INCFSZEXPB;EXPB=EXPA?GOTOSCLOOP;否,继续右移MOVFEXPA,0;是,存和(差)的阶码MOVWFEXPBPADDMOVFACCAHI,0;ACCAHI或ACCBHIIORWFACCBHI,0MOVWFSIGN;存于SIGN寄存器MOVFACCBHI,0;暂存ACCBHIMOVWFEXPACALLD_add;尾数相加BTFSSSIGN,7;ACCA和ACCB有负数?BTFSCACCBHI,7;否,把和的最高位和次高位同时进位?GOTOADD2;否,转ADD2BTFSSACCAHI,7;ACCA为负吗?GOTOADD3;ACCA和ACCB不同时为负,转ADD3BTFSSEXPA,7;是,ACCB为负吗?GOTOADD3BSFSTATUS,C;ACCA和ACCB同为负,带负号右移RRFACCBHIRRFACCBLOINCFEXPBADD3CLRFACCCHI;和(差)规格化CLRFACCCLOCALLF_normRETURN;子程序返回ADD2BCFSTATUS,C;最高位次高位不同时进位,ACCB右移INCFEXPBGOTOSHFTRSHFTSRBCFSTATUS,C;ACCB带符号右移子程序BTFSCACCBHI,7BSFSTATUS,CSHFTRRRFACCBHIRRFACCBLORETURN;子程序返回F_swapMOVFACCAHI,0;ACCAHI、ACCBHI互换MOVWFTEMPMOVFACCBHI,0MOVWFACCAHIMOVFTEMP,0MOVWFACCBHIMOVFACCALO,0;ACCALO、ACCBLO互换MOVWFTEMPMOVFACCBLO,0MOVWFACCALOMOVFTEMP,0MOVWFACCBLOMOVFEXPA,0;EXPA、EXPB互换MOVWFTEMPMOVFEXPB,0MOVWFEXPAMOVFTEMP,0MOVWFEXPBRETURNSUBADJMOVFEXPA,0;EXPA异或EXPB,结果送C_DIVXORWFEXPB,0MOVWFC_DIVMOVFEXPA,0;EXPB-EXPASUBWFEXPB,0BTFSSC_DIV,7;EXPA和EXPB同号?RETURN;是,进位位的值真确反映两者的大小,返回BTFSSSTATUS,C;否,进位位的值取反GOTOCHANGECBCFSTATUS,CRETURNCHANGECBSFSTATUS,CRETURNF_normMOVFACCBHI;ACCB=0?BTFSSSTATUS,ZGOTOC_normMOVFACCBLOBTFSCSTATUS,ZRETURN;是,不需规格化,返回C_normBTFSCACCBHI,7;否。ACCB为负?GOTOC_norm2C_norm1BTFSCACCBHI,6;为正。规格化完毕?RETURN;ACCBHI.6=1,规格化结束CALLSHFTSL;否。ACCB左移DECFEXPB;EXPB减1GOTOC_norm1;重新判断规格化完毕否?C_norm2BTFSSACCBHI,6;ACCB为负。规格化完毕否?RETURN;ACCBHI.6=0,规格化结束BCFSTATUS,CCALLSHFTSL;否,ACCB左移BSFACCBHI,7;加符号DECFEXPB;EXPB减1GOTOC_norm2;重新判断规格化完毕否?SHFTSLBCFSTATUS,C;ACCB左移子程序RLFACCCLORLFACCCHIRLFACCBLORLFACCBHIRETURN(5)浮点数乘法程序LISTp=16f877INCLUDEp16f877.incACCALOEQU20;存放乘数尾数ACCAHIEQU21EXPAEQU22;存放乘数阶码ACCBLOEQU23;存放被乘数尾数和乘积高16位ACCBHIEQU24EXPBEQU25;存放被乘数阶码ACCCLOEQU26;存放乘积低16位ACCCHIEQU27ACCDLOEQU28;临时寄存器ACCDHIEQU29;临时寄存器TEMPEQU2A;临时寄存器TEMP1EQU30;临时寄存器TIMESEQU31;临时寄存器SIGNEQU2B;存放乘
本文标题:PIC单片机应用开发典型模块_第八章程序
链接地址:https://www.777doc.com/doc-559930 .html