8

Looking over old BASICs I find two general approaches to handling string variables.

HP/Wang/Atari/et al used C-like array-of-char whose memory size is specified with a DIM statement, with the upside of no heap management and the downside of always using the dimensioned amount of memory even for a string of length 0.

DEC/Microsoft used a heap of strings that did vary in length, but sometimes required periodic garbage collection runs to collect space again. BBC BASIC IIRC worked the same but didn't bother with the GC.

It seems one could use a fixed-size heap with fragments of, say 8-bytes, which would help GC at the cost of an extra byte per string (pointer to index for next fragment) and some wasted space (0>=x<8)

Are there any other approaches used in classic BASICs?

  • 1
    Well, wouldn't it be more like two bytes for the pointer (after all, when allocating a string, free fragments wouldn't always be less than 256 bytes away, or would they). Also, this will make all operation on strings slower, as they have to walk the fragment chain. – Raffzahn Jul 23 at 20:38
  • There was an implementation that did look thru a list of freed strings to allocate a new one, before going ahead an starting a garbage collection. I only vaguely remember this, so not an answer here. – Raffzahn Jul 23 at 20:41
  • @Raffzahn Also apparently Commodore BASIC 2.0 for the PET had very slow GC which was fixed when "BASIC 4.0 introduced an improved garbage collection system with back pointers" according to Wikipedia. Maybe a discussion of the various GC strategies would be on-topic here? – snips-n-snails Jul 23 at 21:11
  • 1
    @snips-n-snails Well, it would at least be related. After all, GC handling is a direct result of variable allocation strategy. – Raffzahn Jul 23 at 21:47
4

I don't know which of these variable length strings are used in Basic, but mainly there are several ways to implement variable arrays (maybe the list is incomplete);

  1. As you mention: pointer to the next fragment; or actually, it denotes the length of the string (i.e. the number of characters following)
  2. Same like 1, but the first byte contains the length including the first byte (so the same as one, but the first byte is 1 higher)
  3. More common in C variable strings: Starting with the characters, and and end-of-string delimiter of a 0 (\0) byte. In the ZX80 the " was used as end-of-string (see quote below).
  4. The high order bit as end of string flag (see second quote, TinyBasic).

ZX80

However, I could find one fragment about ZX80 Basic, which uses the " character as string delimiter, see wiki/String_(computer_science):

Using a special byte other than null for terminating strings has historically appeared in both hardware and software, though sometimes with a value that was also a printing character. $ was used by many assembler systems, : used by CDC systems (this character had a value of zero), and the ZX80 used "[3] since this was the string delimiter in its BASIC language.

Somewhat similar, "data processing" machines like the IBM 1401 used a special word mark bit to delimit strings at the left, where the operation would start at the right. This bit had to be clear in all other parts of the string. This meant that, while the IBM 1401 had a seven-bit word, almost no-one ever thought to use this as a feature, and override the assignment of the seventh bit to (for example) handle ASCII codes.

Early microcomputer software relied upon the fact that ASCII codes do not use the high-order bit, and set it to indicate the end of a string. It must be reset to 0 prior to output.

TINY BASIC

See TinyBasic Design, chapter Encoding partly:

In a number of places we have to indicate the end of a string of characters (or else we have to provide for its length somewhere). Commonly, one uses a special character (NUL = OOH for example) to indicate the end. This costs one byte per string but is easy to check. A better way depends upon the fact that ASCII code does not use the high order bit; normally it is used for parity on transmission. We can use it to indicate the end (that is, last character) of a string. When we process the characters we must AND the character with 07FH to scrub off the flag bit.

  • Nice list, but not related to the question, as it asks for examples from existign BASICs and their (internal) String Handling. – Raffzahn Jul 23 at 20:36
  • I know, although I can't imagine why BASICs wouldn't use this way too. So it's more a possibility. – Michel Keijzers Jul 23 at 20:40
  • 1
    @wizzwizz4 Not fully what I wished, but I could find something about the ZX80 Strings in Basic, see quote/link. – Michel Keijzers Jul 23 at 22:11
  • @MichelKeijzers That's great! Now it's officially an answer. ☺ Can you find anything more? – wizzwizz4 Jul 23 at 22:15
  • 2
    Interestingly, this is precisely how Atari BASIC marked the strings in its variable name table. When it encountered a new variable in code it added the name (no length limit) to the end of the list, and set the high bit on the last char. This meant that scans were expensive, but it only did this during edit time, at runtime the variables were replaced by a pointer to their value so there was no string matching at all. I wonder why they didn't do this for all strings? – Maury Markowitz Jul 24 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.