NCL 105: Keeping Track of More

So far, whenever our programs needed to remember a value, we've used $000.

For example, this loop counts from 1 to 5:

MOVE $000 1

:again
D.TXT $000
D.TXT " "
INC $000
BLE :again $000 5

D.BLT

Throughout the program, $000 has one job: it keeps track of the current number.

But a program often needs to keep track of more than one thing at a time.

Adding the numbers together

Suppose we want to add the numbers from 1 through 5.

The calculation we want is:

1 + 2 + 3 + 4 + 5 = 15

We already know how to make $000 count from 1 through 5.

But we also need somewhere to keep the total as it grows.

If we use $000 for the total, we lose the number we're counting with.

Fortunately, $000 is only one memory location.

We can use another one:

$001

This lets us give the two locations different jobs:

Location Job
$000 Current number
$001 Running total

We'll begin the current number at 1:

MOVE $000 1

The total starts at 0:

MOVE $001 0

Now we can add the current number to the total:

ADD $001 $001 $000

Remember that the first value after ADD is where the result goes.

If $000 contains 1 and $001 contains 0, this instruction adds:

0 + 1

and stores the result back in $001.

Afterward:

$000 = 1
$001 = 1

Most importantly, $000 has not changed.

We can still use it to control our loop.

Putting it in a loop

Let's combine that with the loop from the previous lesson:

MOVE $000 1
MOVE $001 0

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

D.TXT $001
D.BLT

There are now two values changing as the program runs.

To understand what the program is doing, we need to keep track of both.

Let's follow the first couple of trips through the loop.

The first two instructions give us:

$000 = 1
$001 = 0

Then we reach :again.

The first instruction in the loop:

ADD $001 $001 $000

adds the current number to the running total.

On the first trip through the loop, that means:

0 + 1 = 1

So $001 becomes 1.

Then:

INC $000

changes $000 from 1 to 2.

Finally:

BLE :again $000 5

asks whether 2 is less than or equal to 5.

It is, so the CPU goes back to :again.

This time, ADD adds:

1 + 2 = 3

The total in $001 is now 3, while the current number in $000 is still 2.

The two memory locations are doing different jobs, and each changes independently.

Tracing more than one value

When a program uses several memory locations, it can become difficult to keep all of their values in your head.

Instead, we can write down the value of each location after every instruction that changes one.

For our loop:

Instruction $000 $001
MOVE $000 1 1
MOVE $001 0 1 0
ADD $001 $001 $000 1 1
INC $000 2 1
ADD $001 $001 $000 2 3
INC $000 3 3
ADD $001 $001 $000 3 6
INC $000 4 6
ADD $001 $001 $000 4 10
INC $000 5 10
ADD $001 $001 $000 5 15
INC $000 6 15

After the final INC, $000 contains 6.

The branch:

BLE :again $000 5

asks whether 6 is less than or equal to 5.

It isn't, so the loop ends.

Then:

D.TXT $001
D.BLT

displays:

15

Trace one instruction at a time

When a program has several changing values, you don't need to keep all of them in your head.

Write them down, then follow one instruction at a time. Whenever an instruction changes a value, update that value in your trace.

This becomes increasingly useful as programs get larger.

Try changing it

Change the loop so it adds the numbers from 1 through 10.

You only need to change one instruction.

Before running it, follow the first few trips through the loop.

What is in $000?

What is in $001?

When will the loop stop?

You don't need to trace the entire calculation if you already understand the pattern.

Then run the program and see what it displays.

Another memory location

We can also put the end of the count into memory.

Instead of:

BLE :again $000 10

we could store 10 in another location:

MOVE $002 10

and compare against $002:

BLE :again $000 $002

Our program becomes:

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

Now the program is keeping track of three different values:

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

$002 does not change during the loop, but keeping the value there means the program can use it wherever it needs the ending number.

It also shows us something important about the branch:

BLE :again $000 $002

The values being compared do not have to be numbers written directly into the instruction.

Here, the CPU compares the value in $000 with the value in $002.

Try changing:

MOVE $002 10

to another value.

The rest of the loop does not need to change.

Keeping track of memory

Our program is still fairly small, but there is a new problem.

Look at this instruction:

ADD $001 $001 $000

If you haven't looked at the program for a while, do you remember what $001 is for?

What about $000?

Or $002?

We can keep reminding ourselves:

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

But as programs get larger and use more memory locations, remembering what every numbered location is supposed to contain quickly becomes inconvenient.

We'll solve that problem next.


In the next lesson, NCL 106: Naming Memory, we'll give memory locations names that describe what they're being used for.