Channels ▼
RSS

Parallel

The Commando Forth Compiler


Strings

To use strings, you need to place them globally on a single line. The first word is " and the second word is the name of the string. The third word is a "terminating word" (typically a quote but can be anything).


" HelloWorld "
Hello World!
"
" PromptString -
"Would you like to play a game?"
-

" SillyString EndOfString
This is a dumb way to end 
a string with more than one line.
EndOfString

Then using the word "HelloWorld" will give you the address of this string. If you use a string, the compiler will place it in flash at the end of your program. Multiline strings get CRLF at the end of each line (except the last line). If you omit the "end token" the " character is the default. Note if you want extra bytes on the first line, you must specify the end token as well.

If you want to put extra bytes at the front or end of a string you can do that by putting them on the first or last line. So:


" CountAndNullString " 5
Hello
" 0

This makes a string of "\005Hello\000"

You can also put the word "pack" (must be lowercase) in the 4th position to get a packed string (see the assembler doc for more on packed strings).


" packingstring " pack 5
Hello
" 0

If you want a counted string, you can automatically add a word count to the start of a string with #:


" astring " #
This counts
" 0

This counts the number of characters (words). In a packed string, it still counts the number of words, not characters. The count is not included in the word count. So, in the above example, there are 12 characters (including the 0 but not including the count itself).

You can use @f-addr and @f-next to access the string, or see monitor.4th for a ." word that will print a zero terminated string.

The Library

The compiler doesn't really understand any words other than the interpreted words. All of the normal words appear in header files. The basic core words use assembly language and then many of the words build on those core words.

There are two library files included with each program: 1forth.lib contains basic words or words that use assembly for efficiency. The forth-core.lib file contains definitions that use other Forth words exclusively (and would not require porting for a new platform). The compiler driver script includes the forth-core.lib file first so that the machine-specific file can override things in the generic library. For example, both library files might have integer multiply words, but presumably the specific version is preferable.

In addition, programs can include monitor.4th if they need to use services from the One-Der monitor (for example, printing and reading numbers from the serial terminal).


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video