MOV A, R3 RRC A MOV B, A MOV A, R2 RRC A MOV C, B.0 RRC A XCH A, B RR A ANL A, #00111111B MOV R5, A RL A ADD A, R5 XCH A, B ;------------------------------ ADD A, B JC BIN2BCD_L1 SJMP BIN2BCD_L2 BIN2BCD_L1: INC R5 ADD A, #6 BIN2BCD_L2: ADD A, B JC BIN2BCD_L1B SJMP BIN2BCD_L2B BIN2BCD_L1B: INC R5 ADD A, #6 BIN2BCD_L2B: MOV B, #25 DIV AB CJNE A, #10, BIN2BCD_L3 INC R5 CLR A BIN2BCD_L3B: MOV R7, A ; *100 MOV A, R2 ANL A, #00000011B MOV R6, A MOV A, #10 XCH A, B RL A RL A ORL A, R6 DIV AB SWAP A ORL A, B XCH A, R5 MOV B, #10 DIV AB XCH A, R7 SWAP A ORL A, B SWAP A MOV R6, A RET BIN2BCD_L3: SJMP BIN2BCD_L3B ;----------END OF BIN2BCD-------
优化 一般
MOV A, R3 MOV A, R3
RRC A CLR C
RRC A
MOV B, A MOV B, A
MOV A, R2 MOV A, R2
XCH A, B
RRC A RRC A
MOV C, B.0 XCH A, B
CLR C
RRC A RRC A
XCH A, B XCH A, B
RR A RRC A
ANL A, #00111111B XCH A, B
MOV R5, A MOV R5, A
RL A RL A
ADD A, R5 ADD A, R5
XCH A, B XCH A, B
;--------------------------------------
TEST_NUM EQU 12345
ORG 000H
MOV R2, #LOW(TEST_NUM) ; #11111111B
MOV R3, #HIGH(TEST_NUM); #11111111B ; 65535
CALL BIN2BCD
SJMP $
;==================================
BIN2BCD:
;--------------------------------------*
;Input: R3:R2 --- 16 bits bin |
; R2--Bit 0 to bit 7 |
; R3--Bit 8 to bit 15 |
;Output: R7:R6:R5 --- 5 Dig Packed BCD |
; R5: 个位/十位 |
; R6: 百位/千位 |
; R7: 万位 |
;Usage: PSW (C, AC, OV), Acc, B |
; |
;PS: 61 CYs only. 73 bytes |
; DengMiao 2007-4 Guangzhou |
;--------------------------------------*
MOV A, R2
ANL A, #00000011B
MOV R6, A ; R6 --- *1 (bit 0 to bit 1) (2 bits)
MOV A, R3
RRC A
MOV B, A
MOV A, R2
RRC A
MOV C, B.0
RRC A
MOV R7, A ; R7 --- *4 (bit 2 to bit 10) (8 bits)
MOV A, #25 ; 25 * 4 = 100
XCH A, B
RR A
ANL A, #00111111B
MOV R5, A ; R5 --- *1024 (bit 11 to bit 15) (6 bits)
RL A
ADD A, R5
XCH A, R7 ; R7 == 3 * R5 (<= 3*63=189 < 255)
; A --- *4
;------------------------------
ADD A, R7 ; 4* (A + 3*R5)
JC BIN2BCD_L1
SJMP BIN2BCD_L2
BIN2BCD_L1:
INC R5 ; +1000
ADD A, #6 ; + 24 (4*6)
BIN2BCD_L2:
ADD A, R7 ; 4*(A + 3*R5 + 3*R5)= 4*A + 24*R5
JC BIN2BCD_L1B
SJMP BIN2BCD_L2B
BIN2BCD_L1B:
INC R5 ; +1000
ADD A, #6 ; + 24 (4*6)
BIN2BCD_L2B:
;MOV B, #25
DIV AB ; A \ 25 (4*25=100)
CJNE A, #10, BIN2BCD_L3 ; A<=255\25=10
INC R5 ; *1000
CLR A
BIN2BCD_L3B:
MOV R7, A ; R7 --- *100 Bin/BCD R7<=9
; B --- *4 Bin B <=24
MOV A, #10
XCH A, B ; A --- *4 Bin B <=24
RL A ; A*4
RL A ; A --- *1 Bin A <=4*24=96
ORL A, R6 ; R6 --- *1 Bin/BCD R6<=3
; A --- *1 Bin A<=96+3=99
;MOV B, #10
DIV AB
SWAP A
ORL A, B
XCH A, R5 ; R5 --- *1 BCD 个位/十位
; A --- *1000 Bin A<=65
MOV B, #10
DIV AB
XCH A, R7 ; R7 --- *10000 bin/BCD 万位 R7<=6
; A --- *100 Bin/BCD A<=9
; B --- *1000 Bin/BCD B<=9
SWAP A
ORL A, B
SWAP A
MOV R6, A ; R6 --- *100 BCD 百位/千位
RET
BIN2BCD_L3:
SJMP BIN2BCD_L3B
;----------END OF BIN2BCD-------
END