A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.3.3 Using Bits to Read and Control Devices

🔒 Lesson slides are available to signed-in users. Sign in

4.3.3 Using Bits to Read and Control Devices

A computer-controlled system can represent several device states within one byte. Input bits can report conditions detected by sensors, while output bits can determine whether actuators are switched on or off. Bit masks allow the program to inspect or change one position without disturbing the others.

By the end of this section, you should be able to:

  • Explain how one byte can represent several sensor or actuator states.
  • Distinguish an input/status byte from an output/control byte.
  • Use AND masks to test whether an input flag is set.
  • Use OR masks to switch selected output bits on.
  • Use AND masks to switch selected output bits off while preserving the rest.
  • Trace a load–mask–compare–update–store sequence.
  • Explain how repeated sensor readings provide feedback to a control program.
  • Design an original bit allocation and masks for a simple device-control scenario.

Where This Page Fits

Related content Role in this page
Monitoring and control systems Provides the sensor–controller–actuator–feedback context.
4.3.1 Binary Shifts and Their Effects Explains how bits move within a register.
4.3.2 Bitwise Logic and Masks Introduces AND, OR, XOR, test, set, clear and toggle.
4.3.3 Using Bits to Read and Control Devices Applies those operations to device status and control bytes.
Scope: This page briefly recalls sensors, actuators and feedback, but it does not repeat the full monitoring-and-control lesson. Its main purpose is to connect those concepts to bit-level assembly operations.

Representing Device States in Bytes

A device interface can expose an 8-bit status value to the processor. Each bit has an agreed meaning. A second 8-bit value can be used to send control decisions to actuators.

Input/status byte

Bits are read by the program.

00010110 Sensor conditions currently reported

Output/control byte

Bits are written by the program.

00001001 Actuators currently requested
Flag bit: a single bit used to represent whether a condition or device state is false/off (0) or true/on (1).
Common misconception: The processor does not infer a bit's meaning from the pattern itself. The system designer defines what each position represents.

Original Example: Aquaculture Tank Controller

The example system uses one byte for sensor states and one byte for actuator commands. Bit 0 is always the rightmost bit.

Input byte: SENSOR_STATE

Bit Meaning when 1 Single-bit test mask
4Service hatch is openB00010000
3Filter pressure is too highB00001000
2Water level is too lowB00000100
1Dissolved oxygen is too lowB00000010
0Water temperature is too highB00000001

Output byte: CONTROL_STATE

Bit Meaning when 1 Single-bit set mask
4Maintenance lock enabledB00010000
3Warning beacon onB00001000
2Cooling fan onB00000100
1Refill pump onB00000010
0Aerator onB00000001

The Repeated Control Cycle

A control program normally repeats its work while the system is operating. The exact hardware arrangement varies, but the software logic follows a clear cycle:

1. ReadObtain the latest input byte.
2. MaskIsolate the bit of interest.
3. DecideCompare the masked result.
4. UpdateSet or clear an output bit.
5. RepeatRead new feedback.

The new sensor byte acts as feedback: it shows the current condition after earlier actuator commands have had time to affect the physical system.

Real-time control: processing that reads and responds to changing inputs within the time limits required by the controlled system.

Read a Sensor Bit with AND

Suppose SENSOR_STATE contains:

00010110

Bit 1 reports low oxygen. Isolate that position with the mask B00000010:

00010110 AND 00000010 = 00000010

The result is non-zero, so the low-oxygen flag is set.

LDD SENSOR_STATE
AND B00000010
CMP #0
JPE OXYGEN_OK

When the result equals zero, JPE branches to OXYGEN_OK. When it is not zero, execution continues to the code that starts the aerator.

Exam tip: State both the bit being tested and the meaning of the result. “The mask isolates bit 1; a non-zero result means the oxygen-low input is active.”

Switch an Actuator On with OR

The aerator is controlled by output bit 0. To make this bit 1 without changing the other actuator states, use OR with B00000001.

00001000 OR 00000001 = 00001001
LDD CONTROL_STATE
OR B00000001
STO CONTROL_STATE

The warning beacon at bit 3 remains on, and the aerator at bit 0 is also switched on.

Load–modify–store: load the current output byte, apply a mask in ACC, then store the modified byte back to the device-control location.

Switch an Actuator Off with an AND Clear Mask

When the next sensor reading shows that oxygen is no longer low, output bit 0 should be cleared. The mask contains 0 at bit 0 and 1 everywhere else:

00001001 AND 11111110 = 00001000
LDD CONTROL_STATE
AND B11111110
STO CONTROL_STATE

The aerator is switched off while the warning beacon remains unchanged.

Common mistake: AND B00000001 would preserve only bit 0 and clear every other output bit. The correct clear mask uses 1s in all positions that must remain unchanged.

Complete Sensor-to-Actuator Response

The following original fragment checks the low-oxygen input and updates the aerator output. It is written to show the decision clearly rather than to imitate a particular processor implementation.

