4.6. String Values

Strings are used to contain text. You can define a string like this:

        .var message = "Hello World"
        .text message    // Gives .text "Hello world"

Normally quotes (") will denote the end or start of the string. You can use the quote as a character in the string by adding a backslash in front of the quote:

        .text “He said: \"Hello World\""

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.2. 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.3. 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