| |
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 |
|
... 1 post hidden. Click here to view all posts.... |
| |
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
|
| |
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. |
| |
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. |
| |
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
|
| |
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 |
| |
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. |
| |
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 |
| |
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. |
| |
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. |
| |
Fungus
Registered: Sep 2002 Posts: 686 |
The drive is going to have to do a block read and block write anyways, so you may as well just use a prg or seq or even a usr file to do it. it won't be any faster or be less code to handle it with a rel file.
If you want to, you can just use the disk buffer and b-p command if you only need to read/write specific bytes at a specific offset in the block, ie, there is only ONE high score per level.
Of course it depends on how many levels you have, and how many records you need.
If it is how I think it is, you just need to use a few dos commands in order to handle it. listen, second, unlsn for writing the bytes to the buffer, and talk, tksa, and untalk for reading them. as well as acptr/getin, ciout/chrout for the actual data transferring... but you probly know how to do all that so I'm just being long winded now... |
Previous - 1 | 2 - Next |