Let's look at this simple code:
Code: Select all
var Svect1:byte;
begin
Svect1.6:=1;
Svect1.7:=1;
if (Svect1.6=0) or ((Svect1.6=1) and (Svect1.7=1)) then
begin
Svect1:=0; // never executed
end;
end.
Moreover, the problem is not restricted to if statement. This
Code: Select all
var Svect1:byte; Roboo:boolean;
begin
Svect1.6:=1;
Svect1.7:=1;
Roboo:=(Svect1.6=1) and (Svect1.7=1);
Roboo:=Roboo or (Svect1.6=0);
if Roboo then
begin
Svect1:=0;
end;
end.
Solution to this problem is stopping the optimizer by introducing asm statement in between logical assignments:
Code: Select all
begin
Svect1.6:=1;
Svect1.7:=1;
Roboo:=(Svect1.6=1) and (Svect1.7=1);
asm nop end; // stops optimizer operation
Roboo:=Roboo or (Svect1.6=0);
if Roboo then
begin
Svect1:=0;
end.