Best way to complement a bit?

Go To Last Post
9 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

The avr's exclusive or instruction is EOR, and only works between registers, not with immediate data. So you must first load another register with your mask, ie

ldi r1, $80
eor r0, r1

will invert the msb of register r0. That saves two instructions for the register bit case, but doesn't help for the i/o bit. I suppose you could try

ldi r1, $80
in r0, ioaddr
eor r0, r1
out ioaddr, r0

four instructions instead of six. I agree with Lee, bit manipulation in the avr devices is clumsy compared to 8051. However, if you keep a couple of registers available for jobs like this, you can make life easier by creating macros for these functions.

  • 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

Hi,

Another usefull instruction which will perform a one's compliment on all bits in a register is COM.

Syntax: COM Rd

Cheers Jack

  • 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

Hi,

Eeyore ways says that.

Cheers
Jack

  • 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