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 > decimal to hex convert and REL format
2010-12-04 19:02
Erhan

Registered: Feb 2005
Posts: 17
decimal to hex convert and REL format

Hi,
i have two questions for coding here..

1) in memory, two bytes exists in this format: e.g. 04 and 50 repeatedly, but equals to 450 in decimal format and i need to convert them to 2 byte hexadecimal,
result will be $01C2 what is the shortest way? (i think @codebase, no similar examples)

2) in a diskfile, an existing data file's length is $20 bytes for example, i need to write $08 bytes to specific location in that file. (file format should be REL?)
and i need save routine for this,

Thanks
2010-12-04 20:52
Mace

Registered: May 2002
Posts: 1799
Untested, not optimized, not using decimal mode, answer to your first question:
		cld
		lda #$00
		ldy #$00
		ldx hiInput		; The hundreds
HUNDREDS:	cpx #$00
		beq PROCEED
		dex
		clc
		adc #$64		; x times 100
		bcc HUNDREDS
		iny
		jmp HUNDREDS

PROCEED:	sty hiResult		; store for a while
		sta loResult

		lda loInput		; get the tens and ones
		tay			; safely set it aside
		and #$f0		; get the high nibble
		lsr
		lsr
		lsr
		lsr			; divide by 16, equals the tens
		tax			; put it in x
		lda loResult

TENS:		cpx #$00
		beq STORE
		dex
		clc
		adc #$0a		; add x times 10
		bcc TENS
		inc hiResult		; increase hundreds at overflow
		jmp TENS
	
STORE:		tya
		and #$0f		; get the ones
		clc
		adc loResult		; add to the low byte
		sta loResult
		bcc !+
		inc hiResult		; increase hundreds at overflow
!:		rts
2010-12-04 22:47
Erhan

Registered: Feb 2005
Posts: 17
thanks Mace,
but i tried, i think problem on tens branches, so result is not as expected(wrong) checked with monitor, not assembler..
using your algorithm, this one is true but i think code is long, not optimised, also i need a solution to my second question

$1000 $1001 04 50
$1002 $1003 01 C2 result

	lda#$00
	sta $1002
	sta $1003
	lda $1001	;tens
	tay
	and #$0f
	sta $1003
	tya
	and #$f0
	lsr
	lsr
	lsr
	lsr
	tax
	cpx #$00
	beq HUND
LOOP1	lda $1003
	clc
	adc #$0a
	sta $1003
	dex
	bne LOOP1
HUND	lda $1000	,hundreds
	beq END
	tax
	lda $1003
LOOP2	clc
	adc #$64
	sta $1003
	lda $1002
	adc #$00
	sta $1002
	dex
	bne LOOP2
END	rts
2010-12-04 23:36
Mace

Registered: May 2002
Posts: 1799
Funny, without looking at your code, I also came to the conclusion that is was wiser to start with ones and tens and do hundreds afterwards. :-)

Shorter version, optimized and tested (Kick Assembler):
	.pc = $0810

		lda #$00	; Init result bytes
		sta loResult
		sta hiResult
		lda loInput	; Fetch ones and tens
		tax		; Save to X
		and #$0f	; Strip the ones
		tay		; Save to Y
		txa		; Get original ones and tens
		lsr
		lsr
		lsr
		lsr		; Divide by 16 to get tens only
		tax		; Save to X
		tya		; Put the ones in A
		cpx #$00	; No tens? Then skip
		beq PROCEED
		clc
TENS:		adc #$0a	; Add as many tens as value of X
		dex
		bne TENS
PROCEED:	ldx hiInput	; Fetch the hundreds
		beq END		; No hundreds? Then go to end
HUND:		clc
		adc #$64	; Add as many hundres as value of X
		bcc !+
		inc hiResult	; Increase high byte every passed $FF
!:		dex
		bne HUND
END:		sta loResult	; Store low byte
		rts

hiInput:	.byte $00
loInput:	.byte $01
hiResult:	.byte $00
loResult:	.byte $00


It's also in Codebase64.
2010-12-04 23:56
TNT
Account closed

Registered: Oct 2004
Posts: 189
Do you need thousands? In that case something like this might work (mostly untested, verify results before using it):
bcd2bin
	ldy	#0
	sty	bin_lo

	lax	bcd_hi
	beq	.no_hi

	jsr	.sub
	tax
	tya
.1	clc
	adc	#100
	bcc	.2
	iny
.2	dex
	bne	.1
	sta	bin_lo

.no_hi	lax	bcd_lo

;	16h + l
.sub	and	#$f0		; h << 4 = 16h
	lsr
	sta	tmp		; h << 3 = 8h
	lsr
	lsr			; h << 1 = 2h
	adc	tmp		; 8h + 2h = 10h
	sta	tmp
	txa
	and	#$0f		; l
	adc	tmp		; 10h + l

	adc	bin_lo
	bcc	.x
	iny
.x	rts

Result in Y and A.
2010-12-05 22:19
Erhan

Registered: Feb 2005
Posts: 17
thanks that codes did the trick..
(although i didn't need thousands digit)
3 digit decimal to hex convert code is working

now for the last problem, i need ideas
it will be a seperate diskfile of game high score for example 16 bytes total
AAAABBBBCCCCDDDD
i want to overwrite the file, changing contents of BBBB or any other 4 bytes blocks

thanks

2010-12-06 09:27
JackAsser

Registered: Jun 2002
Posts: 2014
Extremly slow version using decimal mode (untested and assumptions made on how decimal mode works):

sed
ldx #0
ldy #0
:
        inx
	bne :+
		iny
	:
	sec
	lda lobyte
	sbc #1
	sta lobyte
	lda hibyte
	sbc #0
	sta hibyte
bne :--
stx lobyte
sty hibyte


Doesn't even work for value=0... :P
2010-12-06 10:16
Frantic

Registered: Mar 2003
Posts: 1648
Don't use REL files. 16 bytes is not much... you might just as well save the whole 16 byte chunk each time, rather than bothering with changing specific bytes directly on the disk.
2010-12-06 11:07
Erhan

Registered: Feb 2005
Posts: 17
that will be an example of hiscore saver on a disk, one file, ingame routine detects high score is passed on level X e.g.
and my purpose is, only writing new hiscore of level X, without changing other hiscores by fully overwriting the file
2010-12-06 12:05
TNT
Account closed

Registered: Oct 2004
Posts: 189
JackAsser: start with x=y=-1 and change bne to bcs, no more problems with 0.

erhan: you already have to have all high scores in memory to test for new one, so I don't see the problem with overwrite.
2010-12-06 12:57
Frantic

Registered: Mar 2003
Posts: 1648
@erhan: Since you need to keep track of the old scores in order to know when a hiscore has been achieved the file must be loaded in any case, and then you can just as well rewrite the whole file anyway. If I am not mistaken, many (or even most?) file copiers do not support REL files anyway and therefore you may want to avoid that kind of solution. ...especially since 16 bytes is such a small amount of data anyway.
 
... 1 post hidden. Click here to view all posts....
 
Previous - 1 | 2 - 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
Mike
rambo/Therapy/ Resou..
krissz
grass/LETHARGY
visionvortex
Holy Moses/Role
MWR/Visdom
Jammer
Hexhog
Murphy/Exceed
Guests online: 102
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Swappers
1 Derbyshire Ram  (10)
2 Jerry  (9.8)
3 Violator  (9.7)
4 Acidchild  (9.7)
5 Cash  (9.6)

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