Inline assembly issue lazarus / 21-General ---------------------------------------------------------------------- Aik_T · 2024-11-06 15:25 Hi all, I wanted to learn more about assembly and decided to start with simple assembly inserts for Free Pascal. But I ran into a problem. I tried operations with integer digits, no problems with them. But when trying to do something with floating-point numbers, I keep getting an error: Error: Asm: [fld reg???] invalid combination of opcode and operands Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Division(A, B: Single): Single; assembler;asm  Fld A  Fdiv B   Fst @Resultend;At the same time, the same code works in Delphi CE and gives a correct answer: j := Division( 10, 2); >> 5.00000000000000E+0000 Why can't I use Fld in inline assembly? Or am I doing it wrong? ---------------------------------------------------------------------- marcov · 2024-11-06 15:31 Did you enabled {$asmmode intel} ? ---------------------------------------------------------------------- Aik_T · 2024-11-06 15:37 Quote from: marcov on November 06, 2024, 04:31:20 pmDid you enabled {$asmmode intel} ? Of course, as I said above, I've already played around with integer operations ---------------------------------------------------------------------- Kays · 2024-11-06 15:48 Quote from: Programmer’s GuideFloating point opcodes are currently not supported (except those which involve only floating point registers). ---------------------------------------------------------------------- Aik_T · 2024-11-06 16:19 Quote from: Kays on November 06, 2024, 04:48:22 pmQuote from: Programmer’s GuideFloating point opcodes are currently not supported (except those which involve only floating point registers).Oh... Sad. Thanks anyway ---------------------------------------------------------------------- Thaddy · 2024-11-06 16:29 It is not sad at all, FPC is just more accurate than Delphi is. ---------------------------------------------------------------------- marcov · 2024-11-06 17:36 Mostly from the old days when assembler syntax was mostly AT&T ---------------------------------------------------------------------- Aik_T · 2024-11-06 18:53 Quote from: Thaddy on November 06, 2024, 05:29:32 pmIt is not sad at all, FPC is just more accurate than Delphi is. What do you mean? ---------------------------------------------------------------------- Thaddy · 2024-11-06 19:18 If you want load a specific address's content instead of the address? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //intentionally stupid example for stupid people.asm  mov eax, dword ptr [ecx]Happy coding... You have no clue. Yes, Grumpy is alive and well,  (btw that code works but that was not my intention... ) ---------------------------------------------------------------------- Aik_T · 2024-11-06 19:23 Quote from: Thaddy on November 06, 2024, 08:18:58 pmIf you want load a specific address's content instead of the address? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //intentionally stupid example for stupid people.asm  mov eax, dword ptr [ecx]Happy coding... You have no clue. Yes, Grumpy is alive and well,  If this is some kind of local humor, I don’t understand it ---------------------------------------------------------------------- Thaddy · 2024-11-06 19:49 It siimply means that I am of the opinion is is obvious; and somebody did not read any official documentation first. It also means you are one of the young guns that think you can beat the compiler.... Well young guns can't and using pure assembler is usually stupid by itself... Grumpy... ---------------------------------------------------------------------- Aik_T · 2024-11-06 19:59 Quote from: Thaddy on November 06, 2024, 08:49:33 pmIt siimply means that I am of the opinion is is obvious; and somebody did not read any official documentation first. It also means you are one of the young guns that think you can beat the compiler.... Well young guns can't and using pure assembler is usually stupid by itself... Grumpy... I just didn't notice the note about Floating point opcodes. I'm not going to learn assembler, I was interested to see what at least basic arithmetic operations are like. Doing this via assembly inlines in Pascal seemed to me the least painful option. It's just for fun and curiosity ---------------------------------------------------------------------- srvaldez · 2024-11-06 23:03 hello Aik_T try this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     program Project1;    {$asmmode intel}    {$MODE OBJFPC}{$H+}     function Division( var A, B:Single): Single; assembler;        asm        fld dword ptr [A]        Fdiv dword ptr [B]        Fstp dword ptr [Result]    end;    var A, B:Single;    begin        A:=7;        B:=9;        writeln(Division(A, B));        write('Press Return ');ReadLn;    end.  the inline assembler refuses to work if the parameters are not passed by reference, also note that dword is for single and qword is for double ---------------------------------------------------------------------- BildatBoffin · 2024-11-07 00:16 Assuming targeting X86_64 and using the systemV ABI then the parameters are passed in XMM0 and XMM1. You can then just do an SSE division and leave (result is in XMM0). If you really do want to use the FPU then you must copy the parameters to locals. And the rest is just like in the previous answer. Full code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$asmmode intel}{$MODE OBJFPC}{$H+} function DivisionSSE(a, b: Single): Single; assembler;asm  divss xmm0, xmm1end; function DivisionFPU(a, b: Single): Single; assembler;var  aa, bb: Single;asm  movss aa, xmm0  movss bb, xmm1  fld   dword ptr [aa]  fld   dword ptr [bb]  fdiv  fstp resultend; var  a, b: Single;begin  a := 10;  b := 5;  writeln(DivisionSSE(A, B));  writeln(DivisionFPU(A, B));end. On windows that might be different. Godbolt : https://godbolt.org/z/sPP3z1K7r ---------------------------------------------------------------------- Aik_T · 2024-11-07 05:38 Quote from: srvaldez on November 07, 2024, 12:03:31 amhello Aik_T the inline assembler refuses to work if the parameters are not passed by reference, also note that dword is for single and qword is for double Thank you very much! I'll try it a little later ---------------------------------------------------------------------- Aik_T · 2024-11-07 05:45 Quote from: BildatBoffin on November 07, 2024, 01:16:12 amAssuming targeting X86_64 and using the systemV ABI then the parameters are passed in XMM0 and XMM1. You can then just do an SSE division and leave (result is in XMM0). If you really do want to use the FPU then you must copy the parameters to locals. And the rest is just like in the previous answer. On windows that might be different. Thank you! I found such a variant in Daniel Kusswurm's book, but I was wondering how it works in a more classic version. Anyway, that's helpful, thanks ---------------------------------------------------------------------- Aik_T · 2024-11-07 06:24 Thanks everyone! The only thing is that you can't pass numbers directly to the function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Division(10, 2);You need to pass variables Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Division(A, B);Thanks everyone for helping ---------------------------------------------------------------------- srvaldez · 2024-11-07 12:31 Aik_T the following works on Windows x86 and x64 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     program Project1;    {$asmmode intel}    {$MODE OBJFPC}{$H+}     function Division( a, b:Single): Single; assembler;    {$ifdef CPU64}    var      x, y: Single;    {$endif}    asm        {$ifdef CPU32}            fld   dword ptr [a]            Fdiv  dword ptr [b]            Fstp  dword ptr [Result]        {$else}            movss x, xmm0          movss y, xmm1            fld   dword ptr [x]            fdiv  dword ptr [y]            Fstp  dword ptr [Result]        {$endif}    end;   var        x, y:Single;    begin        x:=10;        y:=5;        writeln(Division(x, y));        writeln(Division(25, 5));        write('Press Return ');ReadLn;    end. ---------------------------------------------------------------------- srvaldez · 2024-11-07 15:15 integer cube root code borrowed from Mparith Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     program Project1;    {$asmmode intel}    {$MODE OBJFPC}{$H+} // code borrowed from Mparith https://forum.lazarus.freepascal.org/index.php/topic,6844 7.msg528998.html#msg528998// file mp_base.bas, lines 1754 .. 1808// conditional compilation added by srvaldez     {For VPC, D2/D3; cardinal is only 31 bit}    {----------------------------------------- ----------------------------------}    function icbrt32(a: longint): longint;    {-Return the integer cube root sign(a)*floor(|a|^(1/3))}  var    x: longint;    begin    {Ref: http://www.hackersdelight.org/hdcodetxt/icbrt.c.txt}     {Note: There will be RTEs for large a if Range!? checking (not obverflow)}    {is done, e.g. in debug mode. Here a 32-bit BASM port of the Pascal code:}    x := abs(a);    if x>7 then begin      {y=ebx, y2=esi, s=cx, b=edi, x=eax}      asm        {$if defined(Win32)}             push edi            push esi            push ebx        {$elseif defined(Win64)}            push rdi            push rsi            push rbx        {$endif}            mov  eax,[x]            xor  ebx,ebx       {y  := 0}            xor  esi,esi       {y2 := 0}          mov  cx,30         {s  := 30}      @@1:  shl  esi,2         {y2 := y2 shl 2}            add  ebx,ebx            mov  edx,ebx          add  edx,esi       {edx:= y2+y}            mov  edi,edx            add  edi,edi            add  edi,edx       {edi:= 3*(y2+y)}          inc  edi            shl  edi,cl        {b  := 3*(y2 + y) + 1) shl s}          cmp  edi,eax            ja   @@2            sub  eax,edi     {x  := x - b}            add  esi,ebx            inc  ebx           {y  := y + 1}            add  esi,ebx       {y2 := y2 + y + (y+1)}    @@2:  sub  cx,3            jnl  @@1            mov  [x],ebx        {$if defined(Win32)}             pop  ebx            pop  esi          pop  edi        {$elseif defined(Win64)}            pop  rbx          pop  rsi            pop  rdi        {$endif}      end;      if a<0 then icbrt32 := -x      else icbrt32 := x;    end    else begin      { -7 <= a <= 7, ie icbrt = -1,0,1}      if a>0 then icbrt32 := 1      else if a<0 then icbrt32 := -1      else icbrt32 := 0;    end;    end; // test the integer cube root function    var        n, r:longint;    begin        n:=27;        r:=icbrt32(n);        writeln('the cube root of ', n, ' = ', r);        writeln('the cube root of 8 = ', icbrt32(8));    end. ---------------------------------------------------------------------- Aik_T · 2024-11-07 19:55 Quote from: srvaldez on November 07, 2024, 01:31:10 pmAik_T the following works on Windows x86 and x64 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     program Project1;    {$asmmode intel}    {$MODE OBJFPC}{$H+}     function Division( a, b:Single): Single; assembler;    {$ifdef CPU64}    var      x, y: Single;    {$endif}    asm        {$ifdef CPU32}            fld   dword ptr [a]            Fdiv  dword ptr [b]            Fstp  dword ptr [Result]        {$else}            movss x, xmm0          movss y, xmm1            fld   dword ptr [x]            fdiv  dword ptr [y]            Fstp  dword ptr [Result]        {$endif}    end;   var        x, y:Single;    begin        x:=10;        y:=5;        writeln(Division(x, y));        writeln(Division(25, 5));        write('Press Return ');ReadLn;    end. It works! Thanks!