libmwemu 0.24.1

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
//! Tests for the MOVQ instruction (MMX).
//!
//! MOVQ - Move Quadword
//!
//! Copies a quadword from the source operand (second operand) to the destination
//! operand (first operand). The source and destination operands can be MMX technology
//! registers or 64-bit memory locations. This instruction can be used to move a
//! quadword between two MMX technology registers or between an MMX technology register
//! and a 64-bit memory location.
//!
//! Opcodes:
//! - NP 0F 6F /r: MOVQ mm, mm/m64  (load from memory or register to MMX register)
//! - NP 0F 7F /r: MOVQ mm/m64, mm  (store from MMX register to memory or register)
//!
//! Operation: DEST := SRC
//!
//! Flags affected: None
//!
//! Reference: /Users/int/dev/rax/docs/movq.txt

use crate::*;

// ============================================================================
// MOVQ Tests: Register to Register
// ============================================================================

#[test]
fn test_movq_mm0_mm1() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x0c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM1, [0x2000]
        0x0f, 0x6f, 0xc1,                               // MOVQ MM0, MM1
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x0123456789ABCDEF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x0123456789ABCDEF,
        "MOVQ MM0, MM1 should copy value");
}

#[test]
fn test_movq_mm2_mm3() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x1c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM3, [0x2000]
        0x0f, 0x6f, 0xd3,                               // MOVQ MM2, MM3
        0x0f, 0x7f, 0x14, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM2
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xFEDCBA9876543210);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xFEDCBA9876543210,
        "MOVQ MM2, MM3 should copy value");
}

#[test]
fn test_movq_mm4_mm5() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x2c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM5, [0x2000]
        0x0f, 0x6f, 0xe5,                               // MOVQ MM4, MM5
        0x0f, 0x7f, 0x24, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM4
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xAAAA_BBBB_CCCC_DDDD);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xAAAA_BBBB_CCCC_DDDD,
        "MOVQ MM4, MM5 should copy value");
}

#[test]
fn test_movq_mm6_mm7() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x3c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM7, [0x2000]
        0x0f, 0x6f, 0xf7,                               // MOVQ MM6, MM7
        0x0f, 0x7f, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM6
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x1111_2222_3333_4444);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x1111_2222_3333_4444,
        "MOVQ MM6, MM7 should copy value");
}

#[test]
fn test_movq_same_register() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x6f, 0xc0,                               // MOVQ MM0, MM0
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xCAFEBABEDEADBEEF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xCAFEBABEDEADBEEF,
        "MOVQ MM0, MM0 should preserve value");
}

// ============================================================================
// MOVQ Tests: Memory to Register
// ============================================================================

#[test]
fn test_movq_mm0_m64() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x0011223344556677);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x0011223344556677,
        "MOVQ MM0, [mem] should load from memory");
}

#[test]
fn test_movq_mm1_m64() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00, // MOVQ MM1, [0x2008]
        0x0f, 0x7f, 0x0c, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM1
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2008, 0x8877665544332211);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x8877665544332211,
        "MOVQ MM1, [mem] should load from memory");
}

#[test]
fn test_movq_mm_m64_all_zeros() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x0000000000000000);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x0000000000000000,
        "MOVQ should load all zeros");
}

#[test]
fn test_movq_mm_m64_all_ones() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xFFFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xFFFFFFFFFFFFFFFF,
        "MOVQ should load all ones");
}

#[test]
fn test_movq_mm_m64_alternating() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xAAAAAAAAAAAAAAAA);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xAAAAAAAAAAAAAAAA,
        "MOVQ should load alternating pattern");
}

// ============================================================================
// MOVQ Tests: Register to Memory
// ============================================================================

#[test]
fn test_movq_m64_mm0() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x7f, 0x04, 0x25, 0x08, 0x30, 0x00, 0x00, // MOVQ [0x3008], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x123456789ABCDEF0);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3008).unwrap();
    assert_eq!(result, 0x123456789ABCDEF0,
        "MOVQ [mem], MM0 should store to memory");
}

#[test]
fn test_movq_m64_mm7() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x3c, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM7, [0x2000]
        0x0f, 0x7f, 0x3c, 0x25, 0x10, 0x30, 0x00, 0x00, // MOVQ [0x3010], MM7
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xFEDCBA9876543210);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3010).unwrap();
    assert_eq!(result, 0xFEDCBA9876543210,
        "MOVQ [mem], MM7 should store to memory");
}

#[test]
fn test_movq_store_all_zeros() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x0000000000000000);
    emu.maps.write_qword(0x3000, 0xFFFFFFFFFFFFFFFF); // Pre-fill with ones

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x0000000000000000,
        "MOVQ should store all zeros");
}

