please dont rip this site

PIC Math and SX Math Microcontroller Bit Method

This is a clever (but confusing) trick. It only works on the PIC -- generally there are less-confusing ways to do it on other processors.

swap w and f without a temp register

You have a value (A) in W that you want to swap with the value (B) in some file register X,

  ; Swap (W,X):
  ; from Robin Abbott of Forest Electronic Developments
  ; currently, W contains A. X contains B.
  xorwf X,w ; W := A^B; X = B
  xorwf X   ; W = A^B, X := B^(A^B) = A
  xorwf X,w ; W := (A^B)^A = B; X = A
  ; now W contains B. X contains A.

move a value from X to Y without messing up W

This just builds on the above routine:

  ; X contains A. W contains B.
  movwf Y ; Y := B
  movf  X,W ; W = A
  ; Swap (W,Y):
    xorwf Y,w ; W := A^B. Y = B
    xorwf Y   ; W = A^B.  Y := B^(A^B) = A
    xorwf Y,w ; W := (A^B)^A = B; Y = A
  ; X has never been changed -- it still contains B.
  ; now W contains B (just as at the beginning). Y contains A.

Clever, but it is shorter and faster (not to mention less confusing) to use a temporary variable:

  ; X contains A. W contains B.
  movwf temp
  movf  X,W ; W = A
  movwf Y
  movf  temp,W
  ; X has never been changed -- it still contains B.
  ; now W contains B (just as at the beginning). Y contains A.

swap two variables without a temp register

If you have some value (A) in some file register X, and some value (B) in a different file register Y, and you want to swap them, you can do this:

    ; Swap (X,Y)
    ; from kagato-icss-
    ; currently X contains A. Y contains B.
    MOVF  X,W ; W:=A
    XORWF Y,W ; W:=A^B
    XORWF X,F ; X:=((A^B)^A)=B
    XORWF Y,F ; Y:=((A^B)^B)=A
    ; now X contains B. Y contains A.

details

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:
movwf Temp
movfw sp        ; Stack pointer
movwf FSR    ; Point to it
movfw Temp
movwf INDF

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:

  movwf FSR
  movfw sp
  xorwf FSR
  xorwf FSR,w
  xorwf FSR
  movwf INDF

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

(In ____ notation that would be:

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

)

related:

+
file: /Techref/microchip/math/bit/swap.htm, 4KB, , updated: 2004/8/19 17:51, local time: 2024/12/21 07:51,
TOP NEW HELP FIND: 
3.145.99.15:LOG IN

 ©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="http://sxlist.com/techref/microchip/math/bit/swap.htm"> 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 sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .