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

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

Jump tables

A chain of beq lines jumps straight into the block of the current state. A catch branch at the end sends every unknown state into the fault state.

Lesson 15 min Week 20

What you learn today

  • Distribute states via labels
  • Comparison chain
  • Check the state instead of guessing

Yesterday's tank filler has three states and twelve lines of transitions. Every tick works through all transitions, including those of states the machine is not even in. With five or six states this becomes a long chain in which nobody can see any more which line belongs to which state.

Better: the chip jumps straight into the block of the current state and only calculates there.

The comparison chain

beq state READY s_ready
beq state FILLING s_filling
beq state FULL s_full
beq state FAULT s_fault
move next FAULT         # catch: unknown state
j output

Every line compares the state with a number and jumps to the label if they are equal. beq is right here: the state is a whole number that only your code sets – not a reading where an exact comparison could fail.

One block per state

Each block only deals with its own transitions and then jumps to the shared output:

s_filling:
sge tmp pressure TARGET
select next tmp FULL next
j output

The output at the end writes pump and display for all states in one place. That way every rule is in the code exactly once, and a new state only needs one line in the chain and one block.

Check the state instead of guessing

The second-to-last line of the chain is the catch branch. It catches every number that has no block – for example because you deleted a state while rebuilding or a register gets overwritten by accident. Without a catch the chip simply runs into the first block below the chain and carries on there as if everything were fine. With a catch it ends up in the fault state: pump off, display 3, and you see straight away that something is wrong.

Warning

Some programs use j to jump to a computed line number, such as add tmp state 20 and then j tmp. That saves a few lines but breaks with every change: every new comment line and every new define shifts all line numbers, and the jump lands in the middle of some other block – without an error message. Use labels and the comparison chain.

Variant: state as a subroutine

With beqal every block becomes a subroutine that returns with j ra instead of j output:

beqal state READY s_ready
beqal state FILLING s_filling
beqal state FULL s_full

This only works because the blocks set next while the chain checks state. If a block changed state directly, the next beqal line would hit the next block within the same tick. You need the catch separately here: after the chain, check that the state lies in the allowed range.

Diagram

Flow with a conditional jump start: yield · l r0 r0 > 303.15? hot: On 1 On 0 yes: bgt jumps no: next line j start
Every line of the comparison chain is a conditional jump: if the state matches, execution continues at the label, otherwise the next line checks.

Exercise

Task

The tank filler gets a jump table and a fourth state Fault (3):

  • Ready (0) → Filling (1) on a button press
  • Filling → Full (2) from 3000 kPa
  • Full → Ready below 1000 kPa
  • Fault → Ready on a button press, but only if the analyzer delivers readings
  • Without the analyzer and for every unknown state: Fault

The blocks for Ready and Fault and the output are done. Write the comparison chain with the catch branch and the blocks for Filling and Full.

Hints

Hint 1

Four lines beq state STATE label, one for each state.

Hint 2

Then the catch: move next FAULT and j output.

Hint 3

Filling: sge tmp pressure TARGET, select next tmp FULL next, j output. Full the same with slt and REFILL.

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