FPC BASM32 MUL 错误?

FPC BASM32 MUL bug?

我在将 Delphi BASM32 代码移植到 FPC 时遇到问题:

program MulTest;

{$IFDEF FPC}
  {$mode delphi}
  {$asmmode intel}
{$ELSE}
  {$APPTYPE CONSOLE}
{$ENDIF}

function Mul(A, B: LongWord): LongWord;
asm
         MUL    EAX,EDX
end;

begin
  Writeln(Mul(10,20));
  Readln;
end.

以上代码在 Delphi XE 中编译并按预期工作; FPC 在 MUL EAX,EDX 行输出编译时错误:

Error: Asm: [mul reg32,reg32] invalid combination of opcode and operands

我使用的是Lazarus 1.4.4/FPC2.6.4 for Win32(当前稳定版)

有任何解决方法或解决问题的办法吗?

MUL 总是乘以 AL、AX 或 EAX (more details),因此您应该只指定另一个操作数。

FreePascal 是正确的。 MUL只有3种形式:

MUL r/m8
MUL r/m16
MUL r/m32

Performs an unsigned multiplication of the first operand (destination operand) and the second operand (source operand) and stores the result in the destination operand. The destination operand is an implied operand located in register AL, AX or EAX (depending on the size of the operand); the source operand is located in a general-purpose register or a memory location.

换句话说,第一个操作数(用于输入和输出)在AL/AX/EAX中指定,第二个输入操作数明确指定为general-purpose 寄存器或内存地址。

所以,MUL EAX,EDX确实是一个无效的汇编指令。

如果您在 Delphi 中编译此代码并使用调试器查看生成的程序集,您会看到对 Mul(10,20) 的调用生成了以下汇编代码:

// Mul(10,20)
mov edx,[=11=]000014
mov eax,[=11=]00000a
call Mul

//MUL    EAX,EDX
mul edx

因此,如您所见,Delphi 实际解析您的源代码,发现第一个操作数是 EAX 并为您去除它,从而生成正确的程序集。 FreePascal 不会为您执行该步骤。

解决方法?首先编写正确的汇编代码。不要依赖编译器 re-interpret 为您编写代码。

function Mul(A, B: LongWord): LongWord;
asm
         MUL    EDX
end;

或者,您可以不直接编写汇编代码,让编译器为您完成工作。它知道如何将两个 LongWord 值相乘:

function Mul(A, B: LongWord): LongWord;
begin
  Result := A * B;
end;

虽然 Delphi 在这种情况下确实使用 IMUL 而不是 MUL。来自 Delphi 的 documentation:

The value of x / y is of type Extended, regardless of the types of x and y. For other arithmetic operators, the result is of type Extended whenever at least one operand is a real; otherwise, the result is of type Int64 when at least one operand is of type Int64; otherwise, the result is of type Integer. If an operand's type is a subrange of an integer type, it is treated as if it were of the integer type.

除非禁用堆栈框架并启用优化,否则它还会使用一些难看的臃肿程序集。通过配置这两个选项,可以让 Mul() 生成单个 IMUL EDX 指令(当然还有 RET 指令)。如果您不想更改选项 project-wide,您可以使用 {$STACKFRAMES OFF}/{$W-}{$OPTIMIZATION ON}/[= 将它们隔离为 Mul() 42=]编译指令。

{$IFOPT W+}{$W-}{$DEFINE SF_Was_On}{$ENDIF}
{$IFOPT O-}{$O+}{$DEFINE O_Was_Off}{$ENDIF}
function Mul(A, B: LongWord): LongWord;
begin
  Result := A * B;
end;
{$IFDEF SF_Was_On}{W+}{$UNDEF SF_Was_On}{$ENDIF}
{$IFDEF O_Was_Off}{O-}{$UNDEF O_Was_Off}{$ENDIF}

生成:

imul edx
ret