Version 8, changed by kerry. 10/03/2006. Show version history
Under Construction
nios2-elf-gcc supports inline assembly language. You can mix C and Nios II assembly language statements in a C file using a special syntax. In addition, nios2-elf-gcc will let you use C expressions for assembly language operands.
The following sections show how to use an asm() statement to enter in-line assembly language in your C file.
Simple assembly-language opcodes that have no operands can be inserted in-line like this.
| nios2-elf-gcc Source | Resulting Assembly Language |
|---|---|
int test(void) |
test: |
This same technique works with any in-line assembly language that references fixed registers.
| nios2-elf-gcc Source | Resulting Assembly Language |
|---|---|
void test2(void) |
test2: rdctl r8, ienable |
nios2-elf-gcc recognizes that this opcode must have some kind of side effect, since it references no registers, and so it will not move the instruction significantly during optimization.
Assembly-language opcodes with C int operands can be inserted in-line.
Here is an example that writes control register ctl3 (ienable) with the value of integer parameter x. You can see that the first parameter of the asm statement "wrctl ienable, %0" contains a "%0" where the source operand needs to be. This "%0" is similar to a "%s" in a printf format string. In this usage, it is replaced with the register that holds the int variable x.
void wrctl_ienable(int x)
{
asm volatile ("wrctl ienable, %0" :: "r" (x));
}
The resulting assembly langauge is below. The compiler has stored x in register r4.
wrctl_7:
#APP
wrctl ienable, r4
#NO_APP
ret
#define wrctl(register_number, value)
({ int __x = (value);
asm("wrctl ctl" #register_number ", %0" :
/* no output parameters */ :
"r" (__x));
})void test(int argc, char **argv)
{
wrctl(7, 123);
}
Custom Instructions and Built-in Functions