Open Source

The Commando Forth Compiler

By Al Williams, December 03, 2009

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).

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.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!