PIC單片機無符號BCD加法子程序
!--StartFragment-->;******************* 無符號 BCD 加法 ***************
;
; This routine performs a 2 Digit Unsigned BCD Addition
; It is assumed that the two BCD numbers to be added are in
; locations Num_1 Num_2. The result is the sum of Num_1+Num_2
; and is stored in location Num_2 and the overflow carry is returned
; in location Num_1
;
; Performance :
; Program Memory : 25
; Clock Cycles : 17 ( worst case )
;
;*******************************************************************;
;
Num_1 equ 8 ; 加數(shù)寄存器,加法和的高位寄存器。
result equ 8
;
Num_2 equ 9 ; 加數(shù)寄存器,加法和的低位寄存器。
O_flow equ 9
; 其它的寄存器自己定義
;
BCDAdd
movf Num_1,W
clrf Num_1 ;clear num_1
addwf Num_2,1 ; do binary addition
btfsc STATUS,C ; 256 then skip
goto inc_n1n2 ;else inc all
movlw 66 ;add 66
addwf Num_2,1 ;/
btfss STATUS,DC ;half carry?
goto sub_06 ;no then subtract
btfss STATUS,C ;full carry?
goto sub_60 ;yes then subtract
inc_n1
incf Num_1,1 ;inc it
retlw 0 ;clr w
sub_06
btfss STATUS,C ;full carry
goto sub_66 ;yes subtract 66
movlw 6 ;else subtract 6
goto sub_com ;do common
sub_66
movlw 66
sub_com
subwf Num_2,1
retlw 0
sub_60
movlw 60
goto sub_com
inc_n1n2
movlw 66
addwf Num_2,1
goto inc_n1
;
;********************************************************************
; 測試程序(注意它怎樣使用該子程序)
;*********************************************************************
main
movlw 99
movwf Num_1 ; Set Num_1 = 99 ( max BCD digit )
movlw 99
movwf Num_2 ; Set Num_2 = 99
;
call BCDAdd ; After addition, Num_2 = 98
; ; and Num_1 = 01 ( 99+99 = 198 -> max number )
;
self goto self ;如不是測試程序,這句無用
;
;
org 1FF
goto main
;
END
評論