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
goto add
movwf temp
movf var2, w
movwf var1
movf temp, w
movwf var2
return
add addwf var2, f
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:
- There is no temporary variable and therefore no reads or writes are associated with it.
- The first XOR doesn't perform a memory write so it should be completed faster.
- 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.