GCC: inline assembly good?

So, I just found out that GCC can do inline build, and I was wondering two things:

  • What is the use of the inline build capability?

  • Can GCC be used as an assembly compiler / collector to learn assembly?

I found a couple of articles, but they are all old, 2000 and 2001, not really sure about their significance.

thank

+2


source to share


7 replies


The advantage of inline assembly is to have the assembly code inline (wait wait, don't kill me). By doing this, you don't have to worry about calling conventions, and you have much more control over the final object file (which means you can decide where each variable goes to - which register or if it's stored in memory), since this code won Optimized (if you use volatile keyword).

As for your second question, yes it is possible. What you can do is write simple C programs and then translate them into assembly using

gcc -S source.c

      

With this and the architecture guides ( MIPS , Intel, etc.) as well as GCC , you can go a long way.

There are some materials on the Internet.



http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/

The disadvantage of inline assembly is that usually your code will not be portable between different compilers.

Hope it helps.

+12


source


Inline Assembly is useful for in-place optimization and access to CPU functions that are not visible in any libraries or operating system.

For example, some applications require strict time tracking. On x86 systems, the RDTSC build command can be used to read the internal CPU timer.



Time counter - Wikipedia

Using GCC or any C / C ++ compiler with inline assembly is useful for small snippets of code, but many environments lack good debugging support, which will be more important when developing projects where inline assembly provides specific functionality. Also, portability will become a recurring issue if you use inline assembly. It is advisable to create certain elements in a suitable environment (GNU builder, MASM) and import them if necessary.

+9


source


Inline assembly is typically used to access hardware that has not been otherwise exposed by the compiler (for example, vector SIMD instructions that do not provide internal functionality) and / or to optimize the routine performance of critical sections of code where the compiler generates suboptimal code.

Of course, there is nothing that can stop the use of inline assembly to test routines written in assembly language; however, if you intend to write large sections of code, you are better off using real assembler so you don't get bogged down with irrelevance. You will most likely find the GNU builder installed along with the rest of the toolchain;)

+8


source


  • The advantage of injecting custom assembly code is that sometimes (dare I say, often times) a developer can write more efficient assembly code than the compiler can. Thus, for high-intensity items, a custom written assembly can be useful. Games tend to come to mind ....

  • As far as I can use it to learn assembly, I have no doubt that you could. But I suppose using the actual build SDK might be the best bet. Apart from the standard experimentation of learning how to use the language, you will probably need knowledge of setting up a development environment.

+3


source


You don't have to learn assembly language with the built-in asm function .

As to what it is good for, I agree with jldupont , mostly obfuscation. In theory, this allows you to easily integrate with the compiler, because the complex syntax of extended asm allows you to interact with the compiler when using registers, and it allows you to tell the compiler that you want this and that to be loaded from memory and put into registers for you. and finally, it allows the compiler to be warned that you chain this register or that one.

However, all of this could be done simply by writing standard C code and then writing an assembler module and calling the extension as a normal function. Perhaps many years ago the procedure for calling a car was too slow to tolerate, but today you will not notice.

I believe the real answer is that it's easier when you learn about DSL. People just throw in asm and obfuscate the C program instead of making it difficult to modify the Makefile and add a new module to the build and deployment process.

+2


source


This is not really an answer, but a kind of extended commentary on the answers of other peoples.

The inline assembly is still used to access CPU functionality. For example, in ARM chips used in cell phones, different manufacturers differentiate their offerings with special functions that require unusual machine language instructions that have no C / C ++ equivalent.

In the 80s and early 90s, I used inline assembly to optimize for loops. For example, C compilers targeting 680x0 processors would then do really stupid things like:

calculate a value and put it in data register D1
PUSH D1, A7     # Put the value from D1 onto the stack in RAM
POP D1, A7      # Pop it back off again
do something else with the value in D1

      

But I didn't have to do this, and maybe fifteen years, because modern compilers are much smarter. In fact, current compilers sometimes generate more efficient code than most people. Especially considering processors with long pipelines, branch prediction, etc., the fastest executable sequence of instructions is not always the one that makes the most sense to humans. So you can say "Do ABCD in this order" and the compiler will shrink the order around for more efficiency.

Playing around a bit with inline assembly is great for beginners, but if you're serious, I'm echoing those who suggest you go through "real" assembler after a while.

+2


source


  • Manual optimization for loops that run a lot. This article is old, but may give you an idea of ​​the kinds of optimizations that are hand-coded.

  • You can also use using gcc assembler directly. It is called as

    (see man as

    ). However, many building books and articles assume that you are using a DOS or Windows environment. So it can be difficult to learn on Linux (maybe FreeDOS runs in a virtual machine) because you not only need to know the processor (you can usually download the official guides) that you are coding for, but also how to connect to the OS you are running.

A good book for beginners using DOS is Norton and Soha. It's pretty old (3rd and latest edition is from 1992), so you can get used copies for $ 0.01 (no kidding). The only book I know of that is specific to Linux is the free "Programming from scratch"

+1


source







All Articles