SX Microcontoller 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


file: /techref/scenix/lib/math/bit/swap_sx.htm, updated: 9/14/00 3:08:00 PM, More.., Top, Search

Did you find what you needed?

After you find an appropriate page, you are invited to your directly to this massmind site! (HTML welcomed!):

Link? Put it here: 
if you want a response, please enter your email address: 
SXlist members can LOGIN to post questions, add public or private comments, links or even new pages(!). To start, fill out the form.

AND WE HAVE A WINNER!

In the SXList.com Video Design Contest

SX MASTERS: Eric Smith and Richard Ottosen's SERVID entry clearly meets the contest requirements.
It's an intellegent RS232 video terminal (4x20 character display) in one chip. See the write up here..