[Top][All Lists]

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

[avr-gcc-list] Generating code using sbis, sbic

From: David Brown
Subject: [avr-gcc-list] Generating code using sbis, sbic
Date: Thu, 18 Aug 2005 12:27:50 +0200

I know I started the AVR Tiny thread, but I don't want to get much involved
in the current line of argument, other than to say I fully support Jörg's

However, it did remind me of another issue I spotted while doing that
project, which involved closer examination of generated assembly code than
most projects.  The use of "out", "in", "sbi" and "cbi" when in the
generated assembly code is, as far as I can see, entirely optimal - the line
"PORTB |= 0x04" generates "sbi 56-0x20,2".

Reading input bits, on the other hand, can be a matter of luck.  One would
think the two functions
    unsigned char test1(void) {
         if (PINB & 0x02) return 1; else return 0;
    unsigned char test2(void) {
         while (PINB & 0x02) return 1;
         return 0;

would generate identical code (with avrgcc 3.4.1, -Os, -O2,
or -O3, -mmcu=atmega128).  Indeed, if I add an "inline" to the definitions
and use them in code such as:
    void foo1(void) { if (test1()) doX(); else doY(); }
    void foo2(void) { if (test1()) doX(); else doY(); }
then the generated code for foo1 and foo2 is optimal, using the "sbis"
instruction to check the pin state.

But the generated code for test2 itself is:
        sbis 54-0x20,1
        rjmp .L9
        ldi r24, lo8(1)
        ldi r25, hi8(1)
        ldi r24, lo8(0)
        ldi r25, hi8(0)

Hardly optimal (does the result really have to be promoted to an integer?),
but not bad.  The generated code for test1 is extraordinary:
        in r24,54-0x20
        clr r25
        lsr r25
        ror r24
        movw r18,r24
        andi r18,lo8(1)
        andi r19,hi8(1)
        sbrs r24,0
        rjmp .L2
        ldi r24,lo8(1)
        ldi r25,hi8(1)
        movw r24,r18

Any comments?

David Brown
System Developer
WestControl a.s

"Utvikling er kunsten av å vikle seg ut av det man har viklet seg inn i"

reply via email to

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