Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do C developers work with assembly that's foreign to them?

Tags:

c

assembly

I was looking through a C code snippet when i came across this line of assembly code:

char *buf = alloca(0x2000);
asm volatile("" :: "m" (buf));

I don't know what this means. In my investigation, i've learned that that there are many different types of assembly languages (e.g., MASM, NASM, GAS, etc.), and in my (very limited) experience, the author rarely specifies which one they're using.

What does this line mean; and more importantly, how do C developers (presumably not versed in assembly) research assembly code they come across in this manner?

like image 241
Noob Saibot Avatar asked Oct 28 '14 16:10

Noob Saibot


People also ask

Does C get translated to assembly?

Compilers for high-level languages like C/C++ have the ability to translate high-level language into assembly code. The GNU C and C++ Compiler option of -S will generate an assembly code equivalent to that of the corresponding source program.

Why do programmers still use assembly language?

Assembly languages are also often used by programmers wanting greater control over their computers as assembly languages allow you to directly manipulate your hardware. Because of its speed and importance, some programs are specifically written using assembly language as the code can usually remain smaller.

Do programmers still use assembly?

Yes, absolutely. While a modern optimizing compiler can generally produce code that runs at least as fast as a typical assembly language programmer, there are still a number of situations where you will write assembly language code.

Is assembly language the same as C?

Assembly language is the more than low level and less than high-level language(such as C, C++, Java, Python, etc). So it is an intermediary language.


1 Answers

The snippet is neither MASM, GAS, NASM, etc. It is inline assembly, and the syntax is documented in the C compiler's documentation.

The syntax is tricky even if you are already familiar with pure assembly because it has to specify how to connect the C part with the assembly part and vice-versa.

The statement asm volatile("" :: "m" (buf)); would typically be an empty bit of assembly (not a noop but an actual absence of instructions), with such binding instructions "m" that make the statement amount to a memory barrier from the point of view of the C compiler.

EDIT: a comment by StackOverflow user Jester below a now-deleted answer says that the purpose of the statement is more likely to prevent buf, and thus the alloca call, to be optimized out by the compiler by pretending that the assembly code "" reads from it.

I believe that the C11 standard offers cleaner ways to express memory barriers, but I haven't had the chance to investigate yet. Anyway, as a way to specify a memory barrier, the above can be a way to target “GCC and compilers that aim for GCC compatibility, even if slightly old” as a larger set of compilers than “C compilers correctly implementing all of the C11 standard”. Actually, the Wikipedia page on C11 cites asm volatile ("" : : : "memory"); as an example in the discussion of memory barriers.

like image 64
Pascal Cuoq Avatar answered Oct 31 '22 17:10

Pascal Cuoq