Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > 32bit Decimal convertion
2018-10-05 07:02
JackAsser

Registered: Jun 2002
Posts: 1989
32bit Decimal convertion

Anybody got this shitz for 32-bit numbers? http://codebase64.org/doku.php?id=base:hexadecimal_to_decimal_c..
 
... 17 posts hidden. Click here to view all posts....
 
2018-10-05 11:12
Perplex

Registered: Feb 2009
Posts: 254
Storing and adding XP as base-10 strings is not an option?
2018-10-05 11:13
JackAsser

Registered: Jun 2002
Posts: 1989
Quote: Storing and adding XP as base-10 strings is not an option?

Brr no! :D
2018-10-05 12:09
ChristopherJam

Registered: Aug 2004
Posts: 1378
Alternately, here's my double dabble variant. No tables, 37 bytes of code including the RTS, assuming input and output in zero page. Might be able to save bytes by keeping the intermediate results in petscii rather than 0..9, but this is more readable.
.DEFINE blt bcc

scratch=$f0 ; input in scratch+10..scratch+13, output in scratch+0..scratch+9, most significant first


    jmp test


convert:
    lda#0

    ldx#9
initlp:
    sta scratch,x
    dex
    bpl initlp

    ldy#31
bitlp:

    ldx#9
correctionlp:
    lda scratch,x
    cmp#5
    blt noover
    ;carry now set
    adc#128-5-1
    ;carry now clear
    sta scratch,x
noover:
    dex
    bpl correctionlp

    ldx#13
shiftlp:
    rol scratch,x
    dex
    bpl shiftlp

    dey
    bpl bitlp

    rts


test:
    lda#13
    sta $d021
    jsr $e536
    lda#13
    jsr $ffd2
    lda#0
    sta $d021

    ldx#3
cpl:
    lda testv,x
    sta scratch+10,x
    dex 
    bpl cpl
    jsr convert

    ldx#9
codelp:
    lda#48
    ora scratch,x
    sta $040f,x
    dex
    bpl codelp
    rts


testv:
    .byte $49, $96, $02, $d2 ; 1234567890 = 0x499602d2
2018-10-05 12:36
JackAsser

Registered: Jun 2002
Posts: 1989
Awesome! Please explain the math of it
2018-10-05 12:56
Krill

Registered: Apr 2002
Posts: 2839
Seems to be a variant of that first algorithm.

