Results 1 to 21 of 21

Thread: Why is this ASM slower than C++?

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Why is this ASM slower than C++?

    PHP Code:
    int inline MulAdd__asm(int valint mint a)
    {
        
    __asm
        
    {
            
    mov eaxval
            imul eax
    m
            add eax
    a
        
    }
    }

    int MulAdd__cpp(int valint mint a)
    {
        return (
    val m) + a;

    Why is the inline ASM function about 3 times slower than the C++ version as shown above?
    I don't live here any more.

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Why is this ASM slower than C++?

    I'm not sure if this would do it, but add is mainly done with 16 bit registers/memory. Same holds true with mul. Try iadd eax, a.

    Also isn't it suppose to be written like this:

    inline int MulAdd__asm(int val, int m, int a)

    Where the inline and int are swapped around?

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Why is this ASM slower than C++?

    Woss, try it without inlining.

    Jacob, iadd doesn't exist

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Why is this ASM slower than C++?

    I thought it did? They got fadd, imul, fmul, and why not iadd? Weird.

  5. #5
    Member
    Join Date
    Jan 2004
    Posts
    37

    Re: Why is this ASM slower than C++?

    What does the c++ code compile too?

    Any chance you call the function with fixed values, so the multiply and addition are optimized away, making the function simply return a fixed number?

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Why is this ASM slower than C++?

    Quote Originally Posted by penagate
    Woss, try it without inlining.

    Jacob, iadd doesn't exist

    Oh and just to let wossy and others know, it's always best to only inline when there are more than 5 lines of code in your function/sub. Otherwise it will execute slower than the actual call. The reason why it is like this, I have no idea. Maybe someone can enlighten me.

  7. #7

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why is this ASM slower than C++?

    I have tried it with and without the inline keyword and the performance is identical.

    I don't claim to be any kind of ASM programmer but I was surprised by the outcome.

    Thanks for the insight.
    I don't live here any more.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Why is this ASM slower than C++?

    I've timed both and looked at the compiler listing and both functions compile to exactly the same code. So I don't understand why the inline ASM version is so much slower.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Why is this ASM slower than C++?

    Have you looked at the entire listing? The compiler can optimize across the C++ code, but it can't across the assembly.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Why is this ASM slower than C++?

    Thanks CornedBee, it was indeed elsewhere in the listing... Being set up for speed the compiler had inlined the functions and it had optimised the C++ version.

    Woss, you'll be pleased to know that turning off optimisations (forcing the compiler to make proper function calls) got me these results:
    Code:
    C++ code time: 782 ms
    ASM code time: 765 ms
    It was rather hasty and used GetTickCount but you can see the times are about the same, as you'd expect.

  11. #11

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why is this ASM slower than C++?

    Verrrrrry interrestink.

    Am I right in thinking that any pentium-targetted ASM code can be used in C++? Or does VC++ have a preferred flavour? I'm still struggling to find any tutorials that don't require "a basic understanding of registers and machine language" or a PHD in computer science. I could do with a decent beginner's book really. I'm finding it totally innacessible right now
    I don't live here any more.

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Why is this ASM slower than C++?

    This is the PDF book I learnt basic assembly from - "Programming From The Ground Up" by Jonathon Bartlett. It's targeted for Linux, and syntax is a bit different from VC++'s inline assembly, but the basics are all the same. It teaches you about CPU and memory architecutre and starts right from the basics.

  13. #13
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Why is this ASM slower than C++?

    How do you see the assembly code of a VC++.NET program? It somewhere in the menu. Can't seem to find it though. Maybe we can compare the assembly instructions with the ones in the function.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Why is this ASM slower than C++?

    Well, C++ only provides the asm keyword and states this:
    7.4 The asm declaration [dcl.asm]

    1 An asm declaration has the form
    asm-definition:
    asm ( string-literal ) ;
    The meaning of an asm declaration is implementation-defined. [Note:
    Typically it is used to pass information through the implementation to
    an assembler. ]
    As you see, the definition is minimal. It should also be mentioned that VC++ does not support this syntax, only the _asm {} syntax. GCC supports the syntax, but extends it for better interaction with the compiler.

    What code you can use thus depends on your compiler or the underlying assembler. For example, in VC++ the compiler needs to understand the assembly, thus disabling use of SSEx in VC++6 at least.
    GCC doesn't really understand any assembly, but it forwards it to the underlying assembler, and thus pretty much everything works, depending on the asembler.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Why is this ASM slower than C++?

    Quote Originally Posted by Jacob Roman
    How do you see the assembly code of a VC++.NET program? It somewhere in the menu. Can't seem to find it though. Maybe we can compare the assembly instructions with the ones in the function.
    I did that. They are exactly the same. The difference came from the fact that the compiler (when optimised for speed) inlined all of them and (for me) somehow eliminated my benchmark loop for the C++ version.

    To choose to see the ASM listing when you build an app go to Project -> xxx Properties -> C/C++ -> Output Files -> Assembler Output.

  16. #16
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Why is this ASM slower than C++?

    Thanks penagate, I'll do that

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Why is this ASM slower than C++?

    Woss, what's your ulitmate objective here? Are you simply trying to learn ASM, or are you trying to do custom functions to get a performance boost? I don't know if the latter is still worth the effort these days. There are so many considerations for actual execution speed, the casual optimizer will probably not do as well as the compiler optimizer.
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why is this ASM slower than C++?

    Well I was just playing around with the code really, I have no objective yet. I took delivery of my ASM book yesterday so I can learn it properly.
    I don't live here any more.

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Why is this ASM slower than C++?

    Ah, world domination then. Ok.
    My usual boring signature: Nothing

  20. #20

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why is this ASM slower than C++?

    Shhh don't tell everyone.
    I don't live here any more.

  21. #21
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Why is this ASM slower than C++?

    LOL.

    Some of you people just crack me up..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width