stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 122

Day 122 of 365 · Advanced · Month 5: Memory and structure

Tables in the stack

A table in the stack: address = start + index. A loop goes through the entries and stops as soon as it finds a match.

Lesson 15 min Week 18

What you learn today

  • Compute an index
  • Loop over entries
  • Search values

In front of the smelter stands an input locker. Only ores may go in, anything else should trigger a warning lamp. Which ores are allowed is stored in a table in the stack. The chip searches it for the item in the slot.

Computing the index

A table starts at a fixed address, the start. Entry number i sits at start + i:

define TABLE 10 # first address of the table
add adr TABLE i
get entry db adr

Do not let the table start at 0 if you also use push. A fixed start such as 10 or 400 keeps it away from the push pile. And whoever needs a second table later simply puts it behind the first.

Loop over the entries

Filling happens once before the start label, with a moving address:

move adr TABLE
poke adr IRON
add adr adr 1
poke adr COPPER

Reading happens in a loop with a counter, as you know it from the slots:

move i 0
search:
bge i COUNT done           # all entries done
add adr TABLE i
get entry db adr
beq item entry found       # match: out at once
add i i 1
j search

Searching values

The search above stops at the first match. That saves instructions, especially with long tables. Prefab hashes are whole numbers, so the exact comparison with beq is right here. With readings it would not be.

Other searches follow the same pattern: find the largest element (with max over all entries), remember the position of a value (keep i on a match), count how often something occurs.

Warning

A loop over a long table costs instructions. Here it is six per entry. With 128 instructions per tick a table of about 20 entries still fits into one tick. If it gets longer, the search spreads over several ticks. That is not an error, but the answer comes later.

Tip

If the locker is missing, item is NaN and beq never finds a match. Make a rule out of it: without a locker no warning and no “allowed”.

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
The table starts at a fixed address. Entry i sits at start + i.

Exercise

Task

The input locker “Eingang” (StructureLockerSmall) may only receive ores from the table in slot 0.

  • Before the start label fill the hashes of iron, copper, gold and silicon ore from address TABLE on.
  • Search the table for the OccupantHash of slot 0 and stop on a match.
  • Wall light “Erlaubt” on if the item is in the table. Flashing light “Fremdteil” on if something lies in the slot that is not in the table.
  • Empty slot or missing locker: both off.

Hints

Hint 1

Filling: move adr TABLE, then alternate poke adr … and add adr adr 1.

Hint 2

Guard Occupied with sgtz – NaN becomes 0 and must never go into an and.

Hint 3

Search: bge i COUNT done, compute the address, get, beq item entry found, increase i, back.

Quiz

4 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere