3 ms·
> In GCC, inline assembly has insane syntax. Probably on purpose, to discourage writing it. No, it's primarily for portability and flexibility. Other language
by ajross 2y ago
> In GCC, inline assembly has insane syntax. Probably on purpose, to discourage writing it.
No, it's primarily for portability and flexibility. Other languages have "inline assembly" that is really a separate 8086 assembler built into the C compiler. And that's good for some things (accessing values in the surrounding C code via natural expressions) and bad for others.
In particular, a gcc asm() block can do anything the underlying assembler can, and the gcc "constraint" syntax is designed to express the idea of "what rules must the compiler follow to generate code around this asm block".
Want to expand your asm block as a big string using preprocessor macrobatics? It's just a string, you can do that.
Want to write a macro that expands into a runtime constant that you want to link into a special section? No problem.
Want to call a function in a special ABI that is going to clobber a specific set of registers and need to tell the compiler about it? The gcc syntax has a constraint for that.
Basically, "inline assembly" in DOS tools was... fine. It handled the case of "1980's Compilers Suck and I Want to Hand-Optimize This Code". And that's fine. But that's not why people write assembly in the modern world, and gcc has a much broader reach.
- variadix 2y agoBeing able to use macro expansion, stringification, concatenation, etc. in GCC inline asm is a major advantage over how, e.g. MSVC handled inline asm. Getting the constraints right is the hard part, the syntax isn’t much of an issue beyond the initial learning curve imo.