;BCD 加法 减法运算
;一个字节 两个字节 带进位 有符号 无符号
;高位数据在高位地址 低位数据在底位地址中
R0 EQU 40H
R1 EQU 41H
R2 EQU 42H
R3 EQU 43H
R4 EQU 44H
R5 EQU 45H
R6 EQU 46H
R7 EQU 47H
; R1=R0+R1
;无符号数的加法
ADDBCD:
MOVF R0,0
ADDWF R1,1
MOVF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO ADDH
MOVLW 6
ADDWF R1,1
ADDH: SWAPF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
RETLW 0
MOVLW 60H
ADDWF R1,1
RETLW 0
; R1=R1+R0+CY
; R2=R2+CY
ADDBCDC:
MOVF R0,0
ADDWF R1,1
BTFSC STATUS,C
INCF R2,1
MOVF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO ADDH
MOVLW 6
ADDWF R1,1
BTFSC STATUS,C
INCF R2,1
ADDH: SWAPF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
RETLW 0
MOVLW 60H
ADDWF R1,1
BTFSC STATUS,C
INCF R2,1
RETLW 0
;R3R2=R3R2+R1R0
; R2=R2+R0
; R3=R3+R1+CY
; R4=R4+CY
ADDBCDC:
MOVF R0,0
ADDWF R2,1
BTFSC STATUS,C
INCF R3,1
MOVF R1,0
ADDWF R3,1
MOVF R2,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO ADDH1
MOVLW 6
ADDWF R2,1
BTFSC STATUS,C
INCF R3,1
ADDH1: SWAPF R2,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO ADDL1
MOVLW 60H
ADDWF R2,1
BTFSC STATUS,C
INCF R3,1
ADDL1:
MOVF R3,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO ADDH2
MOVLW 6
ADDWF R3,1
ADDH2: SWAPF R3,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
RETLW 0
MOVLW 60H
ADDWF R3,1
RETLW 0
;R1=R1-R0
;无符号数的减法或R1>R0
SUBBCD:
COMF R0,1
INCF R0,0
ADDWF R1,1
MOVF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO SUBH
MOVLW 6
ADDWF R1,1
SUBH: SWAPF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
RETLW 0
MOVLW 60H
ADDWF R1,1
RETLW 0
;R1=R1-R0
;有符号数的减法或R0>R1 结果R1为负数
SUBBCDS:
MOVF R0,0
SUBWF R1,1
BC NEX
COMF R1,1
INCF R1,1
NEX:
MOVF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO SUBH
MOVLW 6
ADDWF R1,1
SUBH: SWAPF R1,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
RETLW 0
MOVLW 60H
ADDWF R1,1
RETLW 0
;R3R2=R3R2-R1R0
; R2=R2-R0
; R3=R3-R1-CY
SUBBCDC:
MOVF R0,0
SUBWF R2,1
BTFSS STATUS,C
DECF R3,1
MOVF R1,0
SUBWF R3,1
BC NEX1
COMF R2,1
INCF R2,1
COMF R3,1
INCF R3,1
NEX1:
MOVF R2,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO SUBH1
MOVLW 6
ADDWF R2,1
BTFSC STATUS,C
INCF R3,1
SUBH1: SWAPF R2,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO SUBL1
MOVLW 60H
ADDWF R2,1
BTFSC STATUS,C
INCF R3,1
SUBL1:
MOVF R3,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
GOTO SUBH2
MOVLW 6
ADDWF R3,1
SUBH2: SWAPF R3,0
ANDLW 0FH
SUBLW 9
BTFSC STATUS,C
RETLW 0
MOVLW 60H
ADDWF R3,1
RETLW 0
一周热门 更多>