2012-01-17 99 views
-3

请怎么会这样C++函数转换为德尔福:转换一个C++内联汇编函数的德尔福内联汇编函数

int To_Asm_Fnc(dword Amem, dword Al, dword Ac) { 
int b = 0; 
    asm ("push %%ecx; \ 
      call %%eax; \ 
      pop %%ecx;" 
     : "=Al" (b) /* output value */ 
     : "Al" (mem), "Ac" (Al), "d" (Ac) /* input value */ 
     ); 
    return b; 
} 

,这是我的德尔福尝试

Function To_Asm_Fnc(Amem,Al,Ac:dword):Integer; 
var 
b:Integer; 
begin 
Result:=0; 
b:=0; 
//******* 
{ i really didn't get it as in the c++ code } 
//******* 
Result:=b; 
end; 

千恩万谢

+5

这不是C++,这是程序集。 –

+2

什么是您的C++编译器。您需要了解调用约定并注册该约定的维护规则才能了解这一点。 –

+0

在Delphi中,你也可以使用'asm'关键字,http://docwiki.embarcadero.com/RADStudio/en/Using_Inline_Assembly_Code。 – Pol

回答

6

看起来这个函数接受指向另一个函数的指针,并设置参数

function To_Asm_Fnc(Amem: Pointer; _Al, _Ac: cardinal): integer; 
asm 
    // x68 only!; paramateres are passed differently in x64 
    // inputs : "Al" (mem), "Ac" (Al), "d" (Ac) /* input value */ 
    // amem is already in eax 
    // _al is passed in edx and _ac in ecx; but the code expects them reversed 
    xchg edx, ecx 
    push ecx 
    call eax 
    pop ecx 
    // result is already in eax and delphi returns the result in eax 
    // outputs : "=Al" (b) /* output value */ 
end; 
+0

如果所有这些例程所做的都是颠倒参数的顺序,那么从长远角度来看,这样做可能会更好,而无需采用装配。我必须说,我不太清楚你是如何看出原来的asm颠倒了这两个参数的。 –

+2

原始asm没有反转参数。代码:“Ac”(A1),“d”(Ac)表示:将Al变量放入ecx寄存器并将Ac变量放入edx寄存器。德尔福按照以下顺序使用params:eax,edx,ecx,stack;所以为了保持函数签名相同,我们交换传递的参数。 PS。该函数调用另一个由Amem参数 –

+0

指向的函数。出于兴趣,C++编译器以此格式接受asm。我不认识这种语法。哦,+1顺便说一句。 –