flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
edfed 15 Jan 2023, 20:28
read better:
if the value in xAx reg is equal the DESTINATION, the SOURCE is copied to DESTINATION. otherwise, DESTINATION is copied to the xAx reg. A, S, D. if A == D, D = S else A = D |
|||
![]() |
|
DimonSoft 15 Jan 2023, 20:30
Rewrite with particular value if current value equals what is expected (xAx). Otherwise find out what value really is there. Quite common pattern for some multi-threaded code.
|
|||
![]() |
|
Furs 15 Jan 2023, 21:48
Scenario: You want to atomically update some status variable. So you load the value, modify it to how you want it changed, and then use cmpxchg.
You ask, why not just write it directly? Some other thread or CPU could change it (multi-threaded code). Then you change it wrong, because it was already changed, so you have to inspect it again! Any time you use multiple instructions to do it, you're at risk of race condition. So if the comparison succeeds, it's the same as writing it directly. But what happens if it fails? You want to know why it failed, so that's why it gives you the value that is there now (into *ax). After it failed, you can retry by changing it again, but this time using the new value you got (in *ax) and trying cmpxchg again... |
|||
![]() |
|
Zoltanmatey31 16 Jan 2023, 08:55
oh ok, i was tired at night read it wrong. Sorry for the hustle.
|
|||
![]() |
|
edfed 16 Jan 2023, 11:53
you don't have to be sorry. it is interresting to enlight the purpose of this instruction.
i wonder in what practical case i should use it. anyone have an example code? does it have an other function than thread management? |
|||
![]() |
|
Furs 16 Jan 2023, 13:22
edfed wrote: anyone have an example code? does it have an other function than thread management? |
|||
![]() |
|
Hrstka 17 Jan 2023, 09:57
I have seen it many years ago in a code to synchronize execution between multiple threads and thus avoid race conditions. I remember it looked like this:
Code: try_get_lock: xor eax, eax ; set eax to 0 mov ecx, 1 lock cmpxchg [lock_variable], ecx jz lock_acquired ; wait, some other thread has the lock mov ecx, 0x4000 @@: dec ecx jnz @b jmp try_get_lock lock_acquired: ; only one thread at a time can execute this code ; ... lock_release: mov [lock_variable], 0 |
|||
![]() |
|
Furs 17 Jan 2023, 14:09
He did say "other than thread management"
![]() |
|||
![]() |
|
Hrstka 18 Jan 2023, 08:23
Okay, so just ignore my previous post.
|
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.