Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a GCC inline assembler to delphi inline assembler

Tags:

gcc

delphi

please i've this GCC inline assembler piece of code

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

and my delphi attempt is :

asm
mov ecx, &src
mov edx, &n
sar cl,edx
mov eax,edx
end;

please is that correct ?

like image 336
Alexis Avatar asked Feb 24 '12 12:02

Alexis


1 Answers

Inline assembler does not work the same way in Delphi as it does in GCC. For starters, you don't have the same kind of macro and template support in Delphi, so if you want to use a declare-once general purpose assembler routine, you have to declare it as a function:

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}

In Delphi, inline assembler has to be implemented at the spot where it is used, and it is only supported in 32-bit. In such asm blocks you can use the EAX,ECX,EDX freely, plus any identifiers in the surrounding code. For instance:

var
  lValue: LongInt;
  lShift: Byte;
begin
  // Enter pascal code here
  asm
    mov cl,lShift
    sar lValue,cl
  end;
  // Enter pascal code here
end;
like image 78
Henrick Hellström Avatar answered Oct 20 '22 20:10

Henrick Hellström