libmwemu 0.24.3

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 the FLD instruction.
//!
//! FLD - Load Floating-Point Value
//!
//! Pushes the source operand onto the FPU register stack. The source operand can be in
//! single precision, double precision, or double extended-precision floating-point format.
//! If the source operand is in single precision or double precision floating-point format,
//! it is automatically converted to the double extended-precision floating-point format
//! before being pushed on the stack.
//!
//! The FLD instruction can also push the value in a selected FPU register [ST(i)] onto
//! the stack. Here, pushing register ST(0) duplicates the stack top.
//!
//! Reference: /Users/int/dev/rax/docs/fld.txt

use crate::*;

const DATA_ADDR: u64 = 0x2000;

// Helper to write f32 to memory
fn write_f32(mem: u64, addr: u64, value: f32) {
    let mut emu = emu64();
    emu.maps.write_bytes_slice(addr, &value.to_le_bytes());
}

// Helper to write f64 to memory
fn write_f64(mem: u64, addr: u64, value: f64) {
    let mut emu = emu64();
    emu.maps.write_bytes_slice(addr, &value.to_le_bytes());
}

// Helper to read ST(0) as f64 from memory after FSTP
fn read_st0_as_f64(mem: u64, addr: u64) -> f64 {
    let emu = emu64();
    let mut buf = [0u8; 8];
    emu.maps.read_bytes_buff(&mut buf, addr);
    f64::from_le_bytes(buf)
}

// ============================================================================
// FLD m32fp (opcode D9 /0) - Load 32-bit floating point from memory
// ============================================================================

#[test]
fn test_fld_m32fp_positive_one() {
    let mut emu = emu64(); // FLD dword ptr [0x2000]
                           // FSTP qword ptr [0x3000]  ; Store to verify
                           // HLT
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, 1.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 1.0);
}

#[test]
fn test_fld_m32fp_zero() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, 0.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 0.0);
}

#[test]
fn test_fld_m32fp_negative_zero() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, -0.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_sign_negative() && result == 0.0);
}

#[test]
fn test_fld_m32fp_negative_one() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, -1.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, -1.0);
}

#[test]
fn test_fld_m32fp_large_positive() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, 1234567.875);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result - 1234567.875).abs() < 1.0);
}

#[test]
fn test_fld_m32fp_large_negative() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, -9876543.5);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result + 9876543.5).abs() < 1.0);
}

#[test]
fn test_fld_m32fp_small_positive() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, 0.0000152587890625); // 2^-16

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result - 0.0000152587890625).abs() < 1e-10);
}

#[test]
fn test_fld_m32fp_small_negative() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, -0.0000152587890625);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result + 0.0000152587890625).abs() < 1e-10);
}

#[test]
fn test_fld_m32fp_infinity_positive() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, f32::INFINITY);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_infinite() && result.is_sign_positive());
}

#[test]
fn test_fld_m32fp_infinity_negative() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, f32::NEG_INFINITY);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_infinite() && result.is_sign_negative());
}

#[test]
fn test_fld_m32fp_nan() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, f32::NAN);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_nan());
}

#[test]
fn test_fld_m32fp_pi() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, std::f32::consts::PI);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result - std::f64::consts::PI).abs() < 1e-6);
}

// ============================================================================
// FLD m64fp (opcode DD /0) - Load 64-bit floating point from memory
// ============================================================================

#[test]
fn test_fld_m64fp_positive_one() {
    let mut emu = emu64(); // FLD qword ptr [0x2000]
                           // FSTP qword ptr [0x3000]
                           // HLT
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 1.0);
}

#[test]
fn test_fld_m64fp_zero() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 0.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 0.0);
}

#[test]
fn test_fld_m64fp_negative_zero() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -0.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_sign_negative() && result == 0.0);
}

#[test]
fn test_fld_m64fp_negative_one() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -1.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, -1.0);
}

#[test]
fn test_fld_m64fp_large_positive() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.234567890123456e15);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 1.234567890123456e15);
}

#[test]
fn test_fld_m64fp_large_negative() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -9.876543210987654e15);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, -9.876543210987654e15);
}

