NCL 106: Naming Memory

In the last lesson, our program used three memory locations:

MOVE $000 1
MOVE $001 0
MOVE $002 10

:again
ADD $001 $001 $000
INC $000
BLE :again $000 $002

D.TXT $001
D.BLT

Each location had a different job:

Location Job
$000 Current number
$001 Running total
$002 Ending number

We know what they mean because we just wrote the program.

But if you came back to it later, an instruction like:

ADD $001 $001 $000

doesn't tell you what $000 and $001 are being used for.

As programs use more memory locations, remembering what every numbered location means becomes increasingly difficult.

NCL lets us give them names.

Giving a memory location a name

We can give $000 the name #count:

#count $000

This is called an alias.

After defining the alias, we can use:

#count

anywhere we would have used:

$000

For example:

#count $000

MOVE #count 5
D.TXT #count
D.BLT

displays:

5

#count does not create another memory location.

It is another name for $000.

The same memory location

Because #count and $000 refer to the same memory location, we can demonstrate their relationship directly:

#count $000

MOVE #count 5
D.TXT $000
D.BLT

This displays:

5

We stored the value using the name #count and retrieved it using the name $000.

We can do the opposite as well:

#count $000

MOVE $000 5
D.TXT #count
D.BLT

Again, the program displays:

5

An alias is another name, not another location

#count and $000 refer to the same memory location.

There aren't two values and changing one does not copy anything. Both names refer to the same stored value.

For example:

#count $000

MOVE #count 5
INC $000
D.TXT #count
D.BLT

displays:

6

Changing $000 also changes the value we see through #count, because they are the same memory location.

You normally won't want to switch between the two names like this. Using the alias consistently makes the program easier to read.

Naming our memory locations

Let's return to the program from the last lesson.

We were using:

Location Job
$000 Current number
$001 Running total
$002 Ending number

We can give each location a name that describes its job:

#count $000
#total $001
#limit $002

Now we can replace the numbered locations throughout the program:

#count $000
#total $001
#limit $002

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

This program works exactly the same way as the original.

Compare:

ADD $001 $001 $000
INC $000
BLE :again $000 $002

with:

ADD #total #total #count
INC #count
BLE :again #count #limit

The CPU performs the same operations in either case, but the aliases make the purpose of each memory location much easier for us to follow.

For example:

ADD #total #total #count

now tells us that we're adding the current count to the running total and storing the result back in the total.

And:

BLE :again #count #limit

compares the count against the limit.

We no longer need to remember that $000 is the count, $001 is the total, and $002 is the limit every time we read an instruction.

Real assemblers do this too

Symbolic names aren't unique to NCL.

Assemblers for real processors commonly let programmers assign names to things that would otherwise be written as numeric registers, addresses, or values.

The processor still works with the underlying machine representation. The names are there to make the program easier for humans to write and read.

Following the named locations

Aliases also make it easier to trace a program.

In the last lesson, we followed the values in $000, $001, and $002.

Now we can follow those same values by their purpose:

Instruction #count #total #limit
MOVE #count 1 1
MOVE #total 0 1 0
MOVE #limit 10 1 0 10
ADD #total #total #count 1 1 10
INC #count 2 1 10
ADD #total #total #count 2 3 10
INC #count 3 3 10
ADD #total #total #count 3 6 10
INC #count 4 6 10
ADD #total #total #count 4 10 10
INC #count 5 10 10
ADD #total #total #count 5 15 10
INC #count 6 15 10

The values haven't become any simpler.

What has changed is that we no longer need a separate reminder of what each column represents.

#count tells us what the first value is doing.

#total tells us what the second value is doing.

#limit tells us what the third value is doing.

When you're tracing a larger program, meaningful names make it much easier to see what is changing and why.

Choosing useful names

Aliases can be named for whatever job a memory location has in your program.

For example, our program uses:

#count $000
#total $001
#limit $002

Another program might use those same locations for completely different purposes:

#width $000
#height $001
#area $002

There is nothing special about $000 that makes it a counter, or $001 that makes it a total.

Those were simply the jobs we chose for them in our program.

A useful alias describes what the memory location represents.

Try it

Here is another program using three numbered memory locations:

MOVE $000 1
MOVE $001 1
MOVE $002 5

:again
MUL $001 $001 $000
INC $000
BLE :again $000 $002

D.TXT $001
D.BLT

First, follow the program without changing it.

Keep track of $000, $001, and $002.

What does each memory location do?

What value does the program eventually display?

Once you understand what the locations are being used for, choose useful names for them.

Add aliases at the beginning of the program, then replace the numbered locations with those aliases.

There isn't necessarily only one good name for each location. Choose names that make the program's purpose clearer to you.

Run the named version and make sure it still produces the same result.

One value that never changes

Take another look at our earlier program:

#count $000
#total $001
#limit $002

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

#count changes as the loop runs.

#total changes as each number is added to it.

But what about #limit?

We put 10 into it:

MOVE #limit 10

and never change it again.

We gave a memory location a useful name, only to use that location to hold the same value for the entire program.

Do we really need a memory location for that?


In the next lesson, NCL 107: Naming Values, we'll give fixed values useful names of their own.