400-Level Challenge: Build Your Own Stack
We've spent the 400-level organizing ordinary memory into increasingly useful structures.
We began with one that the processor already provides for us: the stack.
PUSH puts a value onto it.
POP takes the most recently pushed value back off.
sp remembers where the stack currently ends.
But by now, none of those ideas should seem particularly magical.
A stack is just a way of organizing memory.
So let's build one ourselves.
The Challenge
Reserve a region of ordinary memory and use it to implement your own stack.
Your stack must support two operations:
- Push — place a value onto the top of the stack.
- Pop — remove the most recently pushed value and recover it.
Do not use:
PUSH
POP
and do not modify:
sp
This one is yours.
Choose where the stack lives.
Choose how large it is.
Choose which direction it grows.
Choose what your stack pointer means.
The only requirement is that the result behaves like a stack.
Requirements
Your program should:
- reserve some ordinary memory for stack data;
- keep track of the current stack position using ordinary memory;
- push several values onto the stack;
- pop those values in reverse order;
- detect an attempt to push when the stack is full;
- detect an attempt to pop when the stack is empty;
- avoid using
PUSH,POP, or the processor'ssp.
For example, this sequence:
push 10
push 20
push 30
pop → 30
pop → 20
push 40
pop → 40
pop → 10
should produce exactly those values.
Then test the boundaries of your design.
Fill the stack completely and try to push one more value.
Empty it completely and try to pop one more value.
Neither operation should access memory outside the region you reserved.
There are many perfectly good ways to solve this.
Stuck?
Try building the stack yourself before opening these.
What state does my stack need?
Think about what has to persist between one operation and the next.
The values themselves need somewhere to live.
Your program also needs to remember where the current end of the stack is.
Still stuck?
Reserve a region of memory for the values, then reserve another location for your own stack pointer.
For example, your program might have:
stack data → some region of memory
pointer → one ordinary memory location
The exact addresses are up to you.
What should my pointer mean?
That's a design decision.
It might identify the value currently on top of the stack.
It might identify the next unused location.
It might even represent the number of values currently stored.
Any of those can work.
What matters is that you choose one meaning and use it consistently.
Still stuck?
One convenient convention is:
The pointer contains the address of the next unused location.
With that convention, an empty stack begins with the pointer at one end of the reserved region.
A push stores a value using that pointer and moves it to the next position.
A pop moves the pointer back and reads the value there.
You don't have to use this convention.
How do I access the location stored in my pointer?
Think back to NCL 403: Indirect Memory.
Your program doesn't know in advance which stack location it needs.
The pointer chooses that location while the program is running.
Still stuck?
Suppose your pointer lives at $000.
The indirect operand:
[$000]
means to use the value stored at $000 as an address.
You can also give the complete indirect operand a name:
#stack_ptr $000
#stack_top [$000]
Now #stack_ptr refers to the pointer itself, while #stack_top refers to the memory location selected by that pointer.
How do I push?
A push has two jobs:
- determine which stack location should receive the value;
- update the pointer so the next operation sees the new stack state.
The order depends on what your pointer means and which direction your stack grows.
Still stuck?
If your stack grows downward and your pointer identifies the next unused location, a push can first move the pointer downward:
SUB #stack_ptr #stack_ptr 1
The indirect operand now refers to the newly selected stack location.
Store the value there.
How do I pop?
Pop must undo what push did.
It needs to recover the most recently stored value and update the pointer so that location is no longer part of the stack.
Think carefully about the order.
Still stuck?
For a downward-growing stack whose pointer identifies the current top, the basic operation is:
MOVE #value #stack_top
FREE #stack_top
ADD #stack_ptr #stack_ptr 1
Read the value before freeing its location.
Your exact code may differ depending on the convention you chose.
How do I know when the stack is empty or full?
This follows from your memory layout and the meaning you chose for the pointer.
Ask two questions:
- What value does the pointer have when no values are stored?
- What value does the pointer have when every reserved location is occupied?
Those are your boundaries.
Check them before performing an operation that would move beyond them.
Still stuck?
Suppose your stack grows downward.
You might give the two limits names:
#stack_empty 0x210
#stack_full 0x200
Then your push code can check the full boundary:
BEQ :full #stack_ptr #stack_full
and your pop code can check the empty boundary:
BEQ :empty #stack_ptr #stack_empty
The particular values depend on where you put your stack and what your pointer means.
Work out the two limits for your own design.
Show me one solution
There are many valid ways to arrange the stack.
Here's one.
This version reserves a region of memory, places its pointer immediately above that region when empty, and grows downward.
-- Our stack pointer and its indirect operand.
#stack_ptr $000
#stack_top [$000]
-- Values used to demonstrate the stack.
#value $001
-- This particular stack occupies $200 through $20F.
-- Empty means the pointer is immediately above it.
MOVE #stack_ptr 0x210
-- Push 10.
MOVE #value 10
CALL :push
-- Push 20.
MOVE #value 20
CALL :push
-- Push 30.
MOVE #value 30
CALL :push
-- Pop 30.
CALL :pop
D.TXT #value
D.TXT "\r\n"
-- Pop 20.
CALL :pop
D.TXT #value
D.TXT "\r\n"
-- Push 40.
MOVE #value 40
CALL :push
-- Pop 40.
CALL :pop
D.TXT #value
D.TXT "\r\n"
-- Pop 10.
CALL :pop
D.TXT #value
D.TXT "\r\n"
D.BLT
JUMP :done
:push
-- $200 is the lowest location reserved for this stack.
BEQ :stack_full #stack_ptr 0x200
SUB #stack_ptr #stack_ptr 1
MOVE #stack_top #value
RET
:pop
-- 0x210 represents an empty stack.
BEQ :stack_empty #stack_ptr 0x210
MOVE #value #stack_top
FREE #stack_top
ADD #stack_ptr #stack_ptr 1
RET
:stack_full
D.TXT "STACK FULL\r\n"
D.BLT
JUMP :done
:stack_empty
D.TXT "STACK EMPTY\r\n"
D.BLT
:done
This particular solution chose:
| Decision | Choice |
|---|---|
| Stack region | $200 through $20F |
| Capacity | 16 values |
| Growth | Downward |
| Empty pointer | 0x210 |
| Pointer meaning | Current top after a push |
Those choices are not part of the challenge.
A stack growing upward could work just as well. So could a larger or smaller stack somewhere else in memory. You could use a different pointer convention and arrive at different push and pop operations.
The important part is the relationship between the memory, the pointer, and the operations.
Going Further
Detecting memory collisions
Your stack knows when it has reached the boundaries of its reserved memory.
But what if something is already stored in the next location?
A stack that checks only its boundaries could overwrite that value without realizing it.
Try modifying your push operation to detect whether the next location is already occupied.
If it is, report a memory collision instead of overwriting the existing value.
This is different from detecting a full stack. The stack might still have plenty of room within its reserved region, but a particular location could already contain something.
Checking a location
Before writing, check the location you're about to use:
S.BNEQ :collision #stack_top ""
An unused memory location contains an empty string.
If something is already stored there, branch away and handle the collision.
Otherwise, the push can continue normally.
Remember that your pointer must identify the location you're about to write before you perform this check.
Can you detect the collision without modifying the existing value or leaving your stack pointer in the wrong position?
That's the end of the 400-level.
We've gone from using the processor's stack to organizing ordinary memory into arrays, records, linked lists, associative arrays, multidimensional arrays, and structures containing other structures.
In this challenge, we came back to where the level began and built a stack ourselves using the same ideas we've used everywhere else: ordinary memory, indirect addressing, and a convention about what that memory means.
Ready to continue? In NCL 501: The Call Stack, we'll turn our attention from organizing data to organizing the subroutines that work with it.