NCL 101: Hello, World!
Welcome to NCL.
We're going to start with the traditional first program: making the computer say hello.
You do not need to understand how the NCS/e works internally. For now, all you need to know is that an NCL program is a list of instructions for the computer to follow.
Your first program
Here is a complete NCL program:
D.CHR "Hello, world!"
That's it.
When you run it, the display shows:
Hello, world!
This program contains one instruction:
D.CHR "Hello, world!"
An instruction tells the computer to do something.
D.CHR writes directly to the visible display. Here, we're giving it the text:
Hello, world!
The quotation marks tell NCL where the text begins and ends. They are not displayed on the screen.
The NCS/e starts with the first instruction and normally works downward, executing one instruction at a time.
In this program, there is only one instruction. After D.CHR executes, there are no more instructions, so the program ends.
You have just written a complete NCL program.
Try changing it
Change the message:
D.CHR "Hello from my NCS/e!"
Run the program again.
Try another message. As long as the text remains between the quotation marks, D.CHR can display it.
Writing to the display
D.CHR is useful when we want to write something to the visible display immediately.
For most of these lessons, however, we'll use a slightly different pair of instructions:
D.TXT "Hello, world!"
D.BLT
Run this program.
It displays the same message:
Hello, world!
The first instruction:
D.TXT "Hello, world!"
writes the text to the display's buffer.
The buffer holds changes before they are made visible. To show those changes, we use:
D.BLT
So these two instructions can be read as:
Write
Hello, world!to the display buffer, then show it.
For the rest of these lessons, we'll usually use D.TXT and D.BLT when displaying something.
Try changing the message again:
D.TXT "NCL is running!"
D.BLT
Notice that the program still works from top to bottom: first D.TXT, then D.BLT, and then there are no more instructions, so the program ends.
For now, that's all you need to know about text. We'll come back to it later.
Remembering a number
Now let's make the CPU remember something.
MOVE r0 5
D.TXT r0
D.BLT
This program displays:
5
The first instruction:
MOVE r0 5
tells the CPU to remember the number 5.
The CPU keeps numbers in places called registers. The register we're using here is called r0.
You can think of r0 as a small place inside the CPU where our program can keep a number while it is running.
MOVE puts a value into a register.
So:
MOVE r0 5
means:
Put
5intor0.
The next instruction:
D.TXT r0
gives the value in r0 to the display.
Since r0 contains 5, the display receives 5.
Finally:
D.BLT
makes it visible.
So our program:
MOVE r0 5
D.TXT r0
D.BLT
can be read as:
Put
5intor0, write the value inr0to the display buffer, then show it.
That was a lot of work to display 5
If all we wanted to do was display the number 5, we could simply write:
D.TXT 5
D.BLT
Putting 5 into r0 first hasn't gained us anything.
Registers become useful when the CPU produces or changes a value while the program is running.
Let's do that.
Adding numbers
NCL can perform arithmetic:
ADD r0 5 2
D.TXT r0
D.BLT
This program displays:
7
The first instruction is:
ADD r0 5 2
ADD adds two values and stores the result in a register.
It follows this form:
ADD destination first-value second-value
In our instruction:
r0is where the result will be stored.5is the first value.2is the second value.
The CPU calculates 5 + 2, which is 7, and puts that result into r0.
So after:
ADD r0 5 2
r0 contains 7.
The next instruction:
D.TXT r0
writes that 7 to the display buffer, and:
D.BLT
makes it visible.
This time, r0 is doing something useful: it is keeping a result produced by the CPU.
Where does the result go?
You might wonder why we need to write:
ADD r0 5 2
instead of simply:
ADD 5 2
The CPU can calculate that 5 + 2 is 7, but it also needs to know where to put the result.
That's what r0 is for.
You can think of it as temporary memory for the value produced by the instruction:
ADD r0 5 2
means:
Calculate
5 + 2, then keep the result inr0.
The result does not disappear when the next instruction begins.
It stays in r0 until another instruction writes a different value there.
For example:
ADD r0 5 2
D.TXT r0
D.TXT r0
D.TXT r0
D.BLT
displays:
777
D.TXT reads the value in r0, but reading it does not remove it.
After the first D.TXT, r0 still contains 7.
After the second, it still contains 7.
After the third, it still contains 7.
A register is not automatically cleared after you use it.
Sometimes we want to keep a result so that several later instructions can use it.
If we want r0 to contain something else, we simply write a new value there:
ADD r0 5 2
D.TXT r0
MOVE r0 9
D.TXT r0
D.BLT
The first D.TXT reads 7.
Then:
MOVE r0 9
replaces the old value in r0.
Now r0 contains 9, so the program displays:
79
The important rules are:
- a register keeps a value;
- reading the register does not remove that value;
- the value remains there until another instruction changes it.
That lets one result become useful later in the program.
Using a result again
Once a value is in a register, another instruction can use it.
Consider:
ADD r0 5 2
ADD r0 r0 3
D.TXT r0
D.BLT
Let's follow it one instruction at a time.
First:
ADD r0 5 2
The CPU calculates 5 + 2.
r0 now contains 7.
Next:
ADD r0 r0 3
This time, one of the values being added comes from r0.
Since r0 currently contains 7, the CPU calculates:
7 + 3
The result, 10, is stored back into r0.
So r0 now contains 10.
Then:
D.TXT r0
writes 10 to the display buffer, and:
D.BLT
makes it visible.
The result of one instruction has become an input to another.
That's one of the reasons computers need to remember values.
A register can be used twice
Both values given to ADD can come from registers. They can even come from the same register.
Try this:
ADD r0 5 2
ADD r0 r0 r0
D.TXT r0
D.BLT
After the first instruction, r0 contains 7.
The second instruction is:
ADD r0 r0 r0
Both input values come from r0.
Since r0 contains 7, this instruction calculates:
7 + 7
and stores the result back into r0.
The program therefore displays:
14
Try it
Try changing the numbers in:
ADD r0 5 2
D.TXT r0
D.BLT
Before you run the program, work out what you expect it to display.
Then try several additions:
ADD r0 5 2
ADD r0 r0 3
ADD r0 r0 10
D.TXT r0
D.BLT
Follow the program from top to bottom.
After the first ADD, r0 contains 7.
What does it contain after the second ADD?
What about after the third?
What do you expect the program to display?
Run it and see if you were right.
Try changing some of the numbers and predict the result again.
Don't worry if you need to go through the instructions one at a time. Following a program this way is an important part of understanding what it does.
In NCL 102: Basic Arithmetic, we'll build on ADD and introduce the other basic arithmetic operations.