User Tools

Site Tools


base:32_bit_hexadecimal_to_decimal_conversion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
base:32_bit_hexadecimal_to_decimal_conversion [2015-04-17 04:30] – created - external edit 127.0.0.1base:32_bit_hexadecimal_to_decimal_conversion [2023-04-20 09:02] (current) tww
Line 55: Line 55:
 </code> </code>
  
 +
 +16-bit version with tightened up loops and routine structure:
 +<code>
 +    // Converts 16 bit values to 5 decimal digits.
 +    ldx #-5
 +!Loop:
 +    ldy #16                    // Divides a 16 bit value by 10 (Remainder in A)
 +    lda #0
 +    clc
 +!:  rol
 +    cmp #10
 +    bcc !+
 +        sbc #10
 +!:  rol value
 +    rol value+1
 +    dey
 +    bpl !--
 +    sta result-[255-4],x
 +    inx
 +    bne !Loop-
 +    // Print result, skip leading zeros
 +    ldx #4
 +!:  lda result,x
 +    beq !Skip+
 +    ora #$30
 +    jsr $ffd2
 +!Skip:
 +    dex
 +    bpl !-
 +    rts
 +
 +value:  .word 65535
 +result: .byte 0,0,0,0,0
 +</code>
 +NB! If you're only looking to convert, not print; Replace jsr $ffd2 with sta result,x.
 +
 +NB!! If you want to maintain digit positioning, replacing proceeding zero's with #$20.
 +
 +NB!!! If you wish to extend to 24, 32 or whatever size, adjust the following values:
 +<code>
 +    ldx #-5               // How many digits as a negative number
 +    ldy #16               // How many bits as a positive number
 +    sta result-[255-4], // Change the "-4" to the negative quantity of digits-1 (i.e. 24 bit => [255-23]
 +    ldx #4                // How many digits-1 as a positive number
 +value:  .word 65535       // Adjust to quantity of bits needed
 +result: .byte 0,0,0,0,  // Adjust to quantity of digits needed
 +Finally adjust the quantity of "rol value+n" to: roundup(bits/8)
 +</code>
base/32_bit_hexadecimal_to_decimal_conversion.1429237815.txt.gz · Last modified: 2015-04-17 04:30 by 127.0.0.1