hex_bcd segment code
public _Hex2Bcd
rseg hex_bcd
_Hex2Bcd:CLR F0 ;F0/符号位
MOV A, R3 ;小数点
PUSH ACC
MOV A, R5 ;R5/指定地址
MOV R0, A
MOV A, R6 ;R6R7/待转化数据
JNB ACC.7,H2B
SETB F0
CPL A
MOV R6, A
MOV A, R7
CPL A
INC A
MOV R7, A
JNZ H2B
INC R6
H2B: MOV R4, #10
MOV B, R4
MOV A, R6
DIV AB
MOV R6, A
MOV A, R7
ANL A, #0F0H
ORL A, B
SWAP A
MOV B, R4
DIV AB
MOV R2, A
MOV A, R7
SWAP A
ANL A, #0F0H
ORL A, B
SWAP A
MOV B, R4
DIV AB
MOV R3, B ;R3/个位
SWAP A
ORL A, R2
SWAP A
MOV R7, A
ANL A, #0F0H
ORL A, R6
SWAP A
MOV B, R4
DIV AB
SWAP A
MOV R1, A
MOV A, R7
ANL A, #0FH
SWAP A
ORL A, B
SWAP A
MOV B, R4
DIV AB
MOV R5, B ;R5/十位
ORL A, R1
MOV B, R4
DIV AB
MOV R6, B ;R6/百位,A/千位
MOV R7, A
POP ACC
MOV B, A
MOV A, R7
JNB F0, KILO4
CLR F0
JNB B.3, KILO2
JNZ KILO1
MOV A, #83 ;-.
SJMP KILO6
KILO1: MOV A, #82 ;-1.
SJMP KILO6
KILO2: JZ KILO3
MOV A, #39 ;-1
SJMP KILO6
KILO3: SETB F0
MOV A, #40 ;-
SJMP KILO6
KILO4: JNB B.3, KILO5
ADD A, #43 ;无、有小数点之差
SJMP KILO6
KILO5: JNZ KILO6
SETB F0
MOV A, #38 ;无
KILO6: MOV @R0, A
MOV A, R6
JNB B.2, HUN1
ADD A, #43
SJMP HUN2
HUN1: JNB F0, HUN3
JNZ HUN2
MOV A, #38
SJMP HUN3
HUN2: CLR F0
HUN3: INC R0
MOV @R0, A
MOV A, R5
JNB B.1, TEN1
ADD A, #43
SJMP TEN2
TEN1: JNB F0, TEN2
JNZ TEN2
MOV A, #38
TEN2: INC R0
MOV @R0, A
INC R0
MOV A, R3
MOV @R0, A
RET
END