Why can't I use the built-in compiler functions in the asm block?

Is this a compiler error?

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 displays all kinds of things except 4/8.

Is this a bug or is it documented that I cannot use SizeOf and other built-in compiler functions in assembler?
If I wanted to use SizeOf(xx) in an asm block, what should I do?

+7
assembly delphi
source share
1 answer

You cannot use the built-in compilers because they are handled by the Delphi compiler, not by assembler. Intrinsics are resolved by Pascal compiler processing and parse Pascal expressions and then emit code. This is the task of the compiler, not the collector. At least this is my mental model.

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

 add eax, type NativeInt 

Or really:

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

This function performs as you expect.

The documentation is here: Assembly expressions, Expression operators .

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

+7
source share

All Articles