将GCC内联汇编程序转换为delphi内联汇编程序

Ale*_*xis 7 delphi gcc

请问我这个GCC内联汇编程序代码

int   src = 0;
dword n;
__asm(
            "sar %%cl,%%edx"
            : "=d" (n) // saves in eax,edx
            : "c" (src), "d" (n) // the inputs
            );
Run Code Online (Sandbox Code Playgroud)

我的delphi尝试是:

asm
mov ecx, &src
mov edx, &n
sar cl,edx
mov eax,edx
end;
Run Code Online (Sandbox Code Playgroud)

请问那是对的吗?

Hen*_*röm 10

内联汇编程序在Delphi中的工作方式与在GCC中的工作方式不同.对于初学者,在Delphi中没有相同类型的宏和模板支持,因此如果要使用declare-once通用汇编程序,则必须将其声明为函数:

function ShiftArithmeticRight(aShift: Byte; aValue: LongInt): LongInt;
{$IFDEF WIN64}
asm
  sar edx,cl
  mov eax,edx
end;
{$ELSE}
{$IFDEF CPU386}
asm
  mov cl,al
  sar edx,cl
  mov eax,edx
end;
{$ELSE}
begin
  if aValue < 0 then
    Result := not (not aValue shr aShift)
  else
    Result := aValue shr aShift;
end;
{$ENDIF}
{$ENDIF}
Run Code Online (Sandbox Code Playgroud)

在Delphi中,内联汇编程序必须在使用它的地方实现,并且只支持32位.在这样的asm块中,您可以自由使用EAX,ECX,EDX以及周围代码中的任何标识符.例如:

var
  lValue: LongInt;
  lShift: Byte;
begin
  // Enter pascal code here
  asm
    mov cl,lShift
    sar lValue,cl
  end;
  // Enter pascal code here
end;
Run Code Online (Sandbox Code Playgroud)

  • @Alexis请你能阅读这个http://meta.stackexchange.com/questions/5234/,然后考虑接受这个答案和你以前的问题的一些答案 (2认同)