We are currently experiencing issues with email delivery; this is being resolved as quickly as possible.

stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. The stack: push, pop, peek

Chapter 6 · Memory & structure

The stack: push, pop, peek

512 extra storage slots – as a pile or by direct address with get and put.

Lesson 15 of 18 9 min

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 value puts 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.

Warning

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

The stack with its pointer slot 0 10 slot 1 20 slot 2 30 slot 3 slot 4 sp = 3 next free slot push 40 pop r0 r0 = 30 last in, first out
push puts on top and increases sp, pop takes the top value off. Last in, first out.

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 with get via db)
  • take all three values off again so that r0 = 30, r1 = 20, r2 = 10
  • r3 = the stack pointer sp at 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.

Esc
↑ ↓ selectEnter open/ opens search anywhere