gpt4 book ai didi

c++ - 使用指针将代码翻译成 Pascal 中的程序集 - Delphi

转载 作者:太空狗 更新时间:2023-10-29 21:08:55 24 4
gpt4 key购买 nike

我在下面有这段代码,我想将它翻译成 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);
}

但它在 Delphi 中不起作用。

在汇编版本中,它将数组乘以 4,因为它是数组每个元素之间的偏移大小,所以两个版本是等效的。

所以,我想知道为什么它不适用于 Delphi。在 Delphi 中,数组中 Integer 值之间的偏移大小与 C++ 不同?

我已经尝试了许多偏移量,如 1、2、4、6、8 等。还有许多类型的数组(指针数组;仅指针;整数数组等),我已经尝试了很多调用约定,cdecl 是唯一适用于非 asm 版本的约定,但对于 ASM,所有测试均无效。

谢谢。

最佳答案

第一个测试应用重现错误:

var
FunctionAddressList: Array of Integer;

function Bar(parameter: Integer): Integer; cdecl;
begin
ShowMessage('Bar '+IntToStr(parameter));
end;

function Foo(parameter: Integer): Integer; cdecl;
asm
mov eax, FunctionAddressList
jmp dword ptr [eax + 5 * 4]
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SetLength(FunctionAddressList, 6);
FunctionAddressList[5]:= Integer(@Bar);
Foo(25);
end;

Bar地址定义正确,但问题是Delphi编译器为Foo生成了prologue和epilog,所以真正的Foo代码是

0046CD30 55               push ebp
0046CD31 8BEC mov ebp,esp
Unit1.pas.46: mov eax, FunctionAddressList
Unit1.pas.47: jmp dword ptr [eax + 5 * 4]
0046CD3B 5D pop ebp
0046CD3C C3 ret

导致堆栈损坏,参数错误,Bar返回地址错误。如果您仍然想这样做,请使用

function Foo(parameter: Integer): Integer; cdecl;
asm
pop ebp
mov eax, FunctionAddressList
jmp dword ptr [eax + 5 * 4]
end;

关于c++ - 使用指针将代码翻译成 Pascal 中的程序集 - Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2173570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com