assemblybit-manipulationbitwise-operatorsboolean-logicbitwise-xor

# How to XOR on a CPU that doesn't have an XOR instruction

This is more of a just for fun question. I’m working on a SC61860 CPU, which is an 8-bit CPU for a Sharp PC-1360 Pocket Computer from 1987 (also used in PC-1401 & 1403’s). Its instruction set doesn’t actually include an XOR. It does have AND, OR, compare, subtraction and addition instructions.

I have tried a few variations of ANDing and ORing values to get result that XOR would produce, but no luck. I was hoping to avoid comparing, but it looks like I don’t have a choice.

If you're interested, you can check out the instruction set (alternatives github and PC-1350 reference).

BTW, this CPU has been great to learn assembly on. Nice and simple, and slow enough (768kHz) that machine language is noticeably faster then using the computers built in BASIC ;) I usually program in C/C++/Java. Assembly has been a breath of fresh air.

Solution

• From boolean algebra we know that:

A XOR B = (NOT(A) AND B) OR (A AND NOT(B))

Update: Thank @Brett Hale, @slebetman, as the CPU surprisingly not supporting the NOT instruction, it can be emulated by arithmetic negation and subtraction, assuming 2's complement negatives representation):

NOT(A) = (-1) - A

Or in case of different negative representation the -1 can be replaced by the corresponding storage type maximum value (i.e. 255 for 8 bit registers or 65565 for 16 bit registers).