This one is kind of weird, because the code generated using obj2asm looks fine. Here's the test case:
void main()
{
int i = 5;
int j = 0;
int* val = &i;
asm
{
mov EAX, val;
lock;
mov EAX, [EAX];
mov j, EAX;
}
printf( "%d\n", j );
}
If the 'lock' instruction is commented out, the program works as expected.
Comment #1 by fvbommel — 2007-02-21T03:34:13Z
From the Intel manual (vol. 2A, Instruction Set Reference A-M):
---
The LOCK prefix can be prepended only to the following instructions and only to those forms
of the instructions where the destination operand is a memory operand: ADD, ADC, AND,
BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR,
XADD, and XCHG. If the LOCK prefix is used with one of these instructions and the source
operand is a memory operand, an undefined opcode exception (#UD) may be generated. An
undefined opcode exception will also be generated if the LOCK prefix is used with any instruc-
tion not in the above list. The XCHG instruction always asserts the LOCK# signal regardless of
the presence or absence of the LOCK prefix.
---
AMD agrees (Architecture Programmer's Manual, vol. 3):
---
The LOCK prefix can only be used with forms of the following instructions that write a memory
operand: ADC, ADD, AND, BTC, BTR, BTS, CMPXCHG, CMPXCHG8B, DEC, INC, NEG, NOT,
OR, SBB, SUB, XADD, XCHG, and XOR. An invalid-opcode exception occurs if the LOCK prefix is
used with any other instruction.
---
So you're using the lock prefix where it isn't allowed.
It's both not allowed on MOV and on other instructions on which it can be used it's only allowed if the destination is a memory operand. Those all seem to be instructions which both read and write their destination (memory) operand.
(I didn't know any of this until I looked it up just now)
Sorry, it seems the bug is in your code...
Comment #2 by fvbommel — 2007-02-21T03:43:14Z
Oops, I at first wanted to change this to an enhancement request to make the code an error, but then changed my mind. Forgot to change the summary back though.
Comment #3 by sean — 2007-02-21T09:25:29Z
[email protected] wrote:
>
> The LOCK prefix can be prepended only to the following instructions and only to
> those forms
> of the instructions where the destination operand is a memory operand: ADD,
> ADC, AND,
> BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR,
> XADD, and XCHG.
Darnit, I dunno why I thought this was supposed to work. It's not like
I haven't read this clause enough :-p I'll use CAS instead. Thanks!