← All exercises

Stateful Masked Move

medium

Copies selected source bits into a retained word while preserving its previous unselected bits.

What it teaches

A masked move is a read-modify-write: the destination is also an input. A 1 mask bit updates from Source; a 0 preserves the destination's previous bit. Example: Source 5 with Mask 5 first produces 5. Changing Source to 2 and Mask to 2 then produces 7 because bit 1 updates while bits 0 and 2 remain stored. Unlike Set Selected Bits, a selected source bit can clear as well as set.

Inputs and outputs

TagTypeDefaultDescription
In_SourceDINT5New values for the selected destination bits
Cfg_MaskDINT5A 1 updates from Source; a 0 preserves the destination
Out_WordDINTRetained destination word

Required behavior

  • A new instance starts with Out_Word equal to 0.
  • For each bit, a 1 in Cfg_Mask copies the matching In_Source bit into Out_Word every scan.
  • For each bit, a 0 in Cfg_Mask preserves the previous value of that Out_Word bit.
  • Selected source bits can set or clear destination bits.
  • A mask of 0 changes nothing, while a mask of -1 replaces all 32 bits of Out_Word.