What you learn today
- Offset instead of label
- Breq, brne, brlt, brgt, brle, brge, jr
- Why labels are wrong there
So far every instruction jumps to a label. There is a second family: relative branches. They do not jump to a particular line but by a number of lines forward or back. That is handy for quickly skipping a single line.
Distance instead of label
The r in breq stands for relative. The last operand is not a target but a distance:
| Distance | Effect |
|---|---|
1 | next line, so no jump |
2 | skip one line |
3 | skip two lines |
-1 | one line back |
move heat 1
brlt temp TARGET 2 # cold: leave out the next line
move heat 0
sbn HEATER HASH("Labor") On heat
If it is cold, the chip jumps two lines ahead, that is over move heat 0. Otherwise it runs into that line and switches off.
The family
| relative | absolute | jumps if |
|---|---|---|
breq a b n | beq | a = b |
brne a b n | bne | a ≠ b |
brlt a b n | blt | a < b |
brgt a b n | bgt | a > b |
brle a b n | ble | a ≤ b |
brge a b n | bge | a ≥ b |
jr n | j | always |
Why labels are wrong there
To the chip a label is just a number: the number of the line it sits on. If start: is on line 12 and you write jr start, the chip jumps 12 lines ahead, not to line 12. The program lands somewhere or past the end. There is no error when saving, but the checker in the IC10 coder reports it.
Every line counts towards the distance: comment lines, empty lines, label lines. If you insert a comment line between the jump and its target, the distance is no longer right. Relative branches therefore belong only over short stretches without comment lines in between.
Watch the direction with NaN. Every comparison with NaN is false, so brlt does not jump then. If move heat 1 comes first and switching off sits in the skipped line, the heater goes off when the sensor fails. Built the other way round, it would switch on.
Diagram
Exercise
Task
Gas sensor “Labor”, wall heater “Labor”, LED display “Labortemp”. Use relative branches (breq, brne, brlt, brgt, brle, brge) for all decisions, no absolute comparison jumps. You may use j for the main loop.
- Heater on below 20 °C, otherwise off. Off on sensor failure.
- Display colour: below 18 °C
Color.Blue, above 26 °CColor.Red, in betweenColor.Green.Color.Redon sensor failure. - The display shows the temperature in °C.
Hints
Hint 1
Heater: move heat 1, then brlt temp TARGET 2 and move heat 0.
Hint 2
Colour: green as default, then two checks with one skipped line each.
Hint 3
Check the NaN direction: brge and brle do not jump on NaN – both lines run, the last one sets red.
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