i am new to assembly programming,can anybody help me out to solve this problem -
i have a assembly code which does the atomic increment
Atomic_Add (volatile unsigned int *addr, int additive)
{
register unsigned int previous;
__asm__ volatile (
"pushf;"
"cli;"
"movl (%2), %0;" /* previous = *addr; */
"add %0, %1;" /* *addr = previous + additive; */
"movl %1, (%2);"
"popf"
: "=&r"(previous)
: "r"(additive), "r"(addr) : "cc");
return previous;
}
here if addr is allocated on the stack(local variable) this function works fine and increment the addr variable correctly by additive, but if i addr is allocated on heap or is from gss, addr is not getting incremented correctly. and one more thing, if i merge the last two instruction the code works fine for all the scenarios.
Atomic_Add (volatile unsigned int *addr, int additive)
{
register unsigned int previous;
__asm__ volatile (
"pushf;"
"cli;"
"movl (%2), %0;" /* previous = *addr; */
"add %1, (%2);" /* *addr = previous + additive; */
"popf"
: "=&r"(previous)
: "r"(additive), "r"(addr) : "cc");
return previous;
}