STRINGS

Strings are held internally by the assembler as ASCII NUL terminated one dimensional arrays of bytes. There is no internal limit to the size of a string. Each character of a string may have any of the binary values 1 to 255 (0 is reserved as the string terminator). An empty string is represented by an array with the first byte set to 0.

Within the assembler source code, strings are any sequence of characters delimited by single or double quotes. The closing quote character must match the open quote character.

e.g.
	 "the quick brown fox"		; this is a valid string using double quote
					; string delimiters

	 'the quick brown fox'		; this is a valid string using single quote
					; string delimiters
String processing is a very important component of the XCASM assembler. The facilities provided are not as sophisticated as those of systems such as M4, ICON or Perl but they do allow resonable string manipulation to the extent required by most assembly programmers.

XCASM allows strings to be easily converted to and from numbers and also to and from labels and assembler variable names.

Strings can be assigned to assembler variables

e.g.
	 fred	.set		; "the quick brown fox"
Strings can be embedded within the emitted data of a program
e.g.
	 bert	.db		; "the quick brown fox"
Strings expressions can be evaluated and assigned to assembler variables
e.g.
	jack	.set	10
	fred	.set	"ans=%d":jack

		.db	fred

	; is equivalent to
		.db	"ans=10"
Strings can also be used in place of assembler variables names. This provides a means of using variables indirectly.

Stings can be:

String Concatination

Strings can be built by combining smaller strings. Appending a string to the end of another string is known as string concatination. Building a string from several smaller strings is a very important basic string manipulation facility.
e.g.
	a1	.set	"ABC"
	a2	.set	"XYZ"
	a3	.set	"123"

	fred	.set	a1+a3+a2

		.db	fred

	; is equivalent to
		.db	"ABC123XYZ"

Adding a number to a string is equivalent to converting the number to the string and then concatinating the two strings.

e.g.
	a1	.set	"ABC"
	a2	.set	"XYZ"
	a3	.set	123
	a4	.set	20
	fred	.set	a1+(a3+a4)+a2

		.db	fred

	; is equivalent to
		.db	"ABC143XYZ"

	; here the parenthesis arounf the a3+a4 causes the sub-expression
	; to be evaluated first and since it is an integer expression this
	; results in 143 which is converted into a string before concatination
see also string CONCATINATION operator for a more detailed description

Format Operator

The format operator (the ':' character) allows strings to be built using format strings as templates. Format strings as similar to C format strings used in printf statements, with the exception that only the first format specifier in the format string is valid.
e.g.
	a1	.set	"123"
	fred	.set	"ABC %d XYZ":a1

		.db	fred

	; is equivalent to
		.db	"ABC 123 XYZ"
see also string FORMAT operator for a more detailed description

Integer Coercion

When evaluating expressions, strings are automatically coerced to integers where the expression implies an integer is required and a string is supplied, and integers are automatically coerced to strings where the expression implies a string is required and an integer is supplied.
e.g.
	a1	.set	123
	fred	.set	"ABC " + a1 + " XYZ" 	; automatic coercion of integer to string

		.db	fred

	; is equivalent to
		.db	"ABC 123 XYZ"


	a1	.set	0x123
	fred	.set	a1 + "0xBC00"	; automatic coercion of string to integer

		.dw	fred

	; is equivalent to
		.dw	0xBD23
These are more detailed descriptions of string to integer coercion and integer to string coercion

Label To String

Converting labels to strings allows complex operations on symbolic addresses to be easilliy performed.

String To Label

Converting strings to labels allows complex operations on symbolic addresses to be easilliy performed.
e.g.
	fred	.set	"taddr_%d":($ >> 8)
		call	fred

Embedded Quoted Strings

It is possible to embed quote characters in a string provided they are different to those of the string delimiter.

e.g.
	 "the answer is 'bert'"		; this is a valid string using double quote
					; string delimiters, it contains a quoted
					; (quoted with single quotes)

	 'the answer is "bert"'		; this is a valid string using double quote
					; string delimiters, it contains a quoted
					; (quoted with single quotes)

	 "the answer is "bert""		; this is a invalid string using double quote
					; string delimiters. The intension is to embed
					; a quoted string within a string, but the assembler
					; treats this sequence as a string followed by the
					; symbol bert followed by an empty string