It is currently Mon Oct 20, 2014 4:49 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: INT to ASCII (as hex)
PostPosted: Sun Dec 05, 2010 12:40 pm 
Offline
 Profile

Joined: Sun May 02, 2010 2:07 pm
Posts: 42
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.


Top
 
PostPosted: Sun Dec 05, 2010 3:26 pm 
Offline
User avatar
 Profile

Joined: Sun Jun 28, 2009 11:37 pm
Posts: 55
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


Top
 
PostPosted: Sun Dec 05, 2010 4:48 pm 
Offline
 Profile

Joined: Sat Sep 04, 2010 5:28 pm
Posts: 92
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.


Top
 
PostPosted: Sun Dec 05, 2010 6:01 pm 
Offline
User avatar
 Profile

Joined: Sun Jun 28, 2009 11:37 pm
Posts: 55
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


Top
 
PostPosted: Sun Dec 05, 2010 10:35 pm 
Offline
 Profile

Joined: Sun May 02, 2010 2:07 pm
Posts: 42
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.


Top
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron