NCL 501: The Call Stack

We've been using subroutines since NCL 309.

A call looks like this:

CALL <target>

For example:

CALL :draw_cell

And the subroutine eventually returns:

RET
:draw_cell
...
RET

CALL transfers execution to the subroutine. RET eventually brings execution back.

We've used that behavior to build quite a few programs.

But there is an important question we haven't answered yet:

Where does CALL remember where to return?

Remembering the Return

When the processor executes:

CALL :work

it needs to do two things:

  1. Remember where execution should continue afterward.
  2. Jump to :work.

When :work eventually executes:

RET

the processor retrieves that remembered destination and continues there.

For a single call, it might seem like the processor only needs to remember one address.

But subroutines can call other subroutines.

Consider:

CALL :outer
D.TXT "Done"
D.BLT

:outer
D.TXT "Outer "
CALL :inner
D.TXT "Again "
RET

:inner
D.TXT "Inner "
RET

The display eventually shows:

Outer Inner Again Done

When the program calls :outer, the processor remembers where the main program should continue.

Then :outer calls :inner.

The processor can't forget the first return destination. :inner needs somewhere to return, but :outer still needs to return afterward too.

There are now two return destinations waiting.

And they have to come back in the opposite order from which they were created.

That should sound familiar.

The Call Stack

A stack is exactly what we need.

The processor maintains a dedicated call stack for return destinations.

Each CALL places a return destination on the call stack.

Each RET removes the most recent return destination and continues execution there.

For our nested example:

Event Pending returns
Program starts none
Main program calls :outer main program
:outer calls :inner main program, :outer
:inner executes RET main program
:outer executes RET none

The most recent call returns first.

Just like the stacks we worked with in the 400 level, the call stack is last in, first out.

The difference is that the processor manages this one for us.

Two Stacks

The call stack is not the value stack we used in the previous level.

They have different jobs:

Value stack Call stack
Stores program values Stores return destinations
Used by PUSH and POP Used by CALL and RET
Lives in ordinary memory Lives inside the processor
Accessible through sp Not directly accessible
Up to 4096 values 32 return destinations

A CALL does not push anything onto the value stack.

It doesn't push arguments, save your variables, or decide how the subroutine should communicate with its caller.

It only remembers where execution needs to return.

This separation means the two stacks are independent. Filling the value stack does not use space on the call stack, and nested calls do not consume ordinary memory.

Do real processors do this?

They can.

Different processor architectures remember return destinations in different ways. Some place them on the same stack used for program data. Some initially keep them in a register.

Others maintain a separate return stack much like NCL's call stack.

The implementation differs, but the problem is universal: once one piece of code calls another, the processor needs some way to find its way back.

Return Obligations

It can be useful to think of each CALL as creating a return obligation.

When we write:

CALL :work

we are saying:

Run :work, then come back here.

That pending return remains on the call stack while :work runs.

If :work calls another subroutine, another obligation is added above it.

When the newest subroutine executes:

RET

the newest obligation is fulfilled first.

So:

CALL creates a return obligation. RET fulfills it.

This is why nested calls naturally unwind in reverse order.

Going Deeper

There is nothing special about nesting two calls.

Suppose we have:

CALL :one
D.TXT "Done"
D.BLT

:one
D.TXT "One "
CALL :two
D.TXT "One again "
RET

:two
D.TXT "Two "
CALL :three
D.TXT "Two again "
RET

:three
D.TXT "Three "
RET

Execution works inward through the calls:

:one
:two
:three

At the deepest point, three return destinations are waiting on the call stack.

Then the returns work outward:

:three
:two
:one

The display shows:

One Two Three Two again One again Done

Nothing about RET says which subroutine it should return to.

It doesn't need to.

The most recent return destination is already waiting at the top of the call stack.

The Limit

The call stack has room for 32 return destinations.

That means a program can have up to 32 calls waiting to return at once.

This isn't normally a concern for a few layers of ordinary subroutines, but it is still a real limit.

A CALL can't create another return obligation if all 32 positions are already occupied.

Likewise, a RET can't fulfill a return obligation if there isn't one.

Both are fatal errors.

RET needs somewhere to return

RET does not mean "finish the program."

It means "return from the most recent CALL."

If the call stack is empty, there is no return destination to retrieve and execution cannot continue.

The call stack follows the same basic rule as every other stack we've worked with:

You can only remove something that was previously placed there.

Try It

Consider these subroutines:

:alpha
D.TXT "A "
CALL :beta
D.TXT "B "
RET

:beta
D.TXT "C "
RET

:gamma
D.TXT "D "
CALL :alpha
D.TXT "E "
RET

Without running the program, work out what this code displays:

D.TXT "START "
CALL :gamma
D.TXT "F "
CALL :beta
D.TXT "END"
D.BLT

As you work through it, keep track of the return destinations waiting on the call stack.

Getting lost?

Don't try to read the program from top to bottom.

Start with the first instruction and follow execution wherever each CALL takes it.

When you reach a RET, continue from immediately after the CALL that created the most recent pending return.

Still stuck?

Write down the call stack whenever a CALL or RET executes.

You only need to track where execution will continue:

after CALL :gamma
after CALL :alpha
after CALL :beta
...
Show me the result

The display shows:

START D A C B E F C END

We've seen how CALL and RET use the processor's call stack to keep track of nested subroutines and find their way back.

But knowing where to return is only part of a subroutine's interface. The caller also needs some way to provide inputs, receive outputs, and understand what the subroutine might change.

In the next lesson, NCL 502: Subroutine Interfaces, we'll look at what a caller and a subroutine need to agree on.