I've been going through a project I'm working on and rewriting some code into inline assembly. Whenever I use the following code:

__asm xor eax, eax

in Visual Studio (2003) I get the following error:

error C2400: inline assembler syntax error in 'opcode'; found 'XOR'

I cannot find any information why this would generate an error or what I can do to correct it. Does anybody know what I am doing wrong here?

Posted on 2009-04-20 12:08:22 by Sparafusile
i don't seem to get that error when i compile that in vs2003.. its probably the code before the xor that causes the error..
Posted on 2009-04-20 13:00:09 by lone_samurai5
There is no code before that that really matters. I can plop that one line anywhere and generate the error. Replace it with __asm mov eax, 0 and everything works fine. I've tried it on two computer, one running 2002 and the other 2003 and get the same result. That's the only opcode that doesn't seem to work.

Posted on 2009-04-20 16:38:07 by Sparafusile
that's odd, it works on all my systems..

I can't seem to reproduce the error at all, can you attach your project files so I can check it out?
Posted on 2009-04-20 17:42:56 by lone_samurai5
Code like this:
#define xor ^
__asm xor eax, eax
will produce such an error. Check your defines/macros.

Posted on 2009-04-20 20:55:31 by drizz
Ah hah! I did #define xor ^ because I think it's a counterintuitive symbol. Never realized the preprocessor directives worked on inline assembly too. Thank you for clearing up my misunderstanding.

Posted on 2009-04-21 07:32:48 by Sparafusile
preprocessor directives are applied blindly without regard to context - use as little as possible.

You'll get used to ^ after a while - otherwise you should probably pick up another language :) - OTOH, apparently "and, or, xor, not" etc are supposed to be keywords in C++ (yuck!), but with MSVC you have to include iso646.h, which #defines them...
Posted on 2009-04-21 11:13:35 by f0dder
The part that got me was the fact that the error message mentioned "XOR". If it had said

syntax error in 'opcode'; found '^'

I would have known right away what happened. I'll try and get use to the carrot-as-xor symbol or just switch to pure assembly. Thanks for the advice.

Posted on 2009-04-21 11:25:14 by Sparafusile

The part that got me was the fact that the error message mentioned "XOR". If it had said

syntax error in 'opcode'; found '^'

I would have known right away what happened.
That is actually a bit confusing, considering the preprocessor should've converted that before the compiler sees it... just for kicks, try doing "__asm ^ eax, eax" and see what the output is? :)
Posted on 2009-04-21 18:26:23 by f0dder
That was the first thing I did when I realized what was wrong. __asm ^ eax, eax produces exactly the same error message include the 'XOR' bit. It's slightly ambiguous, but I guess the moral is don't #define anything that is also an assembly opcode.

Posted on 2009-04-22 09:59:43 by Sparafusile
couldn't you just do return null or something?
Posted on 2009-04-25 07:43:29 by evlncrn8
evlncrn8: if the "xor eax, eax" is used for return value, sure (and in that case, the use of inline assembly would be very very silly :)) - but it might be used in some algorithm.
Posted on 2009-04-25 07:55:44 by f0dder
I've gotten into a habit of clearing registers with xor reg, reg statement. I could always just do mov reg, 0 to get the same result and bypass that particular error. My problem came in when I was encrypting/decrypting data with the xor statement. It's pretty necessary at that point and prompted me to place my ignorance in the public domain for all to see.

Posted on 2009-05-01 10:10:38 by Sparafusile