Exchanging Contents Of PIC Memory Cells

Exchanging Contents Of PIC Memory Cells

Postby Joseph Watson » Sun May 03, 2015 10:29 pm

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
NCR once refused to hire me because I was too short. I'm still waiting on my growth spurt.
User avatar
Joseph Watson
 
Posts: 49
Joined: Sat May 31, 2014 8:06 pm
Location: Ohio, USA
PIC experience: Experienced Hobbyist

Re: Exchanging Contents Of PIC Memory Cells

Postby 1and0 » Mon Jun 27, 2016 1:04 am

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
1and0
Verified identity
 
Posts: 3
Joined: Wed May 28, 2014 1:15 am
PIC experience: Professional 5+ years with MCHP products

Re: Exchanging Contents Of PIC Memory Cells

Postby ric » Mon Jun 27, 2016 3:31 am

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.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Exchanging Contents Of PIC Memory Cells

Postby 1and0 » Mon Jun 27, 2016 3:40 am

Agree on banking. Regarding the ",f" destination bit --> http://www.microchip.com/forums/FindPost/876733
1and0
Verified identity
 
Posts: 3
Joined: Wed May 28, 2014 1:15 am
PIC experience: Professional 5+ years with MCHP products

Re: Exchanging Contents Of PIC Memory Cells

Postby Joseph Watson » Mon Jun 27, 2016 3:30 pm

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.
NCR once refused to hire me because I was too short. I'm still waiting on my growth spurt.
User avatar
Joseph Watson
 
Posts: 49
Joined: Sat May 31, 2014 8:06 pm
Location: Ohio, USA
PIC experience: Experienced Hobbyist


Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 9 guests

cron