NCL 401: The Value Stack

So far, whenever we wanted to keep a value around, we put it in memory.

That works well when the value has a clear purpose and deserves its own location.

Sometimes, though, we only need to put a value aside for a moment.

Suppose $000 contains a value we want to preserve while we use that location for something else:

MOVE $000 42
MOVE $001 $000

MOVE $000 100
D.TXT $000

MOVE $000 $001
D.TXT $000
D.BLT

This works.

$001 is acting as temporary storage while we use $000 for something else.

NCL gives us another place to put temporary values: the value stack.

Pushing a Value

The PUSH instruction places an integer value onto the value stack.

PUSH <value>

For example:

MOVE $000 42
PUSH $000

This copies the current integer value of $000 onto the stack.

It does not change $000.

We can now overwrite $000 without losing the value we pushed:

MOVE $000 42
PUSH $000

MOVE $000 100

At this point, $000 contains 100, while the stack still contains 42.

Popping a Value

The POP instruction removes the most recently pushed integer and stores it in a destination.

POP <dst>

For example:

MOVE $000 42
PUSH $000

MOVE $000 100
POP $000

After the POP, $000 contains 42 again.

Unlike reading a value from memory, POP removes the value from the stack.

We can rewrite our earlier example without using $001 as temporary storage:

MOVE $000 42
PUSH $000

MOVE $000 100
D.TXT $000

POP $000
D.TXT $000
D.BLT

The stack lets us put a value aside, use its memory location for something else, then recover the original value later.

Last In, First Out

The value stack can hold more than one value.

Consider:

PUSH 10
PUSH 20
PUSH 30

POP $000
POP $001
POP $002

We can trace the stack as each instruction executes.

In this table, the top of the stack is on the right.

Instruction Value stack $000 $001 $002
PUSH 10 10 — — —
PUSH 20 10, 20 — — —
PUSH 30 10, 20, 30 — — —
POP $000 10, 20 30 — —
POP $001 10 30 20 —
POP $002 empty 30 20 10

Notice the order.

10 was pushed first, but popped last.

30 was pushed last, but popped first.

This is called last in, first out, usually shortened to LIFO.

The most recently pushed value is called the top of the stack.

PUSH adds a value to the top.

POP removes the value from the top.

Do real processors have stacks like this?

Yes.

Stacks have been a common part of computer architecture for decades. They're widely used for temporary storage, subroutine calls, and preserving values while other work is being done.

Different processors provide different ways to work with them, but the last-in, first-out idea is the same.

NCL's PUSH and POP give us a simple way to use one without worrying about how it's stored.

We'll look underneath that abstraction in the next lesson.

Using the Stack as Temporary Storage

LIFO behavior is especially useful when several temporary values need to be preserved.

For example:

MOVE $000 5
PUSH $000

MUL $000 $000 4
PUSH $000

ADD $000 $000 3
D.TXT $000
D.TXT " "

POP $000
D.TXT $000
D.TXT " "

POP $000
D.TXT $000
D.BLT

The value in $000 changes from:

5

to:

20

and finally:

23

But we saved the earlier values before replacing them.

The first POP restores 20.

The second restores 5.

The program therefore displays:

23 20 5

Each PUSH puts the current value aside.

Each POP returns to the most recently saved one.

Because the stack works in reverse order, we don't need to give each temporary value its own memory location.

Swapping Two Values

Suppose two memory locations contain:

MOVE $000 10
MOVE $001 20

We want to exchange them.

Using only MOVE, we need another location to hold one value temporarily:

MOVE $002 $000
MOVE $000 $001
MOVE $001 $002

The stack gives us somewhere else to put that temporary value:

PUSH $000
MOVE $000 $001
POP $001

Afterward:

$000 = 20
$001 = 10

No scratch location was needed.

Swapping two integer values is common enough that NCL also provides a direct instruction:

SWAP <a> <b>

So the same operation can simply be written:

SWAP $000 $001

SWAP exchanges the integer values stored at the two locations.

The stack hasn't given the processor a fundamentally new ability.

It has given us a convenient place for values that only need to exist temporarily.

Keeping the Stack Balanced

When the value stack is being used for temporary storage, every value we push should eventually be removed again.

For example:

PUSH 10
PUSH 20

POP $000

After this code runs, 20 has been popped into $000.

But 10 is still on the stack.

Nothing automatically removes it.

If this code runs repeatedly, another 10 is left behind each time.

A section of code that leaves the stack in the same state it found it is called balanced.

For example:

PUSH $000
PUSH $001

POP $001
POP $000

adds two values and then removes two values.

The stack ends exactly as it began.

Push in one order, pop in reverse

If several values need to be restored to their original locations, push them in one order and pop them in the opposite order.

PUSH $000
PUSH $001
PUSH $002

-- Use the locations for something else.

POP $002
POP $001
POP $000

Because the stack is LIFO, the values return to the locations they came from.

Keeping the stack balanced becomes especially important as programs grow larger.

A value accidentally left behind changes what a later POP will retrieve.

Stack Limits

The stack can grow to a maximum depth of 4096 values.

Trying to push another value when the stack has no room to grow causes a fatal STACK_ERROR.

Trying to pop a value when the stack is empty also causes a fatal STACK_ERROR.

Operation Invalid condition Result
PUSH Stack has no room to grow Fatal STACK_ERROR
POP Stack is empty Fatal STACK_ERROR

In practice, a stack error usually means that the program's pushes and pops don't match the way the program expects.

We'll look more closely at how the stack is stored in the next lesson.

Try It

Start with:

MOVE $000 10
PUSH $000

ADD $000 $000 5
PUSH $000

MUL $000 $000 2

At this point:

$000 = 30

and two earlier values are waiting on the stack.

Using POP, D.TXT, and D.BLT, display:

30 15 10

Try tracing the stack before running the program.

Show me one solution
MOVE $000 10
PUSH $000

ADD $000 $000 5
PUSH $000

MUL $000 $000 2

D.TXT $000
D.TXT " "

POP $000
D.TXT $000
D.TXT " "

POP $000
D.TXT $000

D.BLT

PUSH and POP let us use the value stack without worrying about where its values are stored.

In the next lesson, NCL 402: The Stack Pointer, we'll look underneath that abstraction and see how the processor keeps track of the stack itself.