I am porting 32-bit Delphi BASM code to 64-bit FPC (Win64 target OS) and wonder why the next instruction does not compile in 64-bit FPC:
{$IFDEF FPC}
{$ASMMODE INTEL}
{$ENDIF}
procedure DoesNotCompile;
asm
LEA ECX,[ECX + ESI + $265E5A51]
end;
// Error: Asm: 16 or 32 Bit references not supported
possible workarounds are:
procedure Compiles1;
asm
ADD ECX,ESI
ADD ECX,$265E5A51
end;
procedure Compiles2;
asm
LEA ECX,[RCX + RSI + $265E5A51]
end;
I just don't understand what is wrong with 32-bit LEA
instruction in Win64 target (it compiles OK in 32-bit Delphi, so it is a correct CPU instruction).
Optimization remarks:
The next code compiled by 64-bit FPC 2.6.2
{$MODE DELPHI}
{$ASMMODE INTEL}
procedure Test;
asm
LEA ECX,[RCX + RSI + $265E5A51]
NOP
LEA RCX,[RCX + RSI + $265E5A51]
NOP
ADD ECX,$265E5A51
ADD ECX,ESI
NOP
end;
generates the next assembler output:
00000000004013F0 4883ec08 sub $0x8,%rsp
project1.lpr:10 LEA ECX,[RCX + RSI + $265E5A51]
00000000004013F4 8d8c31515a5e26 lea 0x265e5a51(%rcx,%rsi,1),%ecx
project1.lpr:11 NOP
00000000004013FB 90 nop
project1.lpr:12 LEA RCX,[RCX + RSI + $265E5A51]
00000000004013FC 488d8c31515a5e26 lea 0x265e5a51(%rcx,%rsi,1),%rcx
project1.lpr:13 NOP
0000000000401404 90 nop
project1.lpr:14 ADD ECX,$265E5A51
0000000000401405 81c1515a5e26 add $0x265e5a51,%ecx
project1.lpr:15 ADD ECX,ESI
000000000040140B 01f1 add %esi,%ecx
project1.lpr:16 NOP
000000000040140D 90 nop
project1.lpr:17 end;
000000000040140E 4883c408 add $0x8,%rsp
and the winner is (7 bytes long):
LEA ECX,[RCX + RSI + $265E5A51]
all 3 alternatives (including LEA ECX,[ECX + ESI + $265E5A51]
which does not compile by 64-bit FPC) are 8 bytes long.
Not sure that the winner is best in speed.