How to declare an 'asm ("...":: "" (output))' ?

Etienne Lorrain
Thu Jun 10 05:22:00 GMT 1999


  I was thinking that an "asm" construct had to be declared
 volatile when it has no output, but it did not need this
 keyword when it has (used) output.
  This is on i386 target for EGCS 2.91.57 (1.1 release)
 on Cygwin, but I have seen it on GCC 2.8.1 - host Linux.
  It is maybe not linked to the use of the "asm" keyword.

--------- file test.c: ---------
extern inline int get (void)
  int result;
  asm ( "in 0x44,%%eax ": "=eax" (result));

  return result;

extern inline volatile void set (unsigned data)
  asm ( "out %%eax,0x44 ":: "eax" (data));

int fct (int param)
  int old, new;

  old = get();
  set (param);
  new = get();    /* never called, old reused ! */

  if (new != param) {
      set (old);
      return 1;
  return 0;
--------- end test.c ---------

gcc -S -O2 test.c

gives test.s:
--------- file test.s: ---------
 .file "test.c"
 .align 4
.globl _fct
 .def _fct; .scl 2; .type 32; .endef
 pushl %ebp
 movl %esp,%ebp
 in 0x44,%eax         # old = get()
 movl %eax,%edx
 movl 8(%ebp),%ecx
 movl %ecx,%eax
 out %eax,0x44         # set(param)
 cmpl %ecx,%edx        # old saved in %edx reused
 jne L7
 xorl %eax,%eax
 jmp L9
 .p2align 4,,7
 movl %edx,%eax
 out %eax,0x44         # set (old)
 movl $1,%eax
 movl %ebp,%esp
 popl %ebp
--------- end test.s ---------

  By the way, if you compile this software
 without "-O2", there is a nice "mov %eax,%eax"...

 pushl %ebp
 movl %esp,%ebp
 subl $16,%esp
 call _get
 movl %eax,%eax        # here
 movl %eax,-4(%ebp)
 movl 8(%ebp),%eax
 pushl %eax
 call _set
 addl $4,%esp
 call _get
 movl %eax,%eax        # here
 movl %eax,-8(%ebp)
 movl -8(%ebp),%eax
 cmpl 8(%ebp),%eax
 je L4
 movl -4(%ebp),%eax
 pushl %eax
 call _set
 addl $4,%esp
 movl $1,%eax
 jmp L3

  Thanks, GCC/EGCS is still a nice compiler.

More information about the Gcc mailing list