NCL 504: Scratch and Preserved State

In the previous lesson, we used subroutines without knowing how they were implemented.

An interface told us what values to provide and what values we'd get back.

But a subroutine usually needs memory while it's working.

Suppose we call:

PUSH #candidate
CALL :isprime
POP #result

We know what went into :isprime.

We know what came out.

What we don't know is what happened to memory in between.

Somewhere to Work

A useful subroutine might need several temporary values.

We could require every subroutine to document exactly which locations it uses:

ISPRIME

Takes Type
value Integer
Returns Type
result Integer
Temporarily uses
$003
$004
$007
$00C

That would work.

It would also mean callers need to keep track of the internal working memory of every subroutine they use.

Instead, we'll establish another agreement.

The first 256 locations of memory are Scratch:

$000–$0FF    Scratch

Scratch is working space.

Programs and subroutines are free to use it for temporary values.

In exchange, nothing stored there is inherently safe.

If you call a subroutine while something important is in Scratch, assume that value may be overwritten.

It doesn't matter whether the current implementation actually uses that particular location.

Scratch is Scratch.

A familiar-looking page

The MOS 6502 processor has a special region called the zero page, covering addresses $0000–$00FF.

The processor can access those addresses using shorter instructions than the rest of memory, making the zero page particularly useful for frequently accessed values and temporary working state.

NCL's $000–$0FF region isn't faster than any other part of memory. Scratch is a software convention, not a special hardware feature.

But a convenient 256-address page at the bottom of memory has a certain historical charm.

When Scratch Needs to Survive

Suppose our program is using $000:

MOVE $000 137

We still need that value, but before we're finished with it we need to call :isprime.

The value to test is in #candidate:

PUSH #candidate
CALL :isprime
POP #result

The call works.

But what happened to $000?

We don't know.

:isprime is allowed to use Scratch. It might have overwritten $000 immediately.

We could inspect its implementation and find out.

But that would defeat the interface we established in the previous lesson.

Worse, perhaps today's :isprime doesn't touch $000, but a future version does.

Its interface hasn't changed.

Our code shouldn't have to know.

Preserving What We Need

The caller knows which of its Scratch values still matter.

Before making the call, it can push those values out of harm's way:

PUSH $000

Then it prepares the call normally:

PUSH $000

PUSH #candidate
CALL :isprime

When the call returns, the result is waiting on top of the value stack:

POP #result

Underneath it is the value we saved earlier:

POP $000

Together:

PUSH $000

PUSH #candidate
CALL :isprime
POP #result

POP $000

:isprime could have overwritten $000 ten times while it was working.

It doesn't matter.

The value we cared about was safely waiting on the value stack.

This is caller-preserved state.

The caller knows which values it needs after the call, so the caller takes responsibility for preserving them.

The Order Matters

The saved state goes onto the value stack before the arguments:

PUSH $000

PUSH #candidate
CALL :isprime

That puts the arguments where the subroutine expects to find them.

The subroutine consumes its arguments and leaves its returns.

When execution comes back, we retrieve those first:

POP #result

Only then do we reach the state we saved:

POP $000

If several Scratch values need to survive, the same rule applies:

PUSH $000
PUSH $001
PUSH $002

PUSH #a
PUSH #b
CALL :work
POP #result

POP $002
POP $001
POP $000

The values are restored in reverse order because the value stack is LIFO.

There is no new stack behavior here.

We're arranging the call around the behavior we already know.

Don't Depend on Accidents

Suppose you've looked inside :work and discovered that it only uses:

$000–$003

Your important value is in $004.

Convenient.

But $004 is still Scratch.

The fact that today's implementation leaves it alone doesn't make it safe.

The library author might rewrite :work tomorrow and use $004 for another temporary.

The interface hasn't changed.

If a Scratch value needs to survive a call, preserve it.

That's what lets the implementation remain somebody else's problem.

A Calling Convention

We've gradually accumulated several agreements about how subroutines are called.

The caller knows the interface.

The value stack carries arguments into the subroutine.

The value stack carries results back out.

Scratch may be overwritten by a call.

If the caller needs something in Scratch afterward, the caller preserves it.

Put together, a normal call has this shape:

  1. Push any Scratch values you need to preserve.
  2. Push the subroutine's arguments.
  3. CALL the subroutine.
  4. Pop its return values.
  5. Pop the Scratch values you preserved.

For example:

PUSH $000
PUSH $001

PUSH #lhs
PUSH #rhs
CALL :library_routine
POP #result

POP $001
POP $000

This shared set of rules is a calling convention.

It means every caller and every subroutine doesn't need to negotiate how a call will work.

They already agree.

Try It

You're writing a routine named :score_move.

It has calculated an intermediate score and stored it in $002.

Before it can finish, it needs to use this library routine:

ISPRIME

Takes Type
value Integer
Returns Type Description
result Integer 1 if value is prime; 0 otherwise.

The value to test is in #candidate.

After :isprime returns, :score_move still needs the intermediate score in $002.

Complete this part of :score_move so that:

  • the value in $002 survives the call;
  • #candidate is passed to :isprime;
  • the returned result is stored in #prime;
  • $002 contains its original value afterward.

Do not assume anything about which Scratch locations :isprime uses.

Protect what you need first

$002 is Scratch.

If you need its current value after calling another subroutine, preserve it before preparing the call.

Still stuck?

Start with:

PUSH $002
The argument goes above the saved value

Once $002 is safe, prepare the call normally.

Still stuck?
PUSH $002

PUSH #candidate
CALL :isprime
Unwind in reverse

After the call, the return value is above your saved state.

Retrieve the return first, then restore $002.

Still stuck?
POP #prime
POP $002
Show me one solution
PUSH $002

PUSH #candidate
CALL :isprime
POP #prime

POP $002

:isprime was free to overwrite $002 while it worked.

The value :score_move cared about was on the value stack for the duration of the call, then restored afterward.


We've established what a caller can assume about Scratch, and how to protect the state it needs across a call.

But callers aren't the only side with responsibilities.

In the next lesson, NCL 505: Subroutine Hygiene, we'll look at how a well-behaved subroutine treats memory belonging to somebody else — and what it should clean up before it leaves.