What you learn today
- and and or with 0/1 values
- xor and nor
- Combine conditions without jumping
An alarm should go off when the pressure is too low or the temperature too high. With jumps that would be two branches and four labels. With logic instructions it is one line.
and and or
| Instruction | result with 0/1 values |
|---|---|
and r? a b | 1 if both are 1 |
or r? a b | 1 if at least one is 1 |
xor r? a b | 1 if exactly one is 1 |
nor r? a b | “neither of them” – but careful, see below |
l r0 d3 Pressure
l r1 d3 Temperature
slt r2 r0 50 # 1 = pressure below 50 kPa
sgt r3 r1 313.15 # 1 = warmer than 40 C
or r4 r2 r3 # 1 = one of the two
s d4 On r4 # warning light
xor: the two-way switch
A long hallway, a lever at each end. Whichever you flip, the light should toggle. That is exactly what xor does: if one lever stands differently from the other, the light is on. Flip either one and the result changes.
l r0 d0 Open
l r1 d1 Open
xor r2 r0 r1 # two-way switch
s d2 On r2
nor is bitwise
All four instructions work bitwise on whole numbers. With and, or and xor you do not notice as long as only 0 and 1 go in. With nor you do: it flips all bits. nor r0 0 0 therefore does not give 1 but −1, and with a 1 involved the result is −2.
For a clean “neither of them” you better use two instructions:
or r3 r0 r1
seqz r3 r3 # 1 only if both are 0
Or you keep nor and cut off the last bit: and r3 r3 1 turns −1 into 1 and −2 into 0.
Only feed in 0 and 1. and r0 2 1 gives 0 although both values look “true”: in binary 2 is 10 and 1 is 01, no bit matches. Clean up values that are not surely 0 or 1 with snez first.
Deciding without jumps
With set instructions, logic and select you build whole decisions without a single label. The program runs from top to bottom every tick and always writes all devices. There is no branch in which an s could be forgotten.
Diagram
Exercise
Task
A hallway with six devices, without conditional jumps (only j start):
d0,d1: levers at both ends of the hallway. The hallway lightd2toggles with every flip (two-way switch).d3: gas sensor. The warning lightd4is on at pressure below 50 kPa or temperature above 40 °C (313.15 K).d5: standby light. It is on when neither lever is flipped – cleanly with 0 or 1.
Hints
Hint 1
Hallway light: xor r2 r0 r1.
Hint 2
Standby: or first, then seqz – or nor followed by and … 1.
Hint 3
Warning light: two comparisons (slt, sgt) and one or.
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