Looking for some PASM code to convert a LONG to ASCII
Bean
Posts: 8,129
I'm trying to convert the STR command from SX/B for the PropBASIC compiler.
In SX/B I basically did this:
subtract 10,000 until overflow
subtract 1,000 until overflow
subtract 100 until overflow
subtract 10 until overflow
But the Propeller has LONGs which would require 10 loops, which is going to use up alot of code space. Or I could just divide the value by 10 each time, but that's not very easy either (dividing by 10).
I'm wondering if anyone has any better ideas about how to convert a LONG into ascii character without using too much code space ?
Any ideas are welcome too...
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·
In SX/B I basically did this:
subtract 10,000 until overflow
subtract 1,000 until overflow
subtract 100 until overflow
subtract 10 until overflow
But the Propeller has LONGs which would require 10 loops, which is going to use up alot of code space. Or I could just divide the value by 10 each time, but that's not very easy either (dividing by 10).
I'm wondering if anyone has any better ideas about how to convert a LONG into ascii character without using too much code space ?
Any ideas are welcome too...
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·
Comments
One "cheap" way to do this would be to initialize a "divisor" to 10^10 and do a conditional subtract (CMPSUB) until the subtract fails, each time incrementing your ASCII digit, then multiple the "divisor" by 10 using shifts and adds ((4*x+x)*2) and repeat until done. You'd need to include zero suppression in the logic.
temp := 10000000000
repeat 10
... digit := "0"
... repeat while value >= temp
...... value -= temp
...... digit ++
... ' output digit
... value := (value << 2 + value) << 1
Post Edited (Mike Green) : 11/5/2009 3:29:14 PM GMT
· Good idea. You mean to multiply the dividend (well really the remainder)·by 10 right ?
· I've never seen it done that way. But you're right, that will use a lot less instructions. Thanks.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
Post Edited (Bean (Hitt Consulting)) : 11/5/2009 4:17:59 PM GMT
The reason for doing it this way is that you don't need to divide by 10 and multiplying by 10 by shifting and adding is very cheap. You could use a table of powers of ten, and the cost on the Prop of indexing through a table of constants is relatively low, but this would work and I think would take less memory.
· Here is how I implemented it into PropBASIC.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·