Skip to content

Element Reference: Math, Data and Program Control ​

Last updated

License feature: Ladder Logic

All the instructions on this page are output instructions that pass power through unchanged: they do their work only on a scan when power in is true, and they never make a rung false. Put the conditions before them.

Math ​

MnemonicNameOperandsDestination =
ADDAddSource A, Source B, DestinationA + B
SUBSubtractSource A, Source B, DestinationA − B
MULMultiplySource A, Source B, DestinationA × B
DIVDivideSource A, Source B, DestinationA ÷ B, or 0 if B is 0 (no fault)
MODModuloSource A, Source B, DestinationRemainder of A ÷ B, or 0 if B is 0
SQRSquare RootSource A, Destination√A (a negative A gives 0 in an N or F tag)
NEGNegateSource A, Destination−A
ABSAbsolute ValueSource A, DestinationA without its sign

The result is a decimal number; an N destination rounds it (7 ÷ 2 → 4). These run on every scan with power in: an ADD with an XIC in front adds on every scan the input is on. To add once per event, use an OSR.

CPT — Compute ​

OperandNotes
ExpressionArithmetic, e.g. {3:Line1/Speed} * 60 / 1000 (placeholder e.g. {1:Tag/Path} * 2 + 10)
DestinationWhere the result goes
  • Refer to a device tag as {deviceId:tagPath}. The device id is the number shown as Device #3 when you browse a tag on another element.
  • Only numbers, spaces and + - * / % ( ) . < > = ! & | ^ ~ ? : and e are allowed after tag substitution. No function names (no sqrt), and Task/Program Tag addresses can't be used.
  • A comparison result ({3:Temp} > 80) is true/false, not a number, and is not written.
  • An invalid expression leaves the Destination unchanged, with no error.
  • Tags in the expression are read at the start of the scan, like every other operand. A negative value works anywhere, including after a minus: 100 - {3:Offset} with Offset = -5 gives 105.

SCP — Scale with Parameters ​

OperandDefault
Input—
Input Min / Input Max0 / 100
Output Min / Output Max0 / 100
Destination—

Destination = (Input − Input Min) × (Output Max − Output Min) ÷ (Input Max − Input Min) + Output Min. The four limits are fixed numbers. The result is not clamped: an input outside the range extrapolates. If Input Min equals Input Max, the result is Output Min.

Example: a 4–20 mA pressure transmitter read as raw counts 6400–32000 for 0–10 bar → Input Min 6400, Input Max 32000, Output Min 0, Output Max 10, Destination F_PressureBar.

Data Move ​

MnemonicNameOperandsBehavior
MOVMoveSource, DestinationCopies Source to Destination as a number (text becomes 0 — use DTOS/STOD for text)
MVMMasked MoveSource, Mask, DestinationDestination = (Source AND Mask) OR (Destination AND NOT Mask), on whole numbers. Set the Mask explicitly — an unset mask is 0 and nothing changes
COPCopySource, Destination, LengthCopies Length consecutive elements: Source N7:0 to Destination N7:10 with Length 3 copies N7:0–2 to N7:10–12. A named device tag Buffer counts as Buffer, Buffer[1], Buffer[2]…
FLLFillSource, Destination, LengthWrites the Source value into Length consecutive Destination elements
CLRClearDestinationWrites 0

Bitwise ​

Values are rounded to whole numbers and treated as 32-bit integers.

MnemonicNameOperandsDestination =
ANDBitwise ANDSource A, Source B, DestinationA AND B
ORBitwise ORSource A, Source B, DestinationA OR B
XORBitwise XORSource A, Source B, DestinationA XOR B
NOTBitwise NOTSource A, DestinationNOT A (NOT 0 = −1)
BSLBit Shift LeftSource A, Source B, DestinationA shifted left by B bits (B 0–31)
BSRBit Shift RightSource A, Source B, DestinationA shifted right by B bits, filling with 0

BSL and BSR shift a number; they are not the RSLogix bit-array shift registers with a bit input.

Conversion ​

MnemonicNameOperandsBehavior
TODTo BCDSource, Destination1234 → 0x1234 (4660). The sign is dropped
FRDFrom BCDSource, Destination0x1234 → 1234
DTOSDINT to StringSource, DestinationWhole number to text, e.g. into ST_Batch
STODString to DINTSource, DestinationText to whole number ("42 kg" → 42, non-numbers → 0)

Program Control ​

MnemonicNameSettingBehavior
JSRJump to SubroutineRoutine — Select a routine… from this programWhen the rung has power at the JSR, the routine runs after this rung finishes. An SFC chart can be called too
SBRSubroutine—Marker only; does nothing
RETReturn—With power, the current routine stops after this rung
JMPJump to LabelLabel (e.g. loop_start)With power, the scan continues at the rung containing the matching LBL in the same routine. A missing label does nothing
LBLLabelLabelMarks a jump target; does nothing
FORFor LoopIndex Tag, Initial, Terminal, StepWith power: sets Index to Initial and runs the rungs down to the matching NXT, then adds Step and repeats while Index ≤ Terminal (≥ for a negative Step). Without power, or if Initial is already past Terminal, those rungs are skipped
NXTNext—Closes the FOR loop above it. FOR/NXT pairs nest like brackets
MCRMaster Control Reset—See below

Note: A loop that runs more than 10,000 times in one scan faults the task (FOR loop in "…" ran more than 10000 times in one scan). A Step of 0 skips the loop.

Warning: A JMP to an earlier rung repeats those rungs within the same scan. If the jump rung stays true, the scan never ends. A 1-second watchdog catches this: the scan is cut off, the task faults with Watchdog: the scan was still running after 1000 ms, at rung N of "…" and stops itself, and the appliance stays responsive. Only jump forward, or make sure the condition goes false inside the loop, so scans stay fast.

Each routine has its own labels, so JMP always finds the right target in the routine it runs in, including after a JSR returns to the caller.

MCR fences off a zone with a pair of MCR elements. The first one the scan reaches is the fence: if its own rung is false, every OTE between it and the second MCR is forced off (writes 0) and passes no power, for that scan. If the fence rung is true, the zone runs normally. The second MCR always closes the zone, whatever its own rung does — it isn't itself conditional, the same as an Allen-Bradley End MCR. OTL, OTU, timers and math inside the zone run normally either way. An MCR with no matching second one stays open for the rest of the scan.

See also ​

Scada.Ink Help Center