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

Etienne Lorrain etienne.lorrain@ibm.net
Thu Jun 10 05:22:00 GMT 1999


  Hello,

  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 ---------

Then:
gcc -S -O2 test.c

gives test.s:
--------- file test.s: ---------
 .file "test.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
 .align 4
.globl _fct
 .def _fct; .scl 2; .type 32; .endef
_fct:
 pushl %ebp
 movl %esp,%ebp
/APP
 in 0x44,%eax         # old = get()
/NO_APP
 movl %eax,%edx
 movl 8(%ebp),%ecx
 movl %ecx,%eax
/APP
 out %eax,0x44         # set(param)
/NO_APP
 cmpl %ecx,%edx        # old saved in %edx reused
 jne L7
 xorl %eax,%eax
 jmp L9
 .p2align 4,,7
L7:
 movl %edx,%eax
/APP
 out %eax,0x44         # set (old)
/NO_APP
 movl $1,%eax
L9:
 movl %ebp,%esp
 popl %ebp
 ret
--------- 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.
  Etienne.



More information about the Gcc mailing list