NCL 403: Indirect Memory

In the previous lesson, we looked underneath PUSH and POP and saw how the stack uses ordinary memory.

We also used something new:

MOVE $000 [sp]

sp contains an address.

The brackets tell the CPU to access the memory at that address.

There is nothing special about sp here.

Any location can provide the address.

An Address in Memory

Suppose we store a value in $009:

MOVE $009 42

Normally, we would read it directly:

MOVE $001 $009

This tells the CPU exactly which memory location to read.

But we can also put the number 9 somewhere else:

MOVE $000 9

Now our memory contains:

Location Value
$000 9
$009 42

The value in $000 can be used as an address.

MOVE $001 [$000]

The CPU first reads $000.

It finds:

9

That selects memory location $009.

The CPU then reads $009 and stores its value in $001.

After the instruction:

Location Value
$000 9
$001 42
$009 42

This is indirect addressing.

Instead of naming the final location directly, we access a location whose address comes from somewhere else.

Direct and Indirect Access

Compare these two instructions:

MOVE $001 $009
MOVE $001 [$000]

The first uses direct addressing.

The instruction itself identifies $009.

The second uses indirect addressing.

The instruction identifies $000, but the value stored in $000 determines which location is actually read.

If $000 contains 9:

MOVE $001 [$000]

reads $009.

If we change it:

MOVE $000 10

the exact same instruction now reads $00A.

The instruction hasn't changed.

The address has.

Writing Indirectly

Indirect addressing also works when writing to memory.

Suppose:

MOVE $000 9

Then:

MOVE [$000] 42

stores 42 in $009.

The destination is resolved in the same way:

Step Result
Read $000 9
Resolve address 9 $009
Store the value $009 = 42

This means a program can decide where to write while it is running.

For example:

MOVE $000 9
MOVE [$000] 42

MOVE $000 10
MOVE [$000] 99

The first indirect write stores 42 in $009.

The second stores 99 in $00A.

The instruction:

MOVE [$000] ...

can therefore refer to different memory locations as the value in $000 changes.

Calculating an Address

The address doesn't have to be written into memory directly.

It can be calculated like any other integer.

Consider:

#address $000
#value [$000]

MOVE #address 9
MOVE #value 100

INC #address
MOVE #value 200

INC #address
MOVE #value 300

#address begins with the value 9.

Because:

#value [$000]

the name #value refers indirectly through $000.

The first:

MOVE #value 100

therefore writes to $009.

Then we increment the address:

INC #address

Now $000 contains 10, so #value refers to $00A.

After another increment, it refers to $00B.

The result is:

Address Value
$009 100
$00A 200
$00B 300

We used the same indirect operand for all three writes.

Only the address changed.

This is the important difference between direct and indirect memory access:

Direct access is useful when we already know which location we mean.

Indirect access is useful when the program chooses the location while it is running.

Naming Indirect Memory

The previous example used two names:

#address $000
#value [$000]

They refer to related things, but they don't mean the same thing.

#address is an alias for $000.

If $000 contains 9, then:

D.TXT #address

displays:

9

#value, however, contains the complete indirect operand:

[$000]

So:

D.TXT #value

reads the location selected by $000.

If $000 contains 9, it reads $009.

This can make indirect memory much easier to work with:

#address $000
#value [$000]

MOVE #address 9
MOVE #value 42

D.TXT #value
D.BLT

The program displays:

42

Constants and indirect operands

A constant can contain an entire indirect operand:

#value [$000]

However, constants are not currently substituted inside brackets.

This does not work:

#address $000
MOVE $001 [#address]

If you want a name for the indirect access, put the complete dereference in the constant instead:

#address $000
#value [$000]

MOVE $001 #value

This is a syntax limitation of constant substitution, not a limitation of indirect memory itself.

Indirect Registers

An indirect address doesn't have to resolve to ordinary memory.

NCL locations also include registers such as pc, sp, and er.

For example:

S.MOVE $000 "sp"
MOVE $001 [$000]

$000 contains:

sp

so [$000] resolves to the stack pointer.

The final instruction therefore copies the current value of sp into $001.

The same location-resolution rules apply.

Most of the time, we'll use indirection with ordinary memory addresses. The important rule is that an indirect operand resolves to a location, not specifically to a numbered memory address.

Following More Than One Address

A location can point to a location that points somewhere else.

Suppose:

Location Value
$000 9
$009 20
$014 42

One level of indirection:

MOVE $001 [$000]

follows $000 to $009.

It therefore reads:

20

But we can add another pair of brackets:

MOVE $001 [[$000]]

Now the CPU follows two levels.

First:

$000 → $009

Then the value in $009 selects $014:

$009 → $014

Finally, $014 contains:

42

so $001 receives 42.

Each additional pair of brackets follows another level of indirection.

Do real processors use indirect memory like this?

Absolutely.

Indirect addressing is a fundamental technique in many computer architectures.

A value that contains the address of another location is commonly called a pointer.

Using that pointer to access the location it identifies is called dereferencing it.

Different processors provide different ways to work with pointers. An instruction might access memory through a register, combine an address with an offset, or provide other specialized addressing modes.

On many architectures, deeper chains of pointers are followed one step at a time. If one pointer leads to another pointer, software loads the first and then follows the second.

NCL allows the same idea to be written directly by nesting brackets:

MOVE $000 [[$001]]

Each pair of brackets dereferences another pointer.

NCL permits up to eight levels of indirection in a single operand:

MOVE $000 [[[[[[[[$001]]]]]]]]

Most programs have little reason to go anywhere near that limit.

One level is already enough to make indirect memory extremely useful.

Invalid Addresses

Indirect addressing only works if the value eventually resolves to a valid location.

If it doesn't, the CPU cannot complete the access.

For example, if an indirect reference tries to resolve something that isn't a valid memory address or register, execution terminates with an ADDRESS_ERROR.

The same applies if more than eight levels of indirection are requested.

Indirect addresses are your responsibility

The CPU follows the address you give it.

It doesn't know which location you intended to access.

When a program calculates or changes an address, make sure the resulting value can resolve to a valid location.

Try It

Four values are stored in memory:

MOVE $020 10
MOVE $021 20
MOVE $022 30
MOVE $023 40

The value in $000 is an offset from $020:

MOVE $000 2

Without using a branch, copy the selected value into $001.

Your solution should still work if $000 is changed to 0, 1, 2, or 3.

You may use another memory location to calculate the address.

Show me one solution
#offset $000
#result $001
#address $002
#selected [$002]

MOVE $020 10
MOVE $021 20
MOVE $022 30
MOVE $023 40

MOVE #offset 2

ADD #address 32 #offset
MOVE #result #selected

D.TXT #result
D.BLT

$020 is address 32.

With an offset of 2:

32 + 2 = 34

Address 34 is $022, so #selected reads the value stored there.

$001 therefore receives 30.

Change:

MOVE #offset 2

to any value from 0 through 3.

The instructions that calculate and access the address do not need to change.


In this lesson, the location accessed by an instruction no longer had to be fixed in the program itself.

We stored addresses, changed them, calculated them, and used them to decide which memory location an instruction would access.

That gives us a much more powerful way to organize related values.

In the next lesson, NCL 404: Arrays, we'll use indirect memory to work with whole groups of values without writing separate code for every location.