NCL 202: Changing Text

In NCL 201, we learned how to store strings in memory:

#message $000

S.MOVE #message "Hello, world!"

D.TXT #message
D.BLT

Once a string is stored in memory, the program can do more than remember it.

It can change it.

Changing a string

Let's turn our message into uppercase:

#message $000

S.MOVE #message "Hello, world!"
S.UPR #message #message

D.TXT #message
D.BLT

The program displays:

HELLO, WORLD!

S.UPR converts the letters in a string to uppercase.

In:

S.UPR #message #message

the first #message is where the result will be stored. The second is the string to convert.

The instruction reads the current value of #message, produces an uppercase version, and stores the result back into the same location.

We've done the same kind of thing with integers:

ADD $000 $000 1

An instruction can read a value from a location and store its result back into that same location.

NCL also provides S.LWR, which converts letters to lowercase:

#message $000

S.MOVE #message "Hello, WORLD!"
S.LWR #message #message

D.TXT #message
D.BLT

This displays:

hello, world!

Both instructions have the same general form:

S.UPR <dst> <str>
S.LWR <dst> <str>

The first operand is the destination.

The second is the string to change.

Keeping the original

The source and destination do not have to be the same location.

For example:

#original $000
#upper $001

S.MOVE #original "Hello!"
S.UPR #upper #original

D.TXT #original
D.TXT " "
D.TXT #upper
D.BLT

The program displays:

Hello! HELLO!

After S.UPR executes, the locations contain:

Location Value
#original Hello!
#upper HELLO!

S.UPR read the string from #original, but stored its result in #upper.

The original string was never changed.

The same is true of S.LWR:

#original $000
#lower $001

S.MOVE #original "Hello!"
S.LWR #lower #original

Whether you replace the original value or keep both versions depends on which location you choose as the destination.

Combining strings

At the end of NCL 201, we used two separate strings:

#first "Ada"
#last "Lovelace"

D.TXT #first
D.TXT " "
D.TXT #last
D.BLT

This displays:

Ada Lovelace

but the program never actually creates the string:

Ada Lovelace

It simply sends three separate values to the display.

We can create a new string by joining strings together with S.JOIN:

#name $000

#first "Ada"
#last "Lovelace"

S.JOIN #name #first " "
S.JOIN #name #name #last

D.TXT #name
D.BLT

S.JOIN has three operands:

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

It joins the two source strings together and stores the result in the destination.

The first instruction:

S.JOIN #name #first " "

joins #first with a space and stores the result in #name.

At that point:

Name Value
#first Ada
#last Lovelace
#name Ada

Then:

S.JOIN #name #name #last

joins the current value of #name with #last.

Now:

Name Value
#first Ada
#last Lovelace
#name Ada Lovelace

Finally:

D.TXT #name

displays the completed string.

This is different from displaying several strings one after another. S.JOIN actually produces a new string and stores it in memory, where the rest of the program can use it.

Joining strings

Joining strings end to end is commonly called concatenation.

Different programming languages provide different syntax for it, but the operation is the same idea as S.JOIN: take two strings and produce one string containing both.

Using numbers in strings

In NCL 201, we saw that memory itself does not have separate integer and string types.

The instruction using a value determines how that value is interpreted.

This means that a value produced by arithmetic can also be used by a string instruction.

Consider:

#value $000
#message $001

MOVE #value 42
ADD #value #value 8

S.JOIN #message "The answer is " #value

D.TXT #message
D.BLT

The program displays:

The answer is 50

First:

MOVE #value 42
ADD #value #value 8

uses #value as an integer.

After the ADD, it contains:

50

Then:

S.JOIN #message "The answer is " #value

uses that same value as a string.

S.JOIN joins:

The answer is 

with:

50

and stores:

The answer is 50

in #message.

We didn't need another instruction to move the result into special string storage.

ADD interprets #value as an integer.

S.JOIN interprets #value as a string.

The value can therefore move directly from a calculation into a piece of text.

Building a message

Let's combine the operations from this lesson.

Suppose we want to calculate a value and display it with a label:

THE ANSWER IS 50

We can keep the label as a constant:

#labelText "the answer is "

Then calculate the number:

#value $000

MOVE #value 42
ADD #value #value 8

We can produce an uppercase version of the label:

#label $001

S.UPR #label #labelText

Now #label contains:

THE ANSWER IS 

Finally, we can join the label with our calculated value:

#message $002

S.JOIN #message #label #value

The complete program is:

#value $000
#label $001
#message $002

#labelText "the answer is "

MOVE #value 42
ADD #value #value 8

S.UPR #label #labelText
S.JOIN #message #label #value

D.TXT #message
D.BLT

The program displays:

THE ANSWER IS 50

The three locations each have a different job:

Location Value
#value 50
#label THE ANSWER IS
#message THE ANSWER IS 50

The arithmetic produces a value.

S.UPR produces a string.

S.JOIN combines them into the final message.

Try it

Start with this program:

#value $000
#label $001
#message $002

#start 5
#labelText "result: "

MOVE #value #start
MUL #value #value 5

S.UPR #label #labelText
S.JOIN #message #label #value

D.TXT #message
D.BLT

Before running it, follow the program in your mind.

What value does #value contain after MUL?

What string does S.UPR store in #label?

What does the final S.JOIN produce?

Then run it and check your prediction.

The result should be:

RESULT: 25

Try changing #start or the arithmetic.

Then try changing:

#labelText "result: "

to another label.

Notice which values remain fixed throughout the program and which values need memory because the program produces or changes them while it runs.

So far, we've learned how to remember strings, change them, and combine them into new strings.

Sometimes, though, a program doesn't need to change a string.

Sometimes it needs to learn something about it.


In the next lesson, NCL 203: Looking Inside Strings, we'll start asking questions about the strings our programs are working with.