# 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



## 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