# Decoding DD and FD Prefixes

How the DD and FD instruction prefixes work

Instructions with either the DD or FD prefixes affect those instructions that operate against the memory addressed by HL, changing them to use either the IX or IY registers with an offset.

Instructions that refer directly to the HL register will then act directly against either IX or IY. For those that refer to (HL), i.e. the memory pointed to by HL then the instructions use an additional relative offset that's added to either the IX or IY registers, and are written as (IX+d) or (IY+d).

Instructions with the DD prefix use the IX register, whilst the FD prefix uses the IY register.

## DDCB and FDCB Prefixes

The DD and FD prefixes extends though the CB prefix as it does for normal instructions. Just like the CB prefix

The format of the instruction also changes slightly as they change the behaviour of the existing instructions with the CB prefix. These instructions are all four bytes long with the third byte consisting of the offset.

For example: The RLC (HL) is encoded as CB06.

With the DD prefix this becomes RLC (IX+d) but the instruction is formatted as DDCBdd06. With the FD prefix this becomes RLC (IY+d), formatted as FDCBdd06.

Note that the offset d is before the final part of the operand, not after as you might expect.

### Decoder

All of these have either DD or FD as the previous prefix byte and a displacement immediately after them.

76543210
00100001 LD
0010X011 INC DEC
0010xx10 LD
0011010D IncDec
00110110 LD