4.6. String Values

Strings are used to contain text. You can define a plain strings or escape code strings like this:

        // Plain strings
        .var message = "Hello World"
        .text message                         // Gives .text "Hello world" 
        .const file="c:\newstuff"

        // String with escape codes ('\esc') start with @
        .print @"First line.\nSecond line."   // Using newline 
        .print @“He said: \"Hello World\""    // Using " inside the string

        .text @"This text will loop now\$ff"  // placing hex values ($ff) in the text
 

@ in front of a string means you can use escape characters. Notice how '\n' in "c:\newstuff" is not a newline while '\n' in @"First line.\nSecond line." is. (Note: This is the opposite of C# and is this way to avoid breaking file references in old sources).

The supported escape codes are:

Table 4.2. Escape codes

Code Example Description
\b @"\b" Backspace
\f @"\f" Form feed
\n @"Line1\nLine2" Newline
\r @"\r" Carriage return
\t .print @"Hello\tWorld" Tab
\\ @"c:\\tmp\\myfile.txt” Backslash
\" @"It's called \"Bodiam Castle\"" Double quotes
\$ @"Hello world\$ff" Two digit hex values

Every object has a string representation and you can concatenate strings with the + operator. For example:

.var x=25
.var myString= “X is “ + x    // Gives myString = "X is 25"

You can use the .print directive to print a string to the console while assembling. This is useful when debugging. Printing x and y can be done like this:

        .print "x="+x 
        .print "y="+y

You can also print labels to see which location they refer to. If you do this, it's best to convert the label value to hexadecimal notation first:

        .print “int1=$”+toHexString(int1) 

int1:   sta regA+1
        stx regX+1
        sty regY+1
        lsr $d019
        // Etc.

Here is a list of functions/operators defined on strings:

Table 4.3. String Values

Function/Operator Description
+ Appends two strings.
asBoolean() Converts the string to a boolean value (eg, “true”.asBoolean()).
asNumber() Converts the string to a number value. Ex, “35”.asNumber().
asNumber(radix) Converts the string to a number value with the given radix (16=hexadecimal, 2=binary etc.). Ex, “f”.asNumber(16) will return 15.
charAt(n) Returns the character at position n.
size() Returns the number of characters in the string.
substring(i1,i2) Returns the substring beginning at i1 and ending at i2 (char at i2 not included).
toLowerCase() Return the lower version of the string.
toUpperCase() Return the uppercase version of the string.

Here are the functions that take a number value and convert it to a string:

Table 4.4. Numbers to Strings

Function Description
toIntString(x) Return x as an integer string (eg x=16.0 will return “16”).
toIntString(x,minSize) Return x as an integer string space-padded to reach the given minsize. (eg toIntString(16,5) will return “ 16”).
toBinaryString(x) Return x as a binary string (eg x=16.0 will return “10000”).
toBinaryString(x,minSize) Return x as a binary string zero-padded to reach the given minSize (eg toBinaryString(16,8) will return “00010000”).
toOctalString(x) Return x as an octal string (eg x=16.0 will return “20”).
toOctalString(x,minSize) Return x as an octal string zero-padded to reach the given minSize (eg toBinaryString(16,4) will return “0020”).
toHexString(x) Return x as a hexadecimal string (eg x=16.0 will return “10”).
toHexString(x,minSize) Return x as an hexadecimal string zero-padded to reach the given minSize (eg toBinaryString(16,4) will return “0010”).

You can get the string representation of an arbitrary value by using the general .string() function. Eg.

        .print 1234.string().charAt(2)    // Prints 3