base:more_hexadecimal_to_decimal_conversion
This is an old revision of the document!
More Hexadecimal to Decimal Conversion
These are equivalent routines to Garth Wilson's Hexadecimal to Decimal routines, eliminating the lookup tables in exchange for a little slower execution speed. A discussion of hexadecimal-to-decimal routines can be found in this thread on the 6502.org Forum.
First is the 8-bit version. Start with the input number in accumulator:
; Convert an 8 bit binary value to BCD ; ; This function converts an 8 bit binary value into a 16 bit BCD. It ; works by transferring one bit a time from the source and adding it ; into a BCD value that is being doubled on each iteration. As all the ; arithmetic is being done in BCD the result is a binary to decimal ; conversion. All conversions take 311 clock cycles. ; ; For example the conversion of a $96 would look like this: ; ; BIN = $96 -> BIN' = $2C C = 1 | BCD $0000 x2 + C -> BCD' $0001 ; BIN = $2C -> BIN' = $58 C = 0 | BCD $0001 x2 + C -> BCD' $0002 ; BIN = $58 -> BIN' = $B0 C = 0 | BCD $0002 x2 + C -> BCD' $0004 ; BIN = $B0 -> BIN' = $60 C = 1 | BCD $0004 x2 + C -> BCD' $0009 ; BIN = $60 -> BIN' = $C0 C = 0 | BCD $0009 x2 + C -> BCD' $0018 ; BIN = $C0 -> BIN' = $80 C = 1 | BCD $0018 x2 + C -> BCD' $0037 ; BIN = $80 -> BIN' = $00 C = 1 | BCD $0037 x2 + C -> BCD' $0075 ; BIN = $00 -> BIN' = $00 C = 0 | BCD $0075 x2 + C -> BCD' $0150 ; ; This technique is very similar to Garth Wilsons, but does away with ; the look up table for powers of two and much simpler than the approach ; used by Lance Leventhal in his books (e.g. subtracting out 1000s, 100s, ; 10s and 1s). ; ; Andrew Jacobs, 28-Feb-2004 .ORG $0200 BINBCD8: SED ; Switch to decimal mode LDA #0 ; Ensure the result is clear STA BCD+0 STA BCD+1 LDX #8 ; The number of source bits CNVBIT: ASL BIN ; Shift out one bit LDA BCD+0 ; And add into result ADC BCD+0 STA BCD+0 LDA BCD+1 ; propagating any carry ADC BCD+1 STA BCD+1 DEX ; And repeat for next bit BNE CNVBIT CLD ; Back to binary BRK ; All Done. ; A test value to be converted .ORG $0300 BIN .DB 234 BCD .DS 2
Here is an equivalent routine for converting 16-bit numbers:
; Convert an 16 bit binary value to BCD ; ; This function converts a 16 bit binary value into a 24 bit BCD. It ; works by transferring one bit a time from the source and adding it ; into a BCD value that is being doubled on each iteration. As all the ; arithmetic is being done in BCD the result is a binary to decimal ; conversion. All conversions take 915 clock cycles. ; ; See BINBCD8 for more details of its operation. ; ; Andrew Jacobs, 28-Feb-2004 .ORG $0200 BINBCD16: SED ; Switch to decimal mode LDA #0 ; Ensure the result is clear STA BCD+0 STA BCD+1 STA BCD+2 LDX #16 ; The number of source bits CNVBIT: ASL BIN+0 ; Shift out one bit ROL BIN+1 LDA BCD+0 ; And add into result ADC BCD+0 STA BCD+0 LDA BCD+1 ; propagating any carry ADC BCD+1 STA BCD+1 LDA BCD+2 ; ... thru whole result ADC BCD+2 STA BCD+2 DEX ; And repeat for next bit BNE CNVBIT CLD ; Back to binary BRK ; All Done. ; A test value to be converted .ORG $0300 BIN .DW 12345 BCD .DS 3
base/more_hexadecimal_to_decimal_conversion.1429237982.txt.gz · Last modified: 2015-04-17 04:33 by 127.0.0.1