NCL 201: Remembering Text
Way back in NCL 101, our first program contained:
D.CHR "Hello, world!"
We've used text like this throughout the course, but so far we've usually written the text directly into an instruction.
Just as the CPU can remember the numbers we've worked with, it can remember text.
Strings
Text in NCL is stored as a string.
A string is a sequence of characters, such as:
Hello, world!
When we write a string directly into a program, we surround it with quotation marks:
"Hello, world!"
This is called a string literal.
The quotation marks tell NCL where the string begins and ends. They are not part of the text itself.
For example:
D.TXT "Hello, world!"
D.BLT
displays:
Hello, world!
not:
"Hello, world!"
You've been using string literals since the beginning. Now we're going to start storing and working with them.
Remembering a string
In the 100-level lessons, we used memory locations to remember values:
MOVE $000 5
We can use the same memory to remember a string.
For string values, NCL provides S.MOVE:
S.MOVE $000 "Hello, world!"
The S. tells us that this instruction works with strings.
Let's store a message and display it:
S.MOVE $000 "Hello, world!"
D.TXT $000
D.BLT
The program displays:
Hello, world!
When:
D.TXT $000
executes, the display receives the value currently stored in $000.
Why remember the string?
For this program:
S.MOVE $000 "Hello, world!"
D.TXT $000
D.BLT
you might reasonably ask why we bothered storing the message at all.
We could have simply written:
D.TXT "Hello, world!"
D.BLT
And for this program, that would be simpler.
Just like our first uses of memory in the 100-level lessons, storing a value becomes useful when the program needs to do something with that value.
For now, let's simply use the same stored string more than once:
S.MOVE $000 "Hello!"
D.TXT $000
D.TXT " "
D.TXT $000
D.BLT
The program displays:
Hello! Hello!
Both instructions:
D.TXT $000
use the same string stored in $000.
Change:
S.MOVE $000 "Hello!"
to:
S.MOVE $000 "Welcome!"
and the program displays:
Welcome! Welcome!
There is still only one stored string. We're simply using its value twice.
Naming the location
We already know that numbered memory locations become difficult to keep track of as programs grow.
Instead of:
S.MOVE $000 "Hello!"
D.TXT $000
we can give $000 a useful name:
#message $000
S.MOVE #message "Hello!"
D.TXT #message
#message is an alias for $000.
So our earlier program can become:
#message $000
S.MOVE #message "Hello!"
D.TXT #message
D.TXT " "
D.TXT #message
D.BLT
This is the same use of aliases we've already seen with numbers. The name helps us remember what the location is being used for.
Memory does not have a type
The instruction determines the interpretation
Memory locations are not specifically integer or string locations.
A location such as $000 can be used with either kind of value.
The instruction using a value determines how that value is interpreted.
We've already used MOVE:
MOVE $000 42
MOVE expects an integer value.
S.MOVE expects a string value:
S.MOVE $000 "Hello!"
This difference matters when the source itself contains text.
Consider:
MOVE $000 "42 Apples"
MOVE $001 " 42 Apples"
MOVE $002 "-42 Apples"
MOVE $003 "Apples 42"
S.MOVE $004 "42 Apples"
After these instructions, memory contains:
| Location | Value |
|---|---|
$000 |
42 |
$001 |
42 |
$002 |
-42 |
$003 |
0 |
$004 |
42 Apples |
Why does MOVE accept \"42 Apples\"?
MOVE interprets its source using NCL's integer conversion rules.
Conversion begins at the start of the value, allowing leading whitespace and a sign, and continues for as long as the text forms a valid integer.
That's why:
"42 Apples" → 42
" 42 Apples" → 42
"-42 Apples" → -42
"Apples 42" → 0
MOVE isn't searching the string for a number. It is interpreting the beginning of the value as an integer.
S.MOVE treats its source as a string instead.
So:
S.MOVE $004 "42 Apples"
stores the complete text:
42 Apples
The memory locations themselves are no different. MOVE and S.MOVE simply interpret their values differently.
We'll make much more use of this as we learn more string operations.
Remembering more than one string
A program can remember several strings in different memory locations.
For example:
#first $000
#second $001
S.MOVE #first "Hello"
S.MOVE #second "World"
D.TXT #first
D.TXT " "
D.TXT #second
D.BLT
The program displays:
Hello World
The two locations contain separate strings:
| Location | String |
|---|---|
#first |
Hello |
#second |
World |
Changing one does not change the other.
For example:
#first $000
#second $001
S.MOVE #first "Goodbye"
S.MOVE #second "World"
D.TXT #first
D.TXT " "
D.TXT #second
D.BLT
displays:
Goodbye World
Try it
Start with:
#first $000
#second $001
S.MOVE #first "Hello"
S.MOVE #second "World"
D.TXT #first
D.TXT " "
D.TXT #second
D.BLT
Change the two strings to make your own message.
Then try using one of the stored strings more than once.
For example, can you make the program display:
Hello World Hello
without writing "Hello" into a D.TXT instruction?
Think about which string is stored in each location and which location each D.TXT uses.
So far, though, the strings themselves haven't changed.
#first contains one string.
#second contains another.
We display them separately and put a space between them:
D.TXT #first
D.TXT " "
D.TXT #second
What if we wanted to change the strings themselves, or combine those pieces into a new string?
In NCL 202: Changing Text, we'll start modifying strings and combining them to create new ones.