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 computer remember something.
MOVE $000 5
D.TXT $000
D.BLT
This program displays:
5
The first instruction:
MOVE $000 5
tells the computer to remember the number 5.
The NCS/e has places where a program can keep values while it is running. These are called memory locations.
The memory location we're using here is called $000.
You can think of $000 as a place where our program can keep a value while it is running.
MOVE puts a value into a memory location.
So:
MOVE $000 5
means:
Put
5into$000.
The next instruction:
D.TXT $000
gives the value in $000 to the display.
Since $000 contains 5, the display receives 5.
Finally:
D.BLT
makes it visible.
So our program:
MOVE $000 5
D.TXT $000
D.BLT
can be read as:
Put
5into$000, write the value in$000to 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 $000 first hasn't gained us anything.
Memory becomes useful when the computer produces or changes a value while the program is running.
Let's do that.
Adding numbers
NCL can perform arithmetic:
ADD $000 5 2
D.TXT $000
D.BLT
This program displays:
7
The first instruction is:
ADD $000 5 2
ADD adds two values and stores the result in a memory location.
It follows this form:
ADD destination first-value second-value
In our instruction:
$000is where the result will be stored.5is the first value.2is the second value.
The computer calculates 5 + 2, which is 7, and puts that result into $000.
The next instruction:
D.TXT $000
writes that 7 to the display buffer, and:
D.BLT
makes it visible.
This time, $000 is doing something useful: it is keeping a result produced by the computer.
Where does the result go?
You might wonder why we need to write:
ADD $000 5 2
instead of simply:
ADD 5 2
The computer can calculate that 5 + 2 is 7, but it also needs to know where to put the result.
That's what $000 is for.
ADD $000 5 2
means:
Calculate
5 + 2, then keep the result in$000.
The result stays in $000 until another instruction writes a different value there.
For example:
ADD $000 5 2
D.TXT $000
D.TXT $000
D.TXT $000
D.BLT
displays:
777
D.TXT reads the value in $000, but reading it does not remove it. Each D.TXT therefore receives the same 7.
If we want $000 to contain something else, we simply write a new value there:
ADD $000 5 2
D.TXT $000
MOVE $000 9
D.TXT $000
D.BLT
The first D.TXT reads 7.
Then:
MOVE $000 9
replaces the old value in $000.
The program therefore displays:
79
Memory keeps its value
- A memory location keeps a value.
- Reading it 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 memory location, another instruction can use it.
Consider:
ADD $000 5 2
ADD $000 $000 3
D.TXT $000
D.BLT
The first instruction calculates 5 + 2 and puts the result, 7, into $000.
The second instruction:
ADD $000 $000 3
uses that 7 as one of its input values.
The computer calculates 7 + 3, which is 10, and stores the new result back into $000.
The program therefore displays:
10
The result of one instruction has become an input to another.
That's one of the reasons computers need to remember values.
A memory location can be used twice
Both values given to ADD can come from memory. They can even come from the same memory location.
Try this:
ADD $000 5 2
ADD $000 $000 $000
D.TXT $000
D.BLT
After the first instruction, $000 contains 7.
The second instruction:
ADD $000 $000 $000
uses that value twice.
Since $000 contains 7, it calculates:
7 + 7
and stores the result back into $000.
The program therefore displays:
14
Try it
Try changing the numbers in:
ADD $000 5 2
D.TXT $000
D.BLT
Before you run the program, work out what you expect it to display.
Then try several additions:
ADD $000 5 2
ADD $000 $000 3
ADD $000 $000 10
D.TXT $000
D.BLT
Follow the program from top to bottom.
After the first ADD, $000 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 the next lesson, NCL 102: Basic Arithmetic, we'll build on ADD and introduce the other basic arithmetic operations.