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.
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.
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
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
TABLEon. - Search the table for the
OccupantHashof 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