NCL 102: Basic Arithmetic

In NCL 101, you used ADD to add two values:

ADD r0 5 2

The CPU adds 5 and 2, then stores the result, 7, in r0.

NCL provides similar instructions for the other basic arithmetic operations.

Subtraction

SUB subtracts one value from another:

SUB r0 10 3
D.TXT r0
D.BLT

This program displays:

7

SUB follows the same general form as ADD:

SUB destination first-value second-value

The instruction:

SUB r0 10 3

means:

Subtract 3 from 10, and put the result in r0.

The order matters.

SUB r0 3 10

calculates 3 - 10, so r0 contains -7.

Registers can be used as values just as they can with ADD:

MOVE r0 10
SUB r0 r0 3
D.TXT r0
D.BLT

Here, r0 starts at 10. SUB subtracts 3 from it and stores the result, 7, back into r0.

Multiplication

MUL multiplies two values:

MUL r0 6 7
D.TXT r0
D.BLT

The CPU calculates 6 × 7 and stores 42 in r0.

As before, the result can be used by another instruction:

MUL r0 6 7
ADD r0 r0 8
D.TXT r0
D.BLT

Follow it from the top.

After:

MUL r0 6 7

r0 contains 42.

Then:

ADD r0 r0 8

adds 8, so r0 contains 50.

The program displays:

50

Division

DIV divides one value by another:

DIV r0 20 4
D.TXT r0
D.BLT

This calculates 20 ÷ 4, so the program displays:

5

But what happens when the result isn't a whole number?

Try:

DIV r0 10 3
D.TXT r0
D.BLT

You might expect something like:

3.333...

Instead, the NCS/e displays:

3

Where did the decimal go?

The registers we have been using store integers.

An integer is a whole number such as:

-12
0
5
127

It does not contain a fractional or decimal part.

Because DIV works with integers, the result of:

10 ÷ 3

is 3.

The part after the whole-number result is not stored.

For now, that's all you need to know about integer division.

Remainders

Sometimes the part left over after division is exactly what we want.

NCL provides MOD for this:

MOD r0 10 3
D.TXT r0
D.BLT

10 divided by 3 gives 3, with 1 left over.

MOD stores that remainder, so this program displays:

1

If you haven't thought about remainders since elementary school, congratulations: you're back.

For example:

10 ÷ 3 = 3 remainder 1
15 ÷ 4 = 3 remainder 3
12 ÷ 4 = 3 remainder 0

So:

MOD r0 10 3

puts 1 in r0.

MOD r0 15 4

puts 3 in r0.

And:

MOD r0 12 4

puts 0 in r0.

One useful property falls out of this immediately.

If a number is even, dividing it by 2 leaves no remainder:

MOD r0 10 2

r0 contains 0.

If it is odd:

MOD r0 11 2

r0 contains 1.

We aren't going to do anything with that information just yet. For now, it's enough to know that the CPU can calculate it.

Adding and subtracting one

Programs very often need to add or subtract exactly 1.

You already know how to do that:

ADD r0 r0 1

adds one to the value in r0.

Similarly:

SUB r0 r0 1

subtracts one.

These operations are so common that NCL provides shorter instructions for them.

Instead of:

ADD r0 r0 1

you can write:

INC r0

INC is short for increment. It adds 1 to the value in the register.

If r0 contains 5:

INC r0

changes it to 6.

Likewise, instead of:

SUB r0 r0 1

you can write:

DEC r0

DEC is short for decrement. It subtracts 1 from the value in the register.

If r0 contains 5:

DEC r0

changes it to 4.

These are simply convenient ways to perform operations you already know.

Try it

Consider this program:

MOVE r0 10
MUL r0 r0 3
SUB r0 r0 5
DIV r0 r0 5
INC r0
D.TXT r0
D.BLT

Before you run it, follow it from top to bottom.

Start with:

r0 = 10

After each instruction, work out the new value in r0.

What do you expect the program to display?

Then run it and see if you were right.

Try changing some of the numbers and predicting the result again.

You can also experiment with MOD:

MOD r0 23 5
D.TXT r0
D.BLT

What remainder do you expect?


NCL provides more mathematical instructions than we've covered here, including powers and integer square roots. You do not need them to continue learning NCL.

When you need them, see the Arithmetic reference.

In the next lesson, NCL 103: Changing the Path, we'll start changing which instruction the CPU executes next.