[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Problem with delay loop
From: |
David Kelly |
Subject: |
Re: [avr-gcc-list] Problem with delay loop |
Date: |
Fri, 28 Sep 2007 13:29:52 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, Sep 28, 2007 at 11:26:43PM +0530, Royce Pereira wrote:
> Hi all,
>
> OK fine I agree.
>
> we have to use 'volatile' and all.
>
> But why must it generate such horrid code...
There are those here who would tell you that any calibrated delay loop
is horrible code.
The stack has to be protected when space is being allocated, believe
this is normal for any auto variable allocation because the registers
being used to offset the stack are the same the runtime will use to
process an interrupt. Notice the SREG is restored in the middle of
updating the stack pointer? This is because the next instruction is
already in the pipe and can't be interrupted.
Make it "static volatile" for simpler code. The other was nasty because
it had to access stack-relative, and because it was volatile it *had* to
be fetched and stored each time.
void delay_loop(unsigned int delay)
{
ba: 90 93 01 01 sts 0x0101, r25
be: 80 93 00 01 sts 0x0100, r24
static volatile unsigned int vdelay;
vdelay = delay;
while( vdelay-- )
c2: 80 91 00 01 lds r24, 0x0100
c6: 90 91 01 01 lds r25, 0x0101
ca: 01 97 sbiw r24, 0x01 ; 1
cc: 90 93 01 01 sts 0x0101, r25
d0: 80 93 00 01 sts 0x0100, r24
d4: 80 91 00 01 lds r24, 0x0100
d8: 90 91 01 01 lds r25, 0x0101
dc: 8f 5f subi r24, 0xFF ; 255
de: 9f 4f sbci r25, 0xFF ; 255
e0: 81 f7 brne .-32 ; 0xc2 <delay_loop+0x8>
e2: 08 95 ret
--
David Kelly N4HHE, address@hidden
========================================================================
Whom computers would destroy, they must first drive mad.
Re: [avr-gcc-list] Problem with delay loop, David Kelly, 2007/09/28