NCL 202: Changing Text
In NCL 201, we learned how to store strings in string registers:
#message s0
SMOVE #message "Hello, world!"
D.TXT #message
D.BLT
Once a string is stored in a register, the program can do more than remember it.
It can change it.
Changing a string
Let's turn our message into uppercase:
#message s0
SMOVE #message "Hello, world!"
SUPR #message #message
D.TXT #message
D.BLT
The program displays:
HELLO, WORLD!
SUPR converts the letters in a string to uppercase.
In:
SUPR #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 register.
We've done the same kind of thing with integers:
ADD r0 r0 1
An instruction can read a value from a register and store its result back into that same register.
Strings are no different.
NCL also provides SLWR, which converts letters to lowercase:
#message s0
SMOVE #message "Hello, WORLD!"
SLWR #message #message
D.TXT #message
D.BLT
This displays:
hello, world!
Keeping the original
The source and destination do not have to be the same register.
For example:
#original s0
#upper s1
SMOVE #original "Hello!"
SUPR #upper #original
D.TXT #original
D.TXT " "
D.TXT #upper
D.BLT
The program displays:
Hello! HELLO!
After SUPR executes, the registers contain:
| Register | String |
|---|---|
#original |
Hello! |
#upper |
HELLO! |
SUPR read the string from #original, but stored its result in #upper.
The original string was never changed.
The same is true of SLWR:
#original s0
#lower s1
SMOVE #original "Hello!"
SLWR #lower #original
Whether you replace the original value or keep both versions depends on which register 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 SJOIN:
#name s0
#first "Ada"
#last "Lovelace"
SJOIN #name #first " "
SJOIN #name #name #last
D.TXT #name
D.BLT
The first SJOIN:
SJOIN #name #first " "
joins #first with a space and stores the result in #name.
At that point:
| Name | String |
|---|---|
#first |
Ada |
#last |
Lovelace |
#name |
Ada |
Then:
SJOIN #name #name #last
joins the current value of #name with #last.
Now:
| Name | String |
|---|---|
#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. SJOIN actually produces a new string that the rest of the program can use.
Numbers and strings
So far, we've kept integers and strings separate.
For example:
#number r0
#message s0
MOVE #number 42
SMOVE #message "Hello!"
#number contains an integer.
#message contains a string.
NCL can also convert between the two kinds of value.
Consider:
#numberText s0
#number 42
SMOVE #numberText #number
D.TXT #numberText
D.BLT
#number is the integer 42.
But SMOVE stores its result in a string register, so NCL converts the integer into the string "42" before storing it in #numberText.
Converting a value from one kind to another is called casting.
Casting can also work in the other direction:
#number r0
#text "42"
MOVE #number #text
ADD #number #number 8
D.TXT #number
D.BLT
The program displays:
50
#text contains the string "42".
When:
MOVE #number #text
executes, NCL converts that string to the integer 42 and stores it in #number.
The following ADD then works with that integer normally.
The destination determines which kind of value is stored:
| Instruction | Supplied value | Stored value |
|---|---|---|
MOVE r0 42 |
integer 42 |
integer 42 |
MOVE r0 "42" |
string "42" |
integer 42 |
SMOVE s0 "42" |
string "42" |
string "42" |
SMOVE s0 42 |
integer 42 |
string "42" |
MOVE writes to an integer register.
SMOVE writes to a string register.
When necessary, NCL casts the supplied value to the kind needed by the destination.
Building a message from a number
Casting becomes useful when a program calculates a number and then needs to use that number as part of a string.
For example:
#number r0
#message s0
#start 42
#prefix "The answer is "
MOVE #number #start
ADD #number #number 8
SMOVE #message #number
SJOIN #message #prefix #message
D.TXT #message
D.BLT
Let's follow what happens.
First:
MOVE #number #start
ADD #number #number 8
calculates:
42 + 8 = 50
so #number contains the integer 50.
Then:
SMOVE #message #number
casts that integer to a string and stores it in #message:
| Name | Value |
|---|---|
#number |
integer 50 |
#message |
string 50 |
Finally:
SJOIN #message #prefix #message
joins the fixed prefix with the string currently in #message.
The result is:
The answer is 50
and that new string is stored back into #message.
The program has taken a number produced by a calculation, converted it to text, and used it to build a message.
Try it
Start with this program:
#number r0
#label s0
#message s1
#start 5
#labelText "result: "
MOVE #number #start
MUL #number #number 5
SUPR #label #labelText
SMOVE #message #number
SJOIN #message #label #message
D.TXT #message
D.BLT
Before running it, follow the program in your mind.
What value does #number contain after MUL?
What string does SUPR store in #label?
What string does SMOVE store in #message?
What does the final SJOIN 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 registers because the program produces or changes them while it runs.
So far, we've learned how to remember strings, change them, combine them, and convert between strings and integers.
But sometimes a program doesn't need to change a string.
Sometimes it needs to learn something about it.
In NCL 203: Looking Inside Strings, we'll start asking questions about the strings our programs are working with.