我在下面有这段代码,我想把它翻译成 ASM,也可以在 Delphi 中使用。
var
FunctionAddressList: Array of Integer;
type TFunction = function(parameter: Integer): Integer; cdecl;
function Function(parameter: Integer): Integer;
var
ExternFunction: TFunction;
begin
ExternFunction := TFunction(FunctionAddressList[5]);
Result := ExternFunction(parameter);
end;
它工作正常,但是当我尝试它的汇编版本时:
function Function(parameter: Integer): Integer; cdecl;
asm
mov eax, FunctionAddressList
jmp dword ptr [eax + 5 * 4]
end;
它应该可以工作,因为在 C++ 中它以两种方式工作:
void *FunctionAddressList;
_declspec(naked) int Function(int parameter)
{
_asm mov eax, FunctionAddressList;
_asm jmp dword ptr [eax + 5 * 4];
}
typedef int (*TFunction)(int parameter);
int Function(int parameter)
{
TFunction ExternFunction = ((TFunction *)FunctionAddressList)[5];
return ExternFunction(parameter);
}
但它在德尔福中不起作用。
在 Assembly 版本中,它将数组乘以 4,因为它是数组中每个元素之间的偏移大小,所以两个版本是等价的。
所以,我想知道为什么它不适用于Delphi。在 Delphi 中,数组中整数值之间的偏移大小与 C++ 不同?
我已经尝试了许多偏移量,例如 1、2、4、6、8 等。以及许多类型的数组(指针数组;仅指针;整数数组等),并且我尝试了许多调用约定,并且 cdecl 是唯一适用于非 asm 版本的,但是对于 ASM,所有测试都不起作用。
谢谢。