PDA

View Full Version : s" word to make a counted string


gtiani
06-06-05, 06:16 PM
How can I define a word to make a counted string?
I'm currently using this rather crude method:
: MSG1 ." HELLO WORLD" ; \ make the string but never call this word
: ^STR_MSG1 \ extract the address and count of the string
['] MSG1 4 + \ address of string
['] MSG1 3 + P@ ; \ count

It works but I would like it to look like this:
: ^STR_MSG1
S" HELLO WORLD" ;

I am using this to send a string to an LCD display.
Thank you
Gary

gtiani
06-07-05, 10:52 AM
I found the answer to my question in a 1-07-05 post by moderator Rob in the FORTH only forum. I tested it and it works fine. It's just what I was looking for.
Thank you
Gary

This is from Rob's post:
: ` ( "a" -- n ) BL WORD 1+ C@ [COMPILE] LITERAL ; IMMEDIATE

: " ( "string" -- a ) STATE @ IF COMPILE BRANCH >MARK THEN
` " WORD DUP >R C@ CELL+ CELL / CELL * ALLOT
STATE @ IF >RESOLVE THEN R> [COMPILE] LITERAL ; IMMEDIATE

I used the following form on my ServoPod:
: ASCII ( "a" -- n ) BL WORD 1+ C@ [COMPILE] LITERAL ; IMMEDIATE

: " ( "string" -- a ) STATE @ IF COMPILE BRANCH >MARK THEN
ASCII " WORD DUP >R C@ 1+ ALLOT
STATE @ IF >RESOLVE THEN R> [COMPILE] LITERAL ; IMMEDIATE