I recently learned C ++ at a basic level (standard library + STL). I decided to learn a little assembler (I once taught him, I didn’t master it completely). Actually the question is, what advantages do asm inserts give in C ++ code, and how to arrange it.

PS Happy New Year everyone!

    4 answers 4

    Benefits:

    1. Manual optimization
    2. Using (again for optimization) processor commands that are not available through standard C ++ tools
    3. In the case of core programming, low-level access to equipment through I / O ports, to processor interrupts to create an abstraction layer.
    4. and others. like

    How to make - depends on the compiler and the platform.

    Happy New Year!

    • As for the design, it was beautiful in watcom. Inserts were drawn up as inline-procedures. - alexlz
    • as a child I did, when I had a Spectrumch :) - dУГ

    Although here they asked about the advantages, I believe that it is necessary to tell and about the disadvantages:

    1. inline-assembler is not in the standard. Different compilers can support it differently.
    2. Nekrossplatformennost. For each processor architecture you have to write your own code. Moreover, most compilers support only assembler x86 code. Programs compiled for x64 or ARM cannot use inline assembler.
    3. As for optimization: remember the 80/20 rule. On average, 20% of the code runs 80% of the time. It is necessary to optimize only this part of the code.
    4. Very often the code has the potential for optimization without using an assembler. Often the code can be accelerated several times, simply by changing the algorithm.
    5. If to rewrite the code on the assembler, without changing the algorithm, then it can be accelerated very slightly. And then, we must try very hard, because the compiler is not so stupid. To write code faster generated by the compiler you need to be a professional, unless, of course, you use advanced processor instructions that this compiler does not support.

    Instead of an assembler, it is better to use a well optimizing compiler, for example, intel C ++. It is also worth trying to use intrinsic-functions that are already written in assembler and optimized, and come with the compiler. In Visual C ++, examples of such functions are memcpy, memcmp, memmove.

    • one
      Yes, it's all clear and correct. In most cases, in normal applications there is no need to use the built-in assembler. Its main use is in nuclear programming, where without it is nowhere. Yes, and there they try to use it at a minimum to facilitate portability of the code. I personally once got a real benefit from using assembly language when using the bswap instruction on x64 to optimize the transfer of numbers from big endean to little endean. But familiar with this technology, as well as generally know the assembler, it is useful. - skegg 4:03 pm
    • > I personally once received a real benefit from using assembly language when using the bswap instruction on x64 to optimize the transfer of numbers from big endean to little endean. What for? Is this a weak point of the program? As I understand it, it was not a built-in assembler, since x64 inline-assembler is not in the compilers? Of course, if it is put into a separate inline-function with defines that choose an implementation depending on the processor, then there is nothing wrong with the assembler. - gammaker
    • Where did you get that built-in assembly in x64 is impossible at all? In gcc, Intel C ++ Compiler has been around for a long time and works great. In that program, it was one of the bottlenecks, since it was necessary to read data from files (several hundred gigabytes) made on Spark, and write to different files for processing on a PC. Allowed about 10% speed up the program. - skegg
    • > Where did you get that built-in assembly in x64 is impossible at all? In gcc, Intel C ++ Compiler has been around for a long time and works great. I do not know. Just so often write. Probably, they mean only Visual C ++, and I understood how all compilers are. I thought that in the processing of files their weak point was reading from the hard disk, and the processing itself takes a little time. - gammaker
    • I wrote: one of the bottlenecks. And there was a real benefit. - skegg 2:01 pm

    The advantage of asm-inserts for me is that I understand how it works, otherwise you know in someone else's compiler and the devil breaks his leg.

    • 2
      Then it is logical to write everything in assembler. - skegg
    • one
      Old Hochma What does "I understand how it works"? On modern processors, you cannot be sure that you know the sequence in which these commands are executed. You never know what you have written - the processor knows better what to do :) - alexlz
    • one
      Your position is strange. Why understand "how it works" when a programmer should focus on higher-level concepts? The user of your program anyway, what registers and instructions you use. What does not suit you in work of the compiler? - gammaker
    • one
      Right. With a very high probability we can say that it works as it is written. And if it is not clear what he wrote himself, then this is no longer an assembler to correct. - alexlz

    The advantage of asm-inserts for me is that I understand how it works, otherwise you know, in someone else's compiler and the devil will break his leg.

    I make it this way: I take files from VS2005: ml.exe,link.exe,lib.exe mspdb80.dll also cmd.exe .

    Then I write in assembler text in a notebook, for example:

     ;***************************** asm_CheckNechet.asm ************************************ ; Данный модуль возвращает 1 если число нечётное, в противном случае 0. ;************************************************************************************** ; Command Line (release): ; ml -c "-Fl$(IntDir)\$(InputName).lst" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)" ; Outputs: ; $(IntDir)\$(InputName).obj .686 .model flat, c .data .code asm_CheckNechet PROC pushad mov EBP, ESP ;************************************************************************************** mov ESI, DWORD PTR [EBP+36] ;загрузить указатель на входное число //int mov EDI, DWORD PTR [EBP+40] ;загрузить указатель на выходное число //float ;-------------------------------------------------------------------------------------- finit mov EAX, DWORD PTR [ESI] sar EAX, 1 ;сдвигаем вправо jc obxod1 fld1 fstp DWORD PTR [EDI] ;число чётное popad ret obxod1: fldz fstp DWORD PTR [EDI] ;число нечётное popad ret asm_CheckNechet endp end 

    Next, using the pre-assembled batch file, compile into the object manager:

     ml -c asm_CheckNechet.asm asm_CheckNechet.obj 

    Then I add to the library:

     lib /OUT:my.lib asm_CheckNechet.obj 

    Well, then I connect this library to the studio by simply putting it in the appropriate directory.

    In the studio, this function is declared as follows:

     extern "C" void asm_CheckNechet(const int*, float*); 
    • It is necessary either to enclose in backward quotes, or before each line to retreat 4 spaces. The answers have a magic button 010101 - insolor
    • This is not the assembler inserts, but the assembler itself. The question was about inserts. > Then add to the library. In my opinion, you can simply connect the obj file without a library. What are you two dug and diG? - gammaker pm
    • I do not see much point in writing batch file and so on. It is enough to set up processing of asm files in the studio .... 3 seconds Google and voila - habrahabr.ru/blogs/development/111275 is a little article ready ... (this is if we talk about individual modules on the asm) - and the debager will work ... - gote