please dont rip this site

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

file: /Techref/scenix/lib/math/bit/swap_sx.htm, 1KB, , updated: 2004/8/19 17:14, local time: 2024/7/14 21:32,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> SX Microcontroller Bit Math Method - swap w and f without a temp register</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!