What you learn today
- Reuse registers
- Avoid intermediate values
- The stack as overflow
The base grows. Three rooms with their own heaters, plus pumps, lights and displays – and at some point your register plan shows: all 16 taken. sp and ra do not count, you need them for the stack and subroutines. A second chip would be one way out. Usually it is enough to be more careful with registers.
What really needs a register
You only need a register for values that change at run time and that you need again later. Everything else is free:
| What | Register? |
|---|---|
| Threshold, prefab hash, device name | no – define |
| Intermediate result that moves straight on | one shared tmp |
| Reading that only matters in this round | yes, but fresh every round |
| State that has to survive across ticks | yes – or the stack |
An alias is just a name, it reserves nothing. Two aliases on the same register are therefore two names for one storage: if alias press r3 ends up below alias temp r3 by accident, every pressure value silently overwrites the temperature.
Reusing registers
A helper register like tmp may mean something different on every line, as long as you no longer need the old content. A subroutine that uses the same registers for several rooms saves the most:
move name HASH("Raum1") # input: which room
jal room
move name HASH("Raum2")
jal room # same registers, other room
Instead of three sets of temperature, state and helper value you need one.
Avoiding intermediate values
Many calculations work without a result register of their own. Keep calculating in the same register and write the result straight to the device:
lbn t SENSOR HASH("Lager") Temperature Average
sub t t CELSIUSOFFSET # in degrees C, same register
slt tmp t FROST # too cold?
sbn LIGHT HASH("Frost") On tmp # out right away, do not keep it
The stack as storage
Whatever has to survive several ticks but is not needed all the time goes into the stack. With get and put at fixed addresses sp stays untouched:
define ADR_ROOM1 0 # stack slot for state of Raum1
get heat db ADR_ROOM1 # fetch state
put db ADR_ROOM1 heat # put state back
One stack slot thus replaces a register that would otherwise be taken for the whole run time. In return every access costs one line.
Fixed addresses and push share the same memory. sp starts at 0 – so the first push overwrites slot 0, where your state may live. Use only one of the two, or put the fixed slots at the top end, for example from address 500.
With push and pop you can also free registers for a moment, for example around a subroutine. pop brings them back in reverse order: after push a and push b comes pop b first, then pop a.
Exercise
Task
Three rooms “Raum1”, “Raum2” and “Raum3” each have a gas sensor and a wall heater with the same name. Every heater works with hysteresis: on below 19 °C, off above 21 °C, in between it stays as it was.
The subroutine room is called for all three rooms with the same registers: name is the name hash, adr the room’s stack slot (0, 1 or 2).
So that the rooms do not overwrite each other’s state, it lives in the stack. Complete the subroutine: fetch the state with get, compute the hysteresis, put the state back with put.
Hints
Hint 1
get heat db adr fetches the room’s state, put db adr heat puts it back.
Hint 2
Hysteresis as usual: slt tmp t OFF and and heat heat tmp, then slt tmp t ON and or heat heat tmp.
Hint 3
Without the stack the state of Raum1 lives on in Raum2 – exactly what the second test checks.
Quiz
5 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