Delphi XE2:为Win64平台转换ASM方法

Cha*_*ang 1 delphi win64 delphi-xe2

当我尝试为Win64平台编译pascal单元时,遇到错误.这些方法包含ASM块.我不知道如何让它适用于Win64平台:

方法1:

Function TSparseList.ForAll( ApplyFunction: Pointer {TSPAApply} ): Integer; Assembler;
Asm
        MOV     EAX,[EAX].TSparseList.FList
        JMP     TSparsePointerArray.ForAll
End;
Run Code Online (Sandbox Code Playgroud)

方法2:

Function TSparsePointerArray.ForAll( ApplyFunction: Pointer {TSPAApply} ):
  Integer;
Var
  itemP: PAnsiChar; { Pointer to item in section } { patched by ccy }
  item: Pointer;
  i, callerBP: Cardinal;
  j, index: Integer;
Begin
  { Scan section directory and scan each section that exists,
    calling the apply function for each non-nil item.
    The apply function must be a far local function in the scope of
    the procedure P calling ForAll.  The trick of setting up the stack
    frame (taken from TurboVision's TCollection.ForEach) allows the
    apply function access to P's arguments and local variables and,
    if P is a method, the instance variables and methods of P's class '}
  Result := 0;
  i := 0;
  Asm
    mov   eax,[ebp]                     { Set up stack frame for local }
    mov   callerBP,eax
  End;
  While ( i < slotsInDir ) And ( Result = 0 ) Do
  Begin
    itemP := secDir^[i];
    If itemP <> Nil Then
    Begin
      j := 0;
      index := i Shl SecShift;
      While ( j < FSectionSize ) And ( Result = 0 ) Do
      Begin
        item := PPointer( itemP )^;
        If item <> Nil Then
          { ret := ApplyFunction(index, item.Ptr); }
          Asm
            mov   eax,index
            mov   edx,item
            push  callerBP
            call  ApplyFunction
            pop   ecx
            mov   @Result,eax
          End;
        Inc( itemP, SizeOf( Pointer ) );
        Inc( j );
        Inc( index )
      End
    End;
    Inc( i )
  End;
End;
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 8

我不熟悉的x64指令的细节,所以我不能重写汇编代码以支持64位的帮助,但我可以告诉你,Embarcadero公司的64位编译器并不目前允许你混合Pascal和大会在同一个功能.您只能编写所有Pascal或所有汇编函数,根本不混合(Pascal函数可以调用汇编函数,反之亦然,但它们不能像x86一样共存).所以你必须重写你的方法.

  • 值得注意的是,在同一例程中不能混合ASM和Pascal的主要原因是为了避免引起堆栈帧问题; 他们对Win64的要求比Win32要严格得多.所以使用asm技巧设置伪堆栈的代码是你可能不想复制的东西. (3认同)
  • @MarcusAdams x86操作码在没有修改的情况下无法在x64上运行,如果你在其中使用了一些指针.但是一些非常基本的32位代码可以在x64上工作(比如`函数递增(A:整数):整数; asm inc eax end;`.但是Chau Chee Yang的那种代码不起作用,因为它依赖于调用约定和指针. (2认同)

Arn*_*hez 6

您可以在x64 ASM中重写整个方法.正如雷米所说,你需要重写整个方法,因为你不能在其中嵌套一些asm .. end块begin .. end.

真正的问题是Win32和Win64模式下的调用约定不一样.寄存器更改(即它们是64位,现在应包括SSE2寄存器),但主要问题是您的调用重新注入器应知道参数的数量:必须在堆栈上为每个参数分配一些空间.

如果您的TSPAApply函数有许多固定参数,您可以将其转换为普通的pascal版本 - 这比所有内容都安全.

type
  TSPAApply = function(index: integer; item: pointer);

Function TSparseList.ForAll( ApplyFunction: Pointer {TSPAApply} ): Integer; 
begin
  result := FList.ForAll(ApplyFunction);
End;

Function TSparsePointerArray.ForAll( ApplyFunction: Pointer {TSPAApply} ):
  Integer;
Var
  itemP: PPointer; 
  i: Cardinal;
  j, index: Integer;
Begin
  Result := 0;
  i := 0;
  While ( i < slotsInDir ) And ( Result = 0 ) Do
  Begin
    itemP := secDir^[i];
    If itemP <> Nil Then
    Begin
      j := 0;
      index := i Shl SecShift;
      While ( j < FSectionSize ) And ( Result = 0 ) Do
      Begin
        If itemP^ <> Nil Then
          result := TSPAApply(ApplyFunction)(index,itemP^.Ptr);
        Inc( itemP );
        Inc( j );
        Inc( index )
      End
    End;
    Inc( i )
  End;
End;
Run Code Online (Sandbox Code Playgroud)

但是你应该更好地依赖一个TMethod列表,以获得更通用的OOP方式.在这里,一些代码重构是个好主意.

  • 普通的Pascal也更加独立于平台.想想Firemonkey/FPC. (3认同)