- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 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(当前稳定版)
有任何解决方法或解决问题的办法吗?
最佳答案
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
中指定,而第二个输入操作数被明确指定为通用寄存器或内存地址。
所以,MUL EAX,EDX
确实是一个无效的汇编指令。
如果您在 Delphi 中编译此代码并使用调试器查看生成的程序集,您会看到对 Mul(10,20)
的调用生成了以下汇编代码:
// Mul(10,20)
mov edx,$00000014
mov eax,$0000000a
call Mul
//MUL EAX,EDX
mul edx
因此,如您所见,Delphi 正在实际解析您的源代码,发现第一个操作数是 EAX
并为您将其剥离,从而生成正确的程序集。 FreePascal 不会为您执行该步骤。
解决方法?首先编写正确的汇编代码。不要依赖编译器为您重新解释代码。
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
。来自德尔福的 documentation :
The value of
x / y
is of typeExtended
, regardless of the types ofx
andy
. For other arithmetic operators, the result is of typeExtended
whenever at least one operand is a real; otherwise, the result is of typeInt64
when at least one operand is of typeInt64
; otherwise, the result is of typeInteger
. 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
指令) ).如果您不想在整个项目范围内更改选项,您可以使用 {$STACKFRAMES OFF}
/{ $W-}
和 {$OPTIMIZATION ON}
/{$O+}
编译指令。
{$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
关于delphi - FPC BASM32 MUL 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523936/
下面是我想要从 Delphi 内联程序集翻译的一些代码,因为在针对 Win64 进行编译时出现错误。当为 Win32 编译时,代码按预期工作。 感谢您的帮助。 var a,b,c,d: LongW
我是一名优秀的程序员,十分优秀!