With inline assembly in GCC, you can specify an immediate asm operand with the "i"
constraint, like so:
void set_to_five(int* p)
{
asm
(
"movl %1, (%0);"
:: "r" (p)
, "i" (5)
);
}
int main()
{
int i;
set_to_five(&i);
assert(i == 5);
}
Nothing wrong with this so far, except that it's in horrible AT&T syntax. So let's try again with .intel_syntax noprefix
:
void set_to_five(int* p)
{
asm
(
".intel_syntax noprefix;"
"mov [%0], %1;"
".att_syntax prefix;"
:: "r" (p)
, "i" (5)
);
}
But this doesn't work, since the compiler inserts a $
prefix before the immediate value, which the assembler no longer understands.
How do I use the "i"
constraint with Intel syntax?
%c1
(see gcc.gnu.org/onlinedocs/gcc/…). Also, consider using -masm=intel instead of the the pseudo-ops. – David Wohlferd-masm=intel
, it causes trouble when including headers with AT&T syntax asm. – user5434231INTEL_ASM_BEGIN
/INTEL_ASM_END
and define them either to the respective pseudo-ops or nothing depending on the-masm
setting used. I don't like the multiple dialects feature, code duplication is a bad thing :) – user5434231