#[test]
fn test_fld_m64fp_small_positive() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.234567890123456e-15);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 1.234567890123456e-15);
}

#[test]
fn test_fld_m64fp_small_negative() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -9.876543210987654e-15);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, -9.876543210987654e-15);
}

#[test]
fn test_fld_m64fp_infinity_positive() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, f64::INFINITY);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_infinite() && result.is_sign_positive());
}

#[test]
fn test_fld_m64fp_infinity_negative() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, f64::NEG_INFINITY);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_infinite() && result.is_sign_negative());
}

#[test]
fn test_fld_m64fp_nan() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, f64::NAN);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result.is_nan());
}

#[test]
fn test_fld_m64fp_pi() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, std::f64::consts::PI);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, std::f64::consts::PI);
}

#[test]
fn test_fld_m64fp_e() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, std::f64::consts::E);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, std::f64::consts::E);
}

// ============================================================================
// FLD ST(i) (opcode D9 C0+i) - Push FPU register onto stack
// ============================================================================

#[test]
fn test_fld_st0_duplicate_top() {
    let mut emu = emu64(); // FLD qword ptr [0x2000]  ; Load 1.0 into ST(0)
                           // FLD ST(0)               ; Duplicate ST(0)
                           // FSTP qword ptr [0x3000] ; Store top (should be 1.0)
                           // FSTP qword ptr [0x4000] ; Store next (should also be 1.0)
                           // HLT
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xD9, 0xC0, // FLD ST(0)
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xDD, 0x1C, 0x25, 0x00, 0x40, 0x00, 0x00, // FSTP qword ptr [0x4000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);

    emu.run(None).unwrap();
    let result1 = emu.fpu_mut().get_st(0);
    let result2 = emu.fpu_mut().get_st(0);
    assert_eq!(result1, 1.0);
    assert_eq!(result2, 1.0);
}

#[test]
fn test_fld_st1() {
    let mut emu = emu64(); // FLD qword ptr [0x2000]  ; Load 1.0 into ST(0)
                           // FLD qword ptr [0x2008]  ; Load 2.0 into ST(0), 1.0 is now ST(1)
                           // FLD ST(1)               ; Push ST(1) (1.0) onto stack
                           // FSTP qword ptr [0x3000] ; Store top (should be 1.0)
                           // HLT
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword ptr [0x2008]
        0xD9, 0xC1, // FLD ST(1)
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);
    emu.maps.write_f64(DATA_ADDR + 8, 2.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 1.0);
}

#[test]
fn test_fld_st2() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000] ; 1.0
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword ptr [0x2008] ; 2.0
        0xDD, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00, // FLD qword ptr [0x2010] ; 3.0
        0xD9, 0xC2, // FLD ST(2) ; Push 1.0
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);
    emu.maps.write_f64(DATA_ADDR + 8, 2.0);
    emu.maps.write_f64(DATA_ADDR + 16, 3.0);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, 1.0);
}

// ============================================================================
// Stack behavior tests
// ============================================================================

#[test]
fn test_fld_stack_push_behavior() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000] ; 1.0
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword ptr [0x2008] ; 2.0
        0xDD, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00, // FLD qword ptr [0x2010] ; 3.0
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000] ; ST(0) = 3.0
        0xDD, 0x1C, 0x25, 0x08, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3008] ; ST(0) = 2.0
        0xDD, 0x1C, 0x25, 0x10, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3010] ; ST(0) = 1.0
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);
    emu.maps.write_f64(DATA_ADDR + 8, 2.0);
    emu.maps.write_f64(DATA_ADDR + 16, 3.0);

    emu.run(None).unwrap();
    assert_eq!(emu.fpu_mut().get_st(0), 3.0);
    assert_eq!(emu.fpu_mut().get_st(0), 2.0);
    assert_eq!(emu.fpu_mut().get_st(0), 1.0);
}

#[test]
fn test_fld_multiple_formats() {
    let mut emu = emu64(); // FLD dword ptr [0x2000]  ; Load f32
                           // FLD qword ptr [0x2008]  ; Load f64
                           // FSTP qword ptr [0x3000] ; Store f64 value
                           // FSTP qword ptr [0x3008] ; Store f32 value (converted to f64)
                           // HLT
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD qword ptr [0x2008]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xDD, 0x1C, 0x25, 0x08, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3008]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, 1.5);
    emu.maps.write_f64(DATA_ADDR + 8, 2.5);

    emu.run(None).unwrap();
    assert_eq!(emu.fpu_mut().get_st(0), 2.5);
    assert_eq!(emu.fpu_mut().get_st(0), 1.5);
}

// ============================================================================
// Special value tests
// ============================================================================

#[test]
fn test_fld_max_f32() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, f32::MAX);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result - f32::MAX as f64).abs() < 1e30);
}

#[test]
fn test_fld_min_positive_f32() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f32(DATA_ADDR, f32::MIN_POSITIVE);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!((result - f32::MIN_POSITIVE as f64).abs() < 1e-40);
}

#[test]
fn test_fld_max_f64() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, f64::MAX);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, f64::MAX);
}

#[test]
fn test_fld_min_positive_f64() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, f64::MIN_POSITIVE);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert_eq!(result, f64::MIN_POSITIVE);
}

#[test]
fn test_fld_various_fractions() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];

    let test_values = vec![0.5, 0.25, 0.125, 0.75, 0.375, 0.625];
    for (i, &val) in test_values.iter().enumerate() {
        emu.load_code_bytes(&code);
        emu.maps.write_f64(DATA_ADDR, val);

        emu.run(None).unwrap();
        let result = emu.fpu_mut().get_st(0);
        assert_eq!(result, val, "Test value {} failed", i);
    }
}

#[test]
fn test_fld_denormal_f32() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD dword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    let denormal = f32::MIN_POSITIVE / 2.0_f32.powi(23);
    emu.maps.write_f32(DATA_ADDR, denormal);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result > 0.0 && result < f32::MIN_POSITIVE as f64);
}

#[test]
fn test_fld_denormal_f64() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD qword ptr [0x2000]
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP qword ptr [0x3000]
        0xf4,
    ];
    emu.load_code_bytes(&code);
    let denormal = f64::MIN_POSITIVE / 2.0_f64.powi(52);
    emu.maps.write_f64(DATA_ADDR, denormal);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result > 0.0 && result < f64::MIN_POSITIVE);
}

#[test]
fn test_fld_m32fp_subnormal() {
    let mut emu = emu64();
    let code = [
        0xD9, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, 0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, 0xf4,
    ];
    emu.load_code_bytes(&code);
    let subnormal = f32::from_bits(0x00000001); // Smallest positive subnormal
    emu.maps.write_f32(DATA_ADDR, subnormal);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result > 0.0 && result < f32::MIN_POSITIVE as f64);
}

#[test]
fn test_fld_m64fp_subnormal() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, 0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, 0xf4,
    ];
    emu.load_code_bytes(&code);
    let subnormal = f64::from_bits(0x0000000000000001); // Smallest positive subnormal
    emu.maps.write_f64(DATA_ADDR, subnormal);

    emu.run(None).unwrap();
    let result = emu.fpu_mut().get_st(0);
    assert!(result > 0.0 && result < f64::MIN_POSITIVE);
}

#[test]
fn test_fld_st_all_registers() {
    let mut emu = emu64();
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD 1.0
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD 2.0
        0xDD, 0x04, 0x25, 0x10, 0x20, 0x00, 0x00, // FLD 3.0
        0xDD, 0x04, 0x25, 0x18, 0x20, 0x00, 0x00, // FLD 4.0
        0xD9, 0xC3, // FLD ST(3) ; Push 1.0
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, 0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);
    emu.maps.write_f64(DATA_ADDR + 8, 2.0);
    emu.maps.write_f64(DATA_ADDR + 16, 3.0);
    emu.maps.write_f64(DATA_ADDR + 24, 4.0);

    emu.run(None).unwrap();
    assert_eq!(emu.fpu_mut().get_st(0), 1.0);
}