From: Scott Dattalo <scott@da...>  20001206 15:16:41

On Tue, 5 Dec 2000, Karl Bongers wrote: > Scott, > > I think you are just seeing some optimization at work. It knows > that the loop count i is alway 10 after the loop, so it doesn't > need to check it again. I know I'm seeing optimization at work. I'm quite impressed I might add. However, in this case, the code is not correct. `i' IS 10 after loop. So the if statement after the loop should not increment the failures variable. I'll comment the code snippit to clarify: > >void for2(void) > >{ > > unsigned char i=0; > > > > for(i=0; i<10; i++) > > achar0++; /* at this point, i should be 10 */ /* if i is less than 10 then there's an error */ > > if(i < 10) > > failures++; // should not execute > > > >} As both the pic port and 8051 port show, failures is unconditionally incremented. > >It generated amazingly tight code: > > > > > >;  > >; function for2 > >;  > >_for2: > >; b.c 44 > > movlw 0x0a > > movwf _for2_i_1_1 > >_00165_DS_: > > > > incf _achar0,f > > > > decfsz _for2_i_1_1,f > > goto _00165_DS_ > > > > incf _failures,f > > > >_00166_DS_: > > > > return > > > >Unfortunately it's wrong. On the 8051 version it generates: > > > >; b.c 34 > >;  > >; function for2 > >;  > >_for2: > >; b.c 44 > > mov r2,#0x0A > >00105$: > >; b.c 39 > > inc _achar0 > >; Peephole 132 changed ljmp to sjmp > >; Peephole 205 optimized misc jump sequence > > djnz r2,00105$ > >00111$: > >00112$: > >; b.c 42 > > inc _failures > >00106$: > > C$b.c$44$1$1 ==. > > XG$for2$0$0 ==. > > ret 