-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.c
More file actions
1456 lines (1256 loc) · 41 KB
/
dllmain.c
File metadata and controls
1456 lines (1256 loc) · 41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
//
//
// Online assembler reference ::::
// Online x86 _ x64 Assembler ::: https://defuse.ca/online-x86-assembler.htm#disassembly
// Function pointer code references ::::
// http://www.blitzbasic.com/Community/posts.php?topic=83420
// http://www.blitzbasic.com/codearcs/codearcs.php?code=1639
// Using ESP and EBP ::::
// https://www.powerbasic.com/support/help/pbcc/using_esp_and_ebp.htm
// Notes :: http://tinyurl.com/o6wvlog
//
// The volatile registers are: EAX, ECX, EDX, ST0 - ST7, ES and GS
// The non-volatile registers are: EBX, EBP, ESP, EDI, ESI, CS and DS
#define WIN32_LEAN_AND_MEAN /* speed up */
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
// #include <pthread.h>
// #include "ATOMIC32.h"
#define BBDECL extern "C" __declspec(dllexport)
#define BBCALL __stdcall
typedef unsigned int ui;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef int64_t *int64_tp;
__declspec(dllexport) HANDLE atGetCurrentProcess(void);
// typedef int (*my_intf)(int); // A function pointer...
//__declspec(naked) int64_t *interlockedCompareExchange(volatile int64_t *v, int64_t exValue, int64_t cmpValue);
//__declspec(dllexport) bool casX(volatile ull* addr, ul expected_high, ul expected_low, ul new_high, ul new_low);
//////////////////////// Function prototypes //////////////////////////////////////////////
__declspec(dllexport) bool casX(volatile ull* addr, ul expected_high, ul expected_low, ul new_high, ul new_low);
void (__stdcall *JumpFunction)(void);
// The following functions were created by VirtLands::::
__declspec(naked) __declspec(dllexport) bool cmpxchg8bP(volatile int64_t *mem, int64_t *expected, int64_t *new);
__declspec(naked) __declspec(dllexport) bool cmpxchg_op (volatile int64_t *mem, int64_t *expected, int64_t *new, ui opsize);
__declspec(naked) __declspec(dllexport) bool cmpxchg_op_off(volatile int64_t *mem, int64_t *expected, int64_t *new, ui opsize, ui offset);
__declspec(naked) __declspec(dllexport) bool cmpxchg_1(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) bool cmpxchg_2(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) bool cmpxchg_4(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) bool cmpxchg_1_nz(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) bool cmpxchg_2_nz(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) bool cmpxchg_4_nz(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) ui cmpxchgSBxor_1(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) ui cmpxchgSBxor_2(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) ui cmpxchgSBxor_4(volatile ui *mem, ui expected, ui new);
__declspec(naked) __declspec(dllexport) ui xchg_1(volatile int *mem, int v); // xchg affects no flags
__declspec(naked) __declspec(dllexport) ui xchg_2(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui xchg_4(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui xchg_op(volatile int *mem, int v, int opsize);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_1(volatile int *mem, int v); // add affects several flags
__declspec(naked) __declspec(dllexport) ui AtomicAdd_2(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_1o(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_2o(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4o(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_1z(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_2z(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4z(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4nz(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_1f(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_2f(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4f(volatile int *mem, int v);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_1(volatile int *mem, ui v, ui c); // adc affects several flags
__declspec(naked) __declspec(dllexport) ui AtomicAdc_2(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_4(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_1o(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_2o(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_4o(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_1z(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_2z(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_4z(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_1f(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_2f(volatile int *mem, ui v, ui c);
__declspec(naked) __declspec(dllexport) ui AtomicAdc_4f(volatile int *mem, ui v, ui c);
BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
/*
* Microsoft says:
*
* The DLL is being loaded into the virtual address space of the current
* process as a result of the process starting up or as a result of a call
* to LoadLibrary. DLLs can use this opportunity to initialize any instance
* data or to use the TlsAlloc function to allocate a thread local storage
* (TLS) index.
*/
break;
case DLL_THREAD_ATTACH:
/*
* Microsoft says:
*
* The current process is creating a new thread. When this occurs, the system
* calls the entry-point function of all DLLs currently attached to the process.
* The call is made in the context of the new thread. DLLs can use this opportunity
* to initialize a TLS slot for the thread. A thread calling the DLL entry-point
* function with DLL_PROCESS_ATTACH does not call the DLL entry-point function
* with DLL_THREAD_ATTACH.
*
* Note that a DLL's entry-point function is called with this value only by threads
* created after the DLL is loaded by the process. When a DLL is loaded using
* LoadLibrary, existing threads do not call the entry-point function of the newly
* loaded DLL.
*/
break;
case DLL_THREAD_DETACH:
/*
* Microsoft says:
*
* A thread is exiting cleanly. If the DLL has stored a pointer to allocated memory
* in a TLS slot, it should use this opportunity to free the memory. The system calls
* the entry-point function of all currently loaded DLLs with this value. The call
* is made in the context of the exiting thread.
*/
break;
case DLL_PROCESS_DETACH:
/*
* Microsoft says:
*
* The DLL is being unloaded from the virtual address space of the calling process as
* a result of unsuccessfully loading the DLL, termination of the process, or a call
* to FreeLibrary. The DLL can use this opportunity to call the TlsFree function to
* free any TLS indices allocated by using TlsAlloc and to free any thread local data.
*
* Note that the thread that receives the DLL_PROCESS_DETACH notification is not
* necessarily the same thread that received the DLL_PROCESS_ATTACH notification.
*/
break;
}
/* Return success */
return TRUE;
}
// __declspec( dllexport ) __declspec(naked) int __cdecl summ(int arg1, int arg2)
// BBDECL __declspec( dllexport ) __declspec(naked) int summ(int arg1, int arg2)
__declspec( dllexport ) __declspec(naked) int summ(int a, int b)
{
_asm {
push ebp
mov ebp,esp
mov eax, [ebp+8]
add eax, [ebp+12]
;mov esp,ebp ;; use this only if local variables were created
pop ebp
ret 8 ;; return 8 bytes
}
}
__declspec(naked) int __cdecl naked_sample(int arg1, int arg2)
//__declspec(naked) int _fastcall naked_sample(int arg1, int arg2)
{
/* prologue */
__asm {
push ebp
mov ebp,esp
sub esp,__LOCAL_SIZE
mov [arg1],ecx
mov [arg2],edx
}
/* C code */
// ext_1 = arg1 * arg2;
/* ... */
/* epilogue */
__asm {
mov ebp,esp
pop ebp
ret
}
}
// source reference :: http://stackoverflow.com/questions/833122/cmpxchg-example-for-64-bit-integer
// :: _forceinline int64_t interlockedCompareExchange(volatile int64_t & v,int64_t exValue,int64_t cmpValue)
//
__declspec(naked) int64_t *interlockedCompareExchange(volatile int64_t *v, int64_t exValue, int64_t cmpValue)
{
__asm {
mov esi,v
mov ebx,dword ptr exValue
;mov ecx,dword ptr exValue + 4
mov eax,dword ptr cmpValue
;mov edx,dword ptr cmpValue + 4
lock cmpxchg8b qword ptr [esi]
ret
}
}
// reference :: https://www.cs.sfu.ca/~ashriram/courses/2012/CS885/assignments/Ass1/atomicops.h
//
__declspec(dllexport) bool casX(volatile ull* addr, ul expected_high, ul expected_low, ul new_high, ul new_low)
// ......................this code was checked by VirtLands and it works...............
{
static bool success =0;
__asm {
mov eax, expected_low
mov edx, expected_high
mov ebx, new_low
mov ecx, new_high
mov edi, addr
lock cmpxchg8b QWORD PTR [edi]
setz [success]
}
return success;
}
// cmpxchg8bP -- by VirtLands,, This function returns the resultant "Zero Flag", and may affect var "mem".
// for reference :: http://faydoc.tripod.com/cpu/cmpxchg8b.htm
//
// ::: Usage :::
//
// Compare EDX:EAX with m64. If equal, set ZF and load ECX:EBX into m64. Else, clear ZF and load m64 into EDX:EAX
// or...
// Compare "expected" with "mem". If equal, set ZF and load "new" into "mem". Else, clear ZF and load m64 into "expected" = EDX:EAX
//
// EDX:EAX = expected
// EDX:EAX = new
//
// ... In this particular function, the value of EDX:EAX shall eventually be ignored (discarded).
// ... and EAX ultimately takes on the value of the Zero Flag.
// ..............................................this code was checked by VirtLands and it works...............
__declspec(naked) __declspec(dllexport) bool cmpxchg8bP(volatile int64_t *mem, int64_t *expected, int64_t *new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
push edx
push edi
mov edi,[ebp+8] ;; EDI points to mem
mov ebx,[ebp+12]
mov eax,[ebx] ;; EDX:EAX contains "expected"
mov edx,[ebx+4]
mov ebx,[ebp+16]
mov ecx,[ebx+4] ;; ECX:EBX contains "new"
mov ebx,[ebx]
lock cmpxchg8b QWORD PTR [edi]
setz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop edi
pop edx
pop ecx
pop ebx
;mov esp,ebp ;; <-- not necessary, since no local variables were created.
pop ebp
ret 12 ;; return 12 bytes, (= 3 parameters, at 4 bytes each)
}
}
// In this version of cmpxchg, the user chooses the operand size (opsize)
// === Opsize can be of the following values ===
// opsize =1 (BYTE operand) = 8 bits
// opsize =2 (SHORT operand) = 16 bits
// opsize =4 (INT operand) = 32 bits
//
// (other values of opsize shall be ignored)
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_op(volatile int64_t *mem, int64_t *expected, int64_t *new, ui opsize)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
push edi
push esi
xor ecx,ecx
inc cl ;; CL =1
mov esi,[ebp+20] ;; ESI = opsize
mov edi,[ebp+8] ;; EDI points to "mem"
mov eax,[ebp+12] ;; EAX = "expected"
mov ebx,[ebp+16] ;; EBX = "new"
shr esi,cl
jc cmpxchg_op1
shr esi,cl
jc cmpxchg_op2
shr esi,cl
jc cmpxchg_op4
jmp short cmpxchg_op_exit
cmpxchg_op1:
lock cmpxchg [edi],bl ;; atomic exchange on a Byte.
jmp short cmpxchg_op_exit
cmpxchg_op2:
lock cmpxchg [edi],bx ;; atomic exchange on a Short.
jmp short cmpxchg_op_exit
cmpxchg_op4:
lock cmpxchg [edi],ebx ;; atomic exchange on an INT.
jmp short cmpxchg_op_exit
cmpxchg_op_exit:
setz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop esi
pop edi
pop ecx
pop ebx
;mov esp,ebp ;; <-- not necessary, since no local variables were created.
pop ebp
ret 16 ;; return 16 bytes, (= 4 parameters, at 4 bytes each)
}
}
// In this version of cmpxchg, the user chooses the operand size (opsize)
// === Opsize can be of the following values ===
// opsize =1 (BYTE operand) = 8 bits
// opsize =2 (SHORT operand) = 16 bits
// opsize =4 (INT operand) = 32 bits
//
// (other values of opsize shall be ignored)
// === The "offset" parameter is an offset into "mem"
__declspec(naked) __declspec(dllexport) bool cmpxchg_op_off(volatile int64_t *mem, int64_t *expected, int64_t *new, ui opsize, ui offset)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
push edx
push edi
push esi
xor ecx,ecx
inc cl ;; CL =1
mov esi,[ebp+20] ;; ESI = opsize
mov edi,[ebp+8] ;; EDI points to "mem"
mov eax,[ebp+12] ;; EAX = "expected"
mov ebx,[ebp+16] ;; EBX = "new"
mov edx,[ebp+24] ;; EDX = offset into "mem"
shr esi,cl
jc cmpxchg_op1
shr esi,cl
jc cmpxchg_op2
shr esi,cl
jc cmpxchg_op4
jmp short cmpxchg_op_exit
cmpxchg_op1:
lock cmpxchg [edi+edx],bl ;; atomic exchange on a Byte.
jmp short cmpxchg_op_exit
cmpxchg_op2:
lock cmpxchg [edi+edx],bx ;; atomic exchange on a Short.
jmp short cmpxchg_op_exit
cmpxchg_op4:
lock cmpxchg [edi+edx],ebx ;; atomic exchange on an INT.
jmp short cmpxchg_op_exit
cmpxchg_op_exit:
setz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop esi
pop edi
pop edx
pop ecx
pop ebx
pop ebp
ret 20 ;; return 20 bytes, (= 5 parameters, at 4 bytes each)
}
}
// reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
//
// This atomic functtion returns the ZF, (ZF = result of comparison).
// -------- How it works :: --------
// "expected" is stored into AL, (=8 bits)
// then compare AL with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and AL=MEM (only temporarily) }
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_1(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected; Only AL shall be used.
mov ecx,[ebp+16] ;; ECX = new; Only CL shall be used
lock cmpxchg [ebx],cl
setz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop ecx
pop ebx
pop ebp
ret 12
}
}
// reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
//
// This atomic function returns the ZF, (ZF = result of comparison).
// -------- How it works :: --------
// "expected" is stored into AX, (=16 bits)
// then compare AX with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and AX=MEM (only temporarily) }
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_2(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected; Only AX shall be used.
mov ecx,[ebp+16] ;; ECX = new; Only CX shall be used
lock cmpxchg [ebx],cx
setz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop ecx
pop ebx
pop ebp
ret 12
}
}
// reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
//
// This atomic functtion returns the ZF, (ZF = result of comparison).
// -------- How it works :: --------
// "expected" is stored into EAX, (=32 bits)
// then compare EAX with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and EAX=MEM (only temporarily) }
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_4(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected;
mov ecx,[ebp+16] ;; ECX = new;
lock cmpxchg [ebx],ecx
setz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop ecx
pop ebx
pop ebp
ret 12
}
}
//-----------------------------------------------------------------------------------------------
// reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
//
// This atomic functtion returns the REVERSE of ZF, (ZF = result of comparison).
// -------- How it works :: --------
// "expected" is stored into AL, (=8 bits)
// then compare AL with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and AL=MEM (only temporarily) }
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_1_nz(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected; Only AL shall be used.
mov ecx,[ebp+16] ;; ECX = new; Only CL shall be used
lock cmpxchg [ebx],cl
setnz cl ;; store the not zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop ecx
pop ebx
pop ebp
ret 12
}
}
// reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
//
// This atomic functtion returns the REVERSE of ZF, (ZF = result of comparison).
// -------- How it works :: --------
// "expected" is stored into AX, (=16 bits)
// then compare AX with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and AX=MEM (only temporarily) }
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_2_nz(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected; Only AX shall be used.
mov ecx,[ebp+16] ;; ECX = new; Only CX shall be used
lock cmpxchg [ebx],cx
setnz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop ecx
pop ebx
pop ebp
ret 12
}
}
// reference : http://faydoc.tripod.com/cpu/cmpxchg.htm
//
// This atomic functtion returns the REVERSE of ZF, (ZF = result of comparison).
// -------- How it works :: --------
// "expected" is stored into EAX, (=32 bits)
// then compare EAX with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and EAX=MEM (only temporarily) }
//
__declspec(naked) __declspec(dllexport) bool cmpxchg_4_nz(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected;
mov ecx,[ebp+16] ;; ECX = new;
lock cmpxchg [ebx],ecx
setnz cl ;; store the zero flag result into CL
xor eax,eax
add al,cl ;; store CL into EAX, and return EAX
pop ecx
pop ebx
pop ebp
ret 12
}
}
__declspec(naked) __declspec(dllexport) unsigned int bitNOT(unsigned int val)
{
__asm {
push ebp
mov ebp,esp
mov eax,[ebp+8] ;; EAX = val
not eax ;; EAX = NOT EAX
pop ebp
ret
}
}
//-----------------------------------------------------------------------------------------------
//
// ........one of my weird inventions ........
//
// This atomic function returns: AL xor (ZF shl 7)
// In other words, the sign bit of AL is XORed with the ZF, (where ZF = result of comparison)
// -------- How it works :: --------
// "expected" is stored into AL, (=8 bits)
// then compare AL with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and AL=MEM }
//
__declspec(naked) __declspec(dllexport) ui cmpxchgSBxor_1(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected; Only AL shall be used.
mov ecx,[ebp+16] ;; ECX = new; Only CL shall be used
lock cmpxchg [ebx],cl
setz cl ;; store the zero flag result into CL
shl cl,7
xor al,cl ;; The ZF is XORed into sign bit of byte AL.
pop ecx
pop ebx
pop ebp
ret 12
}
}
//
// ........one of my weird inventions ........
//
// This atomic function returns: AX xor (ZF shl 15)
// In other words, the sign bit of AX is XORed with the ZF, (where ZF = result of comparison)
// -------- How it works :: --------
// "expected" is stored into AX, (=16 bits)
// then compare AX with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and AX=MEM }
//
__declspec(naked) __declspec(dllexport) ui cmpxchgSBxor_2(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected; Only AX shall be used.
mov ecx,[ebp+16] ;; ECX = new; Only CX shall be used
lock cmpxchg [ebx],cx
setz cl ;; store the zero flag result into CL
shl cx,15
xor ax,cx ;; The ZF is XORed into sign bit of SHORT AX.
pop ecx
pop ebx
pop ebp
ret 12
}
}
//
// ........one of my weird inventions ........
//
// This atomic function returns: EAX xor (ZF shl 31)
// In other words, the sign bit of EAX is XORed with the ZF, (where ZF = result of comparison)
// -------- How it works :: --------
// "expected" is stored into EAX, (=32 bits)
// then compare EAX with "mem".
// If equal then { ZF=1, and "expected" is stored in mem }
// else { ZF=0, and EAX=MEM }
//
__declspec(naked) __declspec(dllexport) ui cmpxchgSBxor_4(volatile ui *mem, ui expected, ui new)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = expected;
mov ecx,[ebp+16] ;; ECX = new;
lock cmpxchg [ebx],ecx
setz cl ;; store the zero flag result into CL
shl ecx,31
xor eax,ecx ;; The ZF is XORed into sign bit of INT EAX.
pop ecx
pop ebx
pop ebp
ret 12
}
}
// reference :: http://tinyurl.com/qfuqwhw
// Does an atomic swap of "mem" and register AL, (where AL = v),
// Automically returns the previous "mem" (BYTE).
__declspec(naked) __declspec(dllexport) ui xchg_1(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; AL = v
xchg [ebx],al ;; (The LOCK prefix is not needed for XCHG.)
pop ebx
pop ebp
ret 8
}
}
// reference :: http://tinyurl.com/qfuqwhw
// Does an atomic swap of "mem" and register AX, (where AX = v),
// and also atomically returns the previous "mem" (SHORT).
__declspec(naked) __declspec(dllexport) ui xchg_2(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; AX = v
xchg [ebx],ax
pop ebx
pop ebp
ret 8
}
}
// reference :: http://tinyurl.com/qfuqwhw
// Does an atomic swap of "mem" and register EAX, (where EAX = v),
// and also atomically returns the previous "mem" (INT).
__declspec(naked) __declspec(dllexport) ui xchg_4(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
xchg [ebx],eax
pop ebx
pop ebp
ret 8
}
}
// reference :: http://tinyurl.com/qfuqwhw
// Does an atomic swap of "mem" and register EAX, (where EAX = v),
// and also atomically returns the previous "mem" (INT).
// opsize can be 1,2,4
__declspec(naked) __declspec(dllexport) ui xchg_op(volatile int *mem, int v, int opsize)
{
__asm {
push ebp
mov ebp,esp
push ebx
push ecx
push edx
sub ecx,ecx
inc cx ;; CL =1
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
mov edx,[ebp+16] ;; edx = opsize
shr dl,cl
jc xchg_op1
shr dl,cl
jc xchg_op2
shr dl,cl
jc xchg_op4
jmp short xchg_op_exit
xchg_op1:
xchg [ebx],al
jmp short xchg_op_exit
xchg_op2:
xchg [ebx],ax
jmp short xchg_op_exit
xchg_op4:
xchg [ebx],eax
xchg_op_exit:
pop edx
pop ecx
pop ebx
pop ebp
ret 12
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Does an atomic add of "mem" and a byte value.
// Returns EAX=v.
__declspec(naked) __declspec(dllexport) ui AtomicAdd_1(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
lock add [ebx],al
pop ebx
pop ebp
ret 8
}
}
// Does an atomic add of "mem" and a short value.
// Returns EAX=v.
__declspec(naked) __declspec(dllexport) ui AtomicAdd_2(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
lock add [ebx],ax
pop ebx
pop ebp
ret 8
}
}
// Does an atomic add of "mem" and an INT value.
// Returns EAX=v.
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
lock add [ebx],eax
pop ebx
pop ebp
ret 8
}
}
// Does an atomic add of "mem" and a byte value.
// Atomically returns the overflow flag. (Set to 1 if overflow, else set to 0).
__declspec(naked) __declspec(dllexport) ui AtomicAdd_1o(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
lock add [ebx],al
seto bl
xor eax,eax
add al,bl ;; EAX = overflow flag
pop ebx
pop ebp
ret 8
}
}
// Does an atomic add of "mem" and a short value.
// Atomically returns the overflow flag. (Set to 1 if overflow, else set to 0).
__declspec(naked) __declspec(dllexport) ui AtomicAdd_2o(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
lock add [ebx],ax
seto bl
xor eax,eax
add al,bl ;; EAX = overflow flag
pop ebx
pop ebp
ret 8
}
}
// Does an atomic add of "mem" and an INT value.
// Atomically returns the overflow flag. (Set to 1 if overflow, else set to 0).
__declspec(naked) __declspec(dllexport) ui AtomicAdd_4o(volatile int *mem, int v)
{
__asm {
push ebp
mov ebp,esp
push ebx
mov ebx,[ebp+8] ;; EBX points to mem
mov eax,[ebp+12] ;; EAX = v
lock add [ebx],eax
seto bl
xor eax,eax
add al,bl ;; EAX = overflow flag
pop ebx
pop ebp
ret 8
}
}