Recent

Author Topic: Assembler Procedure  (Read 6539 times)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Assembler Procedure
« on: June 28, 2017, 09:08:48 pm »
What do i have to do to make this procedure compile in fpc mode?

Code: Pascal  [Select][+][-]
  1.   procedure CopySwapPixel(const Source, Destination : Pointer); assembler;
  2.   asm
  3.     push ebx
  4.     mov bl,[eax+0]
  5.     mov bh,[eax+1]
  6.     mov [edx+2],bl
  7.     mov [edx+1],bh
  8.     mov bl,[eax+2]
  9.     mov bh,[eax+3]
  10.     mov [edx+0],bl
  11.     mov [edx+3],bh
  12.     pop ebx
  13.   end;
  14.  
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Assembler Procedure
« Reply #1 on: June 28, 2017, 09:39:23 pm »
You don't mention what problem you are having. If the origin of the assembler is Delphi, did you enable delphi mode ?

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Assembler Procedure
« Reply #2 on: June 28, 2017, 09:48:14 pm »
That asm code can't be compiled, this is the error message:

Quote
unit1.pas(66,8) Error: Unknown identifier "EBX"

It can't be compiled on both mode delphi and objfpc.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Assembler Procedure
« Reply #3 on: June 28, 2017, 09:55:28 pm »
Here it compiles (3.0.2/x86) in mode Delphi, and I get the errors you mention on mode objfpc.

Maybe you placed the $mode wrong and it is ignored?

Thaddy

  • Hero Member
  • *****
  • Posts: 14375
  • Sensorship about opinions does not belong here.
Re: Assembler Procedure
« Reply #4 on: June 28, 2017, 10:02:09 pm »
Here it compiles (3.0.2/x86) in mode Delphi, and I get the errors you mention on mode objfpc.

Maybe you placed the $mode wrong and it is ignored?
It is enough to include {$ASMMODE INTEL} in mode objfpc?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Assembler Procedure
« Reply #5 on: June 28, 2017, 10:36:16 pm »
It is enough to include {$ASMMODE INTEL} in mode objfpc?

Much better. Now only push and pop produce errors:
Asm: [push reg32] invalid combination of opcode and operands
Asm: [pop reg32] invalid combination of opcode and operands
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

feds

  • New Member
  • *
  • Posts: 33
Re: Assembler Procedure
« Reply #6 on: June 28, 2017, 10:41:01 pm »
Pascal, you need to add a mode instruction to select the correct asm syntax.

The following code compiles on my Win 7 - 64 bit box (not sure what you are using):

Code: Pascal  [Select][+][-]
  1. {$asmmode intel}  {FPC knows more than one asm syntax. Hence help it ;) }
  2. procedure CopySwapPixel(const Source, Destination : Pointer); assembler;
  3.  asm
  4.  push rbx       {On a 64-bit system u need to push 64-bit registers: ebx --> rbx}
  5.  mov bl,[eax+0]
  6.  mov bh,[eax+1]
  7.  mov [edx+2],bl
  8.  mov [edx+1],bh
  9.  mov bl,[eax+2]
  10.  mov bh,[eax+3]
  11.  mov [edx+0],bl
  12.  mov [edx+3],bh
  13.  pop rbx
  14.  end;
  15.  

Note that i only compile it. I didn't try if it execute as you expect :)
Please let us know if this works for you.

Regards
Feds
« Last Edit: June 28, 2017, 10:55:11 pm by feds »

Thaddy

  • Hero Member
  • *****
  • Posts: 14375
  • Sensorship about opinions does not belong here.
Re: Assembler Procedure
« Reply #7 on: June 28, 2017, 11:12:27 pm »
It is enough to include {$ASMMODE INTEL} in mode objfpc?

Much better. Now only push and pop produce errors:
Asm: [push reg32] invalid combination of opcode and operands
Asm: [pop reg32] invalid combination of opcode and operands
Yeah... you forgot to tell me you are on a 64bit platform did you?
Maybe the code from feds works, but usually I would write that with lea syntax for 64 bit.
What you also should do is check what FPC does in pure pascal.
That's because the nutheads that want to write everything in assembler because it is faster are most often (I used to be one of them a long time ago) mistaken. >:( >:D 8-)
And it is easier to adapt the assembler output from FPC in something a little! more efficient. O:-)
So write in Pascal, compile with -s and examine assembler output. ALWAYS do that first! Specifically here, where there is something the compiler can optimize extremely good because it is so simple for a compiler!
Oh, and you never knew xchg byte ptr bl,al?
« Last Edit: June 28, 2017, 11:24:23 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Assembler Procedure
« Reply #8 on: June 28, 2017, 11:21:37 pm »
It is enough to include {$ASMMODE INTEL} in mode objfpc?

Much better. Now only push and pop produce errors:
Asm: [push reg32] invalid combination of opcode and operands
Asm: [pop reg32] invalid combination of opcode and operands
Yeah... you forgot to tell me you are on a 64bit platform did you?
right :D

Maybe the code from feds works, but usually I would write that with lea syntax for 64 bit.
Yes it does.
I don't know intel assembler at all. Last time i programmed in assembler was on my Amiga in the beginning of the 90th  8-)

What you also should do is check what FPC does in pure pascal.
That's because the nutheads that want to write everything in assembler because it is faster are most often (I used to be one of them a long time ago) mistaken. >:( >:D 8-)
And it is easier to adapt the assembler output from FPC in something a little! more efficient. O:-)
So write in Pascal, compile with -s and examine assembler output. ALWAYS do that first! Specifically here, where there is something the compiler can optimize extremely good because it is so simple for a compiler!
This code was in a unit i used years ago when i last used Delphi and i just wanted to port this little opengl test project to fpc. ;)
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 14375
  • Sensorship about opinions does not belong here.
Re: Assembler Procedure
« Reply #9 on: June 28, 2017, 11:27:23 pm »
Well, I was editing one last remark  O:-) when you replied,but that code sucks.
On intel there's the xchg instruction that simply swaps the bytes. Job done. And the cycles are..... tada.... NONE.
Internal assignment as per intel documentation. Just needs a cycle for final assignment. I suspect the compiler will do something like that....
« Last Edit: June 28, 2017, 11:42:54 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

feds

  • New Member
  • *
  • Posts: 33
Re: Assembler Procedure
« Reply #10 on: June 28, 2017, 11:50:33 pm »
On intel there's the xchg instruction that simply swaps the bytes.

Don't forget BSWAP which might become very usefull if you want to swap 32-bit values.

However, i agree that the need for real asm hacks are rare (and mostly unportable) on modern systems.

Regards
Feds
 

 

TinyPortal © 2005-2018