Page 1 of 1
Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 4:34 pm
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,
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 6:56 pm
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?
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 7:09 pm
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.
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 7:14 pm
by wilbert
It probably depends on the C++ compiler.
Some use Intel style ASM, other AT&T.
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 7:29 pm
by Fred
It's not a PB related question, moved to off topic
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 8:41 pm
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
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 8:56 pm
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.
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Thu Jun 13, 2013 9:15 pm
by Crusiatus Black
Thank you, using that as reference I discovered -masm=intel will also
enable the intel style. I've got a head start now, cheers!
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Fri Jun 14, 2013 12:48 am
by Fred
The best is to write in in FASM, create a .lib and use it from C
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Fri Jun 14, 2013 1:25 am
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.
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Sun Jun 16, 2013 4:25 pm
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.
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Sat Jun 22, 2013 8:53 am
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.
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Sat Jun 22, 2013 10:32 am
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).
Re: Translating PB Inline ASM to C++ Inline ASM
Posted: Sun Jun 23, 2013 10:49 am
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.