0

I try to compile a program as follow:

unsigned long func(int priority)
{
        unsigned long a;

        if (a)
                __asm__ __volatile__("rep ; stosl"
                        : /* no outputs */ \
                        :"a" (0),"c" (1024),"D" (page)
                        :"ecx");
        return a;
}

but meet a error:

a.c:6:3: error: ‘asm’ operand has impossible constraints
   __asm__ __volatile__("rep ; stosl"
   ^~~~~~~

by modifying code and compiling again,I find it is "ecx" which causes error

if I delete it,such as

unsigned long func(int priority)
{
        unsigned long a;

        if (a)
                __asm__ __volatile__("rep ; stosl"
                        : /* no outputs */ \
                        :"a" (0),"c" (1024),"D" (page)
                        );
        return a;
}

there is no errors

asm allow syntax such as asm("....":output:input:Clobbers),so why my program meet error?

ps:my gcc is gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0,machine is x86-64

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Kid
  • 3
  • 2
  • Use of `__volatile__` along with an explicit reference to a register sounds odd. What happens if you remove the `__volatile__`? – Adrian Mole Nov 17 '22 at 11:55
  • 3
    Because you use `ecx` as input. Turn it into a read-write operand. Note you will also need to do that for `edi` since `stosl` changes that. – Jester Nov 17 '22 at 11:57
  • 1
    You're also missing a `"memory"` clobber. (Or a dummy output operand, which would also let you make it not `volatile`. Otherwise `volatile` is required, @AdrianMole, or the asm statement could be optimized away since none of its output operands are read. `asm` statements with no output operands are implicitly `volatile`, but that won't be the case after changing `"c"` to `"+c"` and `"D"` to `"+D"`. Make sure those are temp vars if you don't want the C `page` variable to actually be modified) – Peter Cordes Nov 17 '22 at 12:14
  • 1
    By the way, gcc has `__builtin_memset`. – Jester Nov 17 '22 at 12:17

0 Answers0