【非典】16 bits to packed BCD, 51 ASM. 55周期/93字节(终结)
TEST_NUM EQU 65535 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: 55 CYs only. 93 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<=25525=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 ;DIV AB ;SWAP A ;ORL A, B ;XCH A, R5 ; R5 --- *1 BCD 个位/十位 ; ; A --- *1000 Bin A<=65 ;MOV B, #10 ;-------------- ADD A, #(BIN2BCD_DB - $ - 3) MOVC A, @A+PC ADD A, R6 DA A XCH A, R5 ; R5 --- *1 BCD 个位/十位 ; A --- *1000 Bin A<=65 ;-------------------------- 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 BIN2BCD_DB: DB 00H, 04H, 08H, 12H, 16H DB 20H, 24H, 28H, 32H, 36H DB 40H, 44H, 48H, 52H, 56H DB 60H, 64H, 68H, 72H, 76H DB 80H, 84H, 88H, 92H, 96H
;----------END OF BIN2BCD-------
END |
|