NCL 104: Choosing a Path

In NCL 103, we made the NCS/e count down from 5:

MOVE $000 5
D.TXT "Counting: "

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

D.BLT

The program displays:

Counting: 5 4 3 2 1 

But what if we want it to count all the way down to 0?

Including zero

Let's look at the end of the loop.

After displaying 1, this instruction:

DEC $000

changes $000 to 0.

Then:

BGT :again $000 0

asks:

Is 0 greater than 0?

It isn't, so the branch does not happen. The CPU continues to D.BLT, and 0 is never displayed.

We need to ask a slightly different question.

Instead of BGT, we can use BGE:

BGE :again $000 0

BGE means branch if greater than or equal to.

Our program becomes:

MOVE $000 5
D.TXT "Counting: "

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

D.BLT

Near the end of the loop, $000 eventually reaches 0.

This time:

BGE :again $000 0

asks:

Is 0 greater than or equal to 0?

Yes.

The CPU goes back to :again, and 0 is displayed.

DEC then changes $000 to -1.

Is -1 greater than or equal to 0?

No.

The loop ends.

The program now displays:

Counting: 5 4 3 2 1 0 

The difference between BGT and BGE is small, but important.

BGT branches only when the first value is greater than the second.

BGE also branches when the two values are equal.

Counting the other way

We've made the NCS/e count downward.

Let's turn the loop around and count upward instead.

We can start at 1:

MOVE $000 1

Instead of subtracting 1 each time with:

DEC $000

we can add 1 with:

INC $000

Now we need the loop to continue while $000 is less than 5.

For that, NCL provides BLT.

BLT means branch if less than.

Our loop becomes:

MOVE $000 1
D.TXT "Counting: "

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

D.BLT

The program begins with $000 containing 1.

It displays 1, then INC changes $000 to 2.

Is 2 less than 5?

Yes, so the CPU goes back to :again.

The loop continues as $000 changes to 3, then 4.

After displaying 4, INC changes $000 to 5.

Then:

BLT :again $000 5

asks:

Is 5 less than 5?

No.

The loop ends.

The program displays:

Counting: 1 2 3 4 

Including five

This should look familiar.

Our countdown originally stopped before 0 because BGT did not include values equal to 0.

Our new loop stops before 5 because BLT does not include values equal to 5.

Just as BGE means greater than or equal to, NCL provides BLE for less than or equal to.

Change:

BLT :again $000 5

to:

BLE :again $000 5

The complete program is now:

MOVE $000 1
D.TXT "Counting: "

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

D.BLT

When $000 reaches 5:

BLE :again $000 5

asks:

Is 5 less than or equal to 5?

Yes.

The CPU goes back to :again, and 5 is displayed.

Afterward, INC changes $000 to 6.

Is 6 less than or equal to 5?

No.

The loop ends.

The program displays:

Counting: 1 2 3 4 5 

Checking for equality

Sometimes we don't care whether one value is greater or less than another.

We want to know whether two values are exactly equal.

NCL provides BEQ, which means branch if equal.

Consider this program:

MOVE $000 5
BEQ :five $000 5

D.TXT "The number is not five."

:five
D.TXT "The number is five."

D.BLT

The branch:

BEQ :five $000 5

asks:

Is the value in $000 equal to 5?

Here, $000 does contain 5, so the CPU branches to :five.

The program displays:

The number is five.

That seems to work.

Now change:

MOVE $000 5

to:

MOVE $000 3

Before you run the program, follow it from the top.

The branch:

BEQ :five $000 5

now asks:

Is 3 equal to 5?

No.

The branch does not happen, so execution continues downward.

The program writes:

The number is not five.

Then the CPU reaches:

:five

Remember that a label does not stop execution. The CPU simply continues with the next instruction:

D.TXT "The number is five."

So our program displays:

The number is not five.The number is five.

That's not what we wanted.

Jumping without a condition

When the number isn't 5, we want to display:

The number is not five.

and then skip over the other message.

We don't need to ask another question to do that. We already know where execution should continue.

NCL provides JUMP for this.

MOVE $000 3
BEQ :five $000 5

D.TXT "The number is not five."
JUMP :done

:five
D.TXT "The number is five."

:done
D.BLT

Unlike the branches we've used so far, JUMP does not compare any values.

This instruction:

JUMP :done

simply means:

Continue execution from :done.

JUMP and GOTO

You may have encountered this operation under another name: goto.

JUMP and GOTO are aliases in NCL, so these instructions mean exactly the same thing:

JUMP :done
GOTO :done

JUMP is the name we'll use throughout this course.

Let's follow both possible paths.

If $000 contains 5:

BEQ :five $000 5

branches to :five.

The CPU executes:

D.TXT "The number is five."

Then execution continues downward through :done to D.BLT.

The program displays:

The number is five.

If $000 does not contain 5, the BEQ does not branch.

The CPU executes:

D.TXT "The number is not five."

Then:

JUMP :done

skips over :five and its message.

Execution continues from :done, and D.BLT makes the selected message visible.

The program now follows one of two different paths depending on the value in $000.

Branching if values are different

BEQ branches when two values are equal.

NCL also provides its opposite:

BNEQ

BNEQ means branch if not equal.

For example:

BNEQ :different $000 5

means:

If the value in $000 is not equal to 5, continue execution from :different.

Just like the other branches, if the condition is false, execution simply continues with the next instruction.

We've now seen several ways to change the path of execution:

Verb Branches when
BEQ Values are equal
BNEQ Values are not equal
BGT First value is greater than second
BGE First value is greater than or equal to second
BLT First value is less than second
BLE First value is less than or equal to second
JUMP Always

They all follow the same basic pattern: compare two values and branch to another location if the condition is true.

If the condition is false, execution simply continues downward.

Try it

Start with our program that chooses between two messages:

MOVE $000 5
BEQ :five $000 5

D.TXT "The number is not five."
JUMP :done

:five
D.TXT "The number is five."

:done
D.BLT

Try several different values in:

MOVE $000 5

Before running the program, follow it from the top and predict which message will be displayed.

Then try changing the value being compared:

BEQ :five $000 10

What else needs to change for the messages to make sense?

Finally, try changing the program to use BNEQ instead of BEQ.

Remember that changing the condition changes which path branches and which path continues downward.

Follow both paths before you run it.


In the next lesson, NCL 105: Keeping Track of More, we'll start using more than one memory location in the same program.