# `bfins`

[M68K Documentation](https://asm-editor.specy.app/documentation/m68k.md) › [Instructions](https://asm-editor.specy.app/documentation/m68k/instruction.md)

Web page: https://asm-editor.specy.app/documentation/m68k/instruction/bfins

**Insert bit field** · 68020 · 68000 with opt bit

Category: Logic

Inserts the low bits of a data register into a bit field, leaving the bits around it as they were.

Select `opt p=68020`, or use `opt bit` with the 68000 or 68010, before any code or data

## Flags

| Flag | Effect |
| --- | --- |
| X | not changed |
| N | depends on the result |
| Z | depends on the result |
| V | always 0 |
| C | always 0 |

## Forms

- `bfins Dn, <ea>{offset:width}`
  - Destination: Dn, (An), d(An), d(An,Xn), abs

## Operands

- [`Dn`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/direct.md): A data register, `d0` to `d7`.
- [`<ea>`](https://asm-editor.specy.app/documentation/m68k/addressing-mode.md): An operand using one of the addressing modes listed for this form. The list determines whether it can be a register, memory or an immediate value. With `opt p=68020`, an allowed `d(An,Xn)` or `d(PC,Xn)` mode also permits the corresponding 68020 indexed and memory indirect forms. See [68020 addressing](https://asm-editor.specy.app/documentation/m68k/addressing-mode/68020.md).
- [`{offset:width}`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/bit-fields.md): A bit field: `width` bits that start `offset` bits below the operand’s most significant bit, written immediately after the operand. For example, `d0{24:8}` is the low byte of `d0`. A literal offset is 0 to 31 and a literal width is 1 to 32. Either may instead be a data register, as in `{d1:d2}`.

## Description

The field’s width determines how many source bits are used. For example, `bfins d1, (a0){0:5}` writes the low 5 bits of `d1` into the highest 5 bits of the byte at `a0`.

N copies the highest bit of the value inserted, and Z is set if that value is zero. V and C are cleared, and X is unchanged. [bfextu](https://asm-editor.specy.app/documentation/m68k/instruction/bfextu.md) extracts the value again.

Write the field as `{offset:width}` immediately after its operand. It selects 1 to 32 bits, counted from bit 31 of a data register or bit 7 of the byte at the memory address. The offset and width may each be a constant or a data register. See [bit fields](https://asm-editor.specy.app/documentation/m68k/addressing-mode/bit-fields.md) for their ranges, register values and wrapping rules.

## Example

```m68k
opt p=68020
* --- Build a 16-bit colour from its red, green and blue ---
lea color, a0
moveq #31, d0
bfins d0, (a0){0:5} ; red, in the first 5 bits
moveq #0, d0
bfins d0, (a0){5:6} ; green, in the next 6
moveq #12, d0
bfins d0, (a0){11:5} ; blue, in the last 5
move.w (a0), d1 ; d1 = 63500, which is %1111100000001100
bra END

color:
    dc.w 0

END:
```
