Electronic – Fastest way to toggle a bit in ASM


What's the fastest way to toggle a bit1 in MPASM for the 14-bit enhanced instruction set? (I'm working with a PIC16F1829)

The code has to be standalone – I mean that it can be called on any moment, without knowing the value of the bit on that moment.

Key criteria is speed here: a program with fewer instruction cycles is better. The number of instructions cycles is calculated as the number when the bit is 0 + the number when the bit is 1, divided by 2.

1: with toggle I mean that the code has to be similar to pin=!pin in C

Best Answer

You could try the following using an XOR:

movlw 0x01 ; move 0x01 to W register
xorwf lat, F ; XOR W with port & store result in port latch

An exclusive OR operation will preserve the values in bits where the bits in the working register are set to zero and invert the values where it is set. So you could also use the same technique to toggle multiple bits.