972
edits
[unchecked revision] | [unchecked revision] |
m (Bot: Replace deprecated source tag with syntaxhighlight) |
|||
(9 intermediate revisions by 6 users not shown) | |||
Line 1:
The idea behind '''Inline Assembly''' is to embed assembler instructions in your C/C++ code, using the <tt>asm</tt> keyword, when there's no option but to use
== Overview ==
Sometimes, even though C/C++ is your language of choice, you '''need''' to use some
One of the options you have is writing an
== Syntax ==
This is the syntax for
<
asm ( assembler template
: output operands (optional)
Line 15:
: clobbered registers list (optional)
);
</syntaxhighlight>
Assembler template is basically [[GAS]]-compatible code, except
<
asm ("movl
asm ("movl %%eax, %%ebx" : );
</syntaxhighlight>
How exactly operands work will be explained in more details in later sections. For now,
<
int a=10, b;
asm ("movl %1, %%eax;
Line 35 ⟶ 36:
:"%eax" /* clobbered register */
);
</syntaxhighlight>
The last "clobbered register" section is used in order to tell GCC that your code is using some of the processor's registers, and that it should move any active data from the running program out of this register before executing the asm snippet. In the example above, we move
=== Assembler Template ===
Line 46 ⟶ 47:
As an example, to halt the CPU, you just have to use the following command:
<
asm( "hlt" );
</syntaxhighlight>
=== Output Operands ===
The Output Operands section is used in order to tell the compiler / assembler how it should handle C variables used to store some output from the ASM code. The Output Operands are a list of pairs, each operand consisting of a string literal, known as "constraint", stating where the C variable should be mapped (registers are generally used for optimal performance), and a C variable to map to (in
In the constraint, 'a' refers to EAX, 'b' to EBX, 'c' to ECX, 'd' to EDX, 'S' to ESI, and 'D' to EDI (read the GCC manual for a full list), assuming that you are coding for the IA32 architecture. An equation sign indicates that your assembly code does not care about the initial value of the mapped variable (which allows some optimization). With all that in mind, it's now pretty clear that the following code sets EAX = 0.
<
int EAX;
asm( "movl $0, %0"
: "=a" (EAX)
);
</syntaxhighlight>
Notice that the compiler enumerates the operand starting with %0, and that you don't have to add a register to the clobbered register list if it's used to store an output operand. GCC is smart enough to figure out what to do all by itself.
Line 66 ⟶ 67:
Starting with GCC 3.1, you can use more readable labels instead of the error-prone enumeration:
<
int current_task;
asm( "str %[output]"
: [output] "=r" (current_task)
);
</syntaxhighlight>
These labels are in a namespace of their own, and will not collide with any C identifiers. The same can be done for input operands, too.
Line 80 ⟶ 81:
If you want to move some value to EAX, you can do it the following way (even though it would certainly be pretty useless to do so instead of directly mapping the value to EAX):
<
int randomness = 4;
asm( "movl %0, %%eax"
:
: "b" (randomness)
:
);
</syntaxhighlight>
Note that GCC will always assume that input operands are read-only (unchanged). The correct thing to do when input operands are written to is to list them as outputs, but without using the equation sign because this time their original value matters. Here is a simple example:
<
asm("mov %%eax,%%ebx": : "a" (amount));//useless but it gets the idea
</syntaxhighlight>
Eax will contain "amount" and be moved into ebx.
Line 98 ⟶ 99:
It is important to remember one thing: ''The C/C++ compiler knows nothing about Assembler''. For the compiler, the asm statement is opaque, and if you did not specify any output, it might even come to the conclusion that it's a no-op and optimize it away. Some third-party docs indicate that using asm volatile will cause the keyword to not be moved. However, according to the GCC documentation, ''The volatile keyword indicates that the instruction has important side-effects. GCC will not delete a volatile asm if it is reachable.'', which only indicates that it will not be deleted (i.e. whether it may still be moved is an unanswered question). An approach that should work is to use asm (volatile) and put '''memory''' in the clobber registers, like so:
<
__asm__("cli": : :"memory"); // Will cause the statement not to be moved, but it may be optimized away.
__asm__ __volatile__("cli": : :"memory"); // Will cause the statement not to be moved nor optimized away.
</syntaxhighlight>
Since the compiler uses CPU registers for internal optimization of your C/C++ variables, and doesn't know about ASM opcodes, you have to warn it about any registers that might get clobbered as a side effect, so the compiler can save their contents before making your ASM call.
Line 112 ⟶ 113:
Forcing to use EAX over any other register, for instance, may force the compiler to issue code that will save what was previously in eax in some other register or may introduce unwanted dependencies between operations (pipeline optimization broken)
The 'wildcards' constraints allows you to give more freedom to GCC
{| {{wikitable}}
|-
| The "g" constraint : <
| x can be whatever the compiler prefers: a register, a memory reference. It could even be a literal constant in another context.
|-
| The "r" constraint : <
| you want x to go through a register. If x wasn't optimized as a register, the compiler will then move it to the place it should be. This means that <code>"movl %0, %%es" : : "r" (0x38)</code> is enough to load a segment register.
|-
| The "N" constraint : <
| tells the value '0x21' can be used as a constant in the out or in operation if ranging from 0 to 255
|}
Line 133 ⟶ 134:
It is possible to assign so-called ASM labels to C/C++ keywords. You can do this by using the <tt>asm</tt> command on variable definitions, as seen in this example:
<
int some_obscure_name asm("param") = 5; // "param" will be accessible in inline Assembly.
Line 140 ⟶ 141:
asm("mov param, %%eax");
}
</syntaxhighlight>
Here's an example of how you can access these variables if you don't explicitly state a name:
<
int some_obscure_name = 5;
Line 151 ⟶ 152:
asm("mov some_obscure_name, %%eax");
}
</syntaxhighlight>
Note that you might also be obliged to use '''_some_obscure_name''' (with a leading underscore), depending on your linkage options.
== asm goto ==
Before
so incorrect code is almost guaranteed to be generated.
<br>You might have been told that "gotos are evil". If you believe that is so, then asm gotos are your worst nightmare coming true.
Line 162 ⟶ 163:
asm goto's are not well documented, but their syntax is as follows:
<
asm goto( "jmp %l[labelname]" : /* no outputs */ : /* inputs */ : "memory" /* clobbers */ : labelname /* any labels used */ );
</syntaxhighlight>
One example where this can be useful, is the CMPXCHG instruction (see [http://en.wikipedia.org/wiki/Compare-and-swap Compare and Swap]), which the Linux kernel source code defines as follows:
<
/* TODO: You should use modern GCC atomic instruction builtins instead of this. */
#include <stdint.h>
Line 180 ⟶ 181:
__ret; \
}
</syntaxhighlight>
In addition to returning the current value in EAX, CMPXCHG sets the zero flag (Z) when successful. Without asm gotos, your code will have to check the returned value;
this CMP instruction can be avoided as follows:
<
/* TODO: You should use modern GCC atomic instruction builtins instead of this. */
// Works for both 32 and 64 bit
Line 198 ⟶ 199:
: fail_label ); \
}
</syntaxhighlight>
This new macro could then be used as follows:
<
struct Item {
Line 217 ⟶ 218:
}
</syntaxhighlight>
== Intel Syntax ==
You can let GCC use intel syntax by enabling it in inline Assembly, like so:
<
asm(".intel_syntax noprefix");
asm("mov eax, ebx");
</syntaxhighlight>
Similarly, you can switch back to AT&T syntax by using the following snippet:
<
asm(".att_syntax prefix");
asm("mov %ebx, %eax");
</syntaxhighlight>
This way you can combine Intel syntax and AT&T syntax inline Assembly. Note that once you trigger one of these syntax types, everything below the command in the source file will be assembled using this syntax, so don't forget to switch back when necessary, or you might get lots of compile errors!
Line 252 ⟶ 253:
[[Category:Assembly]]
[[de:Inline-Assembler_mit_GCC]]
|