#[test]
fn test_movq_store_all_ones() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xFFFFFFFFFFFFFFFF);
    emu.maps.write_qword(0x3000, 0x0000000000000000); // Pre-fill with zeros

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xFFFFFFFFFFFFFFFF,
        "MOVQ should store all ones");
}

// ============================================================================
// MOVQ Tests: Memory to Memory (via Register)
// ============================================================================

#[test]
fn test_movq_mem_to_mem() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xDEADBEEFCAFEBABE);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xDEADBEEFCAFEBABE,
        "MOVQ should transfer data from mem to mem via register");
}

#[test]
fn test_movq_multiple_transfers() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0x0f, 0x6f, 0x0c, 0x25, 0x08, 0x20, 0x00, 0x00, // MOVQ MM1, [0x2008]
        0x0f, 0x7f, 0x0c, 0x25, 0x08, 0x30, 0x00, 0x00, // MOVQ [0x3008], MM1
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x1111111111111111);
    emu.maps.write_qword(0x2008, 0x2222222222222222);

    emu.run(None).unwrap();

    let result1 = emu.maps.read_qword(0x3000).unwrap();
    let result2 = emu.maps.read_qword(0x3008).unwrap();
    assert_eq!(result1, 0x1111111111111111, "First MOVQ transfer");
    assert_eq!(result2, 0x2222222222222222, "Second MOVQ transfer");
}

// ============================================================================
// MOVQ Tests: Chain of Register Transfers
// ============================================================================

#[test]
fn test_movq_register_chain() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x6f, 0xc8,                               // MOVQ MM1, MM0
        0x0f, 0x6f, 0xd1,                               // MOVQ MM2, MM1
        0x0f, 0x6f, 0xda,                               // MOVQ MM3, MM2
        0x0f, 0x7f, 0x1c, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM3
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xBEEFCAFEDEADC0DE);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xBEEFCAFEDEADC0DE,
        "Value should propagate through register chain");
}

#[test]
fn test_movq_circular_move() {
    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, 0x6f, 0xd0,                               // MOVQ MM2, MM0 (temp)
        0x0f, 0x6f, 0xc1,                               // MOVQ MM0, MM1
        0x0f, 0x6f, 0xca,                               // MOVQ MM1, MM2
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0x0f, 0x7f, 0x0c, 0x25, 0x08, 0x30, 0x00, 0x00, // MOVQ [0x3008], MM1
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xAAAAAAAAAAAAAAAA);
    emu.maps.write_qword(0x2008, 0xBBBBBBBBBBBBBBBB);

    emu.run(None).unwrap();

    let result1 = emu.maps.read_qword(0x3000).unwrap();
    let result2 = emu.maps.read_qword(0x3008).unwrap();
    assert_eq!(result1, 0xBBBBBBBBBBBBBBBB, "MM0 should have MM1's value");
    assert_eq!(result2, 0xAAAAAAAAAAAAAAAA, "MM1 should have MM0's value");
}

// ============================================================================
// MOVQ Tests: Edge Cases and Special Values
// ============================================================================

#[test]
fn test_movq_high_bit_set() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x8000000000000000);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x8000000000000000,
        "MOVQ should handle high bit set");
}

#[test]
fn test_movq_max_value() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xFFFFFFFFFFFFFFFF);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0xFFFFFFFFFFFFFFFF,
        "MOVQ should handle max value");
}

#[test]
fn test_movq_one() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x0000000000000001);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x0000000000000001,
        "MOVQ should handle value 1");
}

#[test]
fn test_movq_power_of_two() {
    let mut emu = emu64();
    let test_values = [
        0x0000000000000001, // 2^0
        0x0000000000000002, // 2^1
        0x0000000000000100, // 2^8
        0x0000000000010000, // 2^16
        0x0000000100000000, // 2^32
        0x8000000000000000, // 2^63
    ];

    for (i, &value) in test_values.iter().enumerate() {
        let code = vec![
            0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
            0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
            0xf4,
        ];

        emu.load_code_bytes(&code);
        emu.maps.write_qword(0x2000, value);

    emu.run(None).unwrap();

        let result = emu.maps.read_qword(0x3000).unwrap();
        assert_eq!(result, value,
            "MOVQ should handle power of two #{}", i);
    }
}

// ============================================================================
// MOVQ Tests: Byte Patterns
// ============================================================================

#[test]
fn test_movq_sequential_bytes() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x0001020304050607);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x0001020304050607,
        "MOVQ should preserve sequential bytes");
}

#[test]
fn test_movq_repeating_bytes() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x1212121212121212);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x1212121212121212,
        "MOVQ should preserve repeating bytes");
}

// ============================================================================
// MOVQ Tests: Multiple Registers
// ============================================================================

