[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] efficiency of assigning bits

From: Christof Krueger
Subject: Re: [avr-gcc-list] efficiency of assigning bits
Date: Mon, 14 Mar 2005 12:55:47 +0100
User-agent: Mozilla Thunderbird 1.0 (X11/20050116)

To turn two bits off, the method I would adopt is

PORTD &= 0B11101101;

This is the fastest method.

What does that work out to in assembly?  A cbi/sbi assembly instruction is 2 
clock cycles.  I still don't understand why these instructions would be taken 
out of winavr, they obviously are useful for them to have been included as 
opcodes in the AVR! :)
The line
  PORTD &= 0B11101101;
includes more information than just "Clear bit 1 and 4 in PORTD". It
also says: Do it at the same moment.

See the following listing:

  13:main.c        ****   PORTD &= 0xed;
  84                .LM3:
  85 000c 82B3          in r24,50-0x20
  86 000e 8D7E          andi r24,lo8(-19)
  87 0010 82BB          out 50-0x20,r24

compared to:

  14:main.c        ****   PORTD &= 0xef;
  89                .LM4:
  90 0012 9498          cbi 50-0x20,4
  15:main.c        ****   PORTD &= 0xfd;
  92                .LM5:
  93 0014 9198          cbi 50-0x20,1

So if you know that you want to set/clear exactly two bits in an
efficient way and you also know that it doesn't matter if the bits are
not set/cleared in exactly the same clock cycle, you can still form two
standard c instructions that correctly translate into cbi/sbi.

Is it possible to do inline assembly with cbi/sbi?
Something like this:


Yes, it is possible, but it is not needed, since something like
PORTD |= (1<<PD3);
will always yield a sbi-instruction. If you still want to use sbi/cbi
you can define a preprocessor macro like

#define sbi(x,b) x|=(1<<b)

  Christof Krueger

reply via email to

[Prev in Thread] Current Thread [Next in Thread]