but if I uncomment the line with std::bla-bla, or compile it with -g (debug compile) everything mysteriously starts to work properly! I figured out the shit happens in `if' line, but can not find the way around. I am not really experienced with mixing C and asm, but it looks like a bug to me. ???
computerquip
That is not the problem. BTW, have you actually read the articles you referenced? <hint> __asm is ms specific, while asm is the standard keyword</hint>
One is for GCC and the other mentions that it is MS specific before the article begins. Both provide help and the GCC article mentions use of volatile. I don't know what you're using. I personally glanced at both but I don't feel the need yet to learn to relate place ASM into my C++.
Quip, tell me as one writer to another.... can you read? :))
OK, I put here the simple example, so people please tell me what results you get on which compiler
With -O2 I get garbage output with both gcc 3.3.3 and 4.1.2.
FWIW:
g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ foo.cpp -O2
a.out
n1 = 12685300
n2 = 12685301
----------------------------
g++ --version
g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
------- Comment #1 From Andrew Pinski 2010-03-04 22:08 [reply] -------
The problem you are seeing is Intel vs AT&T asm formats. GNU as defaults to
AT&T format in that it is src, dst. So you have the operands swapped. Note
also you also don't clobber the flags register as bsr sets the Zero flag.
asm volatile("bsr %1, %0" : "=r" (n) : "r" (x) : "flags");
Is the correct code you want. It just happened to work at -O0 because the
register allocator used the same registers for the input and output.