views:

625

answers:

7

So I just found out GCC could do inline assembly and I was wondering two things:

  1. What's the benefit of being able to inline assembly?

  2. Is it possible to use GCC as an assembly compiler/assembler to learn assembly?

I've found a couple articles but they are all oldish, 2000 and 2001, not really sure of their relevance.

Thanks

+5  A: 

Inline assembly is generally used to access hardware features not otherwise exposed by the compiler (e.g. vector SIMD instructions where no intrinsics are provided), and/or for hand-optimizing performance critical sections of code where the compiler generates suboptimal code.

Certainly there is nothing to stop you using the inline assembler to test routines you have written in assembly language; however, if you intend to write large sections of code you are better off using a real assembler to avoid getting bogged down with irrelevancies. You will likely find the GNU assembler got installed along with the rest of the toolchain ;)

moonshadow
+1  A: 
  1. The benefit of embedding custom assembly code is that sometimes (dare I say, often times) a developer can write more efficient assembly code than a compiler can. So for extremely performance intensive items, custom written assembly might be beneficial. Games tend to come to mind....

  2. As far as using it to learn assembly, I have no doubt that you could. But, I imagine that using an actual assembly SDK might be a better choice. Aside from the standard experimentation of learning how to use the language, you'd probably want the knowledge around setting up a development environment.

Frank V
I would not dare to say "often times". Few developers can write better assembler than a compiler. It's kinda like being a high-level wizard.
Tom
You are right, however I took some liberty with regards who might be using such a feature. But, point noted.
Frank V
+8  A: 

The benefit of inline assembly is to have the assembly code, inlined (wait wait, don't kill me). By doing this, you don't have to worry about calling conventions, and you have much more control of the final object file (meaning you can decide where each variable goes- to which register or if it's memory stored), because that code won't be optimized (assuming you use the volatile keyword).

Regarding your second question, yes, it's possible. What you can do is write simple C programs, and then translate them to assembly, using

gcc -S source.c

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

There's some material online.

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

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

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

Hope it helps.

Tom
+5  A: 

Inline Assembly is useful for in-place optimizations, and access to CPU features not exposed by any libraries or the operating system.

For example, some applications need strict tracking of timing. On x86 systems, the RDTSC assembly command can be used to read the internal CPU timer.

Time Stamp Counter - Wikipedia

Using GCC or any C/C++ compiler with inline assembly is useful for small snippets of code, but many environments do not have 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 preferable to create specific items in a suitable environment (GNU assembler, MASM) and import them projects as needed.

meklarian
+2  A: 

You should not learn assembly language by using the inline asm feature.

Regarding what it's good for, I agree with jldupont, mostly obfuscation. In theory, it allows you to easily integrate with the compiler, because the complex syntax of extended asm allows you to cooperate with the compiler on register usage, and it allows you to tell the compiler that you want this and that to be loaded from memory and placed in registers for you, and finally, it allows the compiler to be warned that you have clobbered this register or that one.

However, all of that could have been done by simply writing standard-conforming C code and then writing an assembler module, and calling the extension as a normal function. Perhaps ages ago the procedure call machine op was too slow to tolerate, but you won't notice today.

I believe the real answer is that it is easier, once you know the contraint DSL. People just throw in an asm and obfuscate the C program rather than go to the trouble of modifying the Makefile and adding a new module to the build and deploy workflow.

DigitalRoss
+1  A: 
  1. Manual optimization of loops that are executed a lot. This article is old, but can give you an idea about the kinds of optimizations hand-coded assembly is used for.

  2. You can also use the assembler gcc uses directly. It's called as (see man as). However, many books and articles on assembly assume you are using a DOS or Windows environment. So it might be kind of hard to learn on Linux (maybe running FreeDOS on a virtual machine), because you not only need to know the processor (you can usually download the official manuals) you code for but also how hook to into the OS you are running.

A nice beginner book using DOS is the one by Norton and Socha. It's pretty old (the 3rd and latest edition is from 1992), so you can get used copies for like $0.01 (no joke). The only book I know of that is specific to Linux is the free "Programming from the Ground Up"

mdm
+2  A: 

This isn't really an answer, but kind of an extended comment on other peoples' answers.

Inline assembly is still used to access CPU features. For instance, in the ARM chips used in cell phones, different manufacturers distinguish their offerings via special features that require unusual machine language instructions that would have no equivalent in C/C++.

Back in the 80s and early 90s, I used inline assembly a lot for optimizing loops. For instance, C compilers targeting 680x0 processors back then would 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 haven't needed to do that in, oh, probably fifteen years, because modern compilers are much smarter. In fact, current compilers will sometimes generate more efficient code than most humans would. Especially given CPUs with long pipelines, branch prediction, and so on, the fastest-executing sequence of instructions is not always the one that would make most sense to a human. So you can say, "Do A B C D in that order", and the compiler will scramble the order all around for greater efficiency.

Playing a little with inline assembly is fine for starters, but if you're serious, I echo those who suggest you move to a "real" assembler after a while.

Bob Murphy