CHECK_OXYGEN:
    LDD SENSOR_STATE
    AND B00000010
    CMP #0
    JPE OXYGEN_NORMAL

OXYGEN_LOW:
    LDD CONTROL_STATE
    OR B00000001
    STO CONTROL_STATE
    JMP CHECK_LEVEL

OXYGEN_NORMAL:
    LDD CONTROL_STATE
    AND B11111110
    STO CONTROL_STATE

CHECK_LEVEL:
    LDD SENSOR_STATE
    AND B00000100
    CMP #0
    JPE LEVEL_NORMAL

LEVEL_LOW:
    LDD CONTROL_STATE
    OR B00000010
    STO CONTROL_STATE
    JMP READ_AGAIN

LEVEL_NORMAL:
    LDD CONTROL_STATE
    AND B11111101
    STO CONTROL_STATE

READ_AGAIN:
    JMP CHECK_OXYGEN
Input condition Input mask Output action Output mask
Oxygen low at bit 1 B00000010 Set or clear aerator bit 0 B00000001 / B11111110
Water level low at bit 2 B00000100 Set or clear refill-pump bit 1 B00000010 / B11111101

Deterministic Control Is Usually Safer Than Toggling

XOR is useful when the intention is genuinely to reverse a state. However, a safety-related actuator should usually be set or cleared explicitly.

Operation Effect Suitability for a required actuator state
OR with a set mask Makes the selected bit 1. Suitable when the output must definitely be on.
AND with a clear mask Makes the selected bit 0. Suitable when the output must definitely be off.
XOR with a toggle mask Reverses the selected bit. Use only when reversal is the intended behaviour and the current state is known.
Control-system mistake: Repeatedly applying XOR while a sensor condition remains true would alternate an actuator on and off. For stable control, set the required state explicitly.

Interactive: Device Register Simulator

Toggle sensor conditions, choose one response, and step through the program's read–mask–decide–update sequence. The existing widget identifier and asset paths have been retained.

Input register: SENSOR_STATE

Current program stage Read SENSOR_STATE

Load the latest sensor byte before testing the selected condition.

Stage 1 of 4
Output register: CONTROL_STATE

Warning beacon is on; other mapped actuators are off.

Masked sensor result

Result: 00000010

Decision Condition active
Output operation OR B00000001
Sensor bit 2 Water level low
Sensor bit 1 Oxygen low
Sensor bit 0 Temperature high

Common Mistakes and Misconceptions

  • Counting bit positions from the left instead of placing bit 0 on the right.
  • Confusing the input/status byte with the output/control byte.
  • Using OR to test a sensor bit and accidentally changing ACC.
  • Using a single-1 AND mask to clear an actuator bit, which clears the other outputs.
  • Forgetting to reload the control byte after AND has replaced ACC with a masked sensor result.
  • Forgetting to store the modified output byte back to the control location.
  • Claiming that the sensor decides or acts. The sensor reports; the program decides; the actuator acts.
  • Using XOR repeatedly for an output that must remain steadily on or off.
  • Assuming feedback means the output command rather than a later measurement of the system.

Exam Tips

Separate input testing from output changing

First state which sensor bit is isolated. Then state which actuator bit is set or cleared.

Explain why the other bits are preserved

OR uses 0s in unaffected positions. An AND clear mask uses 1s in unaffected positions.

Trace ACC carefully

After an input mask, ACC contains the masked result—not the original sensor byte and not the output byte. Reload the correct byte before modifying an actuator command.

Practice

Interpret the registers

Use the aquaculture mappings above.

  1. Explain the active sensor conditions in SENSOR_STATE = 00011101.
  2. Explain the active actuators in CONTROL_STATE = 00010110.
  3. Construct a mask to test whether the service hatch is open.
  4. Construct a mask to switch on the cooling fan.
  5. Construct a mask to switch off the refill pump while preserving every other output.

Write assembly fragments

  1. Test bit 3 of SENSOR_STATE and jump to FILTER_OK when clear.
  2. Set warning-beacon bit 3 in CONTROL_STATE.
  3. Clear cooling-fan bit 2 in CONTROL_STATE.
  4. Test whether either oxygen-low bit 1 or water-low bit 2 is active using one mask.

Design a new device map

Create an 8-bit input map and an 8-bit output map for an automated art-storage room. Include at least three sensors and three actuators. Then write one test mask, one set mask and one clear mask.

Review

Task Typical operation Essential explanation
Read a sensor flag AND with a single-1 mask Non-zero means the selected input is active.
Switch an actuator on OR with a single-1 mask The selected output becomes 1; other outputs remain unchanged.
Switch an actuator off AND with a single-0 clear mask The selected output becomes 0; other outputs remain unchanged.
Respond continuously Repeat read–test–update Later sensor readings provide feedback for the next decision.
Final check: Can you explain the complete chain from an input bit, through a mask and comparison, to a safely updated output bit?