Click to See Complete Forum and Search --> : Help with ASM code for inline MSVC


c00lsnoopy
March 20th, 2009, 05:31 PM
Hi everyone,

I'm trying to make the following ASM code compile on MSVC. This code works fine when compiled on GCC. I know the structure is not the same but I couldn`t find any documentation on this issue. Any help will be appreciated. Thanks a bunch!

static inline unsigned long cmpxchg(volatile void *ptr, unsigned long old,
unsigned long _new, int size)
{
unsigned long prev;
switch (size) {
case 1:
__asm__ __volatile__(lock; "cmpxchgb %b1,%2"
: "=a"(prev)
: "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
: "memory");
return prev;

case 2:
__asm__ __volatile__(lock; "cmpxchgw %w1,%2"
: "=a"(prev)
: "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
: "memory");
return prev;
case 4:
__asm__ __volatile__(lock; "cmpxchgl %1,%2"
: "=a"(prev)
: "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
: "memory");
return prev;
}
return old;
}

rxbagain
March 20th, 2009, 09:59 PM
Hi c00lsnoopy,

Here's how your code could be translated into MSVC inline assembly. I added "_" to ptr and old variables since "ptr" is a reserved word in intel assembly.


static inline unsigned long cmpxchg(
volatile void *_ptr, unsigned long _old, unsigned long _new, int size)
{
unsigned long prev;
switch (size) {
case 1:
__asm {
mov eax, [_old] // place the value of _old to EAX
mov ecx, [_new] // place the value of _new to ECX
mov edx, [_ptr] // place the pointer of _ptr to EDX
lock cmpxchg [edx], cl // cmpxchg old (AL) and *ptr ([EDX])
mov [prev], eax // store EAX result value
}
return prev;
case 2:
__asm {
mov eax, [_old] // place the value of _old to EAX
mov ecx, [_new] // place the value of _new to ECX
mov edx, [_ptr] // place the pointer of _ptr to EDX
lock cmpxchg [edx], cx // cmpxchg old (AX) and *ptr ([EDX])
mov [prev], eax // store EAX result value
}
return prev;
case 4:
__asm {
mov eax, [_old] // place the value of _old to EAX
mov ecx, [_new] // place the value of _new to ECX
mov edx, [_ptr] // place the pointer of _ptr to EDX
lock cmpxchg [edx], ecx // cmpxchg old (EAX) and *ptr ([EDX])
mov [prev], eax // store EAX result value
}
return prev;
}
return _old;
}


Hope it will help you :)

c00lsnoopy
March 23rd, 2009, 11:43 AM
Thanx a lot rxBargain for your help.