Best way to complement a bit?

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As a relative newbie to AVR, I miss not having an instruction to complement
a bit. The best I've seen to do a register bit:
CLT
SBRS Rn, m
SET
BLD Rn, m

and for I/O space, add the read/store of the I/O:
CLT
SBIS ioaddr, m
SET
IN Rn, ioaddr
BLD Rn, m
OUT ioaddr, Rn

Is there a better sequence? TIA.

Lee Theusch

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What you are looking for is the XOR instruction I think

for example XOR register,$80 will invert the msb of the byte.

you can invert more than one of the bits at once as well.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I recall thinking at one point they left xor out completely since I did not notice the eor instruction at first.

(eor is the name of a donkey in winnie the pooh, not a binary operator)

This is where having instruction aliases comes in handy to "fix" stuff like this so it is closer to what other systems present the same thing as.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes, such operations very awkward on the AVR and the reason, why the AVR need more code, as the same application on the 8051.

Following 2 macros to do the job:

;***************************************************
; cpl bit

.macro mcpl
sbrs @0, @1
rjmp _cpl1
cbr @0, @1
rjmp _cpl2
_cpl1: sbr @0, @1
_cpl2:
.endmacro
;***************************************************
; xor bit A, bit B A B A' cycle
; 0 0 0 3
; 0 1 1 6
; 1 0 1 3
; 1 1 0 7
.macro mxor
sbrs @2, @3
rjmp _xor2
sbrs @0, @1
rjmp _xor1
cbr @0, @1
rjmp _xor2
_xor1: sbr @0, @1
_xor2:
.endmacro
;***************************************************

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that's "Eeyore", Mr. Pounder :)

Best regards,

Morten, AVR tech. support, Atmel FAE

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Best way to complement a bit is to say you like
what she is wearing. If you want to complement her
a bit more, say you like the way she smells.

I learned this much in university.

Regardless,
Scott

Regards,
Scott