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 always written the text directly into an instruction.

Just as the CPU can remember an integer, 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 doing more with them.

String registers

In the 100-level lessons, we stored integers in registers such as:

r0
r1
r2

NCL also provides registers for storing strings:

s0
s1
s2
...
s15

The two kinds of register store different kinds of values:

Registers Can remember
r0r31 Integers
s0s15 Strings

To put an integer into an integer register, we've been using MOVE:

MOVE r0 5

To put a string into a string register, we use SMOVE:

SMOVE s0 "Hello, world!"

The S at the beginning tells us that this instruction works with strings.

Remembering a string

Let's store a message and display it:

SMOVE s0 "Hello, world!"

D.TXT s0
D.BLT

The program displays:

Hello, world!

This works much like the integer registers we've already used.

Compare:

MOVE r0 5
D.TXT r0
D.BLT

with:

SMOVE s0 "Hello, world!"
D.TXT s0
D.BLT

In the first program, r0 remembers the integer 5.

In the second, s0 remembers the string Hello, world!.

When:

D.TXT s0

executes, the display receives the string currently stored in s0.

Why remember the string?

For this program:

SMOVE s0 "Hello, world!"

D.TXT s0
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 use of r0 back in NCL 101, 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:

SMOVE s0 "Hello!"

D.TXT s0
D.TXT " "
D.TXT s0
D.BLT

The program displays:

Hello! Hello!

Both instructions:

D.TXT s0

use the same string stored in s0.

Change:

SMOVE s0 "Hello!"

to:

SMOVE s0 "Welcome!"

and the program displays:

Welcome! Welcome!

There is still only one stored string. We're simply using its value twice.

Naming string registers

We already know that numbered registers become difficult to keep track of as programs grow.

String registers can have aliases just like integer registers.

Instead of:

SMOVE s0 "Hello!"
D.TXT s0

we can give s0 a useful name:

#message s0

SMOVE #message "Hello!"
D.TXT #message

#message is an alias for s0.

Just as:

#count r0

gives an integer register another name,

#message s0

gives a string register another name.

So our earlier program can become:

#message s0

SMOVE #message "Hello!"

D.TXT #message
D.TXT " "
D.TXT #message
D.BLT

From here on, we'll use useful aliases for string registers just as we did for integer registers.

Integer and string registers

Integer registers and string registers are separate.

For an integer, we use an integer register and MOVE:

#number r0

MOVE #number 5

For a string, we use a string register and SMOVE:

#message s0

SMOVE #message "Hello!"

You don't need to learn a new way of thinking about registers.

They still give the CPU somewhere to remember a value while the program runs.

We simply have different registers for different kinds of values.

Remembering more than one string

Just as a program can use several integer registers, it can use several string registers.

For example:

#first s0
#second s1

SMOVE #first "Hello"
SMOVE #second "World"

D.TXT #first
D.TXT " "
D.TXT #second
D.BLT

The program displays:

Hello World

The two registers contain separate strings:

Register String
#first Hello
#second World

Changing one does not change the other.

For example:

#first s0
#second s1

SMOVE #first "Goodbye"
SMOVE #second "World"

D.TXT #first
D.TXT " "
D.TXT #second
D.BLT

displays:

Goodbye World

Try it

Start with:

#first s0
#second s1

SMOVE #first "Hello"
SMOVE #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 register and which register 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.