Strings

String operations are provided by the String Coprocessor and use the S.* namespace.

Memory itself is untyped. String instructions interpret their operands as strings and write string results back to ordinary memory.

Strings may contain up to 511 Unicode characters.

If a string operation produces a longer result, the value is truncated to 511 characters and er is set to:

STRING_OVERFLOW

Why 511 characters?

511 is the largest value representable by an unsigned 9-bit integer.

It is also enough to hold the complete character data of the NCS/e display in a single memory location.

String movement and stack operations are documented in Memory & Stack:

S.MOVE <dst> <src>
S.SWAP <a> <b>
S.PUSH <value>
S.POP <dst>

Unicode

Strings are stored internally using UTF-16, but string lengths and positions are measured in Unicode characters, not UTF-16 code units.

Characters outside the Basic Multilingual Plane therefore count as one character.

For example:

"A🚀B"

has a length of:

3

rather than 4.

S.CHAR and S.ORD operate on Unicode codepoints and support values outside the Basic Multilingual Plane.

Basic String Operations

S.UPR — Uppercase

S.UPR <dst> <str>

Converts a string to uppercase.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.

Modified locations

Location Description
dst Uppercase string.

Examples

S.UPR $000 "Hello, world!"
-- > $000 = "HELLO, WORLD!"

Uppercase a value already in memory:

S.UPR $000 $000

S.LWR — Lowercase

S.LWR <dst> <str>

Converts a string to lowercase.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.

Modified locations

Location Description
dst Lowercase string.

Examples

S.LWR $000 "Hello, WORLD!"
-- > $000 = "hello, world!"

S.LEN — Length

S.LEN <dst> <str>

Returns the number of Unicode characters in a string.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value String to measure.

Modified locations

Location Description
dst String length as an integer.

Examples

S.LEN $000 "Hello!"
-- > $000 = 6

Characters outside the Basic Multilingual Plane count as one character:

S.LEN $000 "A🚀B"
-- > $000 = 3

Search & Manipulation

S.FIND — Find

S.FIND <dst> <str> <search> [start]

Finds the first occurrence of a substring.

The optional starting position determines where the search begins.

If omitted:

start = 0

If the substring is not found, the result is:

-1

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value String to search.
3 search String value Substring to find.
4 start Integer value Optional starting position.

Modified locations

Location Description
dst Position of the first match, or -1.

Examples

Find a substring:

S.FIND $000 "Hello, world!" "world"
-- > $000 = 7

Begin searching at a particular position:

S.FIND $000 "one two one" "one" 1
-- > $000 = 8

S.JOIN — Join

S.JOIN <dst> <lhs> <rhs>

Concatenates two strings.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs String value Left string.
3 rhs String value Right string.

Modified locations

Location Description
dst Concatenated string.

Examples

S.JOIN $000 "Hello, " "world!"
-- > $000 = "Hello, world!"

Concatenate values from memory:

S.JOIN $002 $000 $001

S.INS — Insert

S.INS <dst> <str> <pos> <insert>

Inserts one string into another.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 pos Integer value Insertion position.
4 insert String value String to insert.

Modified locations

Location Description
dst Resulting string.

Examples

S.INS $000 "Helo" 3 "l"
-- > $000 = "Hello"

Insert at the beginning:

S.INS $000 "world!" 0 "Hello, "
-- > $000 = "Hello, world!"

S.SUB — Substring

S.SUB <dst> <str> <start> <end>

Extracts a range from a string and stores the result in the destination.

start and end are inclusive character positions.

Negative positions count backward from the end of the string.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 start Integer value Starting position.
4 end Integer value Ending position.

Modified locations

Location Description
dst Extracted substring.

Examples

Extract characters 1 through 3:

S.SUB $000 "Hello" 1 3
-- > $000 = "ell"

Use a negative ending position:

S.SUB $000 "Hello" 1 -2
-- > $000 = "ell"

Extract the final character:

S.SUB $000 "Hello" -1 -1
-- > $000 = "o"

Notes

If start is greater than or equal to the string length, the result is an empty string.

If end extends beyond the end of the string, it is clamped to the final character.

A negative end counts backward from the end of the string.

If the resolved range contains no characters, the result is an empty string.

