3

I have this assembler function in my application wriiten in delphi and this executes well on windows .But my requirement is to execute it on Linux as i am migrating my application on Linux. And while compiling this function in Linux i got error : "Unsupported language feature: 'ASM'".

Anybody can help or suggest how to implement it either in c++ or in delphi so that it works for Linux. sharing my code:

type 
  PVersionizedPointer = ^TVersionizedPointer; 
  TVersionizedPointer = packed record 
    Ver : NativeInt; 
    Ptr : Pointer; 
  end; 
  TVersionizedPointerStorage = array[0 .. 2 * sizeof(TVersionizedPointer) - 1] of byte; 

function GetVersionizedPointer(var PointerStorage : TVersionizedPointerStorage) : 
    PVersionizedPointer; assembler;
const
  vp_size = sizeof(TVersionizedPointer);  
      // Note: sizeof(any) inside asm is always $31
  asm
    {$ifdef CPUX86}
      add EAX, vp_size - 1
      and EAX, not(vp_size - 1)
    {$endif}
    {$ifdef CPUX64}
      mov RAX, RCX
      add RAX, vp_size - 1
      and RAX, not(vp_size - 1)
    {$endif}
  end;
end;
9
  • Could you give the definition of TVersionizedPointerStorage? Commented Apr 21, 2020 at 9:59
  • Some hint of what the code is supposed to do would be helpful to. The answer is probably not to bother with assembly code and trust the compiler to generate good code Commented Apr 21, 2020 at 10:09
  • sure i will share the info. Commented Apr 21, 2020 at 10:41
  • 2
    Please don't add details in comments. You can edit the question and add the information there Commented Apr 21, 2020 at 11:53
  • 1
    sure @David . will take care next time. Commented Apr 21, 2020 at 16:31

2 Answers 2

5

It's probably easiest just to concentrate on what the x86 version does:

function GetVersionizedPointer(var PointerStorage: TVersionizedPointerStorage): PVersionizedPointer;
const
  vp_size = sizeof(TVersionizedPointer);
asm
  add EAX, vp_size - 1
  and EAX, not(vp_size - 1)
end;

The x86 ABI means that the address of PointerStorage is passed into the function in EAX. The return value, another address, is also retuned in EAX. That knowledge lets us understand what the function does, and allows us to write it like this in Pascal:

function GetVersionizedPointer(var PointerStorage: TVersionizedPointerStorage): PVersionizedPointer;
var
  Address: NativeUInt;
begin
  Address := NativeUInt(@PointerStorage);
  Address := Address + (SizeOf(TVersionizedPointer) - 1);
  Address := Address and not (SizeOf(TVersionizedPointer) - 1);
  Result := PVersionizedPointer(Address);
end;

I've written it out rather verbosely to make it clear what each step is doing. The address variable is assigned the address of PointerStorage, and so serves the same role as EAX in the original version.

Make your life easier in the future by using this pure Pascal version, and losing the assembler code.

Sign up to request clarification or add additional context in comments.

1 Comment

Remark: This kind of functions may benefit of being inlined. And inlining such pascal code is very likely to be faster than calling an external asm stub. :)
3

This function aim is to align the storage in memory. It is perfectly pointless in modern Intel/AMD CPUs. It may have had a benefit on a 8086 CPU, but not any more. Sounds definitively like wrong/premature optimization.

This whole alignment subroutine may just be avoided: it would make code cleaner and also faster. Just get rid of TVersionizedPointerStorage and only use TVersionizedPointer.

Not aligning the data will be faster, since you won't need to compute any alignment any more, and you would just use the needed memory, whereas TVersionizedPointerStorage is always using twice the size, so it pollutes the CPU L1 cache for no benefit, and add unneeded memory allocation.

2 Comments

Hi @Arnaud Actually this is very old code from 90's and written by other guys , so recently this application/tool came to us and we are struggling to make it work on lInux and understand it completely.
Then clean it from deprecated tricks like TVersionizedPointerStorage. It will help understand it for sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.