Shift one bit of the binary value into the result, then avert decimal overflow for every decimal digit (when decimal digit >= 5, which would be >=10 upon the next iteration's shift) by subtracting 5 and setting decimal digit byte's MSB, which is the carry upon the next iteration's shift and will end up as the LSB of the next more significant decimal digit.

So basically you're doubling the result with every incoming bit of the argument (reflecting the binary nature of the argument) and fixing the result in every iteration (reflecting the decimal nature of the result).
2018-10-05 13:24
JackAsser

Registered: Jun 2002
Posts: 1989
Quote: Seems to be a variant of that first algorithm.

Shift one bit of the binary value into the result, then avert decimal overflow for every decimal digit (when decimal digit >= 5, which would be >=10 upon the next iteration's shift) by subtracting 5 and setting decimal digit byte's MSB, which is the carry upon the next iteration's shift and will end up as the LSB of the next more significant decimal digit.

So basically you're doubling the result with every incoming bit of the argument (reflecting the binary nature of the argument) and fixing the result in every iteration (reflecting the decimal nature of the result).


Smart approach!
2018-10-05 14:25
Krill

Registered: Apr 2002
Posts: 2839
Quoting JackAsser
Smart approach!
Indeed, it's pretty elegant. Kind of in the same "class" of algorithms with bit-wise multiplication, division and square root.
2018-10-05 16:48
ChristopherJam

Registered: Aug 2004
Posts: 1378
Thanks, JackAsser! I was hoping to find something a little different, so while I did do a quick google and found the initial plus three approach elsewhere, I deliberately avoided looking at the solutions posted thus far.

Got halfway through the nybble masking etc required for the hex to BCD conversion when I had the realisation that you'd just have to unpack the nybbles for display purposes at the end anyway.

Switching to one digit per byte from the get go eliminates more than half of the instructions in the inner loop, and potentially frees up a register too.

Nice explanation of how it works, Krill.
2018-10-06 09:40
ChristopherJam

Registered: Aug 2004
Posts: 1378
This one converts directly to screencodes; saves two bytes in the "copy to screen" loop, needs an extra two bytes in the converter, takes an extra 1200 or so cycles. meh. Posting for posterity anyway.

.DEFINE blt bcc
digitbase=48

scratch=$f0 ; input in scratch+10..scratch+13, output in scratch+0..scratch+9, most significant first


    jmp test


convert:
    lda#digitbase

    ldx#9
initlp:
    sta scratch,x
    dex
    bpl initlp

    ldy#31
bitlp:

    ldx#9
correctionlp:
    lda scratch,x
    cmp#5+digitbase
    blt noover
    ;carry now set
    adc#128-5-1
    ;carry now clear
noover:
    sbc #digitbase/2-1
    sta scratch,x
    dex
    bpl correctionlp

    ; note - it doesn't matter what bits you shift into the bottom
    ; of the input bytes, as they never reach the output bytes
    ldx#13
shiftlp:
    rol scratch,x
    dex
    bpl shiftlp

    dey
    bpl bitlp

    rts


test:
    lda#13
    sta $d021
    jsr $e536
    lda#13
    jsr $ffd2
    lda#0
    sta $d021

    ldx#3
cpl:
    lda testv,x
    sta scratch+10,x
    dex 
    bpl cpl
    jsr convert

    ldx#9
codelp:
    lda scratch,x
    sta $040f,x
    dex
    bpl codelp
    rts


testv:
    .byte $49, $96, $02, $d2 ; 1234567890 = 0x499602d2
2018-10-23 14:48
Zirias

Registered: Jan 2014
Posts: 48
For the score display in 8192, I decided to create something reusable, although I needed it for 32 bit -- this should work for anything up to 128 bit if you adjust the constants, output is right-aligned and padded with spaces (0-padding would simplify it further):

.export numtostring
.exportzp nc_string
.exportzp nc_num

NUMSTRSIZE	= $a
NUMSIZE		= $4

.zeropage

nc_string:	.res	NUMSTRSIZE
nc_num:		.res	NUMSIZE

.code

numtostring:
		ldy	#NUMSTRSIZE
		lda	#$0
nts_fillzero:	sta	nc_string-1,y
		dey
		bne	nts_fillzero
		ldy	#(NUMSIZE*8)
nts_bcdloop:	ldx	#(NUMSTRSIZE-2)
nts_addloop:	lda	nc_string+1,x
		cmp	#$5
		bmi	nts_noadd
		adc	#$2
		sta	nc_string+1,x
nts_noadd:	dex
		bpl	nts_addloop
		asl	nc_num
		ldx	#($ff-NUMSIZE+2)
nts_rol:	rol	nc_num+NUMSIZE,x
		inx
		bne	nts_rol
		ldx	#(NUMSTRSIZE-2)
nts_rolloop:	lda	nc_string+1,x
		rol	a
		cmp	#$10
		and	#$f
		sta	nc_string+1,x
nts_rolnext:	dex
		bpl	nts_rolloop
		dey
		bne	nts_bcdloop
nts_scan:	cpx	#(NUMSTRSIZE-1)
		beq	nts_digits
		inx
		lda	nc_string,x
		bne	nts_digits
		lda	#$20
		sta	nc_string,x
		bne	nts_scan
nts_digits:	lda	nc_string,x
		ora	#$30
		sta	nc_string,x
		inx
		cpx	#(NUMSTRSIZE)
		bne	nts_digits
		rts
Previous - 1 | 2 | 3 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
fox/bonzai
TheRyk/MYD!
bugjam
MAT64
Matt
Mike
megasoftargentina
Guests online: 122
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Bromance  (9.6)
10 Memento Mori  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Onscreen 5k  (9.5)
8 Wafer Demo  (9.5)
9 Dawnfall V1.1  (9.5)
10 Quadrants  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Organizers
1 Burglar  (9.9)
2 Sixx  (9.8)
3 hedning  (9.7)
4 Irata  (9.7)
5 MWS  (9.6)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.057 sec.