Simon
Simon

Reputation: 2115

operand type mismatch for `mov'

I want the eflags values, but I get the error

operand type mismatch for `mov'

Here is my code:

int a0 = 0, b0 = 1; short c0;
//  asm("cmp %1, %2\npushf\npop ax\nmov ax, $0": "=r" (c0): "r" (a0), "r" (b0));
asm("cmp %1, %2\n lahf\n mov %%ax, $0": "=r" (c0): "r" (a0), "r" (b0): "ax");

I also tried with movb ah but same error.

Upvotes: 1

Views: 11013

Answers (1)

fuz
fuz

Reputation: 93004

There are two mistakes in your code:

  1. The $ prefix indicates an immediate. mov %ax, $0 attempts to move ax to the immediate 0, this is nonsensical. Perhaps you meant to write %0 instead, indicating c0?
  2. If we replace mov %%ax, $0 with mov %%ax, %0, the second problem is that c0 is an int, thus %0 is replaced with some 32 bit register, so you get something like mov %ax, %ecx. This is wrong, too as both operands to mov must have the same size. You can fix this by making c0 an unsigned char and changing mov %%ax, %0 to mov %%ah, %0.

Anyway, using a mov in inline assembly is usually wrong but here it's hard to avoid as you can't easily tell gcc to expect c0 in the ah register.

Upvotes: 3

Related Questions