NCL 402: The Stack Pointer

In the previous lesson, we used PUSH and POP to temporarily put integer values aside.

PUSH 10
PUSH 20
PUSH 30

POP $000
POP $001
POP $002

We learned that the stack is last in, first out.

The most recently pushed value is the first one popped.

But where are those values actually going?

Before we answer that, there's something about memory addresses we should finally talk about.

Those Addresses Are Hexadecimal

We've been writing memory locations like this since the 100-level:

$000
$001
$002

So far, we've only needed the first few.

There's a detail we've been able to quietly ignore:

Memory addresses are written in hexadecimal.

For the first ten locations, you can't tell. Decimal and hexadecimal both use the digits 0 through 9.

But memory doesn't stop at $009.

It keeps going:

Decimal Address
8 $008
9 $009
10 $00A
11 $00B
12 $00C
13 $00D
14 $00E
15 $00F
16 $010

We learned how hexadecimal works back in NCL 207.

Those memory addresses have been hexadecimal the whole time.

With three hexadecimal digits, NCS/e can address:

$000 through $FFF

That's decimal 0 through 4095 — 4096 memory locations in total.

And that number should look familiar.

The Stack Pointer

The special value sp is the stack pointer.

When a program begins, the stack is empty and:

sp = 4096

That number may look familiar.

The NCS/e has 4096 memory locations, from $000 through $FFF.

The value stack isn't a separate block of storage.

It uses that same memory.

4096 itself isn't a memory address. It places the empty stack just beyond $FFF, ready to grow downward into memory.

Growing Downward

Suppose we execute:

PUSH 10
PUSH 20
PUSH 30

Each PUSH moves sp down one address, then stores the value there.

Instruction sp Address written Value
Start 4096 — —
PUSH 10 4095 $FFF 10
PUSH 20 4094 $FFE 20
PUSH 30 4093 $FFD 30

The stack grows toward lower memory addresses.

After all three pushes, memory near the top looks like this:

Address Value
$FFD 30
$FFE 20
$FFF 10

sp now contains 4093.

The memory location selected by sp therefore contains the value currently on top of the stack.

Accessing the Top

We've normally accessed memory by writing its address directly:

MOVE $000 42

Sometimes, though, the address we want is stored somewhere else.

Square brackets tell NCL to use a value as an address.

MOVE $000 [sp]

If sp contains 4093, [sp] refers to $FFD.

Since $FFD currently contains 30, the instruction above copies 30 into $000.

It doesn't pop anything. We've simply read the memory at the address stored in sp.

We can write through the pointer as well:

MOVE [sp] 42

This replaces the value currently on top of the stack with 42.

The brackets matter

sp is the stack pointer itself.

[sp] is the memory location whose address is stored in the stack pointer.

MOVE $000 sp

copies the pointer.

MOVE $000 [sp]

copies the value it points to.

We'll do much more with this in the next lesson.

The Ends of the Stack

sp can contain values from 0 through 4096, inclusive.

Trying to move it outside that range causes a fatal STACK_ERROR.

MOVE sp -1

and:

MOVE sp 4097

both fail immediately.

The endpoints themselves are valid.

sp Meaning
4096 Stack is empty
4095 Stack contains one value at $FFF
4094 Stack contains two values, beginning at $FFE
... ...
1 Stack reaches $001
0 Stack reaches $000

When sp is 0, $000 is still a valid stack location. The stack simply cannot grow any farther.

Another PUSH would need to move sp below 0, so it causes a fatal STACK_ERROR.

At the other end, when sp is 4096, the stack is empty. There is nothing to POP, so attempting to do so also causes a fatal STACK_ERROR.

An empty stack has no top

4096 is a valid value for sp, but it is not a memory address.

When:

sp = 4096

there is no value for [sp] to refer to.

Reading or writing [sp] while the stack is empty causes a fatal STACK_ERROR.

This is why the previous lesson described the stack as having a maximum depth of 4096 values.

At maximum depth, every memory location from $000 through $FFF is part of the stack.

What PUSH Does

Now that we know how sp and [sp] behave, we can look underneath PUSH.

