1
votes

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?

1
Try using %c1 (see gcc.gnu.org/onlinedocs/gcc/…). Also, consider using -masm=intel instead of the the pseudo-ops.David Wohlferd
I must've read that page like 10 times, yet somehow I missed that bit. Thanks! You should post this as answer.user5434231
And as much as I'd like to use -masm=intel, it causes trouble when including headers with AT&T syntax asm.user5434231
What headers are you using that have inline asm in them? Public headers should use dialects (see 'Multiple assembler dialects in asm templates' under gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#AssemblerTemplate) for exactly this reason. But of course not everybody does.David Wohlferd
The djgpp standard library has a few of these... But I'm thinking now I could reimplement them in Intel syntax. Or use macros like INTEL_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

1 Answers

2
votes

You should be able to use %c1 (see modifiers).

Note that if you are using symbolic names (which I find easier to read/maintain), you can use %c[five].

Lastly, I realize code this is just a "for-instance," but you are modifying memory without telling the compiler. This is a "bad thing." Consider either using a output constraint for the memory ("=m") or adding the "memory" clobber.