NCL 410: Multidimensional Arrays

We've been working in two dimensions for a while.

The Display has an x coordinate and a y coordinate. We use both to identify one position on its grid.

In NCL 309, we did something similar with our Noughts & Crosses board. Nine adjacent memory locations represented a 3×3 grid, and we calculated rows and columns from their positions.

We had a one-dimensional region of memory and interpreted it in two dimensions.

Now we'll turn that relationship around.

A Two-Dimensional Array

Consider this little grid:

A1B1C1D1E1
A2B2C2D2E2
A3B3C3D3E3
A4B4C4D4E4
A5B5C5D5E5

The notation should be familiar from spreadsheets, Battleship, or chess.

Each cell has two coordinates. The letter identifies its column and the number identifies its row.

For example:

D3

means column D, row 3.

We've been calling those coordinates x and y.

The notation above is one-based, so we convert it to the zero-based coordinates we've been using:

D → x = 3
3 → y = 2

D3 → (3, 2)

Now we have a problem.

Memory doesn't have rows or columns.

It has addresses.

Flattening the Grid

Our 5×5 grid contains 25 cells.

We can store those cells in 25 consecutive memory locations:

A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A3 B3 C3 D3 E3 ...

The first row comes first, followed by the second, then the third.

To find D3, we need to find how far into that sequence it appears.

Each complete row contains five cells.

To reach row 3, or y = 2, we first skip two complete rows:

2 × 5 = 10

Then we move three cells across to column D:

10 + 3 = 13

So D3 is element 13.

We can generalize that calculation:

index = y × width + x

For D3:

index = 2 × 5 + 3
      = 13

This process is called flattening.

We take coordinates from a multidimensional structure and turn them into one linear index.

The array has two dimensions. The memory does not.

From Index to Address

Once we have an index, everything from NCL 404: Arrays still applies.

If our grid begins at $100:

address = base + index

Combining that with our new calculation gives us:

address = base + y × width + x

For D3:

address = 0x100 + 2 × 5 + 3
        = 0x10D

The two-dimensional cell D3 is stored at $10D.

Nothing about $10D itself is two-dimensional.

The grid exists because of how we interpret the memory around it.

Calculating It in NCL

Suppose #x and #y contain zero-based coordinates.

Our grid begins at $100 and has a width of 5.

We can calculate the address like this:

#x $000
#y $001
#address $002
#cell [$002]

MUL #address #y 5
ADD #address #address #x
ADD #address #address 0x100

Now #cell refers to the cell at (x, y).

For (3, 2), #address becomes 0x10D, so #cell refers to $10D.

There is no special multidimensional-array instruction here.

We're using multiplication, addition, and indirect addressing to impose a two-dimensional structure on ordinary memory.

Why Width Matters

Our calculation depends on every row having the same width.

With a width of 5:

Row First index
0 0
1 5
2 10
3 15
4 20

Every time y increases by one, we skip another five elements.

That's what makes this work:

y × width

It tells us how many complete rows come before the one we want.

Then:

+ x

moves us to the desired cell within that row.

Records in a Grid

A cell doesn't have to contain only one value.

Suppose we're representing a game map, and each cell contains three fields:

Offset Field
0 Tile
1 Object
2 Flags

Each cell now has a stride of 3.

We already know how to find which cell we want:

element = y × width + x

Then the rules from NCL 405: Strided Arrays tell us how to find that cell's record:

record = base + element × stride

And a field inside that record:

address = base + element × stride + offset

Putting it all together:

address = base + (y × width + x) × stride + offset

It looks considerably more intimidating than the individual ideas that make it up.

Read it from the inside out.

y × width + x

finds which cell.

× stride

finds where that cell's record begins.

+ offset

finds which field inside the cell.

We're combining structures we've already learned.

Three Dimensions

Spooky.

Suppose our grid has another coordinate: z.

Instead of one rectangular grid, we now have several rectangular layers.

If each layer is 5 cells wide and 4 cells high, one complete layer contains:

5 × 4 = 20 cells

To reach z = 2, we need to skip two complete layers:

2 × 20 = 40 cells

Once we're in the correct layer, finding (x, y) works exactly as before.

So:

index = z × width × height + y × width + x

For a 5×4 array, the position (3, 2, 1) becomes:

index = 1 × 5 × 4 + 2 × 5 + 3
      = 20 + 10 + 3
      = 33

Three coordinates have become one index.

We can also write the same calculation as:

index = (z × height + y) × width + x

For (3, 2, 1):

index = (1 × 4 + 2) × 5 + 3
      = 6 × 5 + 3
      = 33

This form shows something useful.

We're flattening the dimensions one at a time.

First z and y become a row.

Then that row and x become one index.

The address is still just:

address = base + index

or, all at once:

address = base + (z × height + y) × width + x

And if each position contains a strided record:

address = base + ((z × height + y) × width + x) × stride + offset

It's getting ugly.

It's still just arithmetic.

There's another way to look at what we've been doing.

In NCL 405: Strided Arrays, a stride told us how far apart consecutive elements were.

Now our coordinates are doing much the same thing.

Moving one step along x advances one element. Moving one step along y skips an entire row. Moving one step along z skips an entire layer.

Each new dimension introduces another regular stride through the flattened array.

x → one element
y → one row
z → one layer

The coordinate tells us how many of that stride to take.

A multidimensional array is still a strided arrangement of linear memory.

What about four dimensions?

Keep going.

A computer doesn't care how many dimensions we imagine the array having. Memory is still linear.

Each additional coordinate tells us how many complete lower-dimensional regions to skip.

The arithmetic gets longer, but the result is still the same kind of thing:

coordinates → one index → one address

The same idea can represent four-dimensional arrays, five-dimensional arrays, or arrays with even more dimensions.

Dimensions describe how we interpret the data. They aren't dimensions of the memory itself.

Choosing a Representation

A multidimensional array hasn't given us a new kind of memory.

We've made an agreement about what ordinary memory means.

A 5×5 array can be stored as 25 consecutive values.

A 5×4×3 array can be stored as 60 consecutive values.

The coordinates tell us how we want to think about those values. Flattening tells us where they actually live.

That gives us a useful separation:

coordinates → index → address → value

The coordinates describe our logical structure.

The index describes a position in the flattened array.

The address describes a location in memory.

The value is whatever we stored there.

Try It

A warehouse is represented by a three-dimensional array.

It has:

  • 4 positions across.
  • 3 positions high.
  • 5 aisles deep.
  • its first element at $200.

#x, #y, and #z contain zero-based coordinates.

Calculate the address of that position and leave it in #address.

Start with two dimensions

We can flatten two coordinates before adding the third.

For a two-dimensional array:

index = y × width + x

In three dimensions, first combine z and y, then combine the result with x.

Still stuck?

The dimensions we need for the calculation are:

width = 4
height = 3

Start with:

z × height + y

Then multiply that result by the width and add x.

Show me one solution
MUL #address #z 3
ADD #address #address #y
MUL #address #address 4
ADD #address #address #x
ADD #address #address 0x200

This calculates:

address = 0x200 + (z × 3 + y) × 4 + x

We've seen how several coordinates can describe a position in a multidimensional structure, then be flattened into the single index that linear memory actually needs.

In the next lesson, NCL 411: Nested Arrays, we'll build grids that don't have to be so regular.