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
I have assumed that the bit you want toggle is part of a register that has "don't cares" in the other bits and therefore swap nibbles would work. You never said it couldn't work like this and if you are so short of registers that you need the other bits then a code rewrite or MCU upgrade is recommended.
You'd need to setup 11110000 (any pattern would do providing bn does not equal bn+4) initially but after that every time you want to invert one of the bits you use: -
It's a one cycle operation and doesn't need 1 in the w register setting up.
Of course, in the rest of the programme you'd have to adhere to this method of changing the bit BUT again, you haven't said that you can't - in other words BCF or BSF instructions are outlawed.
I thought about rotates but they rotate thru carry and this would probably corrupt stuff - I haven't written PIC code in over 20 years so forgiveness ought to be at the forefront of your mind when down-voting!!!