x64 asm 通过 ref 参数将值分配给 Delphi,但不适用于 Lazarus Free Pascal

x64 asm assign value to a by ref parameter works in Delphi, but not Lazarus Free Pascal

在下面的简化代码中,没有分配 Len。

function Test64(const Val: Int64; var Len: Integer): Integer;
begin
asm
  mov           [Len], 
end;
end;

我在 64 位模式下编译。

虽然这在 Delphi(32 位)中有效。我如何让它在 Lazarus 中工作?

我总是喜欢在编写 asm 时明确说明用于传递参数的寄存器。它使发现寄存器滥用变得容易得多。

所以我会这样写:

function Test64(const Val: Int64; var Len: Integer): Integer;
asm
  mov dword ptr [rdx], 
end;

我怀疑您实际上是在为 x86 进行编译,因为 Delphi x64 编译器不像您在问题中那样支持混合使用 Pascal 和 asm。因此,据我了解,问题中的代码无法在 Delphi x64 编译器下编译。

你可以看到在我上面的代码中我没有混合使用 Pascal 和 asm。这是一个纯汇编函数。我的建议是始终以这种方式编码,即使在 x86 上也是如此。这样做可以确保编译器不会干扰您的堆栈和寄存器使用。

在 x86 中,代码为:

function Test64(const Val: Int64; var Len: Integer): Integer;
asm
  mov dword ptr [edx], 
end;

在 Windows x64 上 Microsoft 定义了 ABI,并且只有一个调用约定。它记录在这里:https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx

非 Microsoft 平台的 x64 ABI(System V x64 ABI)不同。我假设您的目标是 Windows。

此处描述了 Delphi x86 ABI:http://docwiki.embarcadero.com/RADStudio/en/Program_Control