; 2 BYTE HEX TO 5 DIGITS BCD ; Author: Slobodan Mandaric, Serbia & Montenegro ; June 3rd, 2005. ; 149 cycles for 2 bytes, 77 cycles for 1 byte MOV r1,#0AFH ; 175d MOV r2,#91H ; 91AFH = 37295d ; Result: R7=3, R6=7, ..., R3=5 ; All 8 registers are used, and also A and B MOV R5,#0 MOV R6,#0 MOV R7,#0 MOV A,R1 MOV B,#10 DIV AB MOV R3,B ; Lower byte, in R1, is transformed into 2 decade digits, MOV R4,A ; it's OK that the higher one can be greater than 9 MOV A,R2 ; Higher byte is in R2 JZ SPRING ; Spring over if 0 MOV B,#10 DIV AB MOV R5,B ; Same as for lower byte MOV R6,A ; Value in R5 is 256 times higher than value standing in R3, R4. Mply R5 with 6, ; add to R3, mply R5 with 5, add to R4, mply R5 with 2 and replace itself in R5 MOV R0,#05 ; Point to R5 ACALL TRANSF ; Transform R5 to 3 lower positions INC R0 ; Repeat from R6, now all registers are 1 higher ACALL TRANSF ; Transform R6 to 3 higher positions SPRING: MOV R0,#03 ; Point to lowest digit ; Adjust to decimal, everything higher than 9 should be, as 1 tenth, added to higher. ; Starting from the lowest digit, divide with 10, keep the rest, and add the 1/10th to ; first higher place (eg. 145, leave 5 and add 14 to the next higher) ACALL DECADJ SJMP $ TRANSF: MOV A,@R0 MOV B,#6 MUL AB DEC R0 DEC R0 ADD A,@R0 MOV @R0,A INC R0 INC R0 MOV A,@R0 MOV B,#5 MUL AB DEC R0 ADD A,@R0 MOV @R0,A INC R0 MOV A,@R0 RL A ; Mply with 2 MOV @R0,A RET DECADJ: MOV A,@R0 DECLOP: MOV B,#10 DIV AB MOV @R0,B INC R0 ADD A,@R0 MOV @R0,A CJNE R0,#7,DECLOP RET END