1

Is it "legal" to have a gcc inline asm statement without the actual instruction?

For example, is the asm statement "legal"? Will it introduce undefined behaviour?

int main(){ int *p = something; asm("":"=m"(p)); return 0; } 
7
  • Since you've provided an output, but the result is not used, the compiler (assuming gcc compatibility) is free to eliminate the asm expression. Commented Feb 28, 2016 at 8:35
  • 1
    It is ok, but what are you hoping to achieve? One common empty asm is asm("":"+m"(f)) to force rounding of a float f on x87. Commented Feb 28, 2016 at 13:52
  • Yes, it is still good. As I said below, using extended asm can (possibly) introduce minor inefficiencies, but it will not result in incorrect code. If it really makes you uncomfortable leaving this blank, you can always add a comment ("# no code here today!"). Commented Feb 28, 2016 at 23:01
  • But @ David, the "=" modifier indicates the value of p is replaced by the inlined instruction (in this example, it should be erased since the instruction is empty) . Therefore, when we use *p in the future, it may cause a seg fault. Is my understanding right? (you may try this example with return *p on gcc-{other than 4.8} -O3). Commented Feb 28, 2016 at 23:49
  • p is not 'erased' by this code. In unoptimized code it will be 'something' before the asm, and unchanged afterwords. When optimizing, it may skip the assignment of p, since it can see that it is about to be overwritten (leaving it undefined), but if 'something' is a function, it may call it anyway, depending. But that's not due to the asm being blank. If the asm has code but fails to assign a value to p, you would have the same problem. If the asm is blank because you are removing some essential functionality, then yeah, changing it to blank could be an issue. Commented Feb 29, 2016 at 0:45

2 Answers 2

2
int main(){ int *p = 0; asm("":"=m"(p)); return 0; } 

Compiles without any errors, but it is unnecessary:

main: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movq $0, -8(%rbp) movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret 

GCC completely ignores that empty asm statement.

Sign up to request clarification or add additional context in comments.

Comments

0

In that specific example, the compiler can see that none of the outputs of the asm (ie p) are ever used. Since the asm is not volatile (and has at least 1 output), the compiler is free to completely discard the statement during optimization.

It may also be worth mentioning that on i386, all extended asm statements (ie ones with parameters) always implicitly clobber fpsr (floating point flags) and eflags (think: cc clobber). If the statement isn't completely discarded (for example if it is volatile), this might have an effect. If it does, it will at worst be a tiny loss of efficiency, not incorrect results.

So to sum up:

  • Yes it is legal.
  • No it doesn't introduce undefined behavior EXCEPT that the value of p could be undefined since you are saying that you are overwriting the contents, but you aren't actually putting anything in it.
  • It can, theoretically, introduce tiny inefficiencies, but it probably won't.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.