S.DEL — String Delete

S.DEL <dst> <str> <start> <end>

Deletes a range from a string and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 str String value — Source string.
3 start Integer value — Starting position.
4 end Integer value — Ending position.

Modified locations

Location Description
dst String with the range deleted.

Examples

-- Delete part of a string.
S.DEL $000 "Hello, world!" 5 6
-- > $000 = "Helloworld!"

-- Delete the last character.
S.DEL $000 "Hello" -1 -1
-- > $000 = "Hell"

-- Delete the last 3 characters.
S.DEL $000 "Hello" -3 -1
-- > $000 = "He"

-- Positions before the beginning of the string are clamped.
S.DEL $000 "Hello" -9 1
-- > $000 = "llo"

-- Positions beyond the end of the string are clamped.
S.DEL $000 "Hello" 2 999
-- > $000 = "He"

-- A range entirely outside the string deletes nothing.
S.DEL $000 "Hello" 7 10
-- > $000 = "Hello"

-- A reversed range deletes nothing.
S.DEL $000 "Hello" 4 2
-- > $000 = "Hello"

Notes

  • start and end are inclusive.
  • Negative positions index from the end of the string, where -1 is the final character.
  • If start resolves before the beginning of the string, it is clamped to the beginning.
  • If end extends beyond the end of the string, it is clamped to the end.
  • If end resolves before the beginning of the string, nothing is deleted.
  • If start is beyond the end of the string, nothing is deleted.
  • If the resolved start position is greater than the resolved end position, nothing is deleted.

S.REP — Replace

S.REP <dst> <str> <search> <replace>

Replaces occurrences of a substring.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 search String value Substring to replace.
4 replace String value Replacement string.

Modified locations

Location Description
dst Resulting string.

Examples

S.REP $000 "one two one" "one" "three"
-- > $000 = "three two three"

Remove a substring:

S.REP $000 "Hello, world!" ", world" ""
-- > $000 = "Hello!"

Padding & Trimming

S.PADL — Pad Left

S.PADL <dst> <str> <pad> <len>

Pads the left side of a string until it reaches the requested length.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 pad String value Padding string.
4 len Integer value Requested length.

Modified locations

Location Description
dst Padded string.

Examples

S.PADL $000 "42" "0" 5
-- > $000 = "00042"

If the string already meets or exceeds the requested length, it is unchanged:

S.PADL $000 "Hello" "0" 3
-- > $000 = "Hello"

S.PADR — Pad Right

S.PADR <dst> <str> <pad> <len>

Pads the right side of a string until it reaches the requested length.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 pad String value Padding string.
4 len Integer value Requested length.

Modified locations

Location Description
dst Padded string.

Examples

S.PADR $000 "42" "0" 5
-- > $000 = "42000"

S.TRIML — Trim Left

S.TRIML <dst> <str> <trim>

Removes characters from the beginning of a string while they occur in the trim set.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 trim String value Characters to remove.

Modified locations

Location Description
dst Trimmed string.

Examples

Remove leading spaces:

S.TRIML $000 "   Hello" " "
-- > $000 = "Hello"

Remove multiple possible characters:

S.TRIML $000 "///...Hello" "/."
-- > $000 = "Hello"

S.TRIMR — Trim Right

S.TRIMR <dst> <str> <trim>

Removes characters from the end of a string while they occur in the trim set.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 trim String value Characters to remove.

Modified locations

Location Description
dst Trimmed string.

Examples

Remove trailing spaces:

S.TRIMR $000 "Hello   " " "
-- > $000 = "Hello"

Remove multiple possible characters:

S.TRIMR $000 "Hello...///" "/."
-- > $000 = "Hello"

Conversion

S.BIN — Convert to Binary

S.BIN <dst> <value> [base]

Converts a number from the specified base to binary text and stores the result in the destination.

If base is omitted, the input is interpreted as decimal.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 value String value — Value to convert.
3 base Integer value 2, 8, 10, 16 Input base. Defaults to 10.

Modified locations

Location Description
dst Binary text.

Examples

S.BIN $000 255
-- > $000 = "11111111"

S.BIN $000 "FF" 16
-- > $000 = "11111111"

