-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisasm.spf
1758 lines (1415 loc) · 45.4 KB
/
disasm.spf
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\ Ôàéë: disasm.spf
\ Àâòîð: VoidVolker
\ Äàòà: 21/07/2012 08:00
\ Âåðñèÿ: 1.0
\ Îïèñàíèå: Äèçàñåìáëåð äëÿ ííêðîíà. Âçÿò èç äèñòðèáóòèâà ñïô. Áîëüøèíñòâî ñëîâ äèçàñåìáëèðóåò áåç ïðîáëåì.
\ Ñëîâà:
\ SEE some_word
\ ' some_word REST
\ DIS ( ADR -- ) Ïåðåéòè â ðåæèì ïîøàãîâîãî ïðîñìîòðà êîäà. Âûõîä ïî q
\ Îñòàëüíûå ñëîâà èñïîëüçóþòñÿ äîâîëüíî ðåäêî.
\ ---------------------------------------------------------------------------------------------------------------------------
\ 80386 DISASSEMBLER
\ ANDREW MCKEWAN, APRIL 1994
\ TOM ZIMMER, 05/18/94 PORT TO WIN32F
\ MODIFIED TO WORD IN DECIMAL 08/03/94 10:04 TJZ
\ 06-??-95 SMUB NEXT SEQUENCE DEFINED IN FKERNEL
\ 06-21-95 SMUB REMOVED REDUNDANT COUNT CALLS FROM TXB, LXS.
\ 04-??-97 EXTENDED BY C.L. TO INCLUDE P6 AND MMX INSTRUCTIONS
\ 14-11-2000 Adapted from SPFOPT (Michael Maximov) by Dmitry Yakimov
\ ??-11-2000 Fixed FE. FF. (Bandaletov) and H.R (Yakimov)
\ 15-11-2000 Fixed MV2 (Yakimov)
\ 25-12-2000 Added float literals recognition (Yakimov)
\ 26-07-2001 Fixed MVX (Maksimov)
\ 11-05-2004 Fixed FDA and CMV (Serguei Jidkov)
\ CR .( Loading Intel Pentium MMX disassembler...)
WARNING 0!
: UMIN 2DUP U< IF DROP ELSE NIP THEN ;
: UMAX 2DUP U< IF NIP ELSE DROP THEN ;
\ ==============================================
\ îòëàäêà - ïîèñê ñëîâà ïî àäðåñó â åãî òåëå
: WL_NEAR_NFA ( addr wid - addr nfa | addr 0 )
CELL+ @
BEGIN 2DUP U<
WHILE CDR
REPEAT
;
: NEAR_NFA ( addr - nfa addr | 0 addr )
0 SWAP
VOC-LIST
BEGIN @ DUP
WHILE DUP >R WL_NEAR_NFA SWAP >R UMAX R> R>
REPEAT DROP
;
\ Ðàñïå÷àòàòü ñïèñîê ñëîâàðåé.
: VOCS
VOC-LIST
BEGIN @ DUP WHILE
DUP CELL+ VOC-NAME.
DUP 3 CELLS + @ \ wid ïðåäêà
?DUP IF ." defined in " VOC-NAME.
ELSE ." is the main vocabulary"
THEN CR
REPEAT
DROP
;
0x200 VALUE MAX-WORD-SIZE
\ Opposite to CDR, might be slow!
\ It does not take wordlists into account.
: NextNFA ( nfa1 -- nfa2 | 0 )
NEAR_NFA SWAP >R
BEGIN
1+ NEAR_NFA ( nfa addr )
OVER 0 >
ROT R@ <> AND
OVER R@ - MAX-WORD-SIZE > OR
UNTIL
DUP R> - MAX-WORD-SIZE >
IF DROP 0
ELSE NEAR_NFA DROP
THEN
;
: NFAInVoc? ( nfa voc -- f )
@ \ last nfa
BEGIN ( nfa 'nfa )
DUP
WHILE
2DUP = IF 2DROP TRUE EXIT THEN
CDR
REPEAT 2DROP 0
;
: VocByNFA ( nfa -- wid | 0 )
VOC-LIST
BEGIN @ DUP WHILE ( nfa voc )
2DUP CELL+ NFAInVoc?
IF
NIP CELL+ EXIT
THEN
REPEAT
2DROP 0
;
\ 94 TOOLS
: .SN ( n --) \ Ðàñïå÷àòàòü n âåðõíèõ ýëåìåíòîâ ñòåêà
>R BEGIN
R@
WHILE
SP@ R@ 1- CELLS + @ DUP 0<
IF DUP U>D <# #S #> TYPE
." (" ABS 0 <# #S [CHAR] - HOLD #> TYPE ." ) " ELSE . THEN
R> 1- >R
REPEAT RDROP
;
: .S ( -- ) \ 94 TOOLS
\ Ñêîïèðîâàòü è ïîêàçàòü çíà÷åíèÿ, íàõîäÿùèåñÿ íà ñòåêå äàííûõ. Ôîðìàò çàâèñèò
\ îò ðåàëèçàöèè.
\ .S ìîæåò áûòü ðåàëèçîâàíî ñ èñïîëüçîâàíèåì ñëîâ ôîðìàòíîãî ïðåîáðàçîâàíèÿ
\ ÷èñåë. Ñîîòâåòñòâåííî, îí ìîæåò èñïîðòèòü ïåðåìåùàåìóþ îáëàñòü,
\ èäåíòèôèöèðóåìóþ #>.
DEPTH .SN
;
: ? ( a-addr -- ) \ 94 TOOLS
\ Ïîêàçàòü çíà÷åíèå, õðàíÿùååñÿ ïî àäðåñó a-addr.
\ ? ìîæåò áûòü ðåàëèçîâàí ñ èñïîëüçîâàíèåì ñëîâ ôîðìàòíîãî ïðåîáðàçîâàíèÿ
\ ÷èñåë. Ñîîòâåòñòâåííî, îí ìîæåò èñïîðòèòü ïåðåìåùàåìóþ îáëàñòü,
\ èäåíòèôèöèðóåìóþ #>.
@ .
;
: AHEAD \ 94 TOOLS EXT
\ Èíòåðïðåòàöèÿ: ñåìàíòèêà íåîïðåäåëåíà.
\ Êîìïèëÿöèÿ: ( C: -- orig )
\ Ïîëîæèòü ìåñòî íåðàçðåøåííîé ññûëêè âïåðåä orig íà ñòåê óïðàâëåíèÿ.
\ Äîáàâèòü ñåìàíòèêó âðåìåíè âûïîëíåíèÿ, äàííóþ íèæå, ê òåêóùåìó îïðåäåëåíèþ.
\ Ñåìàíòèêà íåçàâåðøåíà äî òåõ ïîð, ïîêà orig íå ðàçðåøèòñÿ (íàïðèìåð,
\ ïî THEN).
\ Âðåìÿ âûïîëíåíèÿ: ( -- )
\ Ïðîäîëæèòü âûïîëíåíèå ñ ïîçèöèè, çàäàííîé ðàçðåøåíèåì orig.
HERE BRANCH, >MARK 2
; IMMEDIATE
: [ELSE] \ 94 TOOLS EXT
\ Êîìïèëÿöèÿ: Âûïîëíèòü ñåìàíòèêó âûïîëíåíèÿ, äàííóþ íèæå.
\ Âûïîëíåíèå: ( "<spaces>name..." -- )
\ Ïðîïóñòèòü âåäóùèå ïðîáåëû, âûäåëèòü è îòáðîñèòü îãðàíè÷åííûå ïðîáåëàìè
\ ñëîâà èç ðàçáèðàåìîé îáëàñòè, âêëþ÷àÿ âëîæåííûå [IF]...[THEN] è
\ [IF]...[ELSE]...[THEN], äî âûäåëåíèÿ è îòáðàñûâàíèÿ ñëîâà [THEN].
\ Åñëè ðàçáèðàåìàÿ îáëàñòü îïóñòîøàåòñÿ, îíà ñíîâà çàïîëíÿåòñÿ ïî REFILL.
\ [ELSE] - ñëîâî íåìåäëåííîãî èñïîëíåíèÿ.
1
BEGIN
NextWord DUP
IF
2DUP S" [IF]" COMPARE 0= IF 2DROP 1+ ELSE
2DUP S" [ELSE]" COMPARE 0= IF 2DROP 1- DUP IF 1+ THEN ELSE
S" [THEN]" COMPARE 0= IF 1- THEN
THEN THEN
ELSE 2DROP REFILL AND \ SOURCE TYPE
THEN DUP 0=
UNTIL DROP ; IMMEDIATE
: [IF] \ 94 TOOLS EXT
\ Êîìïèëÿöèÿ: Âûïîëíèòü ñåìàíòèêó âûïîëíåíèÿ, äàííóþ íèæå.
\ Âûïîëíåíèå: ( flag | flag "<spaces>name..." -- )
\ Åñëè ôëàã "èñòèíà", íè÷åãî íå äåëàòü. Èíà÷å, ïðîïóñòèâ âåäóùèå ïðîáåëû,
\ âûäåëÿòü è îòáðàñûâàòü îãðàíè÷åííûå ïðîáåëàìè ñëîâà èç ðàçáèðàåìîé îáëàñòè,
\ âêëþ÷àÿ âëîæåííûå [IF]...[THEN] è [IF]...[ELSE]...[THEN], äî òåõ ïîð, ïîêà íå
\ áóäåò âûäåëåíî è îòáðîøåíî ñëîâî [ELSE] èëè [THEN].
\ Åñëè ðàçáèðàåìàÿ îáëàñòü îïóñòîøàåòñÿ, îíà ñíîâà çàïîëíÿåòñÿ ïî REFILL.
\ [ELSE] - ñëîâî íåìåäëåííîãî èñïîëíåíèÿ.
0= IF POSTPONE [ELSE] THEN
; IMMEDIATE
: [THEN] \ 94 TOOLS EXT
\ Êîìïèëÿöèÿ: Âûïîëíèòü ñåìàíòèêó âûïîëíåíèÿ, äàííóþ íèæå.
\ Âûïîëíåíèå: ( -- )
\ Íè÷åãî íå äåëàòü. [THEN] - ñëîâî íåìåäëåííîãî èñïîëíåíèÿ.
; IMMEDIATE
\ Ruvim Pinka additions:
: [DEFINED] ( -- f ) \ "name"
NextWord SFIND IF DROP TRUE ELSE 2DROP FALSE THEN
; IMMEDIATE
: [UNDEFINED] ( -- f ) \ "name"
POSTPONE [DEFINED] 0=
; IMMEDIATE
: DEFER VECT ;
: DUP>R R> OVER >R >R ;
80 CONSTANT MAXSTRING
255 CONSTANT MAXCOUNTED \ maximum length of contents of a counted string
: 0X BASE @ HEX >R BL WORD ?LITERAL
R> BASE ! ; IMMEDIATE
: "CLIP" ( a1 n1 -- a1 n1' ) \ clip a string to between 0 and MAXCOUNTED
MAXCOUNTED MIN 0 MAX ;
: PLACE ( addr len dest -- )
SWAP "CLIP" SWAP
2DUP 2>R
CHAR+ SWAP MOVE
2R> C! ;
: +PLACE ( addr len dest -- ) \ append string addr,len to counted
\ string dest
>R "CLIP" MAXCOUNTED R@ C@ - MIN R>
\ clip total to MAXCOUNTED string
2DUP 2>R
COUNT CHARS + SWAP MOVE
2R> +! ;
: C+PLACE ( c1 a1 -- ) \ append char c1 to the counted string at a1
DUP 1+! COUNT + 1- C! ;
: OFF 0! ;
: BLANK ( addr len -- ) \ fill addr for len with spaces (blanks)
BL FILL ;
128 CONSTANT SPCS-MAX ( optimization for SPACES )
CREATE SPCS SPCS-MAX ALLOT
SPCS SPCS-MAX BLANK
C" UPC" FIND NIP 0=
[IF]
: UPC ( c -- c' )
DUP [CHAR] Z U>
IF 0xDF AND
THEN ;
[THEN]
: (D.) ( d -- addr len ) TUCK DABS <# #S ROT SIGN #> ;
80 VALUE COLS
: H.R ( n1 n2 -- ) \ display n1 as a hex number right
\ justified in a field of n2 characters
BASE @ >R HEX >R
0 <# #S #> R> OVER - 0 MAX SPACES TYPE
R> BASE ! ;
: H.N ( n1 n2 -- ) \ display n1 as a HEX number of n2 digits
BASE @ >R HEX >R
0 <# R> 0 ?DO # LOOP #> TYPE
R> BASE ! ;
ONLY FORTH ALSO DEFINITIONS
0 VALUE DEFAULT-16BIT?
: DEFAULT-16BIT ( -- )
TRUE TO DEFAULT-16BIT? ;
: DEFAULT-32BIT ( -- )
FALSE TO DEFAULT-16BIT? ;
DEFER SHOW-NAME ( CFA -- ) \ DISPLAY NEAREST SYMBOL
0 VALUE BASE-ADDR
VOCABULARY DISASSEMBLER
DISASSEMBLER ALSO DEFINITIONS
DECIMAL
CREATE S-BUF MAXSTRING ALLOT
: >S ( A1 N1 -- )
S-BUF +PLACE ;
: 0>S ( -- ) \ RESET S-BUF
S-BUF OFF ;
: SSPACES ( N1 -- )
SPCS SWAP S-BUF +PLACE ;
: SSPACE ( -- )
1 SSPACES ;
: EMIT>S ( C1 -- )
S-BUF C+PLACE ;
: S> ( -- A1 N1 )
S-BUF COUNT ;
: (.S") ( addr len -- )
S-BUF +PLACE ;
: .S" ( 'TEXT' -- )
[CHAR] " PARSE
POSTPONE SLITERAL
POSTPONE (.S") ; IMMEDIATE
: D.R>S ( D W -- )
>R (D.) R> OVER - SSPACES >S ;
: .R>S ( N W -- )
>R S>D R> D.R>S ;
: U.R>S ( U W -- )
0 SWAP D.R>S ;
: H.>S ( U -- )
BASE @ SWAP HEX 0 (D.) >S SSPACE BASE ! ;
: H.R>S ( N1 N2 -- )
BASE @ >R HEX >R
0 <# #S #> R> OVER - SSPACES >S
R> BASE ! ;
C" NEAR_NFA" FIND NIP 0=
[IF] : NEAR_NFA ( addr -- NFA addr | 0 addr ) DUP WordByAddr DROP 1- SWAP
2DUP 1000 - U< IF NIP 0 SWAP THEN ;
[THEN]
: ?.NAME>S ( CFA -- )
\ ELIMINATE " 0X"
DUP 1 H.R>S SSPACE
NEAR_NFA
>R DUP
IF .S" ( " DUP COUNT >S
NAME> R> - DUP
IF DUP .S" +" NEGATE H.>S
THEN DROP .S" ) "
ELSE RDROP DROP
THEN
;
' ?.NAME>S TO SHOW-NAME
\ 32 CONSTANT COMMENT-COL
0 VALUE SIZE
0 VALUE 16-BIT-DATA
0 VALUE 16-BIT-ADDR
0 VALUE PREFIX-OP
0 VALUE MMX-REG
: @+ ( ADDR -- ADDR N ) DUP CELL+ SWAP @ ;
: W@+ ( ADDR -- ADDR N ) DUP 2 + SWAP W@ ;
: SEXT ( BYTE -- N ) DUP 128 AND IF 0xFFFFFF00 OR THEN ;
: MOD/SIB ( MOD-R-R/M -- R/M R MOD ) \ R INCLUDING GENERAL, SPECIAL, SEGMENT, MMX
( MOD-OP-R/M -- R/M OP MOD )
( S-I-B -- B I S )
255 AND 8 /MOD 8 /MOD ;
: ??? ( N1 -- )
.S" ??? " DROP ;
: SS. ( N ADR LEN W ) >R DROP SWAP R@ * + R> >S SSPACE ;
: TTTN ( CODE -- ) 15 AND S" O NOB AEE NEBEA S NSP NPL GELEG " 2 SS. ;
: SREG ( SREG -- ) 3 RSHIFT 7 AND S" ESCSSSDSFSGSXXXX" 2 SS. ;
: CREG ( EEE -- ) 3 RSHIFT 7 AND S" CR0???CR2CR3CR4?????????" 3 SS. ;
: DREG ( EEE -- ) 3 RSHIFT 7 AND S" DR0DR1DR2DR3??????DR6DR7" 3 SS. ;
: TREG ( EEE -- ) 3 RSHIFT 7 AND S" ?????????TR3TR4TR5TR6TR7" 3 SS. ; \ OBSOLETE
: MREG ( N -- ) 7 AND S" MM0MM1MM2MM3MM4MM5MM6MM7" 3 SS. ;
: REG8 ( N -- ) 7 AND S" ALCLDLBLAHCHDHBH" 2 SS. ;
: REG16 ( N -- ) 7 AND S" AXCXDXBXSPBPSIDI" 2 SS. ;
: REG32 ( N -- ) 7 AND S" EAXECXEDXEBXESPEBPESIEDI" 3 SS. ;
: REG16/32 ( N -- )
16-BIT-DATA
IF REG16
ELSE REG32
THEN ;
: REG ( A N -- A )
MMX-REG
IF MREG
ELSE SIZE
IF REG16/32
ELSE REG8
THEN
THEN
;
: [BASE16] ( R/M -- ) 4 - S" [SI][DI][BP][BX]" 4 SS. ;
\ R/M = 4 , 5 , 6 , 7
: [IND16] ( R/M -- ) S" [BX+SI][BX+DI][BP+SI][BP+DI]" 7 SS. ;
\ R/M = 0 , 1 , 2 , 3
: [REG16] ( R/M -- ) DUP 4 <
IF [IND16]
ELSE [BASE16]
THEN ;
: [REG32] ( N -- ) 7 AND S" [EAX][ECX][EDX][EBX][ESP][EBP][ESI][EDI]" 5 SS. ;
: [REG*2] ( I -- ) S" [EAX*2][ECX*2][EDX*2][EBX*2][XXX*2][EBP*2][ESI*2][EDI*2]" 7 SS. ;
: [REG*4] ( I -- ) S" [EAX*4][ECX*4][EDX*4][EBX*4][XXX*4][EBP*4][ESI*4][EDI*4]" 7 SS. ;
: [REG*8] ( I -- ) S" [EAX*8][ECX*8][EDX*8][EBX*8][XXX*8][EBP*8][ESI*8][EDI*8]" 7 SS. ;
: [INDEX] ( SIB -- ) MOD/SIB OVER 4 =
IF 2DROP \ NO ESP SCALED INDEX
ELSE CASE ( S )
0 OF [REG32] ENDOF
1 OF [REG*2] ENDOF
2 OF [REG*4] ENDOF
3 OF [REG*8] ENDOF
ENDCASE
THEN DROP ;
: DISP8 ( ADR -- ADR' ) COUNT H.>S ;
: DISP16 ( ADR -- ADR' ) W@+ SHOW-NAME ;
: DISP32 ( ADR -- ADR' ) @+ ( BODY> ) SHOW-NAME ;
: DISP16/32 ( ADR -- ADR' )
16-BIT-ADDR
IF DISP16
ELSE DISP32
THEN ;
: ., ( -- ) .S" , " ;
: .# ., .S" # " ;
: IMM8 ( ADR -- ADR' ) .# COUNT H.>S ;
\ : IMM16 ( ADR -- ADR' ) .# W@+ H.>S ;
: IMM16/32 ( ADR -- ADR' )
.# 16-BIT-DATA
IF W@+
ELSE @+
THEN H.>S ;
: SIB ( ADR MOD -- ADR )
>R COUNT TUCK 7 AND 5 = R@ 0= AND
IF DISP32 SWAP [INDEX] R> DROP \ EBP BASE AND MOD = 00
ELSE R> CASE ( MOD )
1 OF DISP8 ENDOF
2 OF DISP32 ENDOF
ENDCASE
SWAP DUP [REG32] [INDEX]
THEN ;
: MOD-R/M32 ( ADR R/M MOD -- ADR' )
DUP 3 =
IF DROP REG \ MOD = 3, REGISTER CASE
ELSE OVER 4 =
IF NIP SIB \ R/M = 4, SIB CASE
ELSE 2DUP 0= SWAP 5 = AND \ MOD = 0, R/M = 5,
IF 2DROP DISP32 \ DISP32 CASE
ELSE ROT SWAP
CASE ( MOD )
1 OF DISP8 ENDOF
2 OF DISP32 ENDOF
ENDCASE
SWAP [REG32]
THEN
THEN
THEN ;
: MOD-R/M16 ( ADR R/M MOD -- ADR' )
2DUP 0= SWAP 6 = AND
IF 2DROP DISP16 \ DISP16 CASE
ELSE CASE ( MOD )
0 OF [REG16] ENDOF
1 OF SWAP DISP8 SWAP [REG16] ENDOF
2 OF SWAP DISP16 SWAP [REG16] ENDOF
3 OF REG ENDOF
ENDCASE
THEN ;
: MOD-R/M ( ADR MODR/M -- ADR' )
MOD/SIB NIP 16-BIT-ADDR
IF MOD-R/M16
ELSE MOD-R/M32
THEN ;
: R/M8 0 TO SIZE MOD-R/M ;
: R/M16/32 1 TO SIZE MOD-R/M ;
: R/M16 TRUE TO 16-BIT-DATA R/M16/32 ;
: R,R/M ( ADR -- ADR' )
COUNT DUP 3 RSHIFT REG ., MOD-R/M ;
: R/M,R ( ADR -- ADR' )
COUNT DUP>R MOD-R/M ., R> 3 RSHIFT REG ;
: R/M ( ADR OP -- ADR' )
2 AND
IF R,R/M
ELSE R/M,R
THEN ;
\ -------------------- SIMPLE OPCODES --------------------
: INH ( -<NAME>- )
CREATE
BL WORD COUNT HERE PLACE
HERE C@ 1+ ALLOT
DOES> COUNT >S SSPACE DROP ;
INH CLC CLC
INH STC STC
INH CLD CLD
INH STD STD
\ INH RPNZ REPNZ
\ INH REPZ REPZ
INH CBW CBW
INH CDQ CDQ
INH DAA DAA
INH DAS DAS
INH AAA AAA
INH AAS AAS
\ INH LOCK LOCK
INH INB INSB
INH OSB OUTSB
INH SAH SAHF
INH LAH LAHF
\ INH AAM AAM
\ INH AAD AAD
INH HLT HLT
INH CMC CMC
INH XLT XLAT
INH CLI CLI
INH STI STI
INH CLT CLTS
INH INV INVD
INH WIV WBINVD
INH UD2 UD2
INH WMR WRMSR
INH RTC RDTSC
INH RMR RDMSR
INH RPC RDPMC
INH EMS EMMS
INH RSM RSM
INH CPU CPUID
INH UD1 UD1
\ INH LSS LSS
\ INH LFS LFS
\ INH LGS LGS
\ INH D16: D16:
\ INH A16: A16:
\ INH ES: ES:
\ INH CS: CS:
\ INH DS: DS:
\ INH FS: FS:
\ INH GS: GS:
: AAM ( ADR CODE -- ADR' )
.S" AAM" DROP COUNT DROP ;
: AAD ( ADR CODE -- ADR' )
.S" AAD" DROP COUNT DROP ;
: D16 ( ADR CODE -- ADR' )
DROP .S" D16:"
TRUE TO 16-BIT-DATA
TRUE TO PREFIX-OP
;
: A16 ( ADR CODE -- ADR' )
DROP .S" A16:"
TRUE TO 16-BIT-ADDR
TRUE TO PREFIX-OP
;
: RPZ ( ADR CODE -- ADR' )
DROP .S" REPNZ"
TRUE TO PREFIX-OP
;
: REP ( ADR CODE -- ADR' )
DROP .S" REPZ"
TRUE TO PREFIX-OP
;
: LOK ( ADR CODE -- ADR' ) \ THIS SHOULD HAVE ERROR CHECKING ADDED
DROP .S" LOCK"
TRUE TO PREFIX-OP
;
: CS: ( ADR CODE -- ADR' )
DROP .S" CS:"
TRUE TO PREFIX-OP
;
: DS: ( ADR CODE -- ADR' )
DROP .S" DS:"
TRUE TO PREFIX-OP
;
: SS: ( ADR CODE -- ADR' )
DROP .S" SS:"
TRUE TO PREFIX-OP
;
: ES: ( ADR CODE -- ADR' )
DROP .S" ES:"
TRUE TO PREFIX-OP
;
: GS: ( ADR CODE -- ADR' )
DROP .S" GS:"
TRUE TO PREFIX-OP
;
: FS: ( ADR CODE -- ADR' )
DROP .S" FS:"
TRUE TO PREFIX-OP
;
: ISD ( ADR CODE -- ADR' )
DROP 16-BIT-DATA
IF .S" INSW "
ELSE .S" INSD "
THEN ;
: OSD ( ADR CODE -- ADR' )
DROP 16-BIT-DATA
IF .S" OUTSW "
ELSE .S" OUTSD "
THEN ;
: INP ( ADDR CODE -- ADDR' )
.S" IN " 1 AND
IF 16-BIT-DATA
IF .S" AX , "
ELSE .S" EAX , "
THEN
ELSE .S" AL , "
THEN
COUNT H.>S ;
: OTP ( ADDR CODE -- ADDR' )
.S" OUT " 1 AND
IF COUNT H.>S 16-BIT-DATA
IF .S" , AX"
ELSE .S" , EAX"
THEN
ELSE COUNT H.>S .S" , AL"
THEN
;
: IND ( ADDR CODE -- ADDR' )
.S" IN " 1 AND
IF 16-BIT-DATA
IF .S" AX , DX"
ELSE .S" EAX , DX"
THEN
ELSE .S" AL , DX"
THEN
;
: OTD ( ADDR CODE -- ADDR' )
.S" OUT " 1 AND
IF 16-BIT-DATA
IF .S" DX , AX"
ELSE .S" DX , EAX"
THEN
ELSE .S" DX , AL"
THEN
;
\ -------------------- ALU OPCODES --------------------
: .ALU ( N -- )
7 AND S" ADDOR ADCSBBANDSUBXORCMP" 3 SS. 4 SSPACES
;
: ALU ( ADR OP -- ADR' )
DUP 3 RSHIFT .ALU R/M ;
: ALI ( ADR OP -- ADR' )
>R COUNT
DUP 3 RSHIFT .ALU
MOD-R/M
R> 3 AND ?DUP
IF 1 =
IF IMM16/32
ELSE .# COUNT SEXT 0 .R>S SSPACE
THEN
ELSE IMM8
THEN ;
: ALA ( ADR OP -- ADR' )
DUP 3 RSHIFT .ALU
1 AND IF 0 REG IMM16/32 ELSE 0 REG8 IMM8 THEN ;
\ -------------------- TEST/XCHG --------------------
: TXB ( ADDR OP -- ADDR' )
DUP 3 AND S" TESTTESTXCHGXCHG" 4 SS. 3 SSPACES
1 AND
IF 1 TO SIZE R,R/M \ SMUB REMOVED COUNT
ELSE 0 TO SIZE R,R/M \ SMUB REMOVED COUNT
THEN
;
: TST ( ADDR OP -- ADDR' )
.S" TEST " 1 AND
IF 16-BIT-DATA
IF .S" AX , "
ELSE .S" EAX , "
THEN
IMM16/32
ELSE .S" AL , " IMM8
THEN
;
\ -------------------- INC/DEC ----------------------
: INC ( ADDR OP -- ADDR' )
.S" INC " REG16/32 ;
: DEC ( ADDR OP -- ADDR' )
.S" DEC " REG16/32 ;
\ -------------------- PUSH/POP --------------------
: PSH ( ADDR OP -- ADDR' )
.S" PUSH " REG16/32 ;
: POP ( ADDR OP -- ADDR' )
.S" POP " REG16/32 ;
: PSS ( ADDR OP -- ADDR' )
.S" PUSH " SREG ;
: PPS ( ADDR OP -- ADDR' )
.S" POP " SREG ;
: PSA ( ADDR OP -- ADDR' )
DROP 16-BIT-DATA
IF .S" PUSHA "
ELSE .S" PUSHAD "
THEN ;
: PPA ( ADDR OP -- ADDR' )
DROP 16-BIT-DATA
IF .S" POPA "
ELSE .S" POPAD "
THEN ;
: PSI ( ADDR OP -- ADDR' )
.S" PUSH " 2 AND
IF IMM8
ELSE IMM16/32
THEN ;
: PSF ( ADDR OP -- ADDR' )
DROP 16-BIT-DATA
IF .S" PUSHF "
ELSE .S" PUSHFD "
THEN ;
: PPF ( ADDR OP -- ADDR' )
DROP 16-BIT-DATA
IF .S" POPF "
ELSE .S" POPFD "
THEN ;
: 8F. ( ADDR OP -- ADDR' )
DROP COUNT .S" POP " R/M16/32 ;
\ -------------------- MOVE --------------------
: MOV ( ADDR OP -- ADDR' )
.S" MOV " R/M ;
: MRI ( ADDR OP -- ADDR' ) ( MOV REGISTER, IMM )
.S" MOV " DUP 8 AND
IF REG16/32 IMM16/32
ELSE REG8 IMM8
THEN ;
: MVI ( ADR OP -- ADR' ) ( MOV MEM, IMM )
.S" MOV " DROP COUNT MOD-R/M
SIZE
IF IMM16/32
ELSE IMM8
THEN
;
: MRS ( ADDR OP -- ADDR' )
\ ? REMOVE REDUNDANT >R , R>
16-BIT-DATA
IF .S" MOV " DROP
1 TO SIZE
COUNT DUP MOD-R/M .,
SREG
ELSE ???
THEN ;
: MSR ( ADDR OP -- ADDR' )
16-BIT-DATA
IF .S" MOV " DROP
1 TO SIZE
COUNT DUP SREG .,
MOD-R/M
ELSE ???
THEN ;
: MRC ( ADDR OP -- ADDR' )
.S" MOV "
DROP COUNT DUP REG32 .S" , "
CREG ;
: MCR ( ADDR OP -- ADDR' )
.S" MOV "
DROP COUNT DUP CREG .S" , "
REG32 ;
: MRD ( ADDR OP -- ADDR' )
.S" MOV "
DROP COUNT DUP REG32 .S" , "
DREG ;
: MDR ( ADDR OP -- ADDR' )
.S" MOV "
DROP COUNT DUP DREG .S" , "
REG32 ;
: MRT ( ADDR OP -- ADDR' )
\ OBSOLETE
.S" MOV "
DROP COUNT DUP REG32 .S" , "
TREG ;
: MTR ( ADDR OP -- ADDR' )
\ OBSOLETE
.S" MOV "
DROP COUNT DUP TREG .S" , "
REG32 ;
: MV1 ( ADDR OP -- ADDR' )
.S" MOV " 1 AND
IF 16-BIT-DATA
IF .S" AX , "
ELSE .S" EAX , "
THEN
ELSE .S" AL , "
THEN
DISP16/32 ;
: MV2 ( ADDR OP -- ADDR' )
.S" MOV " SWAP DISP16/32 .,
SWAP 1 AND
IF 16-BIT-DATA
IF .S" AX"
ELSE .S" EAX"
THEN
ELSE .S" AL"
THEN
;
: LEA ( ADDR OP -- ADDR' )
.S" LEA " DROP 1 TO SIZE R,R/M ;
: LXS ( ADDR OP -- ADDR' )
1 AND
IF .S" LDS "
ELSE .S" LES "
THEN
R,R/M \ SMUB REMOVED COUNT
;
: BND ( ADDR OP -- ADDR' )
.S" BOUND " DROP 1 TO SIZE R,R/M ;
: ARP ( ADDR OP -- ADDR' )
.S" ARPL " DROP
1 TO SIZE
TRUE TO 16-BIT-DATA
R,R/M
;
: MLI ( ADDR OP -- ADDR' )
1 TO SIZE
.S" IMUL " 0x69 =
IF R,R/M IMM16/32
ELSE R,R/M IMM8
THEN ;
\ -------------------- JUMPS AND CALLS --------------------
0 VALUE MAX_REFERENCE
: >MAX_R DUP MAX_REFERENCE UMAX TO MAX_REFERENCE ;
: REL8 ( ADDR OP -- ADDR' )
COUNT SEXT OVER + BASE-ADDR - >MAX_R H.>S ;
: REL16/32 ( ADDR OP -- ADDR' )
16-BIT-ADDR
IF W@+
ELSE @+
THEN OVER + BASE-ADDR - >MAX_R SHOW-NAME ;
: JSR ( ADDR OP -- ADDR' )
.S" CALL " DROP REL16/32 ;
: JMP ( ADDR OP -- ADDR' )
.S" JMP " 2 AND IF REL8 ELSE REL16/32 THEN ;
: .JXX ( ADDR OP -- ADDR' )
.S" J" TTTN 4 SSPACES ;
: BRA ( ADDR OP -- ADDR' )
.JXX REL8 ;
: LUP ( ADDR OP -- ADDR' )
3 AND S" LOOPNZLOOPZ LOOP JECXZ " 6 SS. 1 SSPACES REL8 ;
: LBR ( ADDR OP -- ADDR' )
.JXX REL16/32 ;
: RTN ( ADDR OP -- ADDR' )
.S" RET NEAR " 1 AND 0=
IF W@+ H.>S
THEN ;
: RTF ( ADDR OP -- ADDR' )
.S" RET FAR " 1 AND 0=
IF W@+ H.>S
THEN ;
: ENT ( ADDR OP -- ADDR' )
.S" ENTER " W@+ . ., COUNT H.>S ;
: CIS ( ADDR OP -- ADDR' )
0x9A =
IF .S" CALL "
ELSE .S" JMP "
THEN
16-BIT-DATA
IF .S" PTR16:16 "
ELSE .S" PTR16:32 "
THEN
COUNT MOD-R/M ;
: NT3 ( ADDR OP -- ADDR' )
DROP .S" INT 3 "
;
: INT ( ADDR OP -- ADDR' )
DROP .S" INT "
COUNT H.>S ;
INH LEV LEAVE
INH IRT IRET
INH NTO INTO
\ -------------------- STRING OPS --------------------
: STR INH DOES> COUNT >S 1 AND IF .S" D" ELSE .S" B" THEN ;
STR MVS MOVS
STR CPS CMPS
STR STS STOS
STR LDS LODS
STR SCS SCAS
\ -------------------- EXCHANGE --------------------
: XGA ( ADDR OP -- ADDR' )
.S" XCHG EAX, " REG16/32 ;
\ : XCH ( ADDR OP -- ADDR' )
\ .S" XCHG " DROP R,R/M ;
\ -------------------- SHIFTS & ROTATES --------------------
: .SHIFT ( N -- )
7 AND S" ROLRORRCLRCRSHLSHRXXXSAR" 3 SS. 4 SSPACES ;
: SHF ( ADDR OP -- ADDR' )
>R COUNT
DUP 3 RSHIFT .SHIFT
MOD-R/M .,
R> 0xD2 AND
CASE