Jumps work, but they make programs messy quickly. Often there is a cleaner way: the result of a comparison is a number, and you can use it directly.
Set instructions: a comparison as a number
| Instruction | register = 1 if |
|---|---|
sgt r? a b | a > b |
sge r? a b | a ≥ b |
slt r? a b | a < b |
sle r? a b | a ≤ b |
seq r? a b | a = b |
sne r? a b | a ≠ b |
otherwise 0. Since On expects exactly 0 or 1, that fits perfectly:
l r0 d0 Temperature
sgt r1 r0 303.15 # 1 if warmer than 30 C
s d1 On r1 # light follows the comparison directly
select: the switch
select r? condition a b stores a in the register when the condition is non-zero, otherwise b.
select r2 r1 Color.Red Color.Green
s d2 Color r2
Color.Red and Color.Green are built-in names for colour numbers (red = 4, green = 2). An LED display’s colour is set through the LogicType Color.
And, or, not
With 0/1 values you can combine logically: and r3 r1 r2 is 1 when both are 1, or when at least one is 1. “Not” is easiest with seqz r4 r1 (1 becomes 0, 0 becomes 1).
and/or work bitwise. With pure 0/1 values that is exactly logical and/or – with other numbers you get surprises.
Diagram
Exercise
Task
Like the last lesson, but without conditional jumps:
- Warning light
d1on above 30 °C (303.15 K), otherwise off. - The LED display
d2shows the temperature in °C, red when it is too hot, otherwise green.
Only j start for the loop is allowed.
Hints
Hint 1
sgt r1 r0 303.15 puts 1 or 0 into r1 – exactly what On needs.
Hint 2
The colour: select r2 r1 Color.Red Color.Green, then s d2 Color r2.
Hint 3
The number on the display goes through Setting as before.