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/A | Absent | Absent | Absent | Absent | Absent | Absent | Absent | Absent | Absent | Absent | Unknown | Exists | Exists | Unknown |
|
|
|
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.
|
|