Re: why tow local variables take the same register?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Parmenides <mobile.parmenides@xxxxxxxxx> writes:

> In a the following function, the inline assembly delcares that both
> 'input' and 'output' use registers.
>
> void tst(void)
> {
>      int input = 3, output = 5;
>      asm volatile(
>           "addl %1, %0\n\t"
>           : "=r" (output)
>           : "r" (input)
>           );
> }
>
> But, it seems that the logic is wrong in its corresponding assembly code:
>
>         movl    $3, -8(%ebp)
>         movl    $5, -4(%ebp)
>         movl    -8(%ebp), %eax   # eax is allocated for 'input'
> #APP
>         addl %eax, %eax
>
> #NO_APP
>         movl    %eax, -4(%ebp)   #eax is allocated for 'output'
>
> Obviously, the gcc has allocated the same eax for both 'input' and
> 'output'. Althrough it is can be resolved by including 'output' in the
> input list within the inline assembly, I wonder what is the strategy
> taken by gcc to allocate registers in this kind of situation.

You said that "output" is an output register of the asm.  That means
that the value stored in that variable before entering the asm is
irrelevant.  gcc is perfectly entitled to use the same register for the
input and output in such a case.

In your case, of course, "output" is not purely an output register, and
the value it has when entering the asm is relevant.  I would recommend
something like

     asm volatile(
          "addl %1, %0\n\t"
          : "=r" (output)
          : "r" (input), "0" (output)
          );

(I would also recommend actually using the output value, and removing
the "volatile" qualifier.)

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux