libmwemu 0.24.0

x86 32/64bits and system internals emulator, for securely emulating malware and other stuff.
Documentation
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
//! Tests for PANDN instruction (MMX).
//!
//! PANDN - Logical AND NOT (MMX)
//!
//! Performs bitwise AND NOT operation: dest = (~dest) & src
//! The destination operand is inverted, then ANDed with the source.
//!
//! Opcode: 0F DF /r
//!
//! Flags affected: None
//!
//! Reference: /Users/int/dev/rax/docs/pandn.txt

use crate::*;

fn write_mm_via_mem(mem: u64, addr: u64, value: u64) {
    let mut emu = emu64();
    emu.maps.write_qword(addr, value);
}

// ============================================================================
// PANDN mm, mm/m64 (opcode 0F DF /r)
// ============================================================================

#[test]
fn test_pandn_mm_mm_basic() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00, // MOVQ MM1, [0x2008]
        0x0f, 0xdf, 0xc1,                               // PANDN MM0, MM1
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00, // MOVQ [0x2010], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);

    // (~0xF0F0F0F0F0F0F0F0) & 0xFF00FF00FF00FF00
    // = 0x0F0F0F0F0F0F0F0F & 0xFF00FF00FF00FF00
    // = 0x0F000F000F000F00
    emu.maps.write_qword(0x2000, 0xF0F0F0F0F0F0F0F0);
    emu.maps.write_qword(0x2008, 0xFF00FF00FF00FF00);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0x0F000F000F000F00, "PANDN: basic operation");
}

#[test]
fn test_pandn_all_zeros() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    // (~0x0000000000000000) & 0xFFFFFFFFFFFFFFFF = 0xFFFFFFFFFFFFFFFF
    emu.maps.write_qword(0x2000, 0x0000000000000000);
    emu.maps.write_qword(0x2008, 0xFFFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0xFFFFFFFFFFFFFFFF, "PANDN: all zeros dest");
}

#[test]
fn test_pandn_all_ones() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    // (~0xFFFFFFFFFFFFFFFF) & anything = 0x0000000000000000
    emu.maps.write_qword(0x2000, 0xFFFFFFFFFFFFFFFF);
    emu.maps.write_qword(0x2008, 0xAAAAAAAAAAAAAAAA);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0x0000000000000000, "PANDN: all ones dest");
}

#[test]
fn test_pandn_identity() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    // (~X) & X = 0
    emu.maps.write_qword(0x2000, 0x1234567890ABCDEF);
    emu.maps.write_qword(0x2008, 0x1234567890ABCDEF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0x0000000000000000, "PANDN: identity (X & ~X = 0)");
}

#[test]
fn test_pandn_complement() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    // (~X) & ~X = ~X
    emu.maps.write_qword(0x2000, 0xAAAAAAAAAAAAAAAA);
    emu.maps.write_qword(0x2008, 0x5555555555555555);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0x5555555555555555, "PANDN: complement pattern");
}

#[test]
fn test_pandn_mm_m64() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x14, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM2, [0x2000]
        0x0f, 0xdf, 0x14, 0x25, 0x08, 0x20, 0x00, 0x00, // PANDN MM2, [0x2008]
        0x0f, 0x7f, 0x14, 0x25, 0x10, 0x20, 0x00, 0x00, // MOVQ [0x2010], MM2
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFF00FF00FF00FF00);
    emu.maps.write_qword(0x2008, 0x0F0F0F0F0F0F0F0F);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xFF00FF00FF00FF00 = 0x00FF00FF00FF00FF
    // 0x00FF00FF00FF00FF & 0x0F0F0F0F0F0F0F0F = 0x000F000F000F000F
    assert_eq!(result, 0x000F000F000F000F, "PANDN: memory operand");
}

#[test]
fn test_pandn_alternating_bits_1() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xAAAAAAAAAAAAAAAA);
    emu.maps.write_qword(0x2008, 0xFFFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xAAAAAAAAAAAAAAAA = 0x5555555555555555
    // 0x5555555555555555 & 0xFFFFFFFFFFFFFFFF = 0x5555555555555555
    assert_eq!(result, 0x5555555555555555, "PANDN: alternating bits");
}

