Using Assembly Language in C/C++

前端 未结 14 1288
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 02:39

I remember reading somewhere that to really optimize & speed up certain section of the code, programmers write that section in Assembly language. My questions are -

相关标签:
14条回答
  • 2020-12-23 02:48

    use this:

    __asm__ __volatile__(/*assembly code goes here*/);

    the __asm__ can also just be asm.

    The __volatile__ stops the compiler from making further optimizations.

    0 讨论(0)
  • 2020-12-23 02:50

    There's very few reasons to use assembly language these days, even low-level constructs like SSE and the older MMX have built-in intrinsics in both gcc and MSVC (icc too I bet but I never used it).

    Honestly, optimizers these days are so insanely aggressive that most people couldn't match even half their performance writing code in assembly. You can change how data is ordered in memory (for locality) or tell the compiler more about your code (through #pragma), but actually writing assembly code... doubt you'll get anything extra from it.

    @VJo, note that using intrinsics in high level C code would let you do the same optimizations, without using a single assembly instruction.

    And for what it's worth, there have been discussions about the next Microsoft C++ compiler, and how they'll drop inline assembly from it. That speaks volumes about the need for it.

    0 讨论(0)
  • 2020-12-23 02:51

    (1) Yes, the easiest way to try this out is to use inline assembly, this is compiler dependent but usually looks something like this:

    __asm
    {
        mov eax, ebx
    }
    

    (2) This is highly subjective

    (3) Because you might be able to write more effective assembly code than the compiler generates.

    0 讨论(0)
  • 2020-12-23 02:54
    1. Yes. Use either inline assembly or link assembly object modules. Which method you should use depends on how much assembly code you need to write. Usually it's OK to use inline assembly for a couple of lines and switch to separate object modules once if it's more than one function.
    2. Definitely, but sometimes it's necessary. The prominent example here would be programming an operating system.
    3. Most compilers today optimize the code you write in a high-level language much better than anyone could ever write assembly code. People mostly use it to write code that would otherwise be impossible to write in a high-level language like C. If someone uses it for anything else means he is either better at optimization than a modern compiler (I doubt that) or just plain stupid, e.g. he doesn't know what compiler flags or function attributes to use.
    0 讨论(0)
  • 2020-12-23 02:55

    Take a look here, where the guy improved performances 6 times using assembly code. So, the answer is : it is still being done, but the compiler is doing pretty good job.

    0 讨论(0)
  • 2020-12-23 02:56

    You should read the classic book Zen of Code Optimization and the followup Zen of Graphics Programming by Michael Abrash.

    Summarily in the first book he explained how to use assembly programming pushed to the limits. In the followup he explained that programmers should rather use some higher level language like C and only try to optimize very specific spots using assembly, if necessary at all.

    One motivation of this change of mind was that he saw that highly optimized programs for one generation of processor could become (somewhat) slow in the next generation of the same processor familly compared to code compiled from a high level language (maybe compiler using new instructions for instance, or performance and behavior of existing ones changing from a processor generation to another).

    Another reason is that compilers are quite good and optimize aggressively nowaday, there is usually much more performance to gain working on algorithms that converting C code to assembly. Even for GPU (Graphic Cards processors) programming you can do it with C using cuda or OpenCL.

    There are still some (rare) cases when you should/have to use assembly, usually to get very fine control on the hardware. But even in OS kernel code it's usually very small parts and not that much code.

    0 讨论(0)
提交回复
热议问题