VirtLands

CMPxCHG sample

Apr 23rd, 2021 (edited)
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. Here is some sample code (from ~10 years ago) that I created. This demonstrates using some inline assembly code concerning CMPXCHG, within the Pelles C.
  2. I’ve included the 8-bit, 16-bit, & 32-bit flavors for CMPXCHG:
  3. // Sorry, yet the text formatting may look lousy on a // smartphone .
  4.  
  5. typedef unsigned int ui;
  6.  
  7. // reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
  8. //
  9. //  This atomic functtion returns the ZF,   (ZF = result of comparison).
  10. //  -------- How it works :: --------
  11. //  "expected" is stored into AL,  (=8 bits)
  12. //  then compare AL with "mem".    
  13. //  If equal then { ZF=1, and "expected" is stored in mem }
  14. //  else { ZF=0, and AL=MEM (only temporarily) }
  15. //
  16. __declspec(naked) __declspec(dllexport) bool cmpxchg_1(volatile ui *mem, ui expected, ui new)
  17. {
  18.  __asm {
  19.         push ebp
  20.         mov  ebp,esp
  21.         push ebx
  22.         push ecx
  23.  
  24.         mov ebx,[ebp+8]   ;; EBX points to mem
  25.         mov eax,[ebp+12]  ;; EAX = expected;     Only AL shall be used.
  26.         mov ecx,[ebp+16]  ;; ECX = new;          Only CL shall be used
  27.  
  28.         lock cmpxchg [ebx],cl
  29.  
  30.         setz cl           ;; store the zero flag result into CL
  31.         xor eax,eax
  32.         add al,cl         ;; store CL into EAX, and return EAX
  33.  
  34.         pop ecx
  35.         pop ebx        
  36.         pop ebp
  37.         ret 12  
  38.        }
  39. }
  40.  
  41. // reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
  42. //
  43. //  This atomic function returns the ZF,   (ZF = result of comparison).
  44. //  -------- How it works :: --------
  45. //  "expected" is stored into AX,  (=16 bits)  
  46. //  then compare AX with "mem".  
  47. //  If equal then { ZF=1, and "expected" is stored in mem }
  48. //  else { ZF=0, and AX=MEM (only temporarily) }
  49. //
  50.  __declspec(naked) __declspec(dllexport) bool cmpxchg_2(volatile ui *mem, ui expected, ui new)
  51. {
  52.  __asm {
  53.         push ebp
  54.         mov  ebp,esp
  55.         push ebx
  56.         push ecx
  57.  
  58.         mov ebx,[ebp+8]   ;; EBX points to mem
  59.         mov eax,[ebp+12]  ;; EAX = expected;     Only AX shall be used.
  60.         mov ecx,[ebp+16]  ;; ECX = new;          Only CX shall be used
  61.  
  62.         lock cmpxchg [ebx],cx
  63.  
  64.         setz cl           ;; store the zero flag result into CL
  65.         xor eax,eax
  66.         add al,cl         ;; store CL into EAX, and return EAX
  67.  
  68.         pop ecx
  69.         pop ebx        
  70.         pop ebp
  71.         ret 12  
  72.        }
  73. }
  74.  
  75.  
  76. // reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
  77. //
  78. //  This atomic functtion returns the ZF,   (ZF = result of comparison).
  79. //  -------- How it works :: --------
  80. //  "expected" is stored into EAX,  (=32 bits)
  81. //  then compare EAX with "mem".  
  82. //  If equal then { ZF=1, and "expected" is stored in mem }
  83. //  else { ZF=0, and EAX=MEM (only temporarily) }
  84. //
  85.  __declspec(naked) __declspec(dllexport) bool cmpxchg_4(volatile ui *mem, ui expected, ui new)
  86. {
  87.  __asm {
  88.         push ebp
  89.         mov  ebp,esp
  90.         push ebx
  91.         push ecx
  92.  
  93.         mov ebx,[ebp+8]   ;; EBX points to mem
  94.         mov eax,[ebp+12]  ;; EAX = expected;  
  95.         mov ecx,[ebp+16]  ;; ECX = new;        
  96.  
  97.         lock cmpxchg [ebx],ecx
  98.  
  99.         setz cl           ;; store the zero flag result into CL
  100.         xor eax,eax
  101.         add al,cl         ;; store CL into EAX, and return EAX
  102.  
  103.         pop ecx
  104.         pop ebx        
  105.         pop ebp
  106.         ret 12  
  107.        }
  108. }
  109.  
  110.  
  111.  
Add Comment
Please, Sign In to add comment