NCL 404: Arrays

In the 300-level, our Noughts and Crosses program had a recurring problem.

The board had nine cells, numbered 0 through 8.

But knowing the number of a cell didn't tell the computer where its value was stored.

If #selection contained 4, we had to explicitly choose the code for cell 4:

BEQ :cell0 #selection 0
BEQ :cell1 #selection 1
BEQ :cell2 #selection 2
BEQ :cell3 #selection 3
BEQ :cell4 #selection 4
BEQ :cell5 #selection 5
BEQ :cell6 #selection 6
BEQ :cell7 #selection 7
JUMP :cell8

It worked.

It was also a lot of code for answering one question:

Where is cell 4?

We can answer that differently now.

Putting the Board Together

Suppose we store the nine board cells next to each other in memory:

Cell Address
0 $020
1 $021
2 $022
3 $023
4 $024
5 $025
6 $026
7 $027
8 $028

There's a useful pattern here.

Cell 0 starts at $020.

Cell 1 is one location after it.

Cell 2 is two locations after it.

And so on.

In the previous lessons, we learned two things that let us take advantage of this.

First, memory addresses are hexadecimal. $020 is address 32 in decimal.

Second, we can calculate an address and access it indirectly.

If #selection contains 6, then:

32 + 6 = 38

Decimal address 38 is $026.

That's exactly where cell 6 is stored.

We can make the computer do the calculation:

#selection $000
#address $001
#cell [$001]

MOVE #selection 6

ADD #address 32 #selection
MOVE $002 #cell

After:

ADD #address 32 #selection

#address contains 38.

That means:

#cell

refers indirectly to $026.

So:

MOVE $002 #cell

copies the value of cell 6 into $002.

Change #selection to 2, and the same instructions access $022.

Change it to 8, and they access $028.

We don't need nine branches anymore.

The relationship is simply:

address = base + index

Here, 32 is the base and #selection is the index.

The index tells us how far from the base our value is stored.

And we have just built something rather important.

Arrays 🎉

An array is a collection of values arranged in order and selected by their position.

Our board is an array of nine values:

Index Address
0 $020
1 $021
2 $022
3 $023
4 $024
5 $025
6 $026
7 $027
8 $028

NCL doesn't need a special array declaration or an ARRAY instruction.

We're building one from things we already understand:

  • a sequence of memory locations;
  • a base address where the array begins;
  • an index identifying one position;
  • indirect addressing to access the selected element.

Our Noughts and Crosses board was already conceptually an array.

We just didn't have the tools to treat it like one yet.

Base, Index, and Element

There are three useful terms here.

The base address is where the array begins.

For our board:

base = $020

The index identifies an element by its position in the array.

For example:

index = 4

identifies the fifth element.

The element is the value stored at that position.

Because our first element has index 0, its address is just the base:

32 + 0 = 32

The next element has index 1:

32 + 1 = 33

And element 8 is:

32 + 8 = 40

This is called zero-based indexing.

The first element is element 0, not element 1.

We've already used zero-based numbering in several places throughout NCL. Here, it gives us a particularly convenient property:

address = base + index

Address or Value?

There's an important distinction in our array code:

#address $001
#cell [$001]

#address is the memory location containing the address we've calculated.

#cell is the memory location that address points to.

If:

#address = 36

then #cell refers to $024.

Changing #address changes which board cell #cell refers to.

That means we can use #cell anywhere an ordinary memory location would work.

We can read it:

MOVE $002 #cell

We can write it:

MOVE #cell 1

And we can operate on it directly:

INC #cell

The array doesn't require special instructions.

Once we've selected an element, it's just another memory location.

The address and its contents are different things

$020 is a memory location.

0x20 is an integer with the value 32.

These are not interchangeable.

MOVE #address 0x20

puts the number 32 into #address.

MOVE #address $020

reads whatever value is currently stored in $020.

When we're calculating addresses, we need their numeric values.

Walking an Array

Sometimes we don't want one particular element.

We want to visit all of them.

Because our elements are stored consecutively, the address after one element is the address of the next.

Suppose four scores are stored here:

Address Value
$040 12
$041 27
$042 8
$043 19

We could point at the first one:

#address $000
#score [$000]

MOVE #address 0x40

Now #score refers to $040.

After:

INC #address

it refers to $041.

Increment it again, and it refers to $042.

The data hasn't moved.

We've moved the address we're using to access it.

This gives us a simple way to walk through consecutive values with the same code.

For example:

#address $000
#score [$000]

MOVE $040 12
MOVE $041 27
MOVE $042 8
MOVE $043 19

MOVE #address 0x40

:display
D.TXT #score
D.TXT " "

INC #address
BLT :display #address 0x44

D.BLT

The loop displays:

12 27 8 19

Each time through the loop, #score refers to a different element.

We could have written four separate D.TXT instructions.

But the loop doesn't care how many different addresses we wrote into the program.

It cares where the array begins and where it ends.

Are arrays what other languages call lists?

Sometimes.

Collections of values tend to fall into two broad families.

Linear collections identify values by their position.

Position Value
0 "ALPHA"
1 "BRAVO"
2 "CHARLIE"

Depending on the language, names such as array, list, tuple, vector, or sequence may describe variations on this idea.

Their implementations can differ considerably. Values might occupy consecutive memory locations, use a fixed distance between elements, or be connected through addresses.

Associative collections identify values using keys instead.

Key Value
"name" "Nemi"
"score" 42

Names such as dictionary, map, associative array, table, and even object may describe variations on this idea.

The name alone doesn't necessarily tell us how a collection is implemented. Different languages can provide very similar-looking collections using very different machinery underneath.

For now, we'll focus on the simplest case: linear arrays stored consecutively in memory.

We'll explore associative collections much later in the course.

Try It

Five scores are stored from $040 through $044:

MOVE $040 17
MOVE $041 25
MOVE $042 9
MOVE $043 31
MOVE $044 14

An index from 0 through 4 is stored in #selection:

#selection $000

Add 10 to the selected score.

Your code should work for any valid value of #selection without using a branch to choose between the five scores.

Show me one solution
#selection $000
#address $001
#score [$001]

MOVE $040 17
MOVE $041 25
MOVE $042 9
MOVE $043 31
MOVE $044 14

MOVE #selection 2

ADD #address 0x40 #selection
ADD #score #score 10

With an index of 2:

0x40 + 2 = 0x42

so #score refers to $042.

The final instruction therefore changes $042 from 9 to 19.

Change #selection to any value from 0 through 4. The address calculation and the ADD remain exactly the same.


We've seen how consecutive memory locations can become an array, letting us select any element by calculating its address from an index.

So far, each element has occupied exactly one memory location.

In the next lesson, NCL 405: Strided Arrays, we'll see what happens when one element needs several.