views:

100

answers:

2

I am new to assembly language. It seems that gcc doesn't have _bittestandset function in intrin.h like MSVC does, so I implemented a new one. This one works fine in linux, but it goes wrong with mingw in winVista machine, the code is:

inline unsigned char _bittestandset(unsigned long * a, unsigned long b)
{
    __asm__ ( "bts %1, %[b]"
       : "=g"(*a)
       : [b]"Ir"(b), "g"(*a) );
    return 0;
}
+3  A: 

Could you give some further explanation what's not working? Maybe a simple example of the usage or so. It's hard to guess what's wrong with the code..

One thing that looks cheesy so far: You execute a bit-test opcode but ignore the result. The bit that you test (and set) ends up in the carry flag after the opcode.

If you want to get the result you need an additional instruction to get the carry flag into some other output register. (SBB EAX, EAX or something like that).

Otherwise - if you don't need the result - it's much cheaper to replace the BTS instruction with three simpler assembler opcodes:

Something along these lines:

  ; bit-index in cl
  ; Value in ebx
  ; eax used as a temporary (trashed)
  mov eax, 1
  shl eax, cl
  or  ebx, eax

Since I don't have mingw with your exact version running a assembly-output of a simple testcase could give us some clue what's going wrong.

Nils Pipenbrinck
A: 

DON'T use the BTS instruction. It's horribly slow. A better way would be to implement it using simple shift+or. And as a bonus, you can do it without any assembly, in pure, portable C++.

stormsoul