NCL 205: Comparing and Interpreting Strings
So far, whenever information about a string controlled our program, we first turned that information into a number.
S.LEN gave us a length.
S.FIND gave us a position.
In NCL 204, we used those numbers to calculate ranges and check the lengths of extracted strings.
But sometimes the question we want to ask is about the string itself.
Comparing strings
Suppose we have a name:
#name $000
S.MOVE #name "EMMA"
We want to know whether it contains exactly:
EMMA
S.BEQ compares two strings and branches if they are equal:
S.BEQ <target> <lhs> <rhs>
For example:
#name $000
S.MOVE #name "EMMA"
S.BEQ :emma #name "EMMA"
D.TXT "Someone else"
JUMP :done
:emma
D.TXT "Hello, Emma!"
:done
D.BLT
S.BEQ compares the complete string in #name with "EMMA".
Because they are equal, the program displays:
Hello, Emma!
Change:
S.MOVE #name "EMMA"
to:
S.MOVE #name "ALICE"
Now the comparison fails and the program displays:
Someone else
Unlike S.FIND, S.BEQ is not looking for one string inside another. It compares the two complete strings.
Equal and not equal
There is also S.BNEQ:
S.BNEQ <target> <lhs> <rhs>
It branches if two strings are not equal.
For example:
S.BNEQ :other #name "EMMA"
branches to :other if #name contains anything other than "EMMA".
The integer and string equality branches correspond closely:
| Integers | Strings | Branch when |
|---|---|---|
BEQ |
S.BEQ |
Equal |
BNEQ |
S.BNEQ |
Not equal |
The difference is how their values are interpreted.
BEQ and BNEQ compare integer values.
S.BEQ and S.BNEQ compare string values.
For example:
#value $000
S.MOVE #value "042"
BEQ :integerMatch #value 42
S.BEQ :stringMatch #value "42"
BEQ branches because both operands have the integer value 42.
S.BEQ does not branch because "042" and "42" are different strings.
The value stored in #value has not changed. The two instructions simply interpret it differently.
Other string branches
NCL also provides S.BGT, S.BGE, S.BLT, and S.BLE for comparing the lexicographical order of strings.
We won't need them in this lesson. S.BEQ and S.BNEQ are enough when we only need to know whether strings match.
Capitalization matters
String comparisons are case-sensitive.
These strings are different:
EMMA
Emma
emma
For example:
#name $000
S.MOVE #name "Emma"
S.BEQ :emma #name "EMMA"
D.TXT "No match"
JUMP :done
:emma
D.TXT "Match"
:done
D.BLT
displays:
No match
If capitalization should not matter, we can normalize the string before comparing it:
#name $000
S.MOVE #name "Emma"
S.UPR #name #name
S.BEQ :emma #name "EMMA"
D.TXT "No match"
JUMP :done
:emma
D.TXT "Match"
:done
D.BLT
Now #name contains "EMMA" when the comparison occurs, so the program displays:
Match
We used the same technique with S.FIND in NCL 203.
Interpreting a string
Suppose we have:
SAY:Hello!
Using the techniques from NCL 204, we can split that into:
#command = "SAY"
#argument = "Hello!"
For now, suppose that work has already been done:
#command $000
#argument $001
S.MOVE #command "SAY"
S.MOVE #argument "Hello!"
We can use S.BEQ to decide what the command means:
S.BEQ :say #command "SAY"
D.TXT "Unknown command"
JUMP :done
:say
D.TXT #argument
:done
D.BLT
Because #command contains "SAY", the program branches to :say and displays:
Hello!
Change the command to:
S.MOVE #command "BANANA"
and the comparison fails:
Unknown command
S.BEQ does not understand commands. It only compares strings.
The meaning of "SAY" comes from what our program does when that comparison succeeds.
More than one command
More comparisons give us more recognized commands.
Let's add:
SHOUT
#command $000
#argument $001
S.MOVE #command "SHOUT"
S.MOVE #argument "Hello!"
S.BEQ :say #command "SAY"
S.BEQ :shout #command "SHOUT"
D.TXT "Unknown command"
JUMP :done
:say
D.TXT #argument
JUMP :done
:shout
S.UPR #argument #argument
D.TXT #argument
:done
D.BLT
The comparisons form another chain of decisions, like the ones we built with numbers in NCL 109.
The first matching branch selects a path. If none match, execution reaches the default path:
D.TXT "Unknown command"
With "SHOUT" as the command, execution reaches :shout and displays:
HELLO!
Normalizing the command
Our commands are currently case-sensitive.
That means:
SAY
works, but:
say
does not.
For our command language, we can decide that capitalization should not matter:
S.UPR #command #command
S.BEQ :say #command "SAY"
S.BEQ :shout #command "SHOUT"
Now all of these are interpreted as the same command:
SAY
say
Say
sAy
Only #command needs to be normalized.
For:
say:Hello, Emma!
we want to recognize say as SAY while preserving the argument:
Hello, Emma!
exactly as it was supplied.
Putting the pieces together
Now we can combine the splitting from NCL 204 with the comparisons from this lesson.
Our input will have the form:
COMMAND:argument
First, we split and validate the input using the same process as NCL 204.
Then we normalize the extracted command.
Finally, we compare it against the commands we recognize and choose the corresponding behavior.
#separator $000
#length $001
#leftEnd $002
#rightStart $003
#command $004
#argument $005
#input "SHOUT:Hello, Emma!"
S.FIND #separator #input ":"
BEQ :invalid #separator -1
S.LEN #length #input
BEQ :invalid #length 1
MOVE #leftEnd #separator
DEC #leftEnd
MOVE #rightStart #separator
INC #rightStart
S.SUB #command #input 0 #leftEnd
S.SUB #argument #input #rightStart -1
S.LEN #length #command
BEQ :invalid #length 0
S.LEN #length #argument
BEQ :invalid #length 0
S.UPR #command #command
S.BEQ :say #command "SAY"
S.BEQ :shout #command "SHOUT"
D.TXT "Unknown command"
JUMP :done
:say
D.TXT #argument
JUMP :done
:shout
S.UPR #argument #argument
D.TXT #argument
JUMP :done
:invalid
D.TXT "Invalid input"
:done
D.BLT
With:
#input "SHOUT:Hello, Emma!"
the program displays:
HELLO, EMMA!
Change the input to:
#input "say:Hello, Emma!"
and it displays:
Hello, Emma!
The complete program now has three main stages:
- Parse and validate the input.
- Normalize the command.
- Interpret the command and run its behavior.
Try these inputs and predict which path each one will take:
SAY:Good morning!
banana:Hello!
:Hello!
SHOUT:
Try it
Add a third command:
WHISPER:Hello, WORLD!
When the command is WHISPER, display its argument in lowercase.
The result should be:
hello, world!
You'll need another comparison:
S.BEQ :whisper #command "WHISPER"
and another path:
:whisper
You have already used the instruction that converts a string to lowercase.
Try your program with:
WHISPER:NCS/E READY
and predict what it will display before running it.
Then add another command:
REPEAT:Hello!
Make it display:
Hello! Hello!
This one does not require a new string operation.
You already have the argument. You only need to decide what to do with it.
We can now extract pieces from structured text and choose behavior based on what those pieces contain.
In the next lesson, NCL 206: Building and Formatting Strings, we'll start constructing strings into useful layouts and formats.