dilluns, 11 de maig del 2015

Notes about gcc Inline assembly and NIOSII custom instructions

The syntax for gcc inline assembly is

asm ( assembler template:
output operands /* optional */:
input operands /* optional */:
list of clobbered registers /* optional */);


In NIOSII custom instructions are not invoked by using inline assembly, but with MACROS that are created with the BSP that end up using GCC builtin functions, such as

int __builtin_custom_inii (int n, int dataa, int datab);

But if you want to directly invoke custom instructions with inline assembly you can do, for instance

int a;  // first input of the custom instruction
int b;  // second input of the custom instruction
int r;  // result
asm volatile ("custom 16, %0, %1, %2" : "=r" (r) : "r" (a), "r" (b) );

in the assembler template string you can use % and a number to indicate some placeholders that will be substituted by input and output parameters.

in the former example we have %0, %1, and %2 that will be substituted by the elements specified in the output operands and input operands section.

So %0 is substituted by what it is specified by "=r" (r), which can be interpreted as: "=r" -> assigment to a register, (r) -> the C variable where the value will be stored to.

%1 is substituted by what it is specified by "r" (a), that is interpreted as; "r" -> reference to a register, (a) -> the C variable that is used to get the value from

%2 is substituted by what it is specified by "r" (b), that is interpreted as; "r" -> reference to a register, (b) -> the C variable that is used to get the value from

Some more details about inline assembly can be found in https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html

Cap comentari:

Publica un comentari a l'entrada