Playing with ASM and Modulo

Share your advanced PureBasic knowledge/code with the community.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Playing with ASM and Modulo

Post by Flype »

Code updated for 5.20+

hello,

just trying to play with inlined asm. i make a little modulo proc but it seems not faster than the classic procedure :cry: . is it well coded ? i just start with asm so i've many things to learn :wink:

Code: Select all

;-------------------------------------------

Procedure.l Modulo( a.l, b.l )
  ProcedureReturn a - ( ( a / b ) * b )
EndProcedure

;-------------------------------------------

Procedure.l ModuloASM( a.l, b.l )
  EnableASM
    MOV  EAX, [ESP]
    CDQ
    IDIV DWORD [ESP+4]
    MOV  EAX, EDX
    ProcedureReturn
  DisableASM
EndProcedure

;-------------------------------------------

Global result.l, tick.l

Procedure timing()
  res = GetTickCount_()
  If tick = 0
    tick = res
  Else
    MessageRequester( "Resultat", Str(result)+" "+Str(res-tick),0)
    tick = 0
  EndIf
EndProcedure

;-------------------------------------------

#NBTEST = 50000000

timing()
For i=0 To #NBTEST
  result = Modulo( 32, 17 )
Next
timing()

timing()
For i=0 To #NBTEST
  result = ModuloASM( 32, 17 )
Next
timing()

;-------------------------------------------
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
LJ
Enthusiast
Enthusiast
Posts: 177
Joined: Wed Apr 30, 2003 4:00 pm

I like it.

Post by LJ »

I like it. What would be great is that if you can share your thoughts on why this works. Explain each line in as simple language as possible. I can create some graphics to visually illustrate what is happening here if you'd like. But do explain what is happening here and why this works, teaching others what you are learning about ASM is a GREAT way to learn it better yourself.

As to the speed issue, this is probably not a good benchmark program to use because, for basic math operations, Purebasic is already producing highly optimized assembly code, that is why your .exes are so small compared to other languages.

Basically, if you come up with an ASM routine that performs math faster than Fred, you can look at it as competition with Fred. Fred probably already had this discussion with the first releases of Purebasic and he already has the math optimized pretty darn fast in the compiler output in assembly language. If you come up with a faster assembly routine than Fred, well that has got to be very advanced ASM programming and I think Fred would then add your ASM as output code on compiled maths. Trying to out think/speed Fred by creating ASM code faster than what Purebasic compiles to is advanced ASM coding indeed.

You may be able to outspeed Fred's assembly compiler output by fine tuning your code to a specific task. I would think that there is certain functions that Fred had to make a bit longer, in a generic sense because a certain command or operation can be used in a variety of ways and so ASM output code has to accomodate this different functionality and so is not as fine tuned as your assembly code that has only 1 specific purpose.

But all of this is way beyond my understanding and I'm only guessing about Fred needing to use ASM code that is, in some way more generic for the sake of greater functionality and multiple purposes. Perhaps its impossible to create ASM code in Purebasic Inline Assembly that is faster than Fred's compiled output, but I hope you keep trying because this is a fascinating topic and surely a noble and worthy goal to try and achieve.

Great Purebasic Inline Assembly coding and if you have the time, do explain each line by line and why it works.

Thanks again.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

This has been discussed for some time:

viewtopic.php?t=4806&start=0

Have a nice day,
El_Choni
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

thanx El_Choni, i didn't see it.

and LJ, pfffff, hard for me to read your post entirely. i'm a poor french trying to decode all what you've said... anyway, i've understood the most important. You said it's pretty impossible to make asm code faster than Fred's one. For sure, you're right, Fred's output code is piece of cake. I can't say the opposite. But if it was true in ALL case, why Fred created such an inlined asm feature ? The answer may be : to allow users to do optimized things and perhaps for others mad users like me : to play with... I'm not, in any way, fighting against Fred. I'm not doing stupid competition. As i said in first post, i'm just playing... but i'm playing and learning with the goal to do something interessant. Are you sure you wants me to explain the lines of my procs ? i've read some docs, some tutorials (from you in this forum) and in confirmed asm user web sites and that's all... It could be very long to explains four line of code... and i'm a bit lazy today... But if you want, i will. I see in your tutorial how important it is for you to explain what we do, so...
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
LJ
Enthusiast
Enthusiast
Posts: 177
Joined: Wed Apr 30, 2003 4:00 pm

Hmmm...

Post by LJ »

I'm not saying you are in competition with Fred in a literal sense, but rather in a figurative sense (trying to explain the difference between literal and figurative is hard enough when two people speak the same language!)

Yes, I'm certain I would like you to explain line for line what your code does. I know the language barrier would make this a little challenging for you to do in English, but do consider it when you are not so tired.

I think the most wonderful messages in the forum are those that teach a little something, and then give a working example in code, especially with Purebasic Inline Assembly because, before my Purebasic Inline Assembly tutorial, there was so very little information at all on this exciting capability of Purebasic.

I hope you keep writing Purebasic Inline Assembly code and posting it. Thanks again for sharing this.

Peace
Lj
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

thanx lj, you're nice. i will do, not today, but tomorrow or soon.
Image
peace, too :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Fred
Administrator
Administrator
Posts: 18226
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

In fact, the PureBasic output is good, yes, but not impossible to beat at all. Means it's a compiler output, which is much more generic as it has to handle numberous of cases and an hand optimized code will beat it for sure, that's why there an inline ASM function. And that's why you can freely examine the commented ASM code, to help you to see how can be achieved one or another task. Think than when you put code in a procedure, there is code 'around' the procedure (especially registers saving) which can take some times and makes the result false. Also, in your test, there is more code all around the functions than the procedure to tests: 1 For/Next loop, 1 Variable assignement, 1 procedure call. Better compare the PureBasic ASM output directly with your routine with a Gosub (which as no overhead) for a fair comparison.
LJ
Enthusiast
Enthusiast
Posts: 177
Joined: Wed Apr 30, 2003 4:00 pm

Hi Fred

Post by LJ »

Hi Fred,

What do you mean by this:
"And that's why you can freely examine the commented ASM code, to help you to see how can be achieved one or another task."

Where is the commented ASM code to examine?
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

I think he means the ASM generated by passing /COMMENTED to the compiler..
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

Karbon wrote:I think he means the ASM generated by passing /COMMENTED to the compiler..
/Commented and /Reasm are compilerswitches for manual compiling a sourcecode.if you type (in a consolewindow) for example C:\Purebasic\Compilers\PBCompiler /? all possible switches are shown.
I highly recommented to study them .See also the Help-file for 'Using Shell-compiler'
LJ wrote: Where is the commented ASM code to examine?
In the compilersfolder there is a file Purebasic.asm generated.
SPAMINATOR NR.1
LJ
Enthusiast
Enthusiast
Posts: 177
Joined: Wed Apr 30, 2003 4:00 pm

OK

Post by LJ »

Awesome stuff guys. Very cool looking at the .asm file. Thanks Rings, Karbon, Fred, and Flype.
Post Reply