NCL 103: Changing the Path

So far, every program we've written has followed the same path.

The NCS/e starts at the first instruction, executes it, moves to the next instruction, and continues downward until there are no more instructions.

For example:

MOVE $000 5
INC $000
MUL $000 $000 2
D.TXT $000
D.BLT

You can follow this program simply by reading from top to bottom.

But what if we want the computer to do something more than once?

Doing something repeatedly

Suppose we want the NCS/e to count down from 5.

We already know enough NCL to do that:

MOVE $000 5
D.TXT $000
D.TXT " "
DEC $000
D.TXT $000
D.TXT " "
DEC $000
D.TXT $000
D.TXT " "
DEC $000
D.TXT $000
D.TXT " "
DEC $000
D.TXT $000
D.BLT

This works.

The program displays:

5 4 3 2 1

But this is rather tedious.

We're doing the same things over and over:

D.TXT $000
D.TXT " "
DEC $000

If we wanted to count down from 100, we could keep copying those instructions.

We could.

Let's not.

Going back

Instead of writing the same instructions repeatedly, we can tell the CPU to execute the same instructions repeatedly.

MOVE $000 5
D.TXT $000
D.TXT " "
DEC $000
BGT 2 $000 0
D.BLT

This program displays the same countdown:

5 4 3 2 1 

Most of the program should already be familiar.

The first instruction:

MOVE $000 5

puts 5 into $000.

Then:

D.TXT $000
D.TXT " "
DEC $000

writes the value in $000, writes a space after it, and subtracts 1.

The only new instruction is:

BGT 2 $000 0

BGT means branch if greater than.

It compares two values. If the first value is greater than the second, execution continues from the specified location instead of continuing to the next instruction.

Here:

BGT 2 $000 0

means:

If the value in $000 is greater than 0, continue execution from line 2.

If $000 is not greater than 0, the branch does not happen. The CPU simply continues with the next instruction as usual.

Following the branch

Let's follow what happens.

We begin with:

MOVE $000 5

so $000 contains 5.

The program writes 5 and a space to the display buffer, then:

DEC $000

changes $000 to 4.

Now the CPU reaches:

BGT 2 $000 0

Is 4 greater than 0?

Yes.

Instead of continuing downward to D.BLT, the CPU goes back to line 2.

Line 2 is:

D.TXT $000

This time, $000 contains 4, so the program writes 4, writes another space, and subtracts 1 again.

The same process repeats as $000 changes from 4, to 3, to 2, to 1.

Eventually, the program writes 1 and a space, then:

DEC $000

changes $000 to 0.

The CPU reaches the branch again:

BGT 2 $000 0

Is 0 greater than 0?

No.

This time, the branch does not happen.

The CPU continues to the next instruction:

D.BLT

which makes everything we've written to the display buffer visible.

There are no more instructions after D.BLT, so the program ends.

Loops

Our program executed some of its instructions more than once.

It started at line 2, worked downward to BGT, and then went back to line 2 as long as $000 was greater than 0.

This repeated part of a program is called a loop.

Notice that not every instruction is inside the loop.

MOVE $000 5

executes only once, before the loop begins.

That's important. If it were inside the loop, it would keep putting 5 back into $000, and our countdown would never finish.

Likewise:

D.BLT

is outside the loop.

The program builds the entire countdown in the display buffer, then makes it visible once the loop has finished.

Try changing it

Change:

MOVE $000 5

to:

MOVE $000 10

Before you run the program, predict what it will display.

Then try changing:

BGT 2 $000 0

to:

BGT 2 $000 5

Walk through the program one instruction at a time.

Each time you reach BGT, ask yourself:

What is in $000 right now?

Then:

Is that value greater than 5?

When will the branch stop happening?

What do you expect the program to display?

Run it and see if you were right.

A problem with line numbers

Our branch uses:

BGT 2 $000 0

because line 2 is the beginning of the part we want to repeat.

This works, but it makes the program fragile.

If we add or remove instructions above the loop, its line number can change. Every branch that refers to that line may then need to change as well.

People actually programmed like this?

Yes.

Early computers were often programmed using the actual numeric addresses of instructions. If code was inserted or moved, those addresses could change, and references to them had to be corrected.

Symbolic assemblers were developed to make this kind of programming much friendlier.

Labels

Instead of referring to a location by its line number, NCL lets us give that location a name.

Instead of referring to a location by its line number, NCL lets us give that location a name.

MOVE $000 5
D.TXT "Counting: "

:again
D.TXT $000
D.TXT " "
DEC $000
BGT :again $000 0

D.BLT

Here:

:again

is a label.

Labels don't stop execution

A label gives a location in the program a name. It does not perform an action by itself.

When the CPU reaches a label normally, execution simply continues with the instruction after it.

We can use that name as the destination of our branch:

BGT :again $000 0

This means:

If the value in $000 is greater than 0, continue execution from :again.

Now we no longer need to know which line number contains the beginning of the loop.

We can add or remove instructions above :again, and the branch still refers to the same place by name.

For example:

MOVE $000 5
D.TXT "Counting down: "
D.TXT "Starting at five. "

:again
D.TXT $000
D.TXT " "
DEC $000
BGT :again $000 0

D.BLT

The location of the loop has moved, but:

BGT :again $000 0

does not need to change.

:again still identifies the beginning of the part we want to repeat.

Try it

Start with the labelled version:

MOVE $000 5
D.TXT "Counting: "

:again
D.TXT $000
D.TXT " "
DEC $000
BGT :again $000 0

D.BLT

Change the starting value and predict the result before running it.

Try changing the value used by BGT and follow the program one instruction at a time.

Pay particular attention each time the CPU reaches:

BGT :again $000 0

Ask yourself:

What is in $000 right now?

Then:

Is that value greater than 0?

If it is, go back to :again as you follow the program.

If it isn't, continue downward.


In the next lesson, NCL 104: Choosing a Path, we'll build on branches and loops to make programs follow different paths.