| | |
atomic increment is not working properly..
![]() |
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Solved Threads: 0
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
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.
i have a assembly code which does the atomic increment
assembly Syntax (Toggle Plain Text)
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; }
assembly Syntax (Toggle Plain Text)
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; }
Last edited by cscgal; Sep 15th, 2008 at 11:24 am. Reason: Added code tags
So is the "not working properly" related to the "add" part of the problem or the "lock" part of the problem?
cli isn't going to do anything useful at all if you have multiple cores. AFAIK, it's a "per core" thing, so disabling it on one doesn't stop the other from messing with the variable.
The use case you quote of locals good, globals bad makes it seem like a cache coherency problem. Or it could be down to out-of-order execution of instructions meaning the popf happens beforre the memory write. IIRC, the instruction to stop this is 'sync'
cli isn't going to do anything useful at all if you have multiple cores. AFAIK, it's a "per core" thing, so disabling it on one doesn't stop the other from messing with the variable.
The use case you quote of locals good, globals bad makes it seem like a cache coherency problem. Or it could be down to out-of-order execution of instructions meaning the popf happens beforre the memory write. IIRC, the instruction to stop this is 'sync'
![]() |
Other Threads in the Assembly Forum
- Previous Thread: Patricknet need's your help
- Next Thread: Understand assembly code - very simple
| Thread Tools | Search this Thread |






