What you learn today
- Stock per item
- Table with hash and quantity
- Search
“How much copper do we have?” In the ingot locker copper lies in three different slots, with iron and steel in between. Instead of searching all 30 slots for copper every time, the chip builds a table: one row per kind, hash and amount.
Stock per item
Every row has two places in the stack: the prefab hash on the even address, the amount on the odd one behind it.
| Row | Hash address | Amount address |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 2 | 3 |
| 2 | 4 | 5 |
| 3 | 6 | 7 |
The chip stores the hashes once before the start label:
poke 0 IRON
poke 2 COPPER
Filling the table
Every pass the chip sets all amounts to 0 and then goes through the locker's slots. For every slot it looks for the matching row and adds the amount:
row:
mul addr j 2
get tmp db addr
beq tmp kind hit
add j j 1
blt j ROWS row
Empty slots have the hash 0, which stands in no row – they simply fall through. Kinds that are not in the table do the same.
Search
The search goes through the table once and returns the amount for the wanted kind. Not found means −1. Somebody writes the wanted hash into a logic memory “Suche” – a player via a console or another chip.
30 slots times up to four rows are a good 900 instructions – the chip needs about eight ticks for a whole pass. That is why the display does not jump in the same tick in which an ingot arrives. For a store that is perfectly fine.
In the middle of a pass the table holds half sums. Whoever reads the table from outside with get, for example a second chip, then sees wrong numbers. Only read it at the end of the pass – or write the finished values into a second area.
Diagram
Exercise
Task
The table lives in addresses 0–7 (iron, copper, steel, silicon). Every pass set the amounts to 0, sort all 30 slots of the locker “Barren” into it and show on “Bestand” the amount for the hash in the logic memory “Suche”: −1 when the kind is not in the table, −2 when the locker is missing.
Hints
Hint 1
Clearing amounts: address 2 · j + 1 with poke … 0.
Hint 2
Per slot: find the row with get tmp db addr and beq tmp kind hit, then add the amount.
Hint 3
Search: amount first −1, on a hit get amount db addr; finally a missing locker with −2.
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