3

I'm trying to understand the syntax of writing in assembly to first write the code correctly and second efficiently. in this example it shows the example using "=r"

asm volatile ("MRS %0, PMUSERENR_EL0\n": "=r"(value));

this reads the value of the register and stores it in the value variable. The other example uses ::"r"

asm volatile ("MSR PMUSERENR_EL0, %0\n":: "r"(value));

This writes the value variable to the PMUSERENR_ELO register. Here is another example of it:How to measure program execution time in ARM Cortex-A8 processor?.

When I attempt to compile a simple test code with the two above commands i get the error: :9:2: error: output operand constraint lacks '=' If I add the "=" and remove one ":" it will compile but when I test it, it simply says Illegal instruction

If someone could please explain the difference that would be helpful numerous assembly tutorials show the same format but no explanation. Its on a 64 bit arm platform if that offers any insight. Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
kaminsknator
  • 1,135
  • 3
  • 15
  • 26
  • Are you sure used two colons? (`::`) – Jester Apr 11 '16 at 22:51
  • See [this collection of links to GNU C inline asm](http://stackoverflow.com/questions/34520013/using-base-pointer-register-in-c-inline-asm/34522750#34522750), including links to [the official gcc docs](https://gcc.gnu.org/onlinedocs/gcc/Using-Assembly-Language-with-C.html) that explain input vs. output constraints. – Peter Cordes Apr 11 '16 at 22:58
  • this is not assembly this is inline assembly specific to one compiler, basically this is a C/C++ question not an assembly question. – old_timer Apr 11 '16 at 23:15
  • 1
    Perhaps it's being confused with the :: operator? It might be worth trying to add a space between the colons (`: :`). Just a thought. – David Wohlferd Apr 12 '16 at 00:15
  • whats the difference between the :: operator and the : operator? – kaminsknator Apr 12 '16 at 13:00

2 Answers2

5

Found the answer in the book: Professional Assembly Language: Extended ASM

If no output values are associated with the assembly code, the section must be blank, but two colons must still separate the assembly code from the input operands.

The why is because that's the standard. One colon for outputs and two for inputs.

vmemmap
  • 510
  • 4
  • 20
kaminsknator
  • 1,135
  • 3
  • 15
  • 26
  • 1
    `asm("template" : outputs : inputs : clobbers)`. It's "two colons" for input only if you don't have any outputs in the same asm statement. – Peter Cordes Sep 01 '22 at 18:08
1

You have rightly pointed to the explanation, but wrongly formatted the statement "One colon for outputs and two for inputs"

In the extend version of asm format as shown below

asm("assembly code" : outputs : inputs : clobbers)

if you do not have outputs, you should still include the Colon, which translates as follows

asm("assembly code"::inputs:clobbers)

If you do not have clobbers (changed registers) you can omit the last Colon, which translates as asm("assembly code":outputs:inputs)

gvk51
  • 131
  • 1
  • 4