Text I/O

You learned earlier how to do single character I/O. This section concentrates on using strings of characters. RetroForth has several varieties of strings, and it is good to know when to use each type.

The "normal" string is a Forth string, consisting of an address,count pair. That is, it is represented on the stack by an address which points to the start of the character data, and a count of characters. In stack diagrams it is often listed as ( a # -- ). To create such a string, you may use a double-quote character, ". That word parses until it finds another double-quote, and then it puts the address and count on the stack. Inside a colon-definition, a special version called s" should be used instead.

" This is a string" type cr
: STR s" Hi there!" ;

STR type cr

The word type prints out the Forth string on top of the stack.

The second type of string supported by RetroForth is the ASCIIZ or zero-terminated string. That is, a string which is terminated by a NUL byte (the ASCII value 0). These are the native strings for C, and both Windows and Linux API functions expect such strings. Use the word zt to temporarily convert a Forth string to an ASCIIZ string.

" RetroForth rocks!" zt | Make an ASCIIZ string

When using s" inside a colon-definition, the strings will be compiled to a separate area which by default is 64K in size. So any "compiled" string will be physically separate from the "temporary" string area, and there is no danger of overwriting them (unless you have a bug in your code...). If you try to compile more than 64K of string data, RetroForth will likely crash. You can allocate mores space by doing:

here st0 ! xxxx allot

Replace xxxx with the amount of memory to allocate for strings.