CalculateQuestion 1 · Recall
Loading…
By the end you can explain how a single byte can monitor or control up to eight devices, test one flag with a mask, and build a control byte by setting and clearing individual bits.
A greenhouse controller has to switch a heater, a fan, a pump, a lamp, an alarm and a door lock. That is six devices, each either on or off.
You could run six separate wires and six separate yes/no values. Or you could use one byte, where each bit is one device:
| Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Device | spare | spare | door lock | alarm | lamp | pump | fan | heater |
One byte carries all six states. The processor can read them all at once, but it is usually interested in exactly one — and that is where masks come back.
Two different jobs use the same trick:
A sensor's status byte arrives in ACC (from its address with LDD, or from an input port). Each bit of that byte is a flag set by the hardware.
Suppose bit 2 of the status byte means "too hot". To find out:
100 LDD 400 ; read the status byte from address 400
101 AND B0000 0100 ; keep only bit 2
102 CMP #0 ; is it zero?
103 JPE cool ; equal to zero -> not too hot
104 ; too hot: switch the fan on (see below)
AND throws away the seven other flags, so the result is either 0000 0100 (bit set) or 0000 0000 (bit clear). Comparing with 0 then turns that into True or False, which JPE/JPN can act on.
One warning: test a bit with AND, never with OR. OR leaves the other bits in place, so a non-zero answer would tell you nothing about bit 2 specifically.
Worked example: Building a control byte
The control byte starts at 0000 0000 (everything off). Carry out these steps, giving the byte each time:
a) switch the heater and fan on b) also switch the pump on c) also switch the lamp on d) switch the heater off