Inline Assembly: Difference between revisions

m
no edit summary
[unchecked revision][unchecked revision]
mNo edit summary
Line 36:
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 b to eax in the first instruction, effectively erasing its content, so we need to ask GCC to clear this register from unsaved data before operation.
 
===Assembler Template===
The Assembler Template defines the assembler instructions to inline. The default is to use AT&T syntax here. If you want to use Intel syntax, <tt>-masm=intel</tt> should be specified as a command-line option.
 
Line 44:
</pre>
 
===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 braces).
 
Line 64:
</pre>
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.
 
===Input Operands===
While the Output Operands are generally used for... well... output, the Input Operands allows to parametrize the ASM code; i.e., passing read-only parameters from C code to ASM block. Again, string literals are used to specify the details.
 
Line 83 ⟶ 84:
Eax will contain "amount" and be moved into ebx.
 
===Clobbered Registers List===
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. (You can avoid this by stating asm volatile, which also allows you to make sure that your asm code won't be moved somewhere else by the optimizer.)
 
Line 90 ⟶ 91:
The Clobbered Registers List is a comma-seperated list of register names, as string literals.
 
===Wildcards: How you can let the compiler choose===
You don't need to tell the compiler which specific register it should use in each operation, and in general, except you have good reasons to prefer one register specifically, you should better let the compiler decide for you.
 
Anonymous user