www.retrosoftware.co.uk
http://www.retrosoftware.co.uk/forum/

INT to ASCII (as hex)
http://www.retrosoftware.co.uk/forum/viewtopic.php?f=73&t=548
Page 1 of 1

Author:  PaulA [ Sun Dec 05, 2010 12:40 pm ]
Post subject:  INT to ASCII (as hex)

This is the only part of a big block of code that isn't working and I've had enough of messing with it or trying to outwit google.

Code:
LDA &0900,X                 
AND #&0F                         
ADC #48                           
JSR &FFE3                         
LDA &0900,X                       
AND #&F0                         
LSR A:LSR A:LSR A:LSR A           
ADC #48                           
JSR &FFE3                         
CLC


The code is simply enough, break a byte into two nibbles, and add 48 so its ASCII 0-9 +(A-F). I still have to write a branch to do the A-F part.

For some reason its letting some junk through, of particular annoyance is VDU2 and teletext control codes.

Author:  PaulDv [ Sun Dec 05, 2010 3:26 pm ]
Post subject:  Re: INT to ASCII (as hex)

At a glance, I can't see anything obviously wrong with that code (other than printing the two nybbles the wrong way round).

I tend to use a small lookup table for printing hex numbers. Something like this:

Code:
.HEXDIGITS EQUB 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70
LDA &0900,X
PHA
LSR A:LSR A:LSR A:LSR A
TAY:LDA HEXDIGITS,Y
JSR OSWRCH
PLA
AND #&0F
TAY:LDA HEXDIGITS,Y
JMP OSWRCH

Author:  tautology [ Sun Dec 05, 2010 4:48 pm ]
Post subject:  Re: INT to ASCII (as hex)

Looks like Carry could be a problem, the only CLC is at the end.

You can also save a byte here:
Code:
LDA &0900,X                       
AND #&F0                         
LSR A:LSR A:LSR A:LSR A           
ADC #48                           

By changing it to:
Code:
LDA &0900,X                       
LSR A:LSR A:LSR A:LSR A     
CLC     
ADC #48                           

As you're shifting the bits right you don't need the AND #&F0; all you need to do is make sure C is clear before the add! (Same number of cycles though.)

Have you tried using the debugger on BeebEm to debug it - it's not the world's best debugger (I'd love to have a break if X=123 or ?&70=123 type function), but it's good to single step through code an see exactly what's happening.

Author:  PaulDv [ Sun Dec 05, 2010 6:01 pm ]
Post subject:  Re: INT to ASCII (as hex)

A problem with the carry would only manifest as an off-by-one error though. It wouldn't generate the character codes mentioned in the first post.

The original code looks like it's taking advantage of the carry being in a known state. For example, AND #&F0 followed by four LSRs will always result in C=0. Hence the lack of CLC.

@PaulA: Is this code copied from your source file or have you re-typed it. Just want to rule out the possibility of a typo like 'ADC 48' instead of 'ADC #48'.

BTW, if you're looking to optimise the routine, a couple more bytes could also be saved by replacing

Code:
CLC
ADC #48

with

Code:
ORA #48

and using PHA/PLA instead of the second LDA &0900,X

Author:  PaulA [ Sun Dec 05, 2010 10:35 pm ]
Post subject:  Re: INT to ASCII (as hex)

PaulDv wrote:
At a glance, I can't see anything obviously wrong with that code (other than printing the two nybbles the wrong way round).

I tend to use a small lookup table for printing hex numbers. Something like this:

Code:
.HEXDIGITS EQUB 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70
LDA &0900,X
PHA
LSR A:LSR A:LSR A:LSR A
TAY:LDA HEXDIGITS,Y
JSR OSWRCH
PLA
AND #&0F
TAY:LDA HEXDIGITS,Y
JMP OSWRCH


Thanks, I ended up using
Code:
.hexdigits EQUS "0123456789ABCDEF"

As it didn't like the commas and several lines of of EQUB statements is just messy.

Afraid I found the problem in some basic code that does the same thing so I can compare the two, I swear I deleted it earlier but obviously not.

I left the two AND statements as is, it makes the code more readable and in this case that matters.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/