1

Is it possible to write inline assembly (Intel syntax) with GCC or Clang, without needing to understand the clobber list "stuff"?

I'm going to guess "no" because the clobber list "stuff" ensures you don't over-write the register the compiler wrote to (immediately before your inline assembly begins)?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
intrigued_66
  • 16,082
  • 51
  • 118
  • 189
  • 1
    Yes, but that destroys the point of using inline assembly which is for seamless integration to compiler generated code. Also your access to variables will be limited. See the [basic asm section in the manual](https://gcc.gnu.org/onlinedocs/gcc/Basic-Asm.html). – Jester Feb 16 '20 at 17:45
  • @Jester Thanks. I assume without clobber lists is "basic asm", with clobber lists is "extended asm"? – intrigued_66 Feb 16 '20 at 17:47
  • Yes, that is correct. – Jester Feb 16 '20 at 17:51
  • 2
    No switches needed, unless you write assembly for the other dialect (e.g. `-masm=intel`). – Jester Feb 16 '20 at 18:04
  • 1
    You don't need extra switches to use inline asm in gcc. If you're having trouble, post your code. – Nate Eldredge Feb 16 '20 at 18:04
  • @Jester if you post an answer i'll accept, you were first. – intrigued_66 Feb 16 '20 at 18:47
  • It's not like I can beat Peter's :) Doesn't matter who was first go ahead and accept his. – Jester Feb 16 '20 at 18:48
  • @mezamorphic What are you trying to achieve by getting rid of clobber lists? Note that you can get away with using no clobber lists if you only ever access input and output operands without changing memory (other than said operands) or using extra registers. – fuz Feb 16 '20 at 18:55
  • 2
    @fuz, Although the question specifically asks about “clobber lists”, I think it is really asking about operand lists, too. (In other words, I think the question is confused about what part of the syntax is the clobber list.) – prl Feb 16 '20 at 21:13
  • 1
    To answer the question **as written** (ignoring the possible confusion about the meaning of clobber list): yes, hardly any of my inline assembly uses a clobber list, because it only modifies the registers assigned to output operands. – prl Feb 16 '20 at 21:16

1 Answers1

5

GNU C Basic inline asm statements (no operand/clobber lists) are not recommended for basically anything except maybe the body of an __attribute__((naked)) function. Why can't local variable be used in GNU C basic inline asm statements? (globals can't safely be used either.)

https://gcc.gnu.org/wiki/DontUseInlineAsm says to see ConvertBasicAsmToExtended for reasons not to use Basic asm statements. You can't really do anything safely in Basic asm; even asm("cli"); can get reordered with any memory accesses that aren't volatile.

If you're going to use inline asm at all (instead of writing a stand-alone function in asm, or C with intrinsics), you need to describe your string of asm instruction in exact detail to the compiler, in terms of a black box with input and/or output operands, and/or clobbers. See https://stackoverflow.com/tags/inline-assembly/info for links to guides, including some SO answers about using input / output constraints.

Think hard before deciding it's really worth using GNU C inline asm for anything. If you can get the compiler to emit the same instructions another way, that's almost always better. Intrinsics or pure C allow constant-propagation optimization; inline asm doesn't (unless you do stuff like if(_builtin_constant_p(x)) { pure C version } else { inline asm version }).


Intel syntax: in GCC, compile with -masm=intel so your asm template will be part of an Intel-syntax .s, and the compiler will substitute in operands in Intel syntax. (Like dword ptr [rsp] instead of (%rsp) for "m"(my_int)).

In clang I'm not sure there's any convenient way to use Intel-syntax in normal asm statements.


There is one other option though, if you don't care about efficient code (but then why are you using asm?): clang supports -fasm-blocks to allow syntax like MSVC's inefficient style of inline asm. And yes, this uses Intel syntax.

Is there any way to complie a microsoft style inline-assembly code on a linux platform? shows how inefficient the resulting code is: full of compiler-generated instructions to store input variables to memory for the asm{} block to read them. Because MSVC-style asm blocks can't do inputs or outputs in registers. (Clang doesn't support the leave-a-value-in-EAX method for getting a single value out so the output has to be stored/reloaded as well.)

You don't get to specify clobbers for this, so I assume an asm block implies a "memory" clobber, along with clobbers on all registers you write. (Or maybe even just mention.)

I would not recommend this; it's basically not possible to wrap a single instruction or handful of instructions efficiently this way. Only if you're writing a whole loop can you amortize the overhead of getting inputs into an asm{} block.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847