The Delphi Bug List

Entry No.
683
Compiler - Code generation
Modifying EAX register breaks for loop
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
N/AAbsentAbsentAbsentAbsentAbsentAbsentAbsentAbsentAbsentAbsentUnknownExistsExistsUnknown
Description
Reported by Nick Walter; checked by Jordan Russell
From Delphi Help:
An asm statement must preserve the EDI, ESI, ESP, EBP, and EBX registers, but can freely modify the EAX, ECX, and EDX registers.
With Delphi 6 there is general trouble using EAX in an asm statement:
procedure Delphi6EAXBug; // loops forever with Delphi 6
var
  i: integer;
begin
  for i := 0 to 15 do
    asm
      mov eax,2
    end;
end;
Solution / workaround
Workaround:
procedure Delphi6EAXBug;
var
  i: integer;
begin
  for i := 0 to 15 do
    asm
      push eax
      mov eax,2
      pop eax
    end;
end;
User-contributed comments
Abel Belzunces
13 Feb 2002  11:39 AM GMT
This is not a bug, by default Delphi use Optimizations, which use the asm registers to generate faster code.

If we look at the Delphi code...

begin
for i:=0 to 15 do
mov eax,2
end;

And then the generated Assembler code...

asm
mov eax,15
@1:
mov eax,2
dec eax
jnz @1
end;

So, you've two options, disable optimizations or don't mixed Asm & Object Pascal in loops.
Ficedula
16 Feb 2002  07:58 PM GMT
It IS a bug because of the documentation, which says that you can modify EAX freely. The compiler should push/pop the registers for you, if it's using a register already and you use it in an ASM. Either it should do that, or not claim that EAX is freely modifiable.
Latest update of this entry: 2002-04-05

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.