5.29 __xch Intrinsic Function

The IAR __xch intrinsic function provides access to the xch instruction, available on AVRxm devices. The xch (Exchange) instruction swaps the memory contents at the address held by the Z register with the Rd register specified.

Suggested Replacement

There is no MPLAB XC equivalent built-in function; however, the instruction can be inserted explicitly using in-line assembly code.

Use in-line assembly to directly write an xch instruction.

Caveats

None

Examples

Consider migrating IAR code such as:
unsigned char exchange(unsigned char regD, unsigned char * Zptr) {
    unsigned char swapped = __xch(regD, Zptr);
    return swapped;
}
to MPLAB XC8 code similar to:
#include <xc.h>

unsigned char xch(unsigned char v, unsigned char* addr) {
  __asm__("movw r30, %[ADDR] \n\t"
          "xch Z, %[Rd] \n\t"
          : [Rd] "+r" (v), "+m" (*addr)
          : [ADDR] "r" ((unsigned int)addr)
          : "r30", "r31");
  return v;
}

unsigned char exchange(unsigned char regD, unsigned char * Zptr) {
    unsigned char swapped = xch(regD, Zptr);
    return swapped;
}

Further Information

See the In-line Assembly section in the MPLAB XC8 C Compiler User's Guide for AVR MCUs for more information on adding in-line assembly.