arm inline gcc error : expeced a ")" - gcc

I am using the following code to perform multiplication using DS5 ARM C Compiler 5. The compiler throws build errors stating expected a ")"
static __inline int32_t multiply(int32_t x, int32_t y)
{
__asm volatile(
"mul %0, %1, %2\n\t"
: "=a"(y) // error pointed to this line
: "r"(x), "a"(y));
return y;
}
Note: target --cpu is armV7a. Is there any way to overcome this error?

Related

gcc inline assembly unreferenced input operand

I was going through the gcc inline assembly documentation when i saw this example:
int test(char p[]) {
int count;
asm("repne scasb"
: "=c" (count), "+D" (p)
: "m" (*(const char (*)[]) p), "0" (-1), "a" (0));
}
I couldn't figure out the purpose of "m" (*(const char (*)[]) p)
Looking at the generated code with and without that shows that the only difference is the initialization of a register with the address of p which is not used by repne scasb. So it appears to be redundant. Is that statement useful in any other way?

Prevent risc-v inline assembly instruction being optimized out in gcc -O3 [duplicate]

I looked into some C code from
http://www.mcs.anl.gov/~kazutomo/rdtsc.html
They use stuff like __inline__, __asm__ etc like the following:
code1:
static __inline__ tick gettick (void) {
unsigned a, d;
__asm__ __volatile__("rdtsc": "=a" (a), "=d" (d) );
return (((tick)a) | (((tick)d) << 32));
}
code2:
volatile int __attribute__((noinline)) foo2 (int a0, int a1) {
__asm__ __volatile__ ("");
}
I was wondering what does the code1 and code2 do?
(Editor's note: for this specific RDTSC use case, intrinsics are preferred: How to get the CPU cycle count in x86_64 from C++? See also https://gcc.gnu.org/wiki/DontUseInlineAsm)
The __volatile__ modifier on an __asm__ block forces the compiler's optimizer to execute the code as-is. Without it, the optimizer may think it can be either removed outright, or lifted out of a loop and cached.
This is useful for the rdtsc instruction like so:
__asm__ __volatile__("rdtsc": "=a" (a), "=d" (d) )
This takes no dependencies, so the compiler might assume the value can be cached. Volatile is used to force it to read a fresh timestamp.
When used alone, like this:
__asm__ __volatile__ ("")
It will not actually execute anything. You can extend this, though, to get a compile-time memory barrier that won't allow reordering any memory access instructions:
__asm__ __volatile__ ("":::"memory")
The rdtsc instruction is a good example for volatile. rdtsc is usually used when you need to time how long some instructions take to execute. Imagine some code like this, where you want to time r1 and r2's execution:
__asm__ ("rdtsc": "=a" (a0), "=d" (d0) )
r1 = x1 + y1;
__asm__ ("rdtsc": "=a" (a1), "=d" (d1) )
r2 = x2 + y2;
__asm__ ("rdtsc": "=a" (a2), "=d" (d2) )
Here the compiler is actually allowed to cache the timestamp, and valid output might show that each line took exactly 0 clocks to execute. Obviously this isn't what you want, so you introduce __volatile__ to prevent caching:
__asm__ __volatile__("rdtsc": "=a" (a0), "=d" (d0))
r1 = x1 + y1;
__asm__ __volatile__("rdtsc": "=a" (a1), "=d" (d1))
r2 = x2 + y2;
__asm__ __volatile__("rdtsc": "=a" (a2), "=d" (d2))
Now you'll get a new timestamp each time, but it still has a problem that both the compiler and the CPU are allowed to reorder all of these statements. It could end up executing the asm blocks after r1 and r2 have already been calculated. To work around this, you'd add some barriers that force serialization:
__asm__ __volatile__("mfence;rdtsc": "=a" (a0), "=d" (d0) :: "memory")
r1 = x1 + y1;
__asm__ __volatile__("mfence;rdtsc": "=a" (a1), "=d" (d1) :: "memory")
r2 = x2 + y2;
__asm__ __volatile__("mfence;rdtsc": "=a" (a2), "=d" (d2) :: "memory")
Note the mfence instruction here, which enforces a CPU-side barrier, and the "memory" specifier in the volatile block which enforces a compile-time barrier. On modern CPUs, you can replace mfence:rdtsc with rdtscp for something more efficient.
asm is for including native Assembly code into the C source code. E.g.
int a = 2;
asm("mov a, 3");
printf("%i", a); // will print 3
Compilers have different variants of it. __asm__ should be synonymous, maybe with some compiler-specific differences.
volatile means the variable can be modified from outside (aka not by the C program). For instance when programming a microcontroller where the memory address 0x0000x1234 is mapped to some device-specific interface (i.e. when coding for the GameBoy, buttons/screen/etc are accessed this way.)
volatile std::uint8_t* const button1 = 0x00001111;
This disabled compiler optimizations that rely on *button1 not changing unless being changed by the code.
It is also used in multi-threaded programming (not needed anymore today?) where a variable might be modified by another thread.
inline is a hint to the compiler to "inline" calls to a function.
inline int f(int a) {
return a + 1
}
int a;
int b = f(a);
This should not be compiled into a function call to f but into int b = a + 1. As if f where a macro. Compilers mostly do this optimization automatically depending on function usage/content. __inline__ in this example might have a more specific meaning.
Similarily __attribute__((noinline)) (GCC-specific syntax) prevents a function from being inlined.
The __asm__ attribute specifies the name to be used in assembler code for the function or variable.
The __volatile__ qualifier, generally used in Real-Time-Computing of embedded systems, addresses a problem with compiler tests of the status register for the ERROR or READY bit causing problems during optimization. __volatile__ was introduced as a way of telling the compiler that the object is subject to rapid change and to force every reference of the object to be a genuine reference.

gcc assembly extended register

What parameter/constraint do I have to use in order to tell the compiler to use an extended register pair as the output/input?
static inline void mac_dsp(int64_t *c, uint32_t a, uint32_t b){
//This does not compile - "madd.u [%a15]0,[%a15]0,%d15,%d2: Too many operands"
__asm__ ("madd.u %0,%0,%1,%2" : "+m" (*c) : "d" (a), "d" (b));
//This does compile but I don't explicitly want to use e2 only but let the compiler
//chose which one...
__asm__ ("madd.u %%e2,%%e2,%1,%2" : "+m" (*c) : "d" (a), "d" (b));
}
Can anyone help me here? Thank you!

Inline assembely read word and double word from IO port

I have a segmentation fault after the inw command in the following code:
n=0;
unsigned short port_in = 0;
set_dr7(0x60000480);
asm volatile("movw %0, %%dx \n\t"
"outsw \n\t"
: : "i"((short)(TESTDEV_IO_PORT + 3)),
"S"(st1));
asm volatile("inw %1, %0\n\t" : "=a"(port_in) : "i"((short)(TESTDEV_IO_PORT+3)));
I have tried in, inl and more... but i still have a segmentation fault.
When I tried inb with port_in declared as unsigned char - it doesn't have segmentation fault - but I need inw and inl for another test.
What is the problem in my code?
Generally I'm trying to add a test to the kvm-unit-test.

Inline assembly in C

Pretty self explanatory code. Why doesn't it work!
#include <stdio.h>
int main() {
__asm__("number dw 0"); // declare number?
printf("%d",number);
__asm__("mov %eax,number"
"inc %eax"
"mov number,%eax");
printf("%d",number);
return 0;
}
cc ex1.c -o ex1
ex1.c: In function ‘main’:
ex1.c:22:17: error: ‘number’ undeclared (first use in this function)
ex1.c:22:17: note: each undeclared identifier is reported only once for each function it appears in
make: *** [ex1] Error 1
Thanks.
I have a lot of knowledge gaps to fill... the gcc manual was confusing me with regards to inline assembly as was google results for tutorials...
working on an intel i7 processor
Use this syntax, you can access variables declared in C from the inline assembly
#include <stdio.h>
int main() {
int number = 0;
printf("%d\n",number);
asm(
"mov %[number],%%eax\n"
"inc %%eax\n"
"mov %%eax,%[number]\n"
: [number] "=m" (number) : "m" (number) : "eax", "cc" );
printf("%d\n",number);
return 0;
}
You can let the compiler load number into the eax register for you by specifying the "a" constraint on the input
#include <stdio.h>
int main() {
int number = 0;
printf("%d\n",number);
asm(
"inc %%eax\n"
"mov %%eax,%[number]\n"
: [number] "=m" (number) : "a" (number) : "cc" );
printf("%d\n",number);
return 0;
}
And since x86 inc instruction can operate on memory directly you could reduce it to this
#include <stdio.h>
int main() {
int number = 0;
printf("%d\n",number);
asm(
"incl %[number]\n" /* incl -> "long" (32-bits) */
: [number] "=m" (number) : "m" (number) : "cc" );
printf("%d\n",number);
return 0;
}
For more information see gcc documentation:
6.41 Assembler Instructions with C Expression Operands

Resources