How do C developers work with builds that are alien to them?

I looked at a C code snippet when I came across this line of assembly code:

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

I do not know what it means. In my research, I learned that there are many different types of assembler languages ​​(for example, MASM, NASM, GAS, etc.), and in my (very limited) experience, the author rarely indicates which one they use.

What does this line mean; and more importantly, how do C developers (presumably not assembly-savvy) collect the research code they encounter in this way?

+5
source share
1 answer

This snippet is neither MASM, nor GAS, nor NASM, etc. This is an inline assembly , and the syntax is documented in the C compiler documentation.

The syntax is complex, even if you are already familiar with a clean assembly, because it should indicate how to connect part C to the part of the assembly and vice versa.

asm volatile("" :: "m" (buf)); operator asm volatile("" :: "m" (buf)); it is usually an empty assembly bit (not noop, but the actual lack of instructions), with binding instructions such as "m" that make the sum of the operator a memory barrier from the point of view of the C compiler.

EDIT: StackOverflow Jester's comment under the now deleted answer says that the purpose of the statement is to prevent buf , and therefore the alloca call, which will be optimized by the compiler, pretending to read assembly code from it "" .

I believe that the C11 standard offers cleaner ways to express memory barriers, but I have not yet had the opportunity to investigate. In any case, as a way to specify a memory barrier, the above could be a way of targeting "GCC and compilers that aim for GCC compatibility, even if they are a bit old," as a wider set of compilers than C compilers that correctly implement all C11 standard ". Actually, the C11 Wikipedia page cites asm volatile ("" : : : "memory"); as an example when discussing memory barriers.

+11
source

Source: https://habr.com/ru/post/1205717/


All Articles