当前位置: 首页 > 单片机初学者 > PIC单片机(提高篇) > 正文

PIC单片机无符号 BCD 减法

日期:2012-12-18阅读:来源:单板机学习网    ★★★
;*******************无符号 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
<!--StartFragment-->;*******************无符号 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_1
result  equ     8
;
Num_2   equ     9       ; Num_2 - Num_1 overwrites Num_2
O_flow  equ     9
;
;其它寄存器自己定义
;
BCDSub
movf    Num_1,w
subwf   Num_2
clrf    Num_1
rlf     Num_1
btfss   STATUS,DC
goto    adjst1
btfss   Num_2,3         ; Adjust LSD of Result
goto    Over_1
btfsc   Num_2,2
goto    adjst1          ; Adjust LSD of Result
btfss   Num_2,1
goto    Over_1          ; No : Go for MSD
adjst1
movlw   6
subwf   Num_2
Over_1
btfss   Num_1,0         ; CY = 0 ?
goto    adjst2          ; Yes, adjust MSD of result
clrf    Num_1
btfss   Num_2,7         ; No, test for MSD >9
RETLW   0
btfsc   Num_2,6
goto    adjst2
btfss   Num_2,5
RETLW   0
adjst2
movlw   60              ; add 6 to MSD
subwf   Num_2
clrf    Num_1
btfss   STATUS,CARRY    ; test if underflow
RETLW   0
movlw   1
movwf   Num_1
Over
RETLW   0
;
;********************************************************************
;      测试程序(注意用法,Num_2-Num_1=Num_2)
;*********************************************************************
main
movlw   23
movwf   Num_1      ; Set Num_1 = 23
movlw   99
movwf   Num_2      ; Set Num_2 = 99
call    BCDSub     ; After subtraction, Num_2 = 76 ( 99-23 )
;                          ;  and Num_1 = 0 ( indicates positive result )
;
self    goto    self   ; 如不是测试程序,本句无用
;
org     1FF
goto    main
;
END
关键字:PIC
热卖工具