What you learn today
- Sections and register plan
- Cut subroutines sensibly
- Save space
The purge from day 136 has grown: the display now turns red in the fault state, and a second room is coming soon. At some point every bigger chip hits the limits – 128 lines, 90 characters per line and above all 4096 characters. With German comments the character limit usually fills up first. And long before that, you no longer find your own bugs in a chip without structure.
A fixed order
| Part | Content |
|---|---|
| Header | define for devices, states and limits, then all alias |
| Start | values set before the loop |
| 1. Inputs | all lbn in one place, the NaN check right behind them |
| 2. States | jump table and one block per state |
| 3. Output | take over the new state, all sbn in one place |
| Subroutines | after j start |
If you build like this, you know where to look for every fault: wrong reading → section 1, wrong state → section 2, device does nothing → section 3.
The register plan
16 registers sound like a lot but are handed out quickly. Decide in the header what each one is for and stick to it:
# Registers: r0-r2 state and counter
# r3-r7 main program
# r8 only for the subroutine show
alias state r0
alias ticks r2
alias colour r8
The most important rule: a subroutine only uses its own registers. If it calculates with tmp while the caller still needs a value in tmp, that value is gone after j ra – without an error message, and the bug shows up somewhere else entirely.
Cutting subroutines sensibly
A subroutine costs at least two extra lines: jal and j ra. It pays off when
- the same code is needed in several places, for example “everything off” for every fault,
- a self-contained part clutters the main loop, such as the display with number and colour.
A subroutine for a single line never pays off. And one that calls jal itself needs push ra and pop ra.
jal show # status and colour
# ... rest of the output ...
j start
show:
seq colour state FAULT
select colour colour RED GREEN
sbn DISPLAY HASH("Status") Color colour
j ra
Saving space
- Keep comments short. One per section and one behind every
defineis enough. Every character counts, spaces too. - Write outputs only once. One
sbnper device in the output instead of one in every state block. selectinstead of jumps.select next tmp FULL nextneeds no label and no return jump.- Leave out empty lines. Each one counts as a line.
Never shorten at the expense of safety: the NaN check and the catch branch stay in, even when space gets tight. Rather shorten a comment than drop a safeguard.
Exercise
Task
The purge has got a subroutine show: it writes the state number and colours the display “Status” red in the fault state, green otherwise. Since then the timing is broken: the run-on ends at once, and whoever presses the button after waiting in Ready ends up straight in the fault state.
Find the register conflict and fix it according to the register plan in the program header. Everything else behaves as on day 136.
Hints
Hint 1
What is in tmp when the line select ticks tmp ticks 0 runs after jal show?
Hint 2
The subroutine calculates the colour in tmp and overwrites the result of seq tmp next state.
Hint 3
According to the register plan r8 belongs to the subroutine: replace tmp with colour there.
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