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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With