新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > PIC单片机无符号 BCD 减法

PIC单片机无符号 BCD 减法

作者:时间:2011-03-08来源:网络收藏
;*******************无符号 BCD 减法***************;; This routine performs a 2 Digit Unsigned BCD Subtraction.; It is assumed that the two BCD numbers to be subtracted are in; locations Num_1 & Num_2. The result is the difference of Num_1 & Num_2; ( Num_2 - Num_1) and is stored in location Num_2 and the overflow carry; is returned in location Num_1.;; Performance :; Program Memory : 31; Clock Cycles : 21 ( worst case );;*******************************************************************;Num_1 equ 8 ; Overflow flow carry overwrites Num_1result equ 8;Num_2 equ 9 ; Num_2 - Num_1 overwrites Num_2O_flow equ 9;;其它寄存器自己定义;BCDSubmovf Num_1,wsubwf Num_2clrf Num_1rlf Num_1btfss STATUS,DCgoto adjst1btfss Num_2,3 ; Adjust LSD of Resultgoto Over_1btfsc Num_2,2goto adjst1 ; Adjust LSD of Resultbtfss Num_2,1goto Over_1 ; No : Go for MSDadjst1movlw 6subwf Num_2Over_1btfss Num_1,0 ; CY = 0 ?goto adjst2 ; Yes, adjust MSD of resultclrf Num_1btfss Num_2,7 ; No, test for MSD >9RETLW 0btfsc Num_2,6goto adjst2btfss Num_2,5RETLW 0adjst2movlw 60 ; add 6 to MSDsubwf Num_2clrf Num_1btfss STATUS,CARRY ; test if underflowRETLW 0movlw 1movwf Num_1OverRETLW 0;;********************************************************************; 测试程序(注意用法,Num_2-Num_1=Num_2);*********************************************************************mainmovlw 23movwf Num_1 ; Set Num_1 = 23movlw 99movwf Num_2 ; Set Num_2 = 99call BCDSub ; After subtraction, Num_2 = 76 ( 99-23 ); ; and Num_1 = 0 ( indicates positive result );self goto self ; 如不是测试程序,本句无用;org 1FFgoto main;END

评论


相关推荐

技术专区

关闭