Page 1 of 1

Exchanging Contents Of PIC Memory Cells

PostPosted: Sun May 03, 2015 10:29 pm
by Joseph Watson
Exhanging The Contents Of Two Memory Locations
Without Using Temporary Storage In PIC Assembly Language


The classic method of exchanging the contents of two memory locations uses a temporary storage location such as in this generalized example:

Code: Select all
    Temp = A
    A = B
    B = Temp


In PIC assembly language, it would likely look something like this:
Code: Select all
    MOVF    A,W
    MOVWF   TEMP,F
    MOVF    B,W
    MOVWF   A
    MOVF    TEMP,W
    MOVWF   B


If we are programming in PIC18 assembly language, we can get W to serve as the temporary storage location because unlike its smaller PIC brethren, a PIC18 has an instruction (MOVFF) that can move data from one memory location to another without affecting W. Therefore, this short sequence does the trick without using any temporary storage in RAM:
Code: Select all
   MOVF    A,W
   MOVFF   B,A
   MOVWF   B


If it is important to avoid using temporary storage in RAM, there is another way to accomplish the same action that can work for the smaller PIC processors. It uses an old programming trick that looks like this generalized example:
Code: Select all
    A = A XOR B
    B = B XOR A
    A = A XOR B


At first glance, this seems like nonsense but it really does work. If we are programming in PIC assembly language, it can look like this:
Code: Select all
    MOVF    B,W
    XORWF   A
    MOVF    A,W
    XORWF   B
    MOVF    B,W
    XORWF   A

Re: Exchanging Contents Of PIC Memory Cells

PostPosted: Mon Jun 27, 2016 1:04 am
by 1and0
It takes only four instructions on any 8-bit PIC devices:
Code: Select all
      movf    B,w
      xorwf   A,w
      xorwf   A
      xorwf   B

http://www.microchip.com/forums/FindPost/790829

Re: Exchanging Contents Of PIC Memory Cells

PostPosted: Mon Jun 27, 2016 3:31 am
by ric
Assuming of course, that no bank selecting is required to access A and B :)
It wouldn't hurt to put ",f" suffixes in the 3rd and 4th instructions too, for clarity.

Re: Exchanging Contents Of PIC Memory Cells

PostPosted: Mon Jun 27, 2016 3:40 am
by 1and0
Agree on banking. Regarding the ",f" destination bit --> http://www.microchip.com/forums/FindPost/876733

Re: Exchanging Contents Of PIC Memory Cells

PostPosted: Mon Jun 27, 2016 3:30 pm
by Joseph Watson
I agree that 1and0 has a very nice piece of code there. Regardless of whether we use the ",F" or not, the result is the same in the object code. It is a good little tool worth tucking into our bag of tricks. How appropriate that this improved bit twiddling trick be submitted by someone called 1and0.

Personally, I never have to add ",W" or ",F" to the end of any of my instructions because I have a macro file that I include on every assembly language source that essentially creates an alternate instruction set for my PIC18 projects. The alternate instructions are designed so that I never have to write ",F", "W", or ",A" at the end of a command. In effect, the ",W" or ",F" is specified by my version of each opcode and the ",A" is handled automatically. Access Memory is used anytime that it can be and the BSR is used to reach any RAM cells that are outside the Access Memory range.

I doubt many others would ever want to use my instruction set but, frankly, I was having a lot of trouble remembering Microchip's mnemonics when I started. It seems to me they do not have any common philosophy to the way in which they were created. That caused me to have to devote more effort to remembering the commands and that was slowing me down. One downside is that whenever I care to share my code with others, I first have to translate each of my commands to the standard instruction set.