XOR on the ST6

The ST6 microcontroller is small and cheap, but it has a very limited CPU. In particular, it is missing my favorite instruction. Here's how to fix it.

A + B = A XOR B + 2(A AND B) (take my word for it)

So A XOR B = A + B - 2(A AND B)

The ST6 can only do arithmetic on a single register, so it is easier to evaluate this if it is arranged

A XOR B = -(2(A AND B) - A - B).

Writing this in a form the assembler can deal with gives

	STA	temp
	AND	B
	ADD	A
	SUB	temp
	SUB	B
	DEC	A
	COM	A
I like to end this with DEC/COM instead of the more usual COM/INC partly because it confuses people, and partly because you can get XNOR by simply chopping the last instruction.