LFL and LFU — LIFO Stack

LFL and LFU in rungs.dev build a last-in first-out stack in an array — push a value on top, take the newest one back off. Use them for undo histories and nesting.

LFL

Source

source

(DN)

Array

array

(EM)

Control

control

Length

length

Position

position

Puts a value on top of a stack on each rising edge, and stops once the stack holds .LEN of them.

LFU

Array

array

(DN)

Destination

destination

(EM)

Control

control

Length

length

Position

position

Takes the newest value off the top of a stack on each rising edge, leaving the others where they are.

What a LIFO is

LIFO means last in, first out — a stack, like a pile of plates. The last value you put on is the first one you take back off.

Reach for a stack when the most recent thing matters most: the last alarm raised, the step you were on before this one, an undo history.

LFL and LFU are the FFL and FFU of a stack. Everything about them is the same — the operands, the rising edge, the shared CONTROL tag, .DN for full and .EM for empty — except which element the unload takes.

Operands

LFL

NameTypeNotes
sourcenumericThe value to push on. A tag or a number
arraynumeric[]Where the stack starts — Buf or Buf[2]
controlCONTROLThe stack's state
lengthnumberHow many slots the stack has
positionnumberHow many are in use at start-up. Usually 0

LFU

NameTypeNotes
arraynumeric[]Where the stack starts
destinationnumericWhere the popped value goes
controlCONTROLThe same control tag the LFL uses
lengthnumberSame as the LFL's
positionnumberSame as the LFL's

The array can be DINT, INT, SINT or REAL, and a value of a different type converts on the way in and out.

How It Works

LFL is FFL exactly: it writes at .POS and moves .POS up one.

LFU differs in one line. Where FFU takes the element at the start and shifts everything down, LFU takes the element at .POS - 1 — the top of the stack — zeroes that slot, and leaves every other element where it is.

InstructionTakes the element atShifts the others?
FFUthe startYes, down one
LFU.POS - 1No

That is the whole difference, and it is why LFU is the faster of the two on a long buffer: it moves one element instead of all of them.

Example — Last Alarm First

Every alarm is pushed on; Acknowledge clears the newest one first.

0

AlarmRaised

LFL

Source

AlarmCode

(DN)

Array

Stack

(EM)

Control

SCtl

Length

8

Position

0

1

Acknowledge

LFU

Array

Stack

(DN)

Destination

TopAlarm

(EM)

Control

SCtl

Length

8

Position

0

2

SCtl.EM

AllClear

Common Mistakes

  • Expecting LFU to shift the array. It does not — only the element it took is cleared.
  • Mixing a LFL with an FFU on one control tag. That is a queue and a stack disagreeing about which end is which.
  • Everything on the FFL and FFU list applies here too.

On this page