S.BIN $000 "377" 8
-- > $000 = "11111111"

Notes

  • The result does not include a 0b prefix.
  • Negative values are represented with a leading -.
  • The input is interpreted according to base, regardless of its normal integer interpretation.

S.OCT — Convert to Octal

S.OCT <dst> <value> [base]

Converts a number from the specified base to octal text and stores the result in the destination.

If base is omitted, the input is interpreted as decimal.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 value String value — Value to convert.
3 base Integer value 2, 8, 10, 16 Input base. Defaults to 10.

Modified locations

Location Description
dst Octal text.

Examples

S.OCT $000 255
-- > $000 = "377"

S.OCT $000 "11111111" 2
-- > $000 = "377"

S.OCT $000 "FF" 16
-- > $000 = "377"

Notes

  • The result does not include a 0o prefix.
  • Negative values are represented with a leading -.
  • The input is interpreted according to base, regardless of its normal integer interpretation.

S.DEC — Convert to Decimal

S.DEC <dst> <value> [base]

Converts a number from the specified base to decimal text and stores the result in the destination.

If base is omitted, the input is interpreted as decimal.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 value String value — Value to convert.
3 base Integer value 2, 8, 10, 16 Input base. Defaults to 10.

Modified locations

Location Description
dst Decimal text.

Examples

S.DEC $000 255
-- > $000 = "255"

S.DEC $000 "11111111" 2
-- > $000 = "255"

S.DEC $000 "377" 8
-- > $000 = "255"

S.DEC $000 "FF" 16
-- > $000 = "255"

Notes

  • Negative values are represented with a leading -.
  • The input is interpreted according to base, regardless of its normal integer interpretation.

S.HEX — Convert to Hexadecimal

S.HEX <dst> <value> [base]

Converts a number from the specified base to hexadecimal text and stores the result in the destination.

If base is omitted, the input is interpreted as decimal.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 value String value — Value to convert.
3 base Integer value 2, 8, 10, 16 Input base. Defaults to 10.

Modified locations

Location Description
dst Hexadecimal text.

Examples

S.HEX $000 255
-- > $000 = "FF"

S.HEX $000 "11111111" 2
-- > $000 = "FF"

S.HEX $000 "377" 8
-- > $000 = "FF"

S.HEX $000 -42
-- > $000 = "-2A"

Notes

  • Hexadecimal digits are uppercase.
  • The result does not include a 0x prefix.
  • Negative values are represented with a leading -.
  • The input is interpreted according to base, regardless of its normal integer interpretation.

S.CHAR — Codepoint to Character

S.CHAR <dst> <codepoint>

Converts a Unicode codepoint to a character.

Operands

Position Name Type Description
1 dst Location Destination location.
2 codepoint Integer value Unicode codepoint.

Modified locations

Location Description
dst Resulting character.

Examples

S.CHAR $000 65
-- > $000 = "A"

Characters outside the Basic Multilingual Plane are supported:

S.CHAR $000 0x1F680
-- > $000 = "🚀"

Notes

Codepoint 0 produces an empty string.

Valid Unicode codepoints from 1 through 0x10FFFF produce the corresponding character.

Invalid codepoints, surrogate codepoints, and Unicode noncharacters produce the Unicode replacement character.

S.ORD — Character to Codepoint

S.ORD <dst> <str> <pos>

Returns the Unicode codepoint of a character in a string.

Positions are measured in Unicode characters.

Negative positions count backward from the end of the string.

Operands

Position Name Type Description
1 dst Location Destination location.
2 str String value Source string.
3 pos Integer value Character position.

Modified locations

Location Description
dst Unicode codepoint.

Examples

S.ORD $000 "ABC" 0
-- > $000 = 65
S.ORD $000 "A🚀B" 1
-- > $000 = 128640

Use a negative position:

S.ORD $000 "ABC" -1
-- > $000 = 67

Notes

If the requested position is outside the string, the result is 0.

If an unpaired UTF-16 surrogate is encountered, the result is 63, the codepoint for ?.

String Comparison

String comparison and conditional branching are provided by the S.B* branch family:

S.BEQ
S.BNEQ
S.BGT
S.BGE
S.BLT
S.BLE

These instructions are documented in Branching.