Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use compiler intrinsics in an asm block?

Is this a compiler bug?

program Project44;
{$APPTYPE CONSOLE}
uses
  System.SysUtils;

function Test: integer;
asm
  xor eax,eax
  add eax,SizeOf(NativeInt);
end;

begin
  WriteLn(Test);  //Typically outputs 31 or 49
  {$ifdef CPUX86}
  WriteLn('should be 4');
  {$else}
  {$ifdef CPUX64}
  WriteLn('should be 8');
  {$endif}{$endif}
  ReadLn
end.

This program outputs all kinds of things, except for 4/8.

Is this an bug or is it documented that I cannot use SizeOf and other compiler intrinsics in assembler?
If I would want to use SizeOf(xx) in an asm block what do I do?

like image 778
Johan Avatar asked Jun 11 '15 21:06

Johan


People also ask

What is intrinsics Assembly?

Using intrinsics Compiler intrinsics are special functions with implementations that are known to the compiler. These intrinsics enable you to easily incorporate domain-specific operations in C and C++ source code without resorting to complex implementations in assembly language.

What are intrinsics programming?

''Intrinsics'' are those features of a language that a compiler recognizes and implements without any need for the program to declare them. The compiler may—or may not—link to a runtime library to perform the operation.

What is intrinsics in Java?

What Are Intrinsics? An intrinsic function is a function that has special handling by the compiler or interpreter for our programming language. More specifically, it's a special case where the compiler or interpreter can replace the function with an alternative implementation for various reasons.

What is inline assembler in arm?

Inline Assembly armclang provides an inline assembler that enables you to write assembly language sequences in C and C++ language source files. The inline assembler also provides access to features of the target processor that are not available from C or C++.


Video Answer


1 Answers

You cannot use compiler intrinsics because they are processed by the Delphi compiler rather than the assembler. Intrinsics are resolved by the Pascal compiler processing and parsing Pascal expressions, and then emitting code. That's the job of a compiler rather than an assembler. At least, that's my mental model.

In the case of SizeOf you need to use the type assembly expression operator:

add eax, type NativeInt

Or indeed:

function Test: integer;
asm
  mov eax, type NativeInt
end;

This function performs as you expect.

Documentation here: Assembly Expressions, Expression Operators.

And yes, the fact that your code compiles should be considered a bug.

like image 90
David Heffernan Avatar answered Oct 11 '22 10:10

David Heffernan