Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Try looking at the assembler code generated when you compile a function using this inline code. I suspect that you will find that line 2 does not do what you think it does. Do you need an input operand for this line?
If you are having problems with inline assembler the first step is to inspect the generated code.
Edit: You might also want to check whether the value that you put in %eax is preserved by the following instruction.
Last edited by iansjack on Sun Jun 10, 2012 1:50 am, edited 1 time in total.
and GCC already know the input registers are clobbered.
PS. It's general assumption to keep cld cleared in C, and you should do cld at startup and keep it cleared.
Alright, I solved the problem.
As expected the problem was in the second instruction.
I've looked at the disassembled code, and found that EAX was getting trashed as it is used for return variables too.
And moreover, since I used "r" operand for __asm, it patched the value through another register, which created chaos even further.
In the end I just changed "r" to "g", to let the compiler decide.