0

The following assembly works (in -O2) if all of my outputs are "output-only" (=&), but not if they are "output" (=). What is the difference between these two modes, and how can the behavior of the program change when the assembly is not reading the register?

  __asm (
     "ldrb %[H], %[P0] \n"                 // P[0]
     "ldrb %[L], %[P4] \n"                 // P[4]
     "add %[H], %[Htbl], %[H], lsl #4 \n"  // H = Htbl + P[0]
     "add %[L], %[Htbl], %[L], lsl #4 \n"  // L = Htbl + P[4]
     "mov %[S], %[Z0], LSL #24 \n"         // S = Z0 << 24
     :
     [H]   "=&r" (H),
     [L]   "=&r" (L),
     [S]   "=&r" (S),
     [tmp] "=&r" (tmp)
     :
     [Z0]   "r" (Z0),
     [Htbl] "r" (Htbl),
     [P0]   "m" (P[0]),
     [P4]   "m" (P[4])
     );
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
personal_cloud
  • 3,943
  • 3
  • 28
  • 38
  • 3
    The `&` prohibits the compiler from using the register for an input operand as well. It doesn't matter whether your code reads the original operand - in fact it should not, because it's an output in either case. For example, the input `Z0` may use the same register as output `H`. Clearly that will break the code since the very first thing you do is change `H` thereby destroying the input `Z0`. This is why you need to use the `&` early clobber. – Jester Jan 01 '23 at 00:53
  • 1
    Reading the output of `gcc -O2 -S` is likely to bring enlightenment. – Nate Eldredge Jan 01 '23 at 00:58
  • @Jester Well that would explain the problem I was having with "=". So then "=" is only useful if your assembly code first captures all of its inputs before clobbering anything? E.g., if it's just a single instruction? – personal_cloud Jan 01 '23 at 01:08
  • Or if the constraints won't allow a collision anyway. – Jester Jan 01 '23 at 01:09
  • 1
    I'm not sure where you're seeing "=&" described as "output-only." Looking at the [docs](https://gcc.gnu.org/onlinedocs/gcc/Modifiers.html), I see "&" referred to as "earlyclobber," which seems to better describe what it does. – David Wohlferd Jan 01 '23 at 03:52
  • 1
    Well, "output only" also makes sense if you consider that the compiler is not allowed to use the same thing for an input as well. – Jester Jan 01 '23 at 18:35

0 Answers0