Recent

Author Topic: Inline assembly issue  (Read 1316 times)

Aik_T

  • New Member
  • *
  • Posts: 24
Inline assembly issue
« on: November 06, 2024, 04:25:46 pm »
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][+][-]
  1. function Division(A, B: Single): Single; assembler;
  2. asm
  3.   Fld A
  4.   Fdiv B
  5.  
  6.   Fst @Result
  7. end;
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

  • Administrator
  • Hero Member
  • *
  • Posts: 11941
  • FPC developer.
Re: Inline assembly issue
« Reply #1 on: November 06, 2024, 04:31:20 pm »
Did you enabled {$asmmode intel} ?

Aik_T

  • New Member
  • *
  • Posts: 24
Re: Inline assembly issue
« Reply #2 on: November 06, 2024, 04:37:37 pm »
Did you enabled {$asmmode intel} ?
Of course, as I said above, I've already played around with integer operations :)

Kays

  • Hero Member
  • *****
  • Posts: 613
  • Whasup!?
    • KaiBurghardt.de
Yours Sincerely
Kai Burghardt

Aik_T

  • New Member
  • *
  • Posts: 24

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Inline assembly issue
« Reply #5 on: November 06, 2024, 05:29:32 pm »
It is not sad at all, FPC is just more accurate than Delphi is. >:D
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11941
  • FPC developer.
Re: Inline assembly issue
« Reply #6 on: November 06, 2024, 06:36:16 pm »
Mostly from the old days when assembler syntax was mostly AT&T

Aik_T

  • New Member
  • *
  • Posts: 24
Re: Inline assembly issue
« Reply #7 on: November 06, 2024, 07:53:28 pm »
It is not sad at all, FPC is just more accurate than Delphi is. >:D
What do you mean?

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Inline assembly issue
« Reply #8 on: November 06, 2024, 08:18:58 pm »
If you want load a specific address's content instead of the address?
Code: Pascal  [Select][+][-]
  1. //intentionally stupid example for stupid people.
  2. asm
  3.   mov eax, dword ptr [ecx]
Happy coding... You have no clue.
Yes, Grumpy is alive and well,  >:D

(btw that code works but that was not my intention... :P )
« Last Edit: November 06, 2024, 08:23:14 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Aik_T

  • New Member
  • *
  • Posts: 24
Re: Inline assembly issue
« Reply #9 on: November 06, 2024, 08:23:33 pm »
If you want load a specific address's content instead of the address?
Code: Pascal  [Select][+][-]
  1. //intentionally stupid example for stupid people.
  2. asm
  3.   mov eax, dword ptr [ecx]
Happy coding... You have no clue.
Yes, Grumpy is alive and well,  >:D
If this is some kind of local humor, I don’t understand it

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Inline assembly issue
« Reply #10 on: November 06, 2024, 08:49:33 pm »
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... >:D >:D >:D O:-)
« Last Edit: November 06, 2024, 08:52:57 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Aik_T

  • New Member
  • *
  • Posts: 24
Re: Inline assembly issue
« Reply #11 on: November 06, 2024, 08:59:33 pm »
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... >:D >:D >:D O:-)
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

  • Full Member
  • ***
  • Posts: 117
Re: Inline assembly issue
« Reply #12 on: November 07, 2024, 12:03:31 am »
hello Aik_T
try this
Code: Pascal  [Select][+][-]
  1.     program Project1;
  2.     {$asmmode intel}
  3.     {$MODE OBJFPC}{$H+}
  4.  
  5.     function Division( var A, B:Single): Single; assembler;
  6.         asm
  7.         fld dword ptr [A]
  8.         Fdiv dword ptr [B]
  9.         Fstp dword ptr [Result]
  10.     end;
  11.     var A, B:Single;
  12.     begin
  13.         A:=7;
  14.         B:=9;
  15.         writeln(Division(A, B));
  16.         write('Press Return ');ReadLn;
  17.     end.
  18.  

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
« Last Edit: November 07, 2024, 12:08:34 am by srvaldez »

BildatBoffin

  • New Member
  • *
  • Posts: 30
Re: Inline assembly issue
« Reply #13 on: November 07, 2024, 01:16:12 am »
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][+][-]
  1. program Project1;
  2.  
  3. {$asmmode intel}
  4. {$MODE OBJFPC}{$H+}
  5.  
  6. function DivisionSSE(a, b: Single): Single; assembler;
  7. asm
  8.   divss xmm0, xmm1
  9. end;
  10.  
  11. function DivisionFPU(a, b: Single): Single; assembler;
  12. var
  13.   aa, bb: Single;
  14. asm
  15.   movss aa, xmm0
  16.   movss bb, xmm1
  17.   fld   dword ptr [aa]
  18.   fld   dword ptr [bb]
  19.   fdiv
  20.   fstp result
  21. end;
  22.  
  23. var
  24.   a, b: Single;
  25. begin
  26.   a := 10;
  27.   b := 5;
  28.   writeln(DivisionSSE(A, B));
  29.   writeln(DivisionFPU(A, B));
  30. end.

On windows that might be different.

Godbolt : https://godbolt.org/z/sPP3z1K7r

Aik_T

  • New Member
  • *
  • Posts: 24
Re: Inline assembly issue
« Reply #14 on: November 07, 2024, 06:38:19 am »
hello 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

 

TinyPortal © 2005-2018