#[test]
fn test_pandn_alternating_bits_2() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x5555555555555555);
    emu.maps.write_qword(0x2008, 0xAAAAAAAAAAAAAAAA);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x5555555555555555 = 0xAAAAAAAAAAAAAAAA
    // 0xAAAAAAAAAAAAAAAA & 0xAAAAAAAAAAAAAAAA = 0xAAAAAAAAAAAAAAAA
    assert_eq!(result, 0xAAAAAAAAAAAAAAAA, "PANDN: alternating bits reverse");
}

#[test]
fn test_pandn_byte_pattern() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x0123456789ABCDEF);
    emu.maps.write_qword(0x2008, 0xFEDCBA9876543210);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x0123456789ABCDEF = 0xFEDCBA9876543210
    // 0xFEDCBA9876543210 & 0xFEDCBA9876543210 = 0xFEDCBA9876543210
    assert_eq!(result, 0xFEDCBA9876543210, "PANDN: byte pattern");
}

#[test]
fn test_pandn_high_bits() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFFFFFFFF00000000);
    emu.maps.write_qword(0x2008, 0x00000000FFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xFFFFFFFF00000000 = 0x00000000FFFFFFFF
    // 0x00000000FFFFFFFF & 0x00000000FFFFFFFF = 0x00000000FFFFFFFF
    assert_eq!(result, 0x00000000FFFFFFFF, "PANDN: high bits");
}

#[test]
fn test_pandn_low_bits() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x00000000FFFFFFFF);
    emu.maps.write_qword(0x2008, 0xFFFFFFFF00000000);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x00000000FFFFFFFF = 0xFFFFFFFF00000000
    // 0xFFFFFFFF00000000 & 0xFFFFFFFF00000000 = 0xFFFFFFFF00000000
    assert_eq!(result, 0xFFFFFFFF00000000, "PANDN: low bits");
}

#[test]
fn test_pandn_nibble_pattern() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xF0F0F0F0F0F0F0F0);
    emu.maps.write_qword(0x2008, 0x0F0F0F0F0F0F0F0F);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xF0F0F0F0F0F0F0F0 = 0x0F0F0F0F0F0F0F0F
    // 0x0F0F0F0F0F0F0F0F & 0x0F0F0F0F0F0F0F0F = 0x0F0F0F0F0F0F0F0F
    assert_eq!(result, 0x0F0F0F0F0F0F0F0F, "PANDN: nibble pattern");
}

#[test]
fn test_pandn_sequential() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,                               // PANDN MM0, MM1
        0x0f, 0xdf, 0xc1,                               // PANDN MM0, MM1 (again)
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFFFF0000FFFF0000);
    emu.maps.write_qword(0x2008, 0x0000FFFF0000FFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0x0000000000000000, "PANDN: sequential operations");
}

#[test]
fn test_pandn_mm3_mm4() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x1c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM3, [0x2000]
        0x0f, 0x6f, 0x24, 0x25, 0x08, 0x20, 0x00, 0x00, // MOVQ MM4, [0x2008]
        0x0f, 0xdf, 0xdc,                               // PANDN MM3, MM4
        0x0f, 0x7f, 0x1c, 0x25, 0x10, 0x20, 0x00, 0x00, // MOVQ [0x2010], MM3
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x8080808080808080);
    emu.maps.write_qword(0x2008, 0x7F7F7F7F7F7F7F7F);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x8080808080808080 = 0x7F7F7F7F7F7F7F7F
    // 0x7F7F7F7F7F7F7F7F & 0x7F7F7F7F7F7F7F7F = 0x7F7F7F7F7F7F7F7F
    assert_eq!(result, 0x7F7F7F7F7F7F7F7F, "PANDN: MM3 and MM4");
}

#[test]
fn test_pandn_single_bit_patterns() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x0000000000000001);
    emu.maps.write_qword(0x2008, 0xFFFFFFFFFFFFFFFE);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x0000000000000001 = 0xFFFFFFFFFFFFFFFE
    // 0xFFFFFFFFFFFFFFFE & 0xFFFFFFFFFFFFFFFE = 0xFFFFFFFFFFFFFFFE
    assert_eq!(result, 0xFFFFFFFFFFFFFFFE, "PANDN: single bit patterns");
}

