# Assembler PIC programming, is this correct? [closed]

The question is:

Write the following pseudo in assembler code:

if portC bit 3 == 0
(switch content of var1 and var2)
else
(add var1 with var2 and place the result in var1)


btfsc portC,3
movf var1,w
movwf temp
movf var2,w
movwf var1
movf temp,w
movwf var2
return



Since I dont know the right answer, can you please help me and tell me if I wrote it correct?

## closed as off-topic by William Morris, Jeff Vanzella, palacsint, Jamal♦, svickAug 15 '13 at 2:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After your code is working you can edit this question for reviewing your working code." – William Morris, Jeff Vanzella, palacsint, Jamal, svick
If this question can be reworded to fit the rules in the help center, please edit the question.

A general disclaimer to this answer should be that I don't know the hardware you're working with, but most of this should be applicable to many architectures.

## Potential optimization #1

Your code seems correct, but you can make it shorter and potentially more efficient by moving the first memory load before the branch:

movf var1, w     ; We're going to load one of the variables anyway

btfsc portC, 3

movwf temp
movf var2, w
movwf var1
movf temp, w
movwf var2
return



Performing the load before the branch might mean the branch instruction could be executed at the same time as the load, since they don't share any data.

## Potential optimization #2

Using a XOR swap should be more efficient, like so:

movf  var1, w   ; load from var1 to w
xorwf var2, w   ; w = var1 xor var2
xofwf var2, f   ; var2 = w xor var2 = (var1 xor var2) xor var2 = var1
xorwf var1, f   ; var1 = w xor var1 = (var1 xor var2) xor var1 = var2


This should have several advantages:

1. There is no temporary variable and therefore no reads or writes are associated with it.
2. The first XOR doesn't perform a memory write so it should be completed faster.
3. No moving memory around until the variables are assigned their final values, so if the hardware is able to cache the values of var1 and var2 from the first two instructions, the last two could gain a performance boost.

I should state again that I don't know the hardware, so this is mostly educated speculation, but my code should be faster even without assumptions 2 and 3.

Furthermore, there's nothing stopping you from combining both ideas, of course. You probably should.

• I see, that is an interesting method. But was the one that I had written correct too? – visaxx Aug 14 '13 at 14:13
• @visaxx: Added a few more thoughts. I just looked up the assembly language reference for your device and the code you wrote does seem correct. – idoby Aug 14 '13 at 14:22