11

Are there any tricks that can be used to invert the carry flag on the 6800, in as few bytes as possible?

This is the shortest subroutine I've come up with, which is straight forward:

INVC:  BCS CLEAR
       SEC
       RTS
CLEAR: CLC
       RTS
10

Shortest coding (I came up so far):

   Instruction Bytes Cycles
   TPA         1     2       Transfer P(rocessor status) to (accumulator) A
   EOR A,#$01  2     2       Invert bit 0 (carry bit) using exclusive OR
   TAP         1     2       Transfer (accumulator) A back to P(rocessor status)
              ---   ---
               4     6

Of course this will only work if A is available.

Without your solution is a good start. Speed should be the same.


And then Supercat came along with two really nifty ideas using ROL/ROR to get access to carry and flip it:

Solution A:

   Instruction Bytes Cycles
   ROL A       1     2       Shift C(arry) into acc A bit 0, save bit 7 in carry
   EOR A,#$01  2     2       Invert bit 0 (carry bit) using exclusive OR
   ROR A       1     2       Shift bit 0 back into C(arry) and restore bit 7
              ---   ---
               4     6

It is as compact and fast as my solution, but preserves the content of acc A

Solution B:

   Instruction Bytes Cycles
   ROL B       1     2       Shift C(arry) into acc B bit 0
   INC B       1     2       Increment by 1 always flips the lowest bit (*)
   ROR B       1     2       Shift bit 0 back into C(arry)
              ---   ---
               3     6

(* 0 + 1 = 1; 1 + 1 = (1)0. The cool ways of binary)

This as well preserves A (but destroyes B) but saves another byte, while still performing in 6 cycles.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.