Exchanges

Exchange registers

These instructions exchange values between registers.

76543210
 
\(AF \longleftrightarrow AF'\)
EX AF, AF'
0000100008
 
\(BC \longleftrightarrow BC', DE \longleftrightarrow DE', HL \longleftrightarrow HL'\)
EXX
11011001D9
 
\(DE \longleftrightarrow HL\)
EX DE, HL
11101011EB
 
\(H \longleftrightarrow (SP+1), L \longleftrightarrow (SP)\)
EX (SP), HL
11100011E3
 
\(IX_h \longleftrightarrow (SP+1), IX_l \longleftrightarrow (SP)\)
EX (SP), IX
11011101DD
11100011E3
 
\(IY_h \longleftrightarrow (SP+1), IY_l \longleftrightarrow (SP)\)
EX (SP), IY
11111101FD
11100011E3

EX AF, AF' (0x08) allows the programmer to switch between the two pairs of Accumulator flag registers.

EX DE, HL (0xEB) exchanges the values between those two registers.

EXX (0xD9) allows the programmer to switch BC, DE and HL and BC', DE' and HL' register pairs.

Internally these instructions toggles a flip-flop which determines which register or register set is the active one. This minimises how long the instruction takes as no data is transferred - just a single bit changes state.

EX (SP),HL exchanges HL with the last value pushed on the stack.

Flags Affected
None.
Opcode Matrix
AF'HLIXIYBC',DE',HL'
AF
EX AF, AF'
0814




DE

EX DE, HL
EB14



(SP)

EX (SP), HL
E3119
EX (SP), IX
DDE3223
EX (SP), IY
FDE3223

BC,DE,HL




EXX
D914
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory