base:hexadecimal_to_decimal_conversion
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Last revision | |||
— | base:hexadecimal_to_decimal_conversion [2015-04-17 04:32] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Hexadecimal to Decimal Conversion ====== | ||
+ | Without division or multiplication, | ||
+ | < | ||
+ | ; A = Hex input number (gets put into HTD_IN) | ||
+ | ; HTD_OUT | ||
+ | ; HTD_OUT+1 = 100s output byte | ||
+ | HTD: CLD ; (Make sure it's not in decimal mode for the | ||
+ | STA HTD_IN | ||
+ | TAY ; Save the input to restore later if desired. | ||
+ | STZ HTD_OUT+1 | ||
+ | STZ HTD_OUT | ||
+ | LDX #8 | ||
+ | |||
+ | | ||
+ | ROL HTD_OUT | ||
+ | ROL HTD_OUT+1 | ||
+ | |||
+ | DEX ; The shifting will happen seven times. | ||
+ | BEQ htd3$ ; the last shift, you don't check for digits of | ||
+ | ; 5 or more. | ||
+ | LDA HTD_OUT | ||
+ | AND #FH | ||
+ | CMP #5 | ||
+ | BMI htd2$ | ||
+ | |||
+ | CLC | ||
+ | LDA HTD_OUT | ||
+ | ADC #3 | ||
+ | STA HTD_OUT | ||
+ | |||
+ | | ||
+ | CMP #50H | ||
+ | BMI htd1$ | ||
+ | |||
+ | CLC | ||
+ | ADC #30H | ||
+ | STA HTD_OUT | ||
+ | |||
+ | BRA htd1$ ; NMOS 6502 can use JMP. | ||
+ | |||
+ | | ||
+ | RTS | ||
+ | </ | ||
+ | The above method is interesting and I used it in a product; but with a little thought I should have applied myself to years ago, I quickly saw there' | ||
+ | < | ||
+ | HTD_IN: | ||
+ | HTD_OUT: | ||
+ | |||
+ | TABLE: | ||
+ | ; (Word directive puts low byte first.) | ||
+ | |||
+ | HTD: SED ; Output gets added up in decimal. | ||
+ | STZ HTD_OUT | ||
+ | STZ HTD_OUT+1 | ||
+ | |||
+ | LDX #0EH ; $E is 14 for 2x7 bits. (0-7 is 8 positions.) | ||
+ | | ||
+ | BCC htd1$ ; don't add anything to the output for this bit. | ||
+ | LDA HTD_OUT | ||
+ | CLC | ||
+ | ADC TABLE, | ||
+ | STA HTD_OUT | ||
+ | LDA HTD_OUT+1 | ||
+ | ADC TABLE+1,X | ||
+ | STA HTD_OUT+1 | ||
+ | |||
+ | | ||
+ | DEX | ||
+ | BPL loop ; If still not done, go back for another loop. | ||
+ | |||
+ | CLD | ||
+ | RTS | ||
+ | </ | ||
+ | The principle should be pretty clear. You can take it out to as many digits as you want. Here it is for 16-bit hex input to 5-digit decimal output. Since the entire input number may not fit in A, put it in HTD_IN first. | ||
+ | < | ||
+ | HTD_IN: | ||
+ | HTD_OUT: | ||
+ | |||
+ | ; The table below has high byte first just to | ||
+ | ; make it easier to see the number progression. | ||
+ | TABLE: | ||
+ | BYTE 0, 0H, | ||
+ | BYTE 0, 2H, | ||
+ | BYTE 0, | ||
+ | |||
+ | HTD: SED ; Output gets added up in decimal. | ||
+ | |||
+ | STZ HTD_OUT | ||
+ | STZ HTD_OUT+1 | ||
+ | STZ HTD_OUT+2 | ||
+ | |||
+ | LDX #2DH ; 2DH is 45 decimal, or 3x15 bits. | ||
+ | | ||
+ | ROL HTD_IN+1 | ||
+ | BCC htd1$ ; then skip to the next bit after that. | ||
+ | LDA HTD_OUT | ||
+ | CLC ; get ready to | ||
+ | ADC TABLE+2, | ||
+ | STA HTD_OUT | ||
+ | LDA HTD_OUT+1 | ||
+ | ADC TABLE+1,X | ||
+ | STA HTD_OUT+1 | ||
+ | LDA HTD_OUT+2 | ||
+ | ADC TABLE, | ||
+ | STA HTD_OUT+2 | ||
+ | |||
+ | | ||
+ | DEX ; multiply by 3 to get the right bytes from the | ||
+ | DEX ; table. | ||
+ | BPL loop | ||
+ | |||
+ | CLD | ||
+ | RTS | ||
+ | </ | ||
+ | |||
+ | Note by Karoshier: The above code can be easily made a little faster and shorter by splitting the data table into three separate tables, one for the low digits, one for the mid digits and one for the high digits. This way the index can be initialized to 15 instead of 45 and be decremented by 1 instead of 3, thereby saving 4 cycles per iteration and making the routine shorter by 2 bytes. If you have space constraints and you need an 8 bit version as well, you can also make the 16 bit version work for both 8 or 16 bits by changing the initial value of X according to the needs (namely 3 times 7 instead of 3 times 15) and patching the ROL opcode on the fly to a BIT instruction, | ||
+ | |||
+ | See also [[More Hexadecimal to Decimal Conversion]]. Andrew Jacobs has written equivalent routines to the ones presented here, eliminating the lookup tables in exchange for a little slower execution speed. |
base/hexadecimal_to_decimal_conversion.txt · Last modified: 2020-11-14 23:09 by mace