Translating PB Inline ASM to C++ Inline ASM

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

Hey clever folk!

I've got this procedure in PureBasic, which performs a XOR operation on all bytes in buffer 1,
using the byte at the same location from buffer 2. Now, I understand this isn't the best code
out there (I'm no ASM expert), but can anyone give me some pointers as to how I could translate
this to C++'s inline assembly support?

C++ inline assembly goes against everything I know (even though I know little) about ASM.

Code: Select all

Procedure.i xorblocks(*block1, *block2, length.l)
  !PUSH     EBP
  !MOV      EBP, ESP
  
  !MOV      ESI, [EBP + 8]
  !MOV      EDI, [EBP + 12]
  !MOV      ECX, [EBP + 16]
  
  !XOR      EAX, EAX

  !xbl_init:
    !MOV    AL, [ESI]
    
    !XOR    AL, [EDI]
    !MOV    [ESI], AL
    !INC    ESI
    !INC    EDI
    !DEC    ECX
    !JNE    xbl_init

  !POP      EBP
  !RETN     12
EndProcedure
Cheers,
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Demivec »

Crusiatus Black wrote:Now, I understand this isn't the best code
out there (I'm no ASM expert), but can anyone give me some pointers as to how I could translate
this to C++'s inline assembly support?

C++ inline assembly goes against everything I know (even though I know little) about ASM.[/code]
Wouldn't it make more sense to ask this on a C++ forum?
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

I'm not sure, for some reason I figured that PB developers with Assembly knowledge
would somewhere include a C++ developer who encountered the same issue, however
I doubt that I'll find many PB developers with Assembly knowledge on a C++ forum.

I know, it sounds silly, but it seemed logical to post my question here as there are many
C++ developers over here as well.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Translating PB Inline ASM to C++ Inline ASM

Post by wilbert »

It probably depends on the C++ compiler.
Some use Intel style ASM, other AT&T.
Windows (x64)
Raspberry Pi OS (Arm64)
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Fred »

It's not a PB related question, moved to off topic
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

wilbert wrote:It probably depends on the C++ compiler.
Some use Intel style ASM, other AT&T.
Does G++ use Intel style ASM? If so, could you point me to a clear reference I
could use? I've searched for several hours, however I haven't found something
clear enough to me.

@Fred, it is regarding translating a PureBasic procedure using PureBasic inline
fASM, but fair enough, I understand. :p
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Translating PB Inline ASM to C++ Inline ASM

Post by wilbert »

The GNU compiler uses AT&T style by default.
I found a page that explains the differences a bit
http://www.ibiblio.org/gferg/ldp/GCC-In ... HOWTO.html

As far as I understand there's a directive
.intel_syntax
that allows you to use Intel style.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

wilbert wrote:The GNU compiler uses AT&T style by default.
I found a page that explains the differences a bit
http://www.ibiblio.org/gferg/ldp/GCC-In ... HOWTO.html

As far as I understand there's a directive
.intel_syntax
that allows you to use Intel style.
Thank you, using that as reference I discovered -masm=intel will also
enable the intel style. I've got a head start now, cheers!
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Fred »

The best is to write in in FASM, create a .lib and use it from C
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

Fred wrote:The best is to write in in FASM, create a .lib and use it from C
Ahh, good point! Why on earth didn't I think of that... Thanks.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Thorium
Addict
Addict
Posts: 1308
Joined: Sat Aug 15, 2009 6:59 pm

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Thorium »

Just write it in C/C++ code.
It might even run faster than the asm version, because the posted asm version is very much unoptimized and the compiler will generate a more optimized version from C/C++ code.

For example:
-Loop isnt aligned.
-Loop isnt unrolled.
-Instructions order is bad for pipelining.
-Without using SIMD you could xor 4 byte at a time using 32bit registers. I dont know if the compiler will to that code transformation for you, but it would be possible.
-With SIMD you could xor 16 (or even 32) bytes at a time using 128bit or 256bit registers.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

Thorium wrote:Just write it in C/C++ code.
It might even run faster than the asm version, because the posted asm version is very much unoptimized and the compiler will generate a more optimized version from C/C++ code.

For example:
-Loop isnt aligned.
-Loop isnt unrolled.
-Instructions order is bad for pipelining.
-Without using SIMD you could xor 4 byte at a time using 32bit registers. I dont know if the compiler will to that code transformation for you, but it would be possible.
-With SIMD you could xor 16 (or even 32) bytes at a time using 128bit or 256bit registers.
Thank you for these pointers, I've actually coded some of my procedures in Assembly now and tried optimizing
them a bit. I'm not an expert with Assembly, so these 5 comments are useful to me. I don't want to code it
in C++ just because my initial try wasn't good enough.

I did run a few tests, MinGW does not generate particularly faster code than doing it in Assembly, especially
when the ASM is optimized. Also, I'm using this to learn more and more about assembly.

Cheers.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Fred »

You should try with VC++ (express edition is free and comes with the real MS compiler, which is known to be very good at optimizations).
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Translating PB Inline ASM to C++ Inline ASM

Post by Crusiatus Black »

Fred wrote:You should try with VC++ (express edition is free and comes with the real MS compiler, which is known to be very good at optimizations).
Will do, but learning optimized assembly is not discouraged, right? :) Thanks.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Post Reply