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

// AAS — ASCII Adjust AL After Subtraction
//
// Opcode: 3F
// Instruction: AAS
// Op/En: ZO (no operands)
// 64-bit Mode: Invalid
// Compat/Leg Mode: Valid
//
// Description:
// Adjusts the result of the subtraction of two unpacked BCD values to create
// an unpacked BCD result. The AL register is the implied source and destination.
// AAS is only useful when it follows a SUB instruction that subtracts (binary
// subtraction) one unpacked BCD value from another and stores a byte result in AL.
//
// Operation:
// IF ((AL AND 0FH) > 9) or (AF = 1)
// THEN
//     AX := AX - 6;
//     AH := AH - 1;
//     AF := 1;
//     CF := 1;
//     AL := AL AND 0FH;
// ELSE
//     CF := 0;
//     AF := 0;
//     AL := AL AND 0FH;
// FI;
//
// Flags Affected:
// AF and CF are set to 1 if there is a decimal borrow; otherwise cleared to 0.
// OF, SF, ZF, and PF are undefined.

// ============================================================================
// AAS - Basic Cases (No Adjustment)
// ============================================================================

#[test]
fn test_aas_no_adjustment_needed() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x05 (valid BCD), no adjustment needed
    let code = [
        0x3f, // AAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x05;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_zero() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x00, no adjustment needed
    let code = [0x3f, 0xf4]; // AAS, HLT
    emu.regs_mut().rax = 0x00;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_max_valid_bcd() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x09 (max valid BCD digit), no adjustment needed
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x09;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

// ============================================================================
// AAS - Adjustment Required (Lower Nibble > 9)
// ============================================================================

#[test]
fn test_aas_lower_nibble_0a() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x0A, requires adjustment
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x010A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x04, "AL should be 0x04 (0x0A - 0x06 = 0x04)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should be decremented to 0x00");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aas_lower_nibble_0b() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x0B
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x010B;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_lower_nibble_0f() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x0F (max value for lower nibble)
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x010F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x09, "AL should be 0x09");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should be decremented to 0x00");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

// ============================================================================
// AAS - With Upper Nibble Set
// ============================================================================

#[test]
fn test_aas_with_upper_nibble_1x() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x1A, upper nibble should be cleared
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x011A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x04, "AL should be 0x04 (upper nibble cleared)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should be decremented to 0x00");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aas_with_upper_nibble_2x() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x2B
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x012B;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_with_upper_nibble_fx() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xFC
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x01FC;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x06, "AL should be 0x06");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should be decremented to 0x00");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

// ============================================================================
// AAS - With Non-Zero AH
// ============================================================================

#[test]
fn test_aas_ah_nonzero_no_adjust() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0x05, AL = 0x03, no adjustment needed
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x0503;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_ah_nonzero_adjust() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0x07, AL = 0x0D, adjustment needed
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x070D;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x07, "AL should be 0x07");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x06, "AH should be decremented to 0x06");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aas_ah_underflow() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0x00, AL = 0x0E, test AH underflow
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x000E;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x08, "AL should be 0x08");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should wrap to 0xFF");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

// ============================================================================
// AAS - With AF Flag Set
// ============================================================================

#[test]
fn test_aas_af_set_valid_bcd() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x05 (valid BCD), but AF is set - should still adjust
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x0105;
    emu.flags_mut().load(0x10); // Set AF
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x0F, "AL should be 0x0F (0x05 - 0x06 = 0xFF, masked = 0x0F)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "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_af_set_zero() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x00, AF is set - should adjust
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x0100;
    emu.flags_mut().load(0x10); // Set AF
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x0A, "AL should be 0x0A");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "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_af_set_nine() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x09, AF is set - should adjust
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x0109;
    emu.flags_mut().load(0x10); // Set AF
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x03, "AL should be 0x03 (0x09 - 0x06 = 0x03)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "AH should be decremented to 0x00");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

// ============================================================================
// AAS - Realistic BCD Subtraction Examples
// ============================================================================

#[test]
fn test_aas_after_sub_8_minus_3() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb0, 0x08, // MOV AL, 8
        0x2c, 0x03, // SUB AL, 3
        0x3f,       // AAS
        0xf4,       // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_after_sub_3_minus_5() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb0, 0x03, // MOV AL, 3
        0x2c, 0x05, // SUB AL, 5 (result: 0xFE, AF set)
        0x3f,       // AAS
        0xf4,       // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x08, "AL should be 0x08 (0xFE - 0x06 = 0xF8, masked = 0x08)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should be 0xFF (borrow)");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_aas_after_sub_9_minus_9() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb0, 0x09, // MOV AL, 9
        0x2c, 0x09, // SUB AL, 9
        0x3f,       // AAS
        0xf4,       // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_after_sub_2_minus_7() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb0, 0x02, // MOV AL, 2
        0x2c, 0x07, // SUB AL, 7 (result: 0xFB, AF set)
        0x3f,       // AAS
        0xf4,       // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should be 0x05 (0xFB - 0x06 = 0xF5, masked = 0x05)");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should be 0xFF (borrow)");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

// ============================================================================
// AAS - Multi-Digit BCD Subtraction
// ============================================================================

#[test]
fn test_aas_multidigit_78_minus_34() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb0, 0x08, // MOV AL, 8
        0x2c, 0x04, // SUB AL, 4
        0x3f,       // AAS
        0xf4,       // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x04, "Ones digit should be 4");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x00, "No borrow to tens");
}

#[test]
fn test_aas_multidigit_52_minus_37() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb0, 0x02, // MOV AL, 2
        0x2c, 0x07, // SUB AL, 7 (result: 0xFB, AF set)
        0x3f,       // AAS
        0xf4,       // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "Ones digit should be 5");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "Borrow from tens");
}

// ============================================================================
// AAS - Edge Cases and Boundary Conditions
// ============================================================================

#[test]
fn test_aas_all_lower_nibbles() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for val in 0x00..=0x0F {
        let code = [0x3f, 0xf4];
        emu.regs_mut().rax = 0x0100 | val;
        emu.load_code_bytes(&code);
    emu.run(None).unwrap();

        if val <= 9 {
            assert_eq!(emu.regs().rax & 0xFF, val, "AL should remain {:#04x}", val);
            assert!(!emu.flags().f_cf, "CF should be clear for {:#04x}", val);
        } else {
            let expected = (val.wrapping_sub(6)) & 0x0F;
            assert_eq!(emu.regs().rax & 0xFF, expected, "AL should be {:#04x} for input {:#04x}", expected, val);
            assert!(emu.flags().f_cf, "CF should be set for {:#04x}", val);
        }
    }
}

#[test]
fn test_aas_upper_bits_cleared() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x01AB;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xF0, 0x00, "Upper 4 bits of AL should be cleared");
}

#[test]
fn test_aas_preserves_high_rax() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x1234_5678_DEAD_BE0F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_sequential_operations() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb8, 0x03, 0x00, // MOV AX, 0x0003 (AH=0, AL=3)
        0x2c, 0x05,       // SUB AL, 5 (result: 0xFE, borrow)
        0x3f,             // AAS (result: AL=8, AH=FF)
        0x80, 0xec, 0x09, // SUB AH, 9 (manually adjust tens)
        0xf4,             // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_max_value_ff() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xFF (all bits set)
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x01FF;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

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

#[test]
fn test_aas_double_borrow() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0xb8, 0x00, 0x00, // MOV AX, 0x0000
        0x2c, 0x05,       // SUB AL, 5 (result: 0xFB)
        0x3f,             // AAS
        0xf4,             // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x05, "AL should be 5");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0xFF, "AH should wrap to 0xFF");
}

#[test]
fn test_aas_with_high_ah() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AH = 0x99, AL = 0x0C (test with high AH value)
    let code = [0x3f, 0xf4];
    emu.regs_mut().rax = 0x990C;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x06, "AL should be 0x06");
    assert_eq!((emu.regs().rax >> 8) & 0xFF, 0x98, "AH should be decremented to 0x98");
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}