NCL 107: Naming Values
At the end of the last lesson, our program looked like this:
#count r0
#total r1
#limit r2
MOVE #count 1
MOVE #total 0
MOVE #limit 10
$again
ADD #total #total #count
INC #count
BLE $again #count #limit
D.TXT #total
D.BLT
The three registers have different jobs:
| Alias | Starts at | Changes? |
|---|---|---|
#count |
1 | Yes |
#total |
0 | Yes |
#limit |
10 | No |
#count changes every time around the loop.
#total changes as each number is added to it.
But #limit is different. We put 10 into it:
MOVE #limit 10
and never change it again.
Do we really need a register for that?
Naming a value directly
Instead of giving r2 the alias #limit:
#limit r2
and then putting 10 into that register:
MOVE #limit 10
we can give the value 10 a name directly:
#limit 10
This is a constant.
Wherever we use:
#limit
it means:
10
Our program can now be written as:
#count r0
#total r1
#limit 10
MOVE #count 1
MOVE #total 0
$again
ADD #total #total #count
INC #count
BLE $again #count #limit
D.TXT #total
D.BLT
We no longer need r2.
The branch:
BLE $again #count #limit
works just as it did before.
Because #limit means 10, it is equivalent to writing:
BLE $again #count 10
Constants in the NCL reference
The NCL reference uses constant as the general term for names beginning with #, including names assigned to registers.
In these lessons, we'll usually call a name assigned to a register, such as #count r0, an alias, and a name assigned directly to a fixed value, such as #limit 10, a constant.
Both use the same NCL syntax.
Aliases and constants
Compare these two definitions:
#count r0
#limit 10
#count is an alias for r0.
The value stored in r0 can change while the program is running:
INC #count
changes the value stored there.
#limit, on the other hand, means the value 10.
There is no register behind it containing a value that can change. Anywhere we use #limit, we're using the value 10.
That's why this makes sense:
BLE $again #count #limit
but trying to change the constant itself does not:
INC #limit
INC needs a register where it can store the new value. #limit names a value, not a register.
If a value needs to change while the program runs, keep it in a register.
If a value stays fixed, a constant may be a better fit.
Why give a value a name?
For our small program, we could simply write:
BLE $again #count 10
There's nothing wrong with that.
Using:
#limit 10
doesn't make the calculation possible. The program already worked without it.
The name tells us what the value means.
Compare:
BLE $again #count 10
with:
BLE $again #count #limit
Both compare against the same value.
In the first version, you need to know why the program is comparing against 10.
In the second, the program tells you: 10 is the limit.
Using a constant more than once
Constants become even more useful when the same value has meaning in several places.
Let's make our program tell us what it is doing:
#count r0
#total r1
#limit 10
D.TXT "Adding numbers through "
D.TXT #limit
D.TXT ": "
MOVE #count 1
MOVE #total 0
$again
ADD #total #total #count
INC #count
BLE $again #count #limit
D.TXT #total
D.BLT
The program displays:
Adding numbers through 10: 55
The value represented by #limit is now used in two places.
Here:
D.TXT #limit
it tells the user what the limit is.
Here:
BLE $again #count #limit
it controls when the loop ends.
Now suppose we want the program to add the numbers through 20.
We only need to change:
#limit 10
to:
#limit 20
Both uses of #limit now mean 20.
We don't need to search through the program for every place where the old limit was used.
Choosing useful names
Just like register aliases, constants are most useful when their names describe what they represent.
For example:
#limit 10
is more informative than:
#number 10
The name #limit tells us why the value matters to this program.
Different programs might give the same value very different meanings:
#limit 10
#width 10
#attempts 10
All three names represent the value 10, but they tell the reader three different things about why that value is there.
Try it
Start with this program:
#count r0
#total r1
#limit 10
D.TXT "Adding numbers through "
D.TXT #limit
D.TXT ": "
MOVE #count 1
MOVE #total 0
$again
ADD #total #total #count
INC #count
BLE $again #count #limit
D.TXT #total
D.BLT
Change only the definition:
#limit 10
Try several different values.
Before running the program, predict what will change.
Which parts of the program use #limit?
Which values need registers because they change while the program runs?
Which value can be a constant because it stays fixed?
Finally, look at these definitions:
#count r0
#total r1
#limit 10
Make sure you can explain the difference between them.
#count and #total are aliases for registers whose contents can change.
#limit is a constant representing the fixed value 10.
In NCL 108: Making Decisions with Numbers, we'll use calculations, branches, loops, aliases, and constants together to make a program respond differently to different values.