Suppose #value contains an integer we want to push.

DEC sp
MOVE [sp] #value

That's what a push does:

decrement → write

The order matters. On an empty stack, sp contains 4096. Decrementing it first selects $FFF, where the new value can be stored.

The same sequence also respects the lower limit of the stack.

If sp is already 0, DEC sp attempts to move it to -1. That is outside the valid range, so the instruction causes a fatal STACK_ERROR before anything can be written.

PUSH packages this operation into a single instruction:

PUSH #value

What POP Does

We can open up POP in the same way.

MOVE #value [sp]
FREE [sp]
INC sp

A pop is:

read → free → increment

The value at [sp] is copied to its destination, the memory location it occupied is freed, then sp moves upward.

After:

PUSH 10
PUSH 20
PUSH 30

sp contains 4093, and $FFD contains 30.

Executing:

POP $000

copies 30 into $000, frees $FFD, and moves sp to 4094.

The order also handles an empty stack naturally.

If sp is 4096, the first instruction of the longer version tries to read [sp]. Since an empty stack has no top, it immediately causes a fatal STACK_ERROR.

POP packages the whole operation into one instruction:

POP #value
Do real processors manage stacks this way?

Often, yes — although the details vary considerably.

Many processors keep a stack pointer that identifies where values are being pushed and popped. Some provide dedicated push and pop instructions, while others expect software to manipulate the stack using more general memory operations.

Stacks don't even agree on which way to grow. Some grow toward lower memory addresses; others grow toward higher ones.

They can also disagree about when the stack pointer moves.

A push might move, then write, or write, then move.

A pop might read, then move, or move, then read.

These choices determine whether the stack pointer refers to the value currently on top of the stack or to the next available position.

NCS/e grows its stack toward lower addresses:

PUSH: decrement → write
POP:  read → free → increment

There isn't one universally correct arrangement. What matters is that the machine uses one convention consistently.

One Memory

The stack and the rest of our program use the same memory.

They simply tend to begin at opposite ends.

We've usually placed our own values at low addresses:

$000
$001
$002
...

The stack begins at the other end:

...
$FFD
$FFE
$FFF

and grows downward as values are pushed.

For a normal, balanced program using low memory addresses, the two regions are usually very far apart.

There is no wall between them, though.

If a program stores ordinary data high in memory, or allows its stack to grow far enough downward, the two regions can eventually meet.

The processor does not reserve $FFF, $FFE, or any other memory location exclusively for the stack.

They're ordinary memory locations.

PUSH simply reaches them from the other direction.

The stack does not protect your data

PUSH knows where sp is.

It does not know which memory locations your program considers important.

If the stack grows into addresses containing program data, those values can be overwritten.

In ordinary programs, keeping temporary stack use balanced and regular data away from the top of memory leaves plenty of room between them.

Try It

Without using PUSH or POP, reproduce this program:

PUSH 10
PUSH 20
PUSH 30

POP $000
POP $001
POP $002

D.TXT $000
D.TXT " "
D.TXT $001
D.TXT " "
D.TXT $002
D.BLT

Your version should display:

30 20 10

Use DEC, INC, MOVE, FREE, sp, and [sp] to perform the stack operations yourself.

Show me one solution
DEC sp
MOVE [sp] 10

DEC sp
MOVE [sp] 20

DEC sp
MOVE [sp] 30

MOVE $000 [sp]
FREE [sp]
INC sp

MOVE $001 [sp]
FREE [sp]
INC sp

MOVE $002 [sp]
FREE [sp]
INC sp

D.TXT $000
D.TXT " "
D.TXT $001
D.TXT " "
D.TXT $002
D.BLT

At the end, sp is back at 4096.

The stack is balanced.


PUSH and POP aren't operating on separate hidden storage.

They're moving sp and accessing ordinary memory.

We also saw something new:

MOVE $000 [sp]

The address we accessed wasn't written directly into the instruction. It came from another value.

In the next lesson, NCL 403: Indirect Memory, we'll use that idea deliberately and learn how a program can choose which memory location to access while it's running.