I'm writing a function to set the nth bit in a number x using bts
instruction through inline assembly. Here is my function:
uint32_t set_bit_assembly(uint32_t x, uint32_t n)
{
asm( "movl %1, %%eax; bts %0, %%eax;"
:"=&r"(x)
:"r"(n)
);
return x;
}
I want variables 'n' and 'x' to be the 1st operand for movl
and bts
respectively. However when I compile, it takes 'x' for movl and totally disregards 'n'. (I tried interchanging %0 and %1, which didn't help). Could you please tell me where I went wrong? Below is the generated assembly code:
00000043 <set_bit_assembly>:
43: 55 push %ebp
44: 89 e5 mov %esp,%ebp
46: 83 ec 10 sub $0x10,%esp
49: 8b 55 0c mov 0xc(%ebp),%edx
4c: 89 d0 mov %edx,%eax
4e: 0f ab c0 bts %eax,%eax
51: 89 45 fc mov %eax,-0x4(%ebp)
54: 8b 45 fc mov -0x4(%ebp),%eax
57: c9 leave
58: c3 ret