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

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

Re: [avr-gcc-list] Why GCC uses CLI with local variables


From: Kasper Pedersen
Subject: Re: [avr-gcc-list] Why GCC uses CLI with local variables
Date: Fri, 5 Apr 2002 10:49:02 +0200

----- Original Message ----- 
From: <address@hidden>
> I noticed that when I write functions with local varibales(for example a 
> Buffer
> [10]) GCC puts this varible onto the stack - normaly, but... If I examine the 
> produced code a bit, I can see some kind of atomic acceses to SP. In fact GCC 
> saves the SREG register, disables interrupts (with CLI) then updates the 
> upper 
> part of SP and then restores SREG.
> 
> Could somebody explain me why GCC does this ?
 
You get this:

/* prologue: frame size=44 */
push r28
push r29
in r28,__SP_L__
in r29,__SP_H__
sbiw r28,42
in __tmp_reg__,__SREG__
cli
out __SP_H__,r29
out __SREG__,__tmp_reg__
out __SP_L__,r28
/* prologue end (size=10) */

The fetching of the stack pointer is safe as an interrupt
won't change it. When writing however, there is a
possiblity that an interrupt will occur between the writes
(where the stack pointer is invalid), causing semirandom
locations to be overwritten with interrupt stack data.

It can be done (and is indeed) like this:

cli
write one half
sei
write other half (*1)

instead of the more understandable

cli
write one half
write other half
sei

because of the way the AVR works,
the instruction after the sei (*1) won't
be interrupted. This reduces the timer
lag/skew by 1 clk.

/Kasper

avr-gcc-list at http://avr1.org



reply via email to

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