簡體   English   中英

使用指針將代碼轉換為Pascal中的Assembly - Delphi

[英]Translate a code using pointer, to Assembly in Pascal - Delphi

我在下面有這個代碼,我想把它翻譯成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中不起作用。

在Assembly版本中,它將數組乘以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生成序言和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;

Array of Integer不是你想象的那樣。 它是一個自動管理的動態數組。

你應該嘗試使用FunctionAddressList: ^Pointer; - 但請注意,您必須進行手動分配和解除分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM