# FLL — Fill

Source: https://rungs.dev/docs/ladder-logic/instructions/fll

> FLL in rungs.dev writes one value into every element of a run of an array. Use it to clear a buffer, preset a recipe, or zero a structure in one rung.

Writes one value into every element of a run of an array.

## What it does [#what-it-does]

`FLL` is the fast way to clear or preset a block of memory: zero a buffer before a new batch,
set every alarm limit to a default, blank a display array.

Like [`COP`](https://rungs.dev/docs/ladder-logic/instructions/cop-cps), it runs on **every scan the rung is true** — not once per rising edge.

## Operands [#operands]

| Name     | Type                                                                   | Notes                                        |
| -------- | ---------------------------------------------------------------------- | -------------------------------------------- |
| `source` | `DINT`, `INT`, `SINT`, `REAL`                                          | The value to write. A tag or a number        |
| `dest`   | `DINT`, `INT`, `SINT`, `REAL`, `STRING`, `TIMER`, `COUNTER`, `CONTROL` | One element — where the run starts           |
| `length` | `DINT`, `INT` or `SINT`                                                | How many elements to fill. A tag or a number |

`length` clamps to the destination, exactly as `COP`'s does, and never faults.

## Converting on the way in [#converting-on-the-way-in]

For an ordinary array, the source is **converted** to the destination's type once, then written
into every element — the same rule [`MOVE`](https://rungs.dev/docs/ladder-logic/instructions/move) uses.

| Source       | Destination | Result        | Why                            |
| ------------ | ----------- | ------------- | ------------------------------ |
| `2.5` (REAL) | `DINT[]`    | `2`           | Halves round to even           |
| `3.5` (REAL) | `DINT[]`    | `4`           | Halves round to even           |
| `300`        | `SINT[]`    | `44`          | Wrapped to 8 bits, not clipped |
| `70000`      | `INT[]`     | `4464`        | Wrapped to 16 bits             |
| `3.0e9`      | `DINT[]`    | `-1294967296` | Wrapped to 32 bits             |

This is conversion, not saturation: a value too big for the type wraps round rather than
sticking at the maximum.

## Filling a structure [#filling-a-structure]

A `TIMER`, `COUNTER` or `CONTROL` destination is different: the source's bytes are written
**raw**, with no conversion, over the whole structure — the numbers and the status bits alike.
A `DINT` or a `REAL` fills each 4-byte word; a `SINT` or an `INT` repeats its own 1 or 2 bytes,
so a `SINT` holding `-2` fills every byte with the same pattern.

`FLL(0, Dwell, 1)` is the useful case: it zeroes the whole timer, preset included.

A `STRING` is filled the same way, over all of its bytes: `FLL(0, Msg, 1)` empties it and
clears every character. `FLL(65, Msg, 1)` does **not** fill it with `A`s: `65` lands in `.LEN`
as well, and only every fourth character is an `A`. To fill a `STRING` with text, use
[`MOVE`](https://rungs.dev/docs/ladder-logic/instructions/move).

To fill only the characters, name the first one: `FLL(0, Msg.DATA[0], 82)` sets all 82 to `0`
and leaves `.LEN` alone. Here each character is one `SINT`, so the value is converted, as it is
for any number array: `FLL(65, Msg.DATA[0], 3)` writes `AAA`. A `length` past the characters
runs on into the two spare bytes and stops at the end of the `STRING`, or of the whole array
when `Msg` is an element of one.

> **WARNING**
>
> **FLL(7, Dwell, 1) does not set the preset to 7 and stop there**
>
> It writes `7` into `.PRE` **and** `.ACC`, because both are words of the same structure. And a
> value with a high bit set lands in the status bits too: `FLL(2.5, Dwell, 1)` writes the
> decimal's bit pattern, which switches `.TT` on.
>
> To set just one member, use [`MOVE`](https://rungs.dev/docs/ladder-logic/instructions/move): `MOVE(5000, Dwell.PRE)`.

## Example — Clear a Buffer [#example--clear-a-buffer]

```ladder
XIC(NewBatch)FLL(0,Readings[0],20);XIC(NewBatch)FLL(0,Total,1)
```

## Structured Text [#structured-text]

`FLL` has **no** Structured Text form. In a Structured Text routine, write a `FOR` loop, or use
[`COP`](https://rungs.dev/docs/ladder-logic/instructions/cop-cps) with the replicate idiom.

## Common Mistakes [#common-mistakes]

- Using `FLL` on a timer to set its preset. It fills every member. Use `MOVE`.
- Expecting `300` into a `SINT` array to clip at `127`. It wraps to `44`.
- Naming the bare array — `FLL(0, Readings, 20)`. Name an element: `Readings[0]`.

## Related [#related]

- [Array instructions overview](https://rungs.dev/docs/ladder-logic/instructions/array-instructions)
- [COP and CPS](https://rungs.dev/docs/ladder-logic/instructions/cop-cps) — copy a run instead of filling it
- [MOVE](https://rungs.dev/docs/ladder-logic/instructions/move) — one value into one destination