16 registers fill up fast. That is why every chip has a stack with 512 slots. The register sp (stack pointer) always points to the next free slot.
Stacking
push 10 # slot 0 = 10, sp = 1
push 20 # slot 1 = 20, sp = 2
pop r0 # r0 = 20, sp = 1
peek r1 # r1 = 10, sp stays 1
push valueputs it on top.pop r?takes the top value off.peek r?only looks, without removing.
What went in last comes out first – like a stack of trays in the canteen.
Direct addressing
With get and put you access a specific slot without changing sp. db is the chip’s own housing:
put db 5 293.15 # slot 5 = 293.15
get r2 db 5 # r2 = 293.15
This even works with other chips: get r0 d0 3 reads slot 3 from the stack of the chip on pin d0. That is how two chips talk to each other.
pop on an empty stack reports StackUnderFlow, push on a full one StackOverFlow. And sp is a normal register – overwrite it by accident and the stack order is gone.
Diagram
Exercise
Task
Push the numbers 10, 20, 30 onto the stack in that order. Then:
r4= the value at slot 1 of the stack (directly withgetviadb)- take all three values off again so that
r0= 30,r1= 20,r2= 10 r3= the stack pointerspat the end
Hints
Hint 1
Three times push, then get r4 db 1 – before you take anything off.
Hint 2
Three times pop: the first pop returns the 30.
Hint 3
move r3 sp reads the stack pointer.