www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - inline assembler bug

It looks like there is a bug in the inline assembler when the "cmpxchg8b"
instruction is used. It always wants to use the EDI register and it seems to
mess up the few instructions following the call. When I tried putting the
following assembly as inline and looked at the obj2asm output it doesn't
match. I would end up getting illegal instructions or seg-v's. I've pasted
below the stand-alone asm file I'm using now as a work-around. What I'd
*really* like, though, is that there be atomic compare-and-set routines for
32 and 64 bit data in phobos somewhere. That would make concurrent libraries
a little simpler to code.

.586
.model flat, c
.code

;; bool CompareAndSet8b(QWORD* p, QWORD* expect, QWORD* update)
;;  Atomically compare 8 bytes at *expect to *p and
;;  replace with *p with *update if equal. Returns true
;;  if equal. Processor must support cmpxchg8b instruction.

CompareAndSet8b PROC
 enter 0,0
 mov edi,10h[ebp]
 mov ebx,[edi]
 mov ecx,4[edi]
 mov edi,0Ch[ebp]
 mov eax,[edi]
 mov edx,4[edi]
 mov edi,8[ebp]
 lock cmpxchg8b QWORD PTR [edi]
 setz al
 leave
 ret
CompareAndSet8b ENDP

end
Aug 16 2004