avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] GCC 3.0.2 unsigned int bug?


From: Chris Elmquist
Subject: Re: [avr-gcc-list] GCC 3.0.2 unsigned int bug?
Date: Tue, 18 Dec 2001 09:27:08 -0600
User-agent: Mutt/1.2.5i

On Tuesday (12/18/2001 at 03:01PM +0100), Joerg Wunsch wrote:
> Chris Elmquist <address@hidden> wrote:
> 
> > In any case, here's my "improved" loop, suitable for fooling the optimizer
> > today:
> > 
> > void delay(void)
> > {
> >         unsigned int    d;
> > 
> >         for (d=0x1000; d; d--)
> >                 __asm__ __volatile__ ( "nop" : : );
> > }
> 
> If you already know about the volatile keyword, why don't you use it
> just right?
> 
> void delay(void)
> {
>       volatile unsigned int d;
> 
>       for (d = 0x1000; d; d--)
>               ;
> }

well, it seems there is a radical difference in the size and complexity 
of the code it produces then.  ie,

this code,

void delay(void)
{
        volatile unsigned int d;

        for (d=0x1000; d; d--)
                asm(";");
}

yields,

0000004a <delay>:
  4a:   a2 e0           ldi     r26, 0x02       ; 2
  4c:   b0 e0           ldi     r27, 0x00       ; 0
  4e:   ea e2           ldi     r30, 0x2A       ; 42
  50:   f0 e0           ldi     r31, 0x00       ; 0
  52:   54 c0           rjmp    .+168           ; 0xfc
  54:   80 e0           ldi     r24, 0x00       ; 0
  56:   90 e1           ldi     r25, 0x10       ; 16
  58:   03 c0           rjmp    .+6             ; 0x60
  5a:   89 81           ldd     r24, Y+1        ; 0x01
  5c:   9a 81           ldd     r25, Y+2        ; 0x02
  5e:   01 97           sbiw    r24, 0x01       ; 1
  60:   89 83           std     Y+1, r24        ; 0x01
  62:   9a 83           std     Y+2, r25        ; 0x02
  64:   89 81           ldd     r24, Y+1        ; 0x01
  66:   9a 81           ldd     r25, Y+2        ; 0x02
  68:   89 2b           or      r24, r25
  6a:   b9 f7           brne    .-18            ; 0x5a
  6c:   e2 e0           ldi     r30, 0x02       ; 2
  6e:   ce 5f           subi    r28, 0xFE       ; 254
  70:   61 c0           rjmp    .+194           ; 0x134

which of course keeps 'd' in ram (ie, on the "stack")  and calls all of
the prolog and epilog functions when entering and leaving the function.

Yet this code:

void delay(void)
{
        unsigned int d;

        for (d=0x1000; d; d--)
                asm(";");
}

produces,

0000004a <delay>:
  4a:   80 e0           ldi     r24, 0x00       ; 0
  4c:   90 e1           ldi     r25, 0x10       ; 16
  4e:   01 97           sbiw    r24, 0x01       ; 1
  50:   f1 f7           brne    .-4             ; 0x4e
  52:   08 95           ret

I suppose it would be more correct to declare 'd' a type "register"
above to ensure it always stays that way.

It's all moot though in the end, as I don't intend to perform my delays
with spin loops for very long :-)  But it has been an interesting
exercise for me to see how GCC (w/ -Os) deals with this simple case on
the AVR.

Regards,

Chris

-- 
Chris Elmquist      mailto:address@hidden     http://www.pobox.com/~chrise



reply via email to

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