Subject: [stella] Binary to Decimal routine (small!) From: "Andrew Davie" <adavie@xxxxxxxxxxxxx> Date: Sat, 10 Feb 2001 02:00:44 +1100 |
I needed a binary-to-decimal conversion routine, to convert my binary (2 byte) score into 5 decimal digits for use by the previously posted double-height 6-digit score routine. The last digit is 0 in my game. As I only use two bytes for my score (binary), my range is 0-65535 (ie: score is 0-655350) I'm becoming paranoid about ROM space, and thought I'd tackle the MOST EFFICIENT binary-to-decimal routine I could. Efficient being "smallest ROM footprint". It was quite tricky, and this code is suitably obscure...but I'm hoping somebody on the list can either improve it, or give another routine which is smaller. Also, I rather enjoyed doing this one, and would like to share my efforts. I thrive on feedback, good or bad - and I don't bite, really. Here's the actual code... ---------------------------------------------------------------------------- --- ToDecimal ; Convert decimal score to binary ; pass a = LOW byte of score ; y = HIGH byte of score ; returns scoredigit[5] = score, little-endian pha ldx #5 bne ZapD Trick tay pla sbc Tens-1,x pha AnotD inc score+1,x ; <-- 0 on init, else increment digit sec sbc Tens-1,x tya sbc TensH-1,x bcs Trick ZapD lda #$FF sta score,x pla pha dex bne AnotD pla sta score+1 ; on exit, scoredigit[5] holds decimal representation rts Tens .byte <10 .byte <100 .byte <1000 .byte <10000 TensH .byte >10 .byte >100 .byte >1000 .byte >10000 ---------------------------------------------------------------------------- --- Attached is a sample bin, and the testbed code, so you can play with it. It actually works, though I had a HELL of a time getting it to! It turns out that PCAE (yes, I was still running the old version) had a bug in the 6502 core, and it choked when doing the following... sec sbc Tens-1,x tya sbc TensH-1,x bcs Trick No matter WHAT was in "TensH-1,x", you would always end with the carry set. I checked this very very very very very very carefully, with hardwired X, A, etc... It took me a long long time (several hours) to discover this bug in the emulator :( I've switched from non-working bin in (yes, old) PCAE to working bin in Z26. Why use the old PCAE? Once bitten, right? The new windows one simply doesn't work on my machine, and the link to the new DOS one was stuffed on the PCAE site, so I couldn't grab it. D'Oh! I hadn't been using Z26, as it is(was) dog-slow under Windows 2000. HOWEVER, it runs about 1/4 normal speed when you use the -r switch, which is an acceptable compromise for me! So, there we go. Now I'm using Z26, and the above code has been tested. It's not so much the algorithm which is neat, but that it's so small in ROM space. Can anyone beat 44 bytes? Re: the double-height 6-digit score routine... From: "Glenn Saunders" cybpunks@xxxxxxxxxxxx > Is there a way to use a JMP or something to get it to run through twice > rather than physically duplicating the code or would the branching logic > going to make the timing too unpredictable? I had a look at it, but it's very difficult. The timing is very tricky, and also there's not many spare cycles. Maybe somebody else can have a play with it... I, too, would like a smaller version :)) Cheers A -- _ _ _| _ _ _| _ * _ _ , (_|| )(_|( (/_\/\/ (_|(_|\/(_(/_ ,~' L_|\ ,-' \ see my Museum of Soviet Calculators at ( \ http://www.taswegian.com/MOSCOW/soviet.html \ __ / L,~' "\__/ @--> v
Attachment:
test.bin
Description: Binary data
Attachment:
test.asm
Description: Binary data
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [stella] double-height 6-digit , Glenn Saunders | Thread | Re: [stella] Binary to Decimal rout, Erik Mooney |
Re: [stella] double-height 6-digit , Glenn Saunders | Date | Re: [stella] Binary to Decimal rout, Erik Mooney |
Month |