Mnemonics

EXX

Exchange, Block Transfer and Search Group
Expansion:
EXchange eXtended
Usage:
exx

The EXX instruction provides us with a quick way to swap BC, DE and HL registers with their shadow counterparts. EXX provides some alternative or missing functionality that we could be missing with EX although EXX does not grant us any flexability as it is bound to exchanging these particular registers only.

By using EXX, you can effectively save and restore the state of the main registers, which can be particularly useful in interrupt service routines or when switching contexts.

Z80.CPU instructions
Instruction Description Opcode
EXX EXchange eXtended D9

Example

; Example ; initial values in the main registers ld bc, $1234 ; load BC with $1234 ld de, $5678 ; load DE with $5678 ld hl, $9ABC ; load HL with $9ABC exx ; exchange BC, DE, HL with their alternate counterparts ; The main registers now hold the values of the alternate registers, and vice versa ld bc, $0000 ; load BC with $0000 (this only affects the main register set) ld de, $1111 ; load DE with $1111 (this only affects the main register set) ld hl, $2222 ; load HL with $2222 (this only affects the main register set) exx ; exchange BC, DE, HL with their alternate counterparts again ; the main registers now hold the values $1234, $5678, $9ABC again, ; and the alternate registers hold the values $0000, $1111, $2222

See also