#195 infinite loop

closed-rejected
nobody
None
5
2001-10-26
2001-10-26
Anonymous
No

I have a routine ..
void beep(unsigned char nticks){
snd_on=nticks;
BUZZ=0;
while(snd_on>0);
BUZZ=1;
}
where snd_on is decremented in an interrupt routine
when the internal clock 'ticks' (using timer 1)

The assemble code produced is ..

;------------------------------------------------------
------
;Allocation info for local variables in function 'beep'
;------------------------------------------------------
------
; Tim1051.c 45
; -----------------------------------------
; function beep
; -----------------------------------------
_beep:
; Tim1051.c 49
mov _snd_on,dpl
; Tim1051.c 47
clr _P1_2
; Tim1051.c 48
clr c
; Peephole 159 avoided xrl during execution
mov a,#(0x00 ^ 0x80)
mov b,_snd_on
xrl b,#0x80
subb a,b
clr a
rlc a
mov r2,a
00101$:
mov a,r2
; Peephole 109 removed ljmp by inverse jump logic
jnz 00101$
00108$:
; Tim1051.c 49
setb _P1_2
00104$:
ret
As you can see, an infinite loop can occur to 00101$
and it appears that snd_on is in continually tested
for 0(as the condition for the while loop)

Stuart Allen

Discussion

  • Nobody/Anonymous

    Logged In: NO

    Hello,

    It's important to declare the snd_on variable with
    'volatile' in this case.
    Have you do it ?

    Gilles Bannay.

     
  • Bernhard Held

    Bernhard Held - 2001-10-26

    Logged In: YES
    user_id=203539

    It's not a bug, it's a feature ;-)

    >where snd_on is decremented in an interrupt routine
    A compiler's optimizer can't know this. For this reason the
    modifier "volatile" was invented. You have to declare
    snd_on as volatile:

    volatile char snd_on;

    This will solve your problems.

     
  • Bernhard Held

    Bernhard Held - 2001-10-26
    • status: open --> closed-rejected
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks