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
use crate::*;

// AAA — ASCII Adjust After Addition
// AAS — ASCII Adjust After Subtraction
//
// Opcodes:
// - 37       AAA       ASCII adjust AL after addition
// - 3F       AAS       ASCII adjust AL after subtraction
//
// AAA Operation:
//   IF ((AL AND 0FH) > 9) OR (AF = 1) THEN
//     AX := AX + 106H;
//     AF := 1;
//     CF := 1;
//   ELSE
//     AF := 0;
//     CF := 0;
//   FI;
//   AL := AL AND 0FH;
//
// AAS Operation:
//   IF ((AL AND 0FH) > 9) OR (AF = 1) THEN
//     AX := AX - 6;
//     AH := AH - 1;
//     AF := 1;
//     CF := 1;
//   ELSE
//     AF := 0;
//     CF := 0;
//   FI;
//   AL := AL AND 0FH;
//
// Flags: AF and CF are modified. OF, SF, ZF, PF are undefined.

// ============================================================================
// AAA (ASCII Adjust After Addition) Tests
// ============================================================================

#[test]
fn test_aaa_no_adjustment_needed() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 5 (0x05), low nibble <= 9 and AF = 0
    let code = [
        0x37, // AAA
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0005;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should remain 5");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should remain 0");
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(!emu.flags().f_af, "AF should be clear");
}

#[test]
fn test_aaa_adjustment_needed_low_nibble() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x0A (low nibble > 9)
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x000A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AX = 0x000A + 0x0106 = 0x0110, then AL masked to 0x0F -> 0x00
    assert_eq!(emu.regs().rax & 0xFF, 0x00, "AL should be 0 after masking");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be incremented to 1");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aaa_adjustment_needed_af_set() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 5, but AF is set (previous operation had auxiliary carry)
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x0005;
    emu.flags_mut().load(0x10); // Set AF flag
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AX = 0x0005 + 0x0106 = 0x010B, then AL masked to 0x0F -> 0x0B
    assert_eq!(emu.regs().rax & 0xFF, 0x0B, "AL should be 0x0B after masking");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be incremented");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aaa_all_digits_0_through_9() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for digit in 0..=9 {
        let code = [0x37, 0xf4]; // AAA, HLT
        emu.regs_mut().rax = digit;
        emu.load_code_bytes(&code);
    emu.run(None).unwrap();

        assert_eq!(emu.regs().rax & 0xFF, digit, "AL should remain {}", digit);
        assert!(!emu.flags().f_cf, "CF should be clear for digit {}", digit);
        assert!(!emu.flags().f_af, "AF should be clear for digit {}", digit);
    }
}

#[test]
fn test_aaa_values_0a_through_0f() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for val in 0x0A..=0x0F {
        let code = [0x37, 0xf4]; // AAA, HLT
        emu.regs_mut().rax = val;
        emu.load_code_bytes(&code);
    emu.run(None).unwrap();

        let expected_al = val.wrapping_add(6) & 0x0F;
        assert_eq!(emu.regs().rax & 0xFF, expected_al, "AL should be masked for value 0x{:02X}", val);
        assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be 1 for value 0x{:02X}", val);
        assert!(emu.flags().f_cf, "CF should be set for value 0x{:02X}", val);
        assert!(emu.flags().f_af, "AF should be set for value 0x{:02X}", val);
    }
}

#[test]
fn test_aaa_with_initial_ah() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 5, AL = 0x0A
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x050A; // AH=5, AL=0x0A
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AX = 0x050A + 0x0106 = 0x0610, then AL masked to 0x0F -> 0x00
    assert_eq!(emu.regs().rax & 0xFF, 0x00, "AL should be 0");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x06, "AH should be 6");
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_aaa_bcd_addition_example() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x000F; // Result of 8 + 7
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should be 5 (ones digit)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be 1 (tens digit)");
}

#[test]
fn test_aaa_preserves_high_bits() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0xDEADBEEF_12340A0F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax >> 16, 0xDEADBEEF_1234, "High bits should be preserved");
}

#[test]
fn test_aaa_max_al_value() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xFF (both nibbles high)
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x00FF;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // 0x00FF + 0x0106 = 0x0205, AL masked to 0x0F -> 0x05
    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should be 5 after masking");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x02, "AH should be 2");
}

#[test]
fn test_aaa_ah_overflow() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0xFF, AL = 0x0A (causes AH to wrap)
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0xFF0A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // 0xFF0A + 0x0106 = 0x10010, AX wraps to 0x0010, then AL masked to 0x00
    assert_eq!(emu.regs().rax & 0xFFFF, 0x0000, "AX should wrap and mask to 0");
}

// ============================================================================
// AAS (ASCII Adjust After Subtraction) Tests
// ============================================================================

#[test]
fn test_aas_no_adjustment_needed() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 5 (0x05), low nibble <= 9 and AF = 0
    let code = [
        0x3F, // AAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0005;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should remain 5");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should remain 0");
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(!emu.flags().f_af, "AF should be clear");
}

#[test]
fn test_aas_adjustment_needed_low_nibble() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x0F (low nibble > 9)
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x000F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = AL - 6 = 0x0F - 6 = 0x09, then masked to 0x0F -> 0x09
    // AH = AH - 1 = 0x00 - 1 = 0xFF
    assert_eq!(emu.regs().rax & 0xFF, 0x09, "AL should be 9");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should be 0xFF (decremented)");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aas_adjustment_needed_af_set() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 5, but AF is set
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x0005;
    emu.flags_mut().load(0x10); // Set AF flag
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = AL - 6 = 5 - 6 = -1 = 0xFF, masked to 0x0F -> 0x0F
    // AH borrows from AL subtraction, then decrements by 1
    assert_eq!(emu.regs().rax & 0xFF, 0x0F, "AL should be 0x0F");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFE, "AH should be decremented with borrow");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aas_all_digits_0_through_9() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for digit in 0..=9 {
        let code = [0x3F, 0xf4]; // AAS, HLT
        emu.regs_mut().rax = digit;
        emu.load_code_bytes(&code);
    emu.run(None).unwrap();

        assert_eq!(emu.regs().rax & 0xFF, digit, "AL should remain {}", digit);
        assert!(!emu.flags().f_cf, "CF should be clear for digit {}", digit);
        assert!(!emu.flags().f_af, "AF should be clear for digit {}", digit);
    }
}

#[test]
fn test_aas_values_0a_through_0f() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for val in 0x0A..=0x0F {
        let code = [0x3F, 0xf4]; // AAS, HLT
        emu.regs_mut().rax = val;
        emu.load_code_bytes(&code);
    emu.run(None).unwrap();

        let expected_al = ((val as i8 - 6) as u8) & 0x0F;
        assert_eq!(emu.regs().rax & 0xFF, expected_al as u64, "AL should be adjusted for value 0x{:02X}", val);
        assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should be 0xFF for value 0x{:02X}", val);
        assert!(emu.flags().f_cf, "CF should be set for value 0x{:02X}", val);
        assert!(emu.flags().f_af, "AF should be set for value 0x{:02X}", val);
    }
}

#[test]
fn test_aas_with_initial_ah() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 5, AL = 0x0A
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x050A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x0A - 6 = 4, masked to 0x0F -> 0x04
    // AH = 5 - 1 = 4
    assert_eq!(emu.regs().rax & 0xFF, 0x04, "AL should be 4");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x04, "AH should be 4");
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_aas_bcd_subtraction_example() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x020C; // Simulating 12 - 6 with intermediate result
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x0C - 6 = 6, masked to 0x0F -> 0x06
    // AH = 2 - 1 = 1
    assert_eq!(emu.regs().rax & 0xFF, 0x06, "AL should be 6");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be 1");
}

#[test]
fn test_aas_preserves_high_bits() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0xDEADBEEF_12340F0F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax >> 16, 0xDEADBEEF_1234, "High bits should be preserved");
}

#[test]
fn test_aas_zero_ah() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0, AL = 0x0E (requires decrement of AH)
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x000E;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x0E - 6 = 8, masked to 0x0F -> 0x08
    // AH = 0 - 1 = 0xFF (wraps)
    assert_eq!(emu.regs().rax & 0xFF, 0x08, "AL should be 8");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should wrap to 0xFF");
}

