Exchanging Contents Of PIC Memory Cells
Posted: 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:
In PIC assembly language, it would likely look something like this:
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:
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:
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:
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