#[test]
fn test_movq_all_registers() {
    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, 0x6f, 0x14, 0x25, 0x10, 0x20, 0x00, 0x00, // MOVQ MM2, [0x2010]
        0x0f, 0x6f, 0x1c, 0x25, 0x18, 0x20, 0x00, 0x00, // MOVQ MM3, [0x2018]
        0x0f, 0x6f, 0x24, 0x25, 0x20, 0x20, 0x00, 0x00, // MOVQ MM4, [0x2020]
        0x0f, 0x6f, 0x2c, 0x25, 0x28, 0x20, 0x00, 0x00, // MOVQ MM5, [0x2028]
        0x0f, 0x6f, 0x34, 0x25, 0x30, 0x20, 0x00, 0x00, // MOVQ MM6, [0x2030]
        0x0f, 0x6f, 0x3c, 0x25, 0x38, 0x20, 0x00, 0x00, // MOVQ MM7, [0x2038]
        // Store them back
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0x0f, 0x7f, 0x0c, 0x25, 0x08, 0x30, 0x00, 0x00, // MOVQ [0x3008], MM1
        0x0f, 0x7f, 0x14, 0x25, 0x10, 0x30, 0x00, 0x00, // MOVQ [0x3010], MM2
        0x0f, 0x7f, 0x1c, 0x25, 0x18, 0x30, 0x00, 0x00, // MOVQ [0x3018], MM3
        0x0f, 0x7f, 0x24, 0x25, 0x20, 0x30, 0x00, 0x00, // MOVQ [0x3020], MM4
        0x0f, 0x7f, 0x2c, 0x25, 0x28, 0x30, 0x00, 0x00, // MOVQ [0x3028], MM5
        0x0f, 0x7f, 0x34, 0x25, 0x30, 0x30, 0x00, 0x00, // MOVQ [0x3030], MM6
        0x0f, 0x7f, 0x3c, 0x25, 0x38, 0x30, 0x00, 0x00, // MOVQ [0x3038], MM7
        0xf4,
    ];

    emu.load_code_bytes(&code);
    for i in 0..8 {
        emu.maps.write_qword(0x2000 + i * 8, 0x1111 * (i + 1) as u64);
    }

    emu.run(None).unwrap();

    for i in 0..8 {
        let result = emu.maps.read_qword(0x3000 + i * 8).unwrap();
        assert_eq!(result, 0x1111 * (i + 1) as u64,
            "MM{} value should be preserved", i);
    }
}

// ============================================================================
// MOVQ Tests: Interaction with Other Instructions
// ============================================================================

#[test]
fn test_movq_with_emms() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0x0f, 0x77,                                      // EMMS
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x123456789ABCDEF0);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x123456789ABCDEF0,
        "MOVQ should work correctly before EMMS");
}

#[test]
fn test_movq_overwrite() {
    let mut emu = emu64();
    let code = vec![
        0x0f, 0x6f, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2000]
        0x0f, 0x6f, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // MOVQ MM0, [0x2008] (overwrite)
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0x1111111111111111);
    emu.maps.write_qword(0x2008, 0x2222222222222222);

    emu.run(None).unwrap();

    let result = emu.maps.read_qword(0x3000).unwrap();
    assert_eq!(result, 0x2222222222222222,
        "Second MOVQ should overwrite first value");
}

#[test]
fn test_movq_load_store_independence() {
    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, 0x6f, 0x14, 0x25, 0x10, 0x20, 0x00, 0x00, // MOVQ MM2, [0x2010]
        0x0f, 0x7f, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // MOVQ [0x3000], MM0
        0x0f, 0x7f, 0x0c, 0x25, 0x08, 0x30, 0x00, 0x00, // MOVQ [0x3008], MM1
        0x0f, 0x7f, 0x14, 0x25, 0x10, 0x30, 0x00, 0x00, // MOVQ [0x3010], MM2
        0xf4,
    ];

    emu.load_code_bytes(&code);
    emu.maps.write_qword(0x2000, 0xAAAAAAAAAAAAAAAA);
    emu.maps.write_qword(0x2008, 0xBBBBBBBBBBBBBBBB);
    emu.maps.write_qword(0x2010, 0xCCCCCCCCCCCCCCCC);

    emu.run(None).unwrap();

    let result0 = emu.maps.read_qword(0x3000).unwrap();
    let result1 = emu.maps.read_qword(0x3008).unwrap();
    let result2 = emu.maps.read_qword(0x3010).unwrap();

    assert_eq!(result0, 0xAAAAAAAAAAAAAAAA, "MM0 value");
    assert_eq!(result1, 0xBBBBBBBBBBBBBBBB, "MM1 value");
    assert_eq!(result2, 0xCCCCCCCCCCCCCCCC, "MM2 value");
}