NCL 505: Subroutine Hygiene
In the previous lesson, we established what a caller can expect from a subroutine call.
Now we're writing the subroutine.
A well-behaved subroutine has two simple responsibilities:
Restore what wasn't yours. Free what you no longer need.
Borrowed State
Scratch is available for temporary work:
$000–$0FF Scratch
Memory outside Scratch isn't ours to freely overwrite.
If a subroutine temporarily needs $180, it should preserve whatever was already there:
S.PUSH $180
...use $180...
S.POP $180
This is callee-preserved state.
There's a reason we're using S.PUSH rather than PUSH.
The subroutine doesn't know what the caller stored in $180. It might be an integer or a string.
S.PUSH preserves the value's type, allowing S.POP to restore exactly what was there.
Preserve what you found
When preserving unknown caller state, don't make assumptions about its type.
S.PUSH $180
...borrow $180...
S.POP $180
The caller preserves Scratch values it needs across a call.
The callee preserves non-Scratch state it temporarily disturbs.
Do I need to do this often?
Usually, no.
Scratch exists specifically so subroutines have somewhere to work without preserving whatever was there before.
Callee preservation is useful when a subroutine genuinely needs to borrow memory outside Scratch, but it comes with extra bookkeeping:
S.PUSH $180
...use $180...
S.POP $180
That saved value also occupies the value stack until it is restored, and its position matters when arguments and return values are involved.
If temporary work fits in Scratch, use Scratch.
The easiest state to preserve is state you never disturbed.
Cleaning Up
Scratch is ours to use while the subroutine is working.
Suppose a calculation uses four locations:
MOVE $000 10
MOVE $001 20
ADD $002 $000 $001
MUL $003 $002 2
Once the calculation is finished, those temporary values don't need to remain in memory.
A temporary value has a lifetime: the part of execution during which its value still matters.
Once that lifetime ends, its memory can be freed:
FREE $000
FREE $001
FREE $002
FREE $003
Scratch is a workspace, not a storage locker.
Of course, if $003 contains our return value, its lifetime isn't over quite yet.
We need to get that value out first:
PUSH $003
FREE $000
FREE $001
FREE $002
FREE $003
RET
The caller can now pop the result while our temporary memory has been released.
Restoring Before Returning
There's one complication when a subroutine has both borrowed state and a return value.
Suppose we preserved $180:
S.PUSH $180
That saved value is now on the value stack.
Later, our result is ready in $003.
We might try:
PUSH $003
S.POP $180
But the value on top of the stack is now our result.
S.POP $180 would pop that instead of the value we originally saved.
The saved $180 is underneath it.
So borrowed state must be restored before return values are pushed:
S.POP $180
PUSH $003
FREE $000
FREE $001
FREE $002
FREE $003
RET
The order matters.
- Restore borrowed state.
- Push return values.
- Free temporary state.
RET.
The value stack is LIFO. Anything a subroutine saved before doing its work must come back off before its results are placed on top.
One Way Out
That exit sequence becomes easier to get wrong when a subroutine has several ways to return.
Consider:
:work
MOVE $000 10
BEQ :nothing #count 0
...work...
FREE $000
RET
:nothing
RET
If #count is zero, the second RET skips the cleanup.
We could clean up both paths separately:
:work
MOVE $000 10
BEQ :nothing #count 0
...work...
FREE $000
RET
:nothing
FREE $000
RET
That works.
But now the cleanup exists in two places.
If the subroutine later starts using $001, both exits need updating. Borrow some non-Scratch memory, and both exits need to restore that too.
Instead, both paths can converge on one exit:
:work
MOVE $000 10
BEQ :done #count 0
...work...
:done
FREE $000
RET
Now there's one place responsible for leaving the subroutine in good order.
Multiple RETs aren't inherently wrong. A small routine with nothing to restore or release may be perfectly clear with several return points.
But every RET is an exit that must satisfy the subroutine's responsibilities.
As those responsibilities grow, a common exit becomes much easier to maintain.
Prefer a common cleanup path
When several branches can finish a subroutine, consider sending them to a common exit:
BEQ :done #something 0
...
:done
...cleanup...
RET
Restoration, return values, and cleanup can then happen in one place and in the correct order.
Try It
This subroutine doubles a value.
Its calculation is correct, but its hygiene isn't:
:double_value
POP $000
S.PUSH $180
MOVE $180 2
BEQ :zero $000 0
MUL $001 $000 $180
S.POP $180
PUSH $001
FREE $000
FREE $001
RET
:zero
PUSH 0
RET
Repair the subroutine without changing what it calculates.
When it returns:
$180must contain exactly what it contained before the call;- the temporary Scratch must be freed;
- both paths must perform the required cleanup;
- the caller must still receive the correct result.
What does the early return skip?
Follow the :zero path.
It produces the correct result, but it returns before restoring $180 or freeing the temporary Scratch.
Still stuck?
Instead of returning immediately, let the zero case store its result and join a common exit.
:zero
MOVE $001 0
JUMP :done
What belongs in the common exit?
Both paths can leave their result in $001.
The shared exit can then restore the borrowed value, push the result, free the temporary memory, and return.
Still stuck?
Remember the order:
:done
S.POP $180
PUSH $001
FREE $000
FREE $001
RET
Show me one solution
:double_value
POP $000
S.PUSH $180
MOVE $180 2
BEQ :zero $000 0
MUL $001 $000 $180
JUMP :done
:zero
MOVE $001 0
:done
S.POP $180
PUSH $001
FREE $000
FREE $001
RET
Both paths leave their result in $001 and reach the same exit.
$180 is restored before anything new is pushed onto the value stack. The result is then pushed, the temporary Scratch is freed, and the subroutine returns.
A subroutine may use Scratch freely, and it may sometimes need to borrow other memory. What matters is the state it leaves behind.
Restore what wasn't yours. Free what you no longer need.
In the next lesson, NCL 506: Stack Frames and Recursion, we'll look at the state belonging to one particular invocation — and what happens when a subroutine calls itself.