NCL 405: Strided Arrays

In the previous lesson, we built arrays by placing values next to each other in memory.

Suppose an array begins at $020:

Index Address
0 $020
1 $021
2 $022
3 $023

To find an element, we add its index to the base address:

address = base + index

That works because every element occupies exactly one memory location.

But an element doesn't have to be just one value.

A Table of Players

Suppose we're keeping track of some players.

For each player, we want to store three pieces of information:

Field Example
Name "NEMI"
Score 42
Wins 7

We could arrange three players in memory like this:

Address Player Field Value
$100 0 Name "NEMI"
$101 0 Score 42
$102 0 Wins 7
$103 1 Name "ALICE"
$104 1 Score 17
$105 1 Wins 3
$106 2 Name "BOB"
$107 2 Score 31
$108 2 Wins 5

Each player now occupies three memory locations.

Together, those three locations form one record.

Player 0 begins at $100.

Player 1 begins at $103.

Player 2 begins at $106.

Our old formula no longer works.

If we tried:

base + index

for player 2, we'd get:

0x100 + 2 = 0x102

But $102 contains player 0's number of wins.

We need to account for the size of each record.

Stride

Look at the beginning of each player record:

Player Address
0 $100
1 $103
2 $106

Each record begins three locations after the previous one.

That distance is called the stride.

Our player records have a stride of 3.

To find a record, we multiply its index by the stride:

address = base + index × stride

For player 2:

0x100 + 2 × 3
= 0x106

Now we arrive at the beginning of the correct record.

The arrays we used in the previous lesson had a stride too.

It was simply 1.

Calculating a Record Address

Let's do the same calculation in NCL.

We'll store the player index in #player and calculate the beginning of the record in #address.

#player $000
#address $001

MOVE #player 2

MUL #address #player 3
ADD #address #address 0x100

Afterward:

#address = 0x106

We can use indirect memory to access that location:

#field [$001]

Since #address contains 0x106, #field refers to $106.

For player 2, that is:

"BOB"

Change #player, and the same calculation selects a different record.

Fields and Offsets

Finding the beginning of a record is only half the job.

Each field has a position within that record.

For our players:

Offset Field
0 Name
1 Score
2 Wins

This position is called the field's offset.

Once we know where a record begins, we can add the offset of the field we want.

For player 2:

record = 0x100 + 2 × 3
       = 0x106

Their name is at offset 0:

0x106 + 0 = 0x106

Their score is at offset 1:

0x106 + 1 = 0x107

Their number of wins is at offset 2:

0x106 + 2 = 0x108

Put everything together and we get:

address = base + index × stride + offset

That one formula lets us select any field from any record.

Selecting a Field

Let's select player 1's score.

Player 1 has index 1.

Score has offset 1.

#player $000
#address $001
#field [$001]

MOVE #player 1

MUL #address #player 3
ADD #address #address 0x100
ADD #address #address 1

D.TXT #field
D.BLT

The calculation is:

0x100 + 1 × 3 + 1
= 0x104

And $104 contains:

17

The same code can select any player by changing #player.

We could also keep the field offset in memory:

#player $000
#offset $001
#address $002
#field [$002]

MOVE #player 2
MOVE #offset 2

MUL #address #player 3
ADD #address #address 0x100
ADD #address #address #offset

D.TXT #field
D.BLT

Now both parts of the selection can be decided while the program is running.

With:

#player = 2
#offset = 2

the program selects player 2's number of wins:

5

Records

We've given a useful structure to an otherwise ordinary region of memory.

These three locations:

Address Value
$106 "BOB"
$107 31
$108 5

aren't physically special.

NCS/e doesn't know that they belong together.

Our program has decided that they form one record:

Offset Meaning
0 Name
1 Score
2 Wins

And we've decided that another record begins every three locations.

That's enough structure for our program to treat the whole region as an array of players instead of nine unrelated memory locations.

Do real computers arrange data like this?

Yes.

Low-level programs commonly store related fields together in records or structures. Each field sits at a known offset from the beginning of the record.

An array of those records can then place one complete record after another in memory.

On a byte-addressed machine, for example, a record with a stride of 12 bytes might begin at offsets:

0
12
24
36

Accessing a field inside one of those records uses essentially the same calculation we've been using:

address = base + index × stride + offset

Some processors even have addressing modes that can perform parts of this calculation while accessing memory.

NCL makes the layout particularly simple because each memory location can hold a complete value. Real machines usually address smaller units such as bytes, so fields can have different sizes and records may contain unused space for alignment.

The underlying idea is the same.

Try It

A table begins at $200.

Each record contains four fields:

Offset Field
0 Name
1 Score
2 Level
3 Status

#index contains the record number.

#offset contains a field offset from 0 through 3.

Calculate the address of that field and copy its value into #result.

Do not use a branch.

Show me one solution
#index $000
#offset $001
#address $002
#result $003
#field [$002]

MUL #address #index 4
ADD #address #address 0x200
ADD #address #address #offset

MOVE #result #field

Each record occupies four locations, so the stride is 4.

First we find the beginning of the record:

0x200 + index × 4

Then we add the field offset:

address = 0x200 + index × 4 + offset

We've seen how an array can contain records made from several related values, and how a stride and field offset let us find any part of those records.

In the next lesson, NCL 406: Array Headers, we'll start storing information about an array inside the array itself.