SX Microcontroller Bit Math Method

Swap w and f without a temp register

Robin Abbott of Forest Electronic Developments says:

This might be of use to someone. Recently I had a project where a subroutine took a value in W and saved to a software stack:
	mov	Temp, W
	mov	W, sp	; Stack pointer
	mov	FSR, W	; Point to it
	mov	W, Temp
	mov	0, W

Trouble is it uses a temporary variable which I didn't have (it is in an interrupt). This alternative which makes use of XOR uses no temporary variable at the expense of 1 extra word:

	mov	FSR, W
	mov	W, sp
	xor	FSR, W
	xor	W, FSR
	xor	FSR, W
	mov	0, W

You can also use this to swap two variables (say x and y) without a temporary variable leaving X (or Y if order is reversed) in W.

	mov	W, x	; Get X
	xor	y, W	; Y is now X^Y
	xor	W, y	; W is now (X^y)^X==Y  (say OldY)
	mov	x, W	; Now X is OldY
	xor	y, W	; finally Y is (OldX^Y)^Y==OldX

I think this may be an old technique - I have vague memories of something similar from the pre-history of programming, but only found a use for