delphi - x64 asm 为 by ref 参数赋值可以在 Delphi 中使用,但不能在 Lazarus Free Pascal 中使用

标签 delphi assembly lazarus freepascal calling-convention

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

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

我正在 64 位模式下编译。

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

最佳答案

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

所以我会这样写:

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

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

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

在 x86 中,代码为:

function Test64(const Val: Int64; var Len: Integer): Integer;
asm
  mov dword ptr [edx], $1
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

关于delphi - x64 asm 为 by ref 参数赋值可以在 Delphi 中使用,但不能在 Lazarus Free Pascal 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362505/

相关文章:

delphi - 如何正确设置Scintilla关键字突出显示?

c++ - 用于比较原始类型的 std::optional 的有趣程序集

delphi - 编号 ListView

linux - 从汇编中的键盘读取整数(Linux IA-32 x86 gcc gas)

image - delphi:如何将图像放入DBGrid标题中?

delphi - WINDOWS下使用ZEOS连接SQL Server

delphi - TDownloadURL 无法从 HTTPS 下载

database - TParams.ParamRef 有什么用?

delphi - 表中的部分搜索(BDE)

java - CPMXCG(比较和交换)在多处理器机器上到底如何工作?