NCL 102: Basic Arithmetic

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

ADD $000 5 2

The computer adds 5 and 2, then stores the result, 7, in $000.

NCL provides similar instructions for the other basic arithmetic operations.

Subtraction

SUB subtracts one value from another:

SUB $000 10 3
D.TXT $000
D.BLT

This program displays:

7

SUB follows the same general form as ADD:

SUB destination first-value second-value

The instruction:

SUB $000 10 3

means:

Subtract 3 from 10, and put the result in $000.

The order matters.

SUB $000 3 10

calculates 3 - 10, so $000 contains -7.

Memory locations can be used as values just as they can with ADD:

MOVE $000 10
SUB $000 $000 3
D.TXT $000
D.BLT

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

Multiplication

MUL multiplies two values:

MUL $000 6 7
D.TXT $000
D.BLT

The computer calculates 6 × 7 and stores 42 in $000.

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

MUL $000 6 7
ADD $000 $000 8
D.TXT $000
D.BLT

After MUL, $000 contains 42.

The next instruction adds 8 and stores the new result, 50, back into $000.

The program displays:

50

Division

DIV divides one value by another:

DIV $000 20 4
D.TXT $000
D.BLT

This calculates 20 ÷ 4, so the program displays:

5

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

Try:

DIV $000 10 3
D.TXT $000
D.BLT

You might expect something like:

3.333...

Instead, the NCS/e displays:

3

Where did the decimal go?

DIV performs integer division.

An integer is a whole number such as:

-12
0
5
127

It does not contain a fractional or decimal part.

Integer division

Because DIV works with integers, it only produces whole-number results.

For example:

10 ÷ 3 = 3

Any fractional part is discarded. The result is not rounded.

Why integer arithmetic?

Integer arithmetic is extremely common in computers.

Many things a program works with are naturally whole numbers: counters, positions, sizes, character codes, and memory locations.

NCL can work with fractional values too, but we'll meet those later.

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 $000 10 3
D.TXT $000
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 $000 10 3

puts 1 in $000.

MOD $000 15 4

puts 3 in $000.

And:

MOD $000 12 4

puts 0 in $000.

One useful property falls out of this immediately.

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

MOD $000 10 2

$000 contains 0.

If it is odd:

MOD $000 11 2

$000 contains 1.

We aren't going to do anything with that information just yet. For now, it's enough to know that the computer 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 $000 $000 1

adds one to the value in $000.

Similarly:

SUB $000 $000 1

subtracts one.

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

Instead of:

ADD $000 $000 1

you can write:

INC $000

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

If $000 contains 5:

INC $000

changes it to 6.

Likewise, instead of:

SUB $000 $000 1

you can write:

DEC $000

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

If $000 contains 5:

DEC $000

changes it to 4.

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

Verb Operation
ADD Addition
SUB Subtraction
MUL Multiplication
DIV Integer division
MOD Remainder
INC Add 1
DEC Subtract 1

Try it

Consider this program:

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

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

Start with:

$000 = 10

After each instruction, work out the new value in $000.

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 $000 23 5
D.TXT $000
D.BLT

What remainder do you expect?


More arithmetic

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.