#[test]
fn test_aas_max_ah() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0xFF, AL = 0x0B
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0xFF0B;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x0B - 6 = 5, masked to 0x0F -> 0x05
    // AH = 0xFF - 1 = 0xFE
    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should be 5");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFE, "AH should be 0xFE");
}

// ============================================================================
// Sequential AAA/AAS Tests
// ============================================================================

#[test]
fn test_aaa_then_aas() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x37, // AAA
        0x3F, // AAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x000E;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFFFF, 0xFF0E, "Result should be 0xFF0E");
}

#[test]
fn test_multiple_aaa_operations() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x37, // AAA
        0x37, // AAA
        0x37, // AAA
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x000F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x03, "AH should be 3");
}

// ============================================================================
// Edge Cases and Corner Cases
// ============================================================================

#[test]
fn test_aaa_with_all_flags_set() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x0003;
    emu.flags_mut().load(0xFFF); // Set all flags
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AF was set, so adjustment occurs: 0x0003 + 0x0106 = 0x0109, masked -> 0x0109
    assert_eq!(emu.regs().rax & 0xFF, 0x09, "AL should be 9");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be 1");
}

#[test]
fn test_aas_with_all_flags_set() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x0508;
    emu.flags_mut().load(0xFFF); // Set all flags
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AF was set, so adjustment occurs: AL = 8 - 6 = 2, AH = 5 - 1 = 4
    assert_eq!(emu.regs().rax & 0xFF, 0x02, "AL should be 2");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x04, "AH should be 4");
}

#[test]
fn test_aaa_zero_value() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x0000;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFFFF, 0x0000, "AX should remain 0");
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(!emu.flags().f_af, "AF should be clear");
}

#[test]
fn test_aas_zero_value() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x0000;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFFFF, 0x0000, "AX should remain 0");
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(!emu.flags().f_af, "AF should be clear");
}

#[test]
fn test_aaa_boundary_9_to_10() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x37, 0xf4]; // AAA, HLT

    emu.regs_mut().rax = 0x0009;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
    assert_eq!(emu.regs().rax & 0xFF, 0x09, "AL=9 needs no adjustment");
    assert!(!emu.flags().f_af, "AF should be clear for 9");

    emu.regs_mut().rax = 0x000A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
    assert_eq!(emu.regs().rax & 0xFFFF, 0x0100, "AL=0x0A should adjust to 0x0100");
    assert!(emu.flags().f_af, "AF should be set for 0x0A");
}

#[test]
fn test_aas_boundary_9_to_10() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3F, 0xf4]; // AAS, HLT

    emu.regs_mut().rax = 0x0509;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
    assert_eq!(emu.regs().rax & 0xFFFF, 0x0509, "AL=9 needs no adjustment");
    assert!(!emu.flags().f_af, "AF should be clear for 9");

    emu.regs_mut().rax = 0x050A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
    assert_eq!(emu.regs().rax & 0xFFFF, 0x0404, "AL=0x0A should adjust");
    assert!(emu.flags().f_af, "AF should be set for 0x0A");
}

#[test]
fn test_aaa_unpacked_bcd_chain() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x37, // AAA on first result
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0011; // 9 + 8 = 17 (0x11 in hex, adjust needed)
    emu.flags_mut().load(0x10); // Set AF to reflect carry from prior addition
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // 0x0011 + 0x0106 = 0x0117, masked -> 0x0107
    assert_eq!(emu.regs().rax & 0xFF, 0x07, "AL should be 7 (ones digit)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x01, "AH should be 1 (tens digit)");
}

#[test]
fn test_aas_unpacked_bcd_chain() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x3F, // AAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x030C; // Intermediate result needing adjustment
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x0C - 6 = 6, AH = 3 - 1 = 2
    assert_eq!(emu.regs().rax & 0xFF, 0x06, "AL should be 6");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x02, "AH should be 2");
}

#[test]
fn test_aaa_masking_high_nibble() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x37, 0xf4]; // AAA, HLT
    emu.regs_mut().rax = 0x00F5; // High nibble = F, low nibble = 5
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should be 5 (high nibble masked)");
}

#[test]
fn test_aas_masking_high_nibble() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3F, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x05F8; // High nibble = F, low nibble = 8
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x08, "AL should be 8 (high nibble masked)");
}