#[test]
fn test_pandn_msb_pattern() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x8000000000000000);
    emu.maps.write_qword(0x2008, 0x7FFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x8000000000000000 = 0x7FFFFFFFFFFFFFFF
    // 0x7FFFFFFFFFFFFFFF & 0x7FFFFFFFFFFFFFFF = 0x7FFFFFFFFFFFFFFF
    assert_eq!(result, 0x7FFFFFFFFFFFFFFF, "PANDN: MSB pattern");
}

#[test]
fn test_pandn_byte_mask_1() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFF00FF00FF00FF00);
    emu.maps.write_qword(0x2008, 0x0000FFFF0000FFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xFF00FF00FF00FF00 = 0x00FF00FF00FF00FF
    // 0x00FF00FF00FF00FF & 0x0000FFFF0000FFFF = 0x0000000F0000000F (wrong calc)
    // 0x00 & 0x00 = 0x00, 0xFF & 0x00 = 0x00, 0x00 & 0xFF = 0x00, 0xFF & 0xFF = 0xFF
    assert_eq!(result, 0x000000FF000000FF, "PANDN: byte mask 1");
}

#[test]
fn test_pandn_word_boundaries() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFFFF0000FFFF0000);
    emu.maps.write_qword(0x2008, 0xFFFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xFFFF0000FFFF0000 = 0x0000FFFF0000FFFF
    // 0x0000FFFF0000FFFF & 0xFFFFFFFFFFFFFFFF = 0x0000FFFF0000FFFF
    assert_eq!(result, 0x0000FFFF0000FFFF, "PANDN: word boundaries");
}

#[test]
fn test_pandn_dword_boundaries() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFFFFFFFF00000000);
    emu.maps.write_qword(0x2008, 0x12345678FEDCBA98);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xFFFFFFFF00000000 = 0x00000000FFFFFFFF
    // 0x00000000FFFFFFFF & 0x12345678FEDCBA98 = 0x00000000FEDCBA98
    assert_eq!(result, 0x00000000FEDCBA98, "PANDN: dword boundaries");
}

#[test]
fn test_pandn_sparse_bits() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x0101010101010101);
    emu.maps.write_qword(0x2008, 0xFEFEFEFEFEFEFEFE);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x0101010101010101 = 0xFEFEFEFEFEFEFEFE
    // 0xFEFEFEFEFEFEFEFE & 0xFEFEFEFEFEFEFEFE = 0xFEFEFEFEFEFEFEFE
    assert_eq!(result, 0xFEFEFEFEFEFEFEFE, "PANDN: sparse bits");
}

#[test]
fn test_pandn_checkerboard_1() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xCCCCCCCCCCCCCCCC);
    emu.maps.write_qword(0x2008, 0x3333333333333333);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xCCCCCCCCCCCCCCCC = 0x3333333333333333
    // 0x3333333333333333 & 0x3333333333333333 = 0x3333333333333333
    assert_eq!(result, 0x3333333333333333, "PANDN: checkerboard pattern");
}

#[test]
fn test_pandn_with_zeros_src() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    // (~anything) & 0 = 0
    emu.maps.write_qword(0x2000, 0x123456789ABCDEF0);
    emu.maps.write_qword(0x2008, 0x0000000000000000);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    assert_eq!(result, 0x0000000000000000, "PANDN: source is zero");
}

#[test]
fn test_pandn_all_mm_registers() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x2c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM5, [0x2000]
        0x0f, 0x6f, 0x34, 0x25, 0x08, 0x20, 0x00, 0x00, // MOVQ MM6, [0x2008]
        0x0f, 0xdf, 0xee,                               // PANDN MM5, MM6
        0x0f, 0x6f, 0x3c, 0x25, 0x10, 0x20, 0x00, 0x00, // MOVQ MM7, [0x2010]
        0x0f, 0xdf, 0xef,                               // PANDN MM5, MM7
        0x0f, 0x7f, 0x2c, 0x25, 0x18, 0x20, 0x00, 0x00, // MOVQ [0x2018], MM5
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xF000000000000000);
    emu.maps.write_qword(0x2008, 0x0F00000000000000);
    emu.maps.write_qword(0x2010, 0x00F0000000000000);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2018).unwrap();
    assert_eq!(result, 0x00F0000000000000, "PANDN: all MM registers");
}

#[test]
fn test_pandn_mask_isolation() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xF0F0F0F0F0F0F0F0);
    emu.maps.write_qword(0x2008, 0x123456789ABCDEF0);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xF0F0F0F0F0F0F0F0 = 0x0F0F0F0F0F0F0F0F
    // 0x0F0F0F0F0F0F0F0F & 0x123456789ABCDEF0 = 0x020406080A0C0E00
    assert_eq!(result, 0x020406080A0C0E00, "PANDN: mask isolation");
}

#[test]
fn test_pandn_bit_clearing() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x00000000000000FF); // Clear all but low byte
    emu.maps.write_qword(0x2008, 0xFFFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x00000000000000FF = 0xFFFFFFFFFFFFFF00
    // 0xFFFFFFFFFFFFFF00 & 0xFFFFFFFFFFFFFFFF = 0xFFFFFFFFFFFFFF00
    assert_eq!(result, 0xFFFFFFFFFFFFFF00, "PANDN: bit clearing");
}

#[test]
fn test_pandn_random_pattern_1() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xDEADBEEFCAFEBABE);
    emu.maps.write_qword(0x2008, 0x2152411035014541);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0xDEADBEEFCAFEBABE = 0x2152411035014541
    // 0x2152411035014541 & 0x2152411035014541 = 0x2152411035014541
    assert_eq!(result, 0x2152411035014541, "PANDN: random pattern 1");
}

#[test]
fn test_pandn_random_pattern_2() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x8421084210842108);
    emu.maps.write_qword(0x2008, 0x7BDEF7BDEF7BDEF7);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x8421084210842108 = 0x7BDEF7BDEF7BDEF7
    // 0x7BDEF7BDEF7BDEF7 & 0x7BDEF7BDEF7BDEF7 = 0x7BDEF7BDEF7BDEF7
    assert_eq!(result, 0x7BDEF7BDEF7BDEF7, "PANDN: random pattern 2");
}

#[test]
fn test_pandn_power_of_two() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0x0000000000000100); // Bit 8
    emu.maps.write_qword(0x2008, 0xFFFFFFFFFFFFFEFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2010).unwrap();
    // ~0x0000000000000100 = 0xFFFFFFFFFFFFFEFF
    // 0xFFFFFFFFFFFFFEFF & 0xFFFFFFFFFFFFFEFF = 0xFFFFFFFFFFFFFEFF
    assert_eq!(result, 0xFFFFFFFFFFFFFEFF, "PANDN: power of two");
}

#[test]
fn test_pandn_complement_validation() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,
        0x0f, 0x7f, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00,
        0xf4,
    ];

    let test_values = vec![
        (0x0000000000000000u64, 0xFFFFFFFFFFFFFFFFu64),
        (0xFFFFFFFFFFFFFFFFu64, 0x0000000000000000u64),
        (0xAAAAAAAAAAAAAAAAu64, 0x5555555555555555u64),
        (0x5555555555555555u64, 0xAAAAAAAAAAAAAAAAu64),
        (0xF0F0F0F0F0F0F0F0u64, 0x0F0F0F0F0F0F0F0Fu64),
    ];

    for (dest, src) in test_values {
        emu.load_code_bytes(&code);
        emu.maps.write_qword(0x2000, dest);
        emu.maps.write_qword(0x2008, src);
    emu.run(None).unwrap();
        let result = emu.maps.read_qword(0x2010).unwrap();
        assert_eq!(result, src, "PANDN: complement should yield source");
    }
}

#[test]
fn test_pandn_associativity() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc1,                               // MM0 = ~MM0 & MM1
        0x0f, 0x6f, 0x14, 0x25, 0x10, 0x20, 0x00, 0x00,
        0x0f, 0xdf, 0xc2,                               // MM0 = ~MM0 & MM2
        0x0f, 0x7f, 0x04, 0x25, 0x18, 0x20, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);

    emu.maps.write_qword(0x2000, 0xFFFFFFFF00000000);
    emu.maps.write_qword(0x2008, 0xFFFF0000FFFF0000);
    emu.maps.write_qword(0x2010, 0xFF00FF00FF00FF00);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x2018).unwrap();
    assert_eq!(result, 0xFF00FF000000FF00, "PANDN: chained operations");
}