View Full Version : spaces in . operator
panorama
07-24-04, 06:03 PM
Hi,
I am outputting data like this:
." X" X CR, where X is 5, for example. I expect to get:
X5<CR>, but what I get is:
X5<space><CR><LF>.
How do I get rid of the space and LF?
Thanks,
Tim
RMDumse
07-26-04, 05:03 AM
I suspect there's something left out of your example, because I don't see the printing command that prints the 5. Unless perhaps X is defined as a printing routine itself, instead of a variable.
Perhaps you mean ." X" X @ . CR
Also I fail to see the space you want to get rid of. Perhaps you could explain that issue further.
However, I can say something about the LF. The definition of CR is D EMIT A EMIT as best I remember, which emits a CR and a LF. So if you only want a CR, just use D EMIT instead. On most terminals this will cause the cursor to move to the left of the line and anything new printed will overwrite whats already on the line.
panorama
07-26-04, 12:38 PM
Randy,
I guess your board software ate some of the characters I used. You are correct that I am doing:
." X" X @ . CR
What it prints is:
X5spaceCRLF.
Thanks for the hint on the emit operator. That will work great. Now just for the space and I will be all set.
Tim
RMDumse
07-26-04, 02:37 PM
The Forth formatting words allow you to control all the characters made up into the output string. You might try this:
: X.
58 EMIT ( emit character for X
X @ 0 ( get X value and make into double
<# #S #> TYPE ( format as string and then type
D EMIT ( issue CR without LF
;
and I don't think you'd have a space included.
panorama
07-26-04, 03:30 PM
Randy,
Well, LOL, yes, it does print the number without a space, but unfortunately it also prints -1 as 65535. I tried appending ffff instead of 0 if the value was negitive, but that only gave me 4 billion and something.
I am beginning to think it will be easier to first check +/- and emit a - sign if neg, then take abs, then divide by 10000 and send a char if any, then 1000, then 100, then 10, then the remainder.
Tim
RMDumse
07-26-04, 04:41 PM
You should be able to use the SIGN formating word, but for reasons I can't figure, I can't make it work just now.
But...
This should work for you.
HEX
: X.
58 EMIT ( emit character for X
X @ ( get X value
DUP 0< IF 2D EMIT THEN
ABS 0 ( convert it to a double
<# #S #> TYPE ( format as string and then type
D EMIT ( issue CR without LF
;
panorama
07-26-04, 11:23 PM
Randy,
Yup, I already did this, but was hoping for something more elegent.
Thanks for the help, got everything working now.
One more question though, What's the easiest way to rearrange bits in a word? For example, suppose I input 8 data bits from PA, but I want them in a different order, such as 14723506.
Is there an easy way to do this besides something like:
0 ( push 0
A @ 01 and if 40 + then ( keep adding to top of stack
A @ 02 and if 01 + then
..etc...
I was hoping for something like indexing where I could run a loop 8 times and have the result.
Thanks.
Tim
RMDumse
07-27-04, 09:39 AM
I've never seen anything elegant for reordering bits in a byte. Nothing comes screaming to mind.
I will say I'd probably try to avoid using the IF THENs though, as they take up lots of memory with branches. I'd probably try to do it with all booleans, and count on 0= returning all 1's or 0's. For instance:
0 B !
A @ 01 AND 0= NOT 40 AND B @ OR B !
A @ 02 AND 0= NOT 01 AND B @ OR B !
Then with some factoring, it could be made smaller and more elegant
: OR>B AND B @ OR B ! ;
: A>MASK A @ AND 0= NOT ;
0 B !
01 A>MASK 40 OR>B
02 A>MASK 01 OR>B
If I were going to take it further, I'd make a data structure that had both patterns in it and index into the structure for the patterns each access.
But probably this is more work than necessary unless you have many of these structures to sort out.
panorama
07-27-04, 09:55 PM
Randy,
Thanks. Memory is not of concern to me, but speed is, and my construct would seem a lot faster.
Tim
vBulletin v3.0.7, Copyright ©2000-2012, Jelsoft Enterprises Ltd.