-
Notifications
You must be signed in to change notification settings - Fork 0
/
486ASM.FS
1944 lines (1804 loc) · 64.9 KB
/
486ASM.FS
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
( 486 and Pentium assembler for Windows 32bit FORTH, version 1.23 )
( copyright [c] 1994, 1995, by Jim Schneider )
( )
( This program is free software; you can redistribute it and/or modify )
( it under the terms of the GNU General Public License as published by )
( the Free Software Foundation; either version 2 of the License, or )
( <at your option> any later version. )
( )
( This program is distributed in the hope that it will be useful, )
( but WITHOUT ANY WARRANTY; without even the implied warranty of )
( MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the )
( GNU General Public License for more details. )
( )
( You should have received a copy of the GNU General Public License )
( along with this program; if not, write to the Free Software )
( Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. )
( declare the vocabularies needed )
vocabulary assembler
also assembler definitions
vocabulary asm-hidden
also asm-hidden definitions
also assembler ( the also assembler is strictly to turn off stack warnings )
( words to manipulate the vocabulary search order )
: in-asm ( all later words are defined in the assembler vocabulary )
forth-wordlist tcomp-wid 2 set-order
also asm-hidden also assembler definitions ;
: in-hidden ( all later words are defined in the hidden vocabulary )
forth-wordlist tcomp-wid 2 set-order
also asm-hidden definitions also assembler ;
: in-forth ( all later words are defined in the forth vocabulary )
forth-wordlist tcomp-wid 2 set-order
forth-wordlist set-current also asm-hidden also assembler ;
in-hidden
( miscellaneous housekeeping )
base @ decimal ( save the base because I hate gratuitous base bashing )
\ : cell- [ 1 cells ] literal - ;
\ : cell/ [ 1 cells ] literal / ;
: 4+ 4 + ;
: 8* 8 * ;
: 8/ 8 / ;
: 8+ 8 + ;
: 8- 8 - ;
: 8*+ 8* + ;
: 16*+ 16 * + ;
: 16/mod 16 /mod ;
hex
: c0-8* 0c0 - 8* ;
: c0+ 0c0 + ;
: c0- 0c0 - ;
( defer some words for ease in porting to a cross assembler )
defer code-c, ' tc, is code-c, ( x -- )
defer code-w, ' tw, is code-w, ( x -- )
defer code-d, ' t, is code-d, ( x -- )
defer code-c! ' tc! is code-c! ( x \ a -- )
defer code-w! ' tw! is code-w! ( x \ a -- )
defer code-d! ' t! is code-d! ( x \ a -- )
defer code-c@ ' tc@ is code-c@ ( a -- x )
defer code-w@ ' tw@ is code-w@ ( a -- x )
defer code-d@ ' t@ is code-d@ ( a -- x )
defer code-here ' there is code-here ( -- a )
defer code-align ' talign is code-align ( -- )
defer code-header ' theader, is code-header ( -- )
defer data-, ' , is data-, ( x -- )
defer data-@ ' @ is data-@ ( a -- x )
defer data-! ' ! is data-! ( x \ a -- )
defer data-+! ' +! is data-+! ( x \ a -- )
defer data-here ' here is data-here ( -- a )
( register out of scope forward references, for use by a cross-compiler )
defer register-ref ' drop is register-ref ( address \ type -- address )
( constants for the type argument )
1 constant 8b-abs ( 8 bit absolute addressing )
2 constant 16b-abs ( 16 bit absolute addressing )
3 constant 32b-abs ( 32 bit absolute addressing )
5 constant 8b-rel ( 8 bit relative addressing )
6 constant 16b-rel ( 16 bit relative addressing )
7 constant 32b-rel ( 32 bit relative addressing )
( defer the error handler words so they can be individually turned off )
( defer them here so they can be used before they are actually defined )
: def-err-hand ( the default error handler for uninitialized error handlers )
( x*i -- x*j )
-1 abort" No error handler installed!" ;
' def-err-hand constant deh-xt
defer ?params deh-xt is ?params ( -- ) \ are there parameters?
defer ?seg deh-xt is ?seg ( -- ) \ is there a seg override?
defer ?lock deh-xt is ?lock ( -- ) \ is there a LOCK prefix?
defer ?rep deh-xt is ?rep ( -- ) \ is there a REP type prefix?
defer ?inst-pre deh-xt is ?inst-pre ( -- ) \ is there an inst prefix?
defer ?operands deh-xt is ?operands ( -- ) \ are there operands?
defer ?opsize deh-xt is ?opsize ( n -- ) \ is the operand size mismatched?
defer ?adsize deh-xt is ?adsize ( n -- ) \ is the address size mismatched?
defer ?short deh-xt is ?short ( -- ) \ is there an illegal SHORT?
defer ?toofar deh-xt is ?toofar ( flag -- ) \ is the dest of a branch to big?
defer ?unres deh-xt is ?unres ( -- ) \ is there an unresolved forward reference?
defer ?noadsize deh-xt is ?noadsize ( -- ) \ is the fwd ref addr size unknown?
defer ?toomanyops deh-xt is ?toomanyops ( n -- ) \ are there too many operands?
defer ?nofar deh-xt is ?nofar ( -- ) \ is there a far reference?
defer ?match deh-xt is ?match ( x1 \ x2 -- ) \ error if x1==x2
defer ?nomatch deh-xt is ?nomatch ( x1 \ x2 -- ) \ error if x1!=x2
defer ?finished deh-xt is ?finished ( -- ) \ are there operands left over?
defer ?badtype deh-xt is ?badtype ( max type val -- ) \ is the type unallowed?
defer ?badcombine deh-xt is ?badcombine ( flag -- ) \ can the types be combined?
defer ?notenough deh-xt is ?notenough ( n -- ) \ are there too few operands?
defer ?noimmed deh-xt is ?noimmed ( -- ) \ is there an illegal immediate op?
defer ?badmode deh-xt is ?badmode ( flag -- ) \ is the address mode illegal?
defer ?reg,r/m deh-xt is ?reg,r/m ( -- ) \ is the dest a reg?
defer ?r/m,reg deh-xt is ?r/m,reg ( -- ) \ is the source a reg?
defer ?mem deh-xt is ?mem ( -- ) \ do we have an illegal register operand?
defer ?reg deh-xt is ?reg ( -- ) \ do we have an illegal memory operand?
( defer the word that calls the words that create the code )
( it comes in two flavors -- prefix and postfix )
( it's deferred here so I can use it now )
: no-opcode-handler -1 abort" No opcode creator installed!" ;
defer do-opcode ' no-opcode-handler is do-opcode ( x? \ x? \ 0|addr -- )
\ postfix mode: this actually saves the current instruction and
\ does the previous one.
in-asm
: a; ( finish the assembly of the previous instruction )
( -- )
0 do-opcode ;
( address and data sizes )
in-hidden
0 constant unknown ( also, operand type and number )
1 constant 8bit
2 constant 16bit
3 constant 32bit
4 constant 64bit
5 constant 80bit
: gt-byte? ( n -- f ) \ greater than byte size?
80 + ff u> ;
: gt-word? ( n -- f ) \ greater than word size?
8000 + ffff u> ;
( determine what size code to generate )
32bit value default-size ( the default use size )
: !default-size ( not the default size, eg. change 16bit to 32bit )
( -- size )
default-size 16bit = if 32bit else 16bit then ;
in-asm
: use16 ( generate 16 bit code by default )
16bit to default-size ;
: use32 ( generate 32 bit code by default )
32bit to default-size ;
( create a stack for operands )
in-hidden
7 constant max-operands ( maximum number of operands on the opstack )
create opstack max-operands 1+ cells allot here constant opstack-end
: clr-opstack opstack dup cell+ swap data-! ;
clr-opstack ( initialize the opstack )
: ?clr-opstack ( clear the operand stack when the flag is non-zero )
( f -- )
if clr-opstack then ;
in-asm
: push-op ( move a parameter stack item to the opstack )
( x -- )
opstack data-@ opstack-end = dup ?clr-opstack
abort" Opstack overflow!" opstack dup data-@ dup cell+ rot data-!
data-! ;
: pop-op ( move an item from the operand stack to the parameter stack )
( -- x )
opstack dup data-@ swap cell+ = dup ?clr-opstack
abort" Opstack underflow!" opstack dup data-@ cell- dup rot
data-! data-@ ;
in-hidden
: op-depth ( check the depth of the operand stack )
opstack dup data-@ swap - cell- cell/ ;
( words to support forward referenced local labels )
100 constant frmax ( max number of unresolved forward references )
140 constant lbmax ( max number of local labels )
create frtable frmax 2* cells allot ( holds unresolved forward references )
create lbtable lbmax cells allot ( holds local label bindings )
: addref ( add a forward reference at code-here )
( ref# -- ref# )
frtable [ frmax 1+ ] literal 0 do
frmax i = dup ?clr-opstack
abort" Too many unresolved forward references!"
dup data-@ if
cell+ cell+ else 2dup data-! code-here over cell+
data-! leave
then
loop drop ;
: backpatch ( backpatch a forward reference to here )
( address \ size -- )
case 8bit of
code-here over 1+ - dup gt-byte? ?toofar swap code-c!
endof 16bit of
code-here over 2+ - dup gt-word? ?toofar swap code-w!
endof 32bit of
code-here over 4+ - swap code-d!
endof ?noadsize drop endcase ;
: refsize ( determine the size of a bound reference )
( addr of instr -- addr of operand \ size )
dup code-c@ 67 ( addr size override prefix ) = if
1+ !default-size
else
default-size
then
( stack: address of actual instruction \ provisional size )
>r dup code-c@ case
0f of ( a near conditional branch )
1+ ( adjust for the first byte of the opcode )
endof 0e9 of ( a jmp near, don't need to do anything )
endof 0e8 of ( a near call, don't need to do anything )
endof ( if we get to here, it must be 8 bit )
r> drop 8bit >r
endcase 1+ r> ;
: resolve ( resolve a forward reference to code-here )
( ref# -- ref# )
frtable frmax 0 do
2dup data-@ = if
dup cell+ data-@ refsize backpatch 0 over data-!
then
cell+ cell+
loop
drop ;
: !label ( bind a label to code-here )
( ref# -- )
resolve code-here swap cells lbtable + data-! ;
: @label ( fetch the binding of a label, or return a pseudo address if not )
( yet bound to an address )
( ref# -- addr )
dup cells lbtable + data-@ ?dup if swap drop else addref drop
code-here then ;
in-asm
: @@ @label ;
: @@: >r a; r> !label ;
[else]
: create-ref ( create words to reference local labels )
( c:: index -- )
( r:: -- addr )
create data-, does> data-@ @@ ;
: create-bind ( create words to bind local labels )
( c:: index -- )
( r:: -- )
create data-, does> data-@ @@: ;
( These references and bindings are named for general use. Do not use them )
( in macros )
in-asm
1 create-ref @@1 1 create-bind @@1:
2 create-ref @@2 2 create-bind @@2:
3 create-ref @@3 3 create-bind @@3:
4 create-ref @@4 4 create-bind @@4:
5 create-ref @@5 5 create-bind @@5:
6 create-ref @@6 6 create-bind @@6:
7 create-ref @@7 7 create-bind @@7:
8 create-ref @@8 8 create-bind @@8:
9 create-ref @@9 9 create-bind @@9:
[then]
in-hidden
0 value in-macro? ( a semaphore to tell if we're in execution of a macro )
0a value macro-labels ( the first label used for macros )
variable macro-label-level ( for labels to use in macros )
: in-macro ( flag the fact that we are in a macro )
( -- )
1 +to in-macro? ;
: !in-macro ( flag the fact that we've left a macro )
( -- )
-1 +to in-macro? ;
: +macro ( get an index into the label table from an offset )
( offset -- index )
macro-label-level data-@ + dup lbmax >
abort" Too many local labels in macros!" ;
: +macro-ref ( reference a label offset from the macro level )
( offset -- addr )
+macro @label ;
: +macro-bind ( bind a label offset from the macro level )
( offset -- )
+macro !label ;
: enter-macro ( set up macro relative local labels )
( -- )
macro-labels macro-label-level dup data-@ rot + dup rot data-! cells
lbtable + macro-labels cells erase in-macro ;
: leave-macro ( go back to the old regime )
( old macro label level -- )
macro-labels macro-label-level dup data-@ rot - swap data-! !in-macro ;
: loc-init ( initialize the tables and variables )
( -- )
frtable [ frmax 2* cells ] literal erase lbtable [ lbmax cells ]
literal erase macro-labels macro-label-level data-! ;
[else]
: create-macro-ref ( create macro-safe local label references )
( c:: label offset -- )
( r:: -- addr )
create data-, does> data-@ +macro-ref ;
: create-macro-bind ( create macro-safe local label bindings )
( c:: label offset -- )
( r:: -- )
create data-, does> >r a; r> data-@ +macro-bind ;
( macro safe local labels )
in-asm
0 create-macro-ref @@m0 0 create-macro-bind @@m0:
1 create-macro-ref @@m1 1 create-macro-bind @@m1:
2 create-macro-ref @@m2 2 create-macro-bind @@m2:
3 create-macro-ref @@m3 3 create-macro-bind @@m3:
4 create-macro-ref @@m4 4 create-macro-bind @@m4:
5 create-macro-ref @@m5 5 create-macro-bind @@m5:
6 create-macro-ref @@m6 6 create-macro-bind @@m6:
7 create-macro-ref @@m7 7 create-macro-bind @@m7:
8 create-macro-ref @@m8 8 create-macro-bind @@m8:
9 create-macro-ref @@m9 9 create-macro-bind @@m9:
[then]
( constants for operand typing )
( operand types )
in-hidden
1 constant indirect ( 16 bit register indirect )
2 constant based ( 32 bit register indirect or scaled index/base )
3 constant index ( 32 bit scaled index )
4 constant immediate ( an immediate operand )
5 constant register ( a general purpose machine register )
6 constant sreg ( a segment register )
7 constant creg ( a control register )
8 constant dreg ( a debug register )
9 constant treg ( a test register )
0a constant freg ( a floating point register )
( encode and decode register representations )
( register encoding: )
( bits use )
( 0-3 data size )
( 4-7 address size )
( 8-11 type )
( 12-13 r/m or s-i-b )
: <enc-reg> ( encode the single cell operand representation from the values )
( on the stack )
( data size \ addr size \ type \ r/m or s-i-b -- reg val )
16*+ 16*+ 16*+ ;
: <dec-reg> ( decode the single cell operand representation to its )
( constituent parts )
( reg val -- data size \ addr size \ type \ r/m or s-i-b )
16/mod 16/mod 16/mod ;
: asm-op ( create the assembler operands from operand descriptions )
( c:: data size \ addr size \ type \ r/m or s-i-b -- )
( r:: -- )
( r::os: -- x )
create <enc-reg> data-, does> data-@ push-op ;
( the assembler operands )
in-asm
8bit unknown register 0 asm-op al
8bit unknown register 1 asm-op cl
8bit unknown register 2 asm-op dl
8bit unknown register 3 asm-op bl
8bit unknown register 4 asm-op ah
8bit unknown register 5 asm-op ch
8bit unknown register 6 asm-op dh
8bit unknown register 7 asm-op bh
16bit unknown register 0 asm-op ax
16bit unknown register 1 asm-op cx
16bit unknown register 2 asm-op dx
16bit unknown register 3 asm-op bx
16bit unknown register 4 asm-op sp
16bit unknown register 5 asm-op bp
16bit unknown register 6 asm-op si
16bit unknown register 7 asm-op di
32bit unknown register 0 asm-op eax
32bit unknown register 1 asm-op ecx
32bit unknown register 2 asm-op edx
32bit unknown register 3 asm-op ebx
32bit unknown register 4 asm-op esp
32bit unknown register 5 asm-op ebp
32bit unknown register 6 asm-op esi
32bit unknown register 7 asm-op edi
unknown 16bit indirect 0 asm-op [bx+si]
unknown 16bit indirect 1 asm-op [bx+di]
unknown 16bit indirect 2 asm-op [bp+si]
unknown 16bit indirect 3 asm-op [bp+di]
unknown 16bit indirect 4 asm-op [si]
unknown 16bit indirect 5 asm-op [di]
unknown 16bit indirect 6 asm-op [bp]
unknown 16bit indirect 7 asm-op [bx]
unknown 32bit based 0 asm-op [eax]
unknown 32bit based 1 asm-op [ecx]
unknown 32bit based 2 asm-op [edx]
unknown 32bit based 3 asm-op [ebx]
unknown 32bit based 4 asm-op [esp]
unknown 32bit based 5 asm-op [ebp]
unknown 32bit based 6 asm-op [esi]
unknown 32bit based 7 asm-op [edi]
unknown 32bit index 8 asm-op [eax*2]
unknown 32bit index 9 asm-op [ecx*2]
unknown 32bit index 0a asm-op [edx*2]
unknown 32bit index 0b asm-op [ebx*2]
unknown 32bit index 0d asm-op [ebp*2]
unknown 32bit index 0e asm-op [esi*2]
unknown 32bit index 0f asm-op [edi*2]
unknown 32bit index 10 asm-op [eax*4]
unknown 32bit index 11 asm-op [ecx*4]
unknown 32bit index 12 asm-op [edx*4]
unknown 32bit index 13 asm-op [ebx*4]
unknown 32bit index 14 asm-op [ebp*4]
unknown 32bit index 16 asm-op [esi*4]
unknown 32bit index 17 asm-op [edi*4]
unknown 32bit index 18 asm-op [eax*8]
unknown 32bit index 19 asm-op [ecx*8]
unknown 32bit index 1a asm-op [edx*8]
unknown 32bit index 1b asm-op [ebx*8]
unknown 32bit index 1d asm-op [ebp*8]
unknown 32bit index 1e asm-op [esi*8]
unknown 32bit index 1f asm-op [edi*8]
16bit unknown sreg 0 asm-op es
16bit unknown sreg 1 asm-op cs
16bit unknown sreg 2 asm-op ss
16bit unknown sreg 3 asm-op ds
16bit unknown sreg 4 asm-op fs
16bit unknown sreg 5 asm-op gs
32bit unknown creg 0 asm-op cr0
32bit unknown creg 2 asm-op cr2
32bit unknown creg 3 asm-op cr3
32bit unknown creg 4 asm-op cr4
32bit unknown dreg 0 asm-op dr0
32bit unknown dreg 1 asm-op dr1
32bit unknown dreg 2 asm-op dr2
32bit unknown dreg 3 asm-op dr3
32bit unknown dreg 6 asm-op dr6
32bit unknown dreg 7 asm-op dr7
32bit unknown treg 3 asm-op tr3
32bit unknown treg 4 asm-op tr4
32bit unknown treg 5 asm-op tr5
32bit unknown treg 6 asm-op tr6
32bit unknown treg 7 asm-op tr7
8bit unknown unknown unknown asm-op byte
16bit unknown unknown unknown asm-op word
32bit unknown unknown unknown asm-op dword
64bit unknown unknown unknown asm-op qword
32bit unknown unknown unknown asm-op float
64bit unknown unknown unknown asm-op double
80bit unknown unknown unknown asm-op long
80bit unknown unknown unknown asm-op extended
80bit unknown unknown unknown asm-op tbyte
unknown 8bit unknown unknown asm-op short
unknown 16bit unknown unknown asm-op near
unknown 32bit unknown unknown asm-op far
unknown unknown immediate unknown asm-op #
unknown unknown unknown unknown asm-op ,
( variables used for instruction coding )
in-hidden
variable inst-prefix ( instruction prefixes )
variable addr-prefix ( address size prefix )
variable data-prefix ( data size prefix )
variable seg-prefix ( segment override prefix )
variable sv-inst-prefix ( the saved instruction prefix )
variable inst-save ( the previously executed instruction )
variable sp-save ( the stack pointer )
variable offset-sv ( save the offset part )
variable immed-sv ( save the immediate part )
variable dt-size ( data item size )
variable ad-size ( address size )
variable rtype ( the working register type )
variable maxtype ( the maximum numerical type value encountered )
variable mod-r/m ( the working area for the mod-r/m byte )
variable s-i-b ( the working area for the s-i-b byte )
variable addmode ( addressing mode flags )
: reset-vars ( store 0 into all instruction coding variables )
0 inst-prefix data-! 0 addr-prefix data-! 0 data-prefix data-!
0 seg-prefix data-! 0 sv-inst-prefix data-! 0 inst-save data-!
0 sp-save data-! 0 offset-sv data-! 0 immed-sv data-! 0 dt-size data-!
0 ad-size data-! 0 rtype data-! 0 maxtype data-! 0 mod-r/m data-!
0 s-i-b data-! 0 addmode data-! ;
: reset-for-next-instr ( store a 0 into intermediate coding variables )
0 offset-sv data-! 0 immed-sv data-! 0 dt-size data-!
0 ad-size data-! 0 rtype data-! 0 maxtype data-! 0 mod-r/m data-!
0 s-i-b data-! 0 addmode data-! ;
( set/reset mode bits )
1 constant immed-bit ( flag an immediate operand )
2 constant direct-bit ( flag the direction )
4 constant mod-r/m-bit ( flag that we've started the mod-r/m )
8 constant s-i-b-bit ( flag the beginning of s-i-b creation )
10 constant full-off-bit ( flag a full offset )
20 constant based-bit ( flag that we've seen a base )
40 constant offset-bit ( flag an offset )
80 constant short-bit ( flag short )
100 constant near-bit ( flag near )
200 constant far-bit ( flag far )
400 constant do-1op-bit ( flag we've been through do-1op once )
800 constant maybe-offset-bit ( flag that maybe we've got an offset )
immed-bit
direct-bit or
mod-r/m-bit or
s-i-b-bit or
full-off-bit or
based-bit or
offset-bit or
short-bit or
near-bit or
far-bit or
do-1op-bit or
maybe-offset-bit or
constant mode-mask ( all mode bits set )
: 1mode-bit! ( set a mode bit )
( bit constant -- )
addmode swap over data-@ or swap data-! ;
: 0mode-bit! ( clear a mode bit )
( bit constant -- )
mode-mask xor addmode swap over data-@ and swap data-! ;
: mode-bit@ ( fetch a mode bit )
( bit mask -- flag )
addmode data-@ and 0<> ;
: has-immed ( flag an immediate operand )
( -- )
immed-bit 1mode-bit! ;
: has-immed? ( do we have an immediate operand? )
( -- flag )
immed-bit mode-bit@ ;
: has-mod-r/m ( we've seen at least one operand )
( -- )
mod-r/m-bit 1mode-bit! ;
: has-mod-r/m? ( have we seen an operand? )
( -- flag )
mod-r/m-bit mode-bit@ ;
: has-s-i-b ( we've started work on the s-i-b )
( -- )
s-i-b-bit 1mode-bit! ;
: has-s-i-b? ( have we started work on the s-i-b )
( -- flag )
s-i-b-bit mode-bit@ ;
: reg,r/m ( addressing mode is register, register/memory )
( -- )
direct-bit 1mode-bit! ;
: r/m,reg ( addressing mode is register/memory, register )
( -- )
direct-bit 0mode-bit! ;
: direction? ( is the destination a register? )
( -- flag )
direct-bit mode-bit@ ;
: has-full-off ( must generate a full offset )
( -- )
full-off-bit 1mode-bit! ;
: has-full-off? ( do we need a full offset? )
( -- flag )
full-off-bit mode-bit@ ;
: has-base ( we have a base )
( -- )
based-bit 1mode-bit! ;
: has-base? ( do we have a base? )
( -- flag )
based-bit mode-bit@ ;
: maybe-s-i-b? ( do we have a possible s-i-b? )
( -- flag )
based-bit mode-bit@ s-i-b-bit mode-bit@ or ;
: has-offset ( flag that we do have an offset )
( -- )
offset-bit 1mode-bit! ;
: has-offset? ( do we have an offset? )
( -- flag )
offset-bit mode-bit@ full-off-bit mode-bit@ or ;
: is-short ( we have a short displacement )
( -- )
short-bit 1mode-bit! ;
: is-short? ( is the displacement short? )
( -- flag )
short-bit mode-bit@ ;
: is-near ( we have a near displacement )
( -- )
near-bit 1mode-bit! ;
: is-near? ( do we have a near displacement? )
( -- flag )
near-bit mode-bit@ far-bit mode-bit@ 0= or ;
: is-far ( we have a far pointer )
( -- )
far-bit 1mode-bit! ;
: is-far? ( do we have a far displacement? )
( -- flag )
far-bit mode-bit@ ;
: do-1op-exed ( we've exec'd do-1op )
( -- )
do-1op-bit 1mode-bit! ;
( Note: when we start to assemble an opcode, all flags are off )
: do-1op-exed? ( have we exec'd do-1op? )
( -- flag )
do-1op-bit mode-bit@ ;
: maybe-has-offset ( flag that we've picked something up from the stack )
( -- )
maybe-offset-bit 1mode-bit! ;
: maybe-has-offset? ( have we picked up something from the stack? )
( -- flag )
maybe-offset-bit mode-bit@ ;
( test for error conditions )
: _?params ( are there parameters on the stack? )
sp@ sp-save data-@ - dup ?clr-opstack
abort" Offset or immediate operand not allowed with this instruction!" ;
' _?params is ?params
: _?seg ( is there a segment override? )
seg-prefix data-@ dup ?clr-opstack
abort" Segment override not allowed with this instruction!" ;
' _?seg is ?seg
: _?lock ( is there a LOCK prefix? )
inst-prefix data-@ 0f0 = dup ?clr-opstack
abort" LOCK prefix not allowed with this instruction!" ;
' _?lock is ?lock
: _?rep ( is there a repeat prefix? )
inst-prefix data-@ 0f3 over = 0f2 rot = or dup ?clr-opstack
abort" REP, etc. not allowed with this instruction!" ;
' _?rep is ?rep
: _?inst-pre ( is there any instruction prefix? )
inst-prefix data-@ dup ?clr-opstack
abort" Instruction prefixes not allowed with this instruction!" ;
' _?inst-pre is ?inst-pre
: _?operands ( are there any operands? )
op-depth dup ?clr-opstack
abort" Operands not allowed with this instruction!" ;
' _?operands is ?operands
: _?opsize1 ( is the operand size mismatched? )
( n -- )
?dup if dt-size data-@ ?dup if - dup ?clr-opstack
abort" Operand size mismatched!" else dt-size data-! then then ;
: _?opsize2 ( just store the operand size )
( n -- )
?dup if dt-size data-! then ;
' _?opsize1 is ?opsize
: _?adsize1 ( is the address size mismatched? )
( n -- )
?dup if ad-size data-@ ?dup if - dup ?clr-opstack
abort" Address size mismatched!" else ad-size data-! then then ;
: _?adsize2 ( just store the address size )
( n -- )
?dup if ad-size data-! then ;
' _?adsize1 is ?adsize
: _?short ( is the address short? )
( -- )
ad-size data-@ 8bit = dup ?clr-opstack
abort" SHORT not allowed with this instruction!" ;
' _?short is ?short
: ?noshort ( do we have an illegal short? )
( -- )
is-short? if 8bit ad-size data-! ?short then ;
: _?toofar ( is the branch offset to far? )
( flag -- )
dup ?clr-opstack
abort" Branch offset too big to fit specified width!" ;
' _?toofar is ?toofar
: _?unres ( are there any unresolved forward reference labels? )
( -- )
frtable frmax 0 do dup data-@ dup ?clr-opstack
abort" Unresolved forward reference!" cell+ cell+ loop drop ;
' _?unres is ?unres
: _?noadsize ( no or unknown address size )
( -- )
clr-opstack -1
abort" No or unknown address size!" ;
' _?noadsize is ?noadsize
: _?toomanyops ( are there too many operands? )
( max allowed operands -- )
op-depth < dup ?clr-opstack
abort" Too many operands!" ;
' _?toomanyops is ?toomanyops
: _?nofar ( is there an unallowed far reference? )
( -- )
ad-size data-@ 32bit = dup ?clr-opstack
abort" FAR references not allowed with this instruction!" ;
' _?nofar is ?nofar
: <_?match> ( the error action for ?match and ?nomatch )
( flag -- )
dup ?clr-opstack
abort" Operand mismatch!" ;
: _?match ( error if the parameters match )
( x1 \ x2 -- )
= <_?match> ;
' _?match is ?match
: _?nomatch ( error if the parameters don't match )
( x1 \ x2 -- )
- <_?match> ;
' _?nomatch is ?nomatch
: _?finished ( are there operands left? )
( -- )
op-depth dup ?clr-opstack
abort" Unconsumed operands!" ;
' _?finished is ?finished
: _?badtype ( is the operand type allowed? )
( max type allowed -- )
maxtype data-@ < dup ?clr-opstack
abort" Addressing mode not allowed!" ;
' _?badtype is ?badtype
: _?badcombine ( can the operand types be combined? )
( flag -- )
dup ?clr-opstack
abort" Illegal operand combination!" ;
' _?badcombine is ?badcombine
: _?notenough ( are there not enough operands? )
( n -- )
op-depth > dup ?clr-opstack
abort" Not enough operands!" ;
' _?notenough is ?notenough
: _?noimmed ( is there an illegal immediate operand? )
( -- ) has-immed? dup ?clr-opstack
abort" Immediate operands not allowed with this instruction!" ;
' _?noimmed is ?noimmed
: _?badmode ( is the address mode illegal? )
( flag -- )
dup ?clr-opstack
abort" Illegal address mode!" ;
' _?badmode is ?badmode
: _?reg,r/m ( is the destination a register? )
( -- )
direction? 0= mod-r/m data-@ 0c0 < and dup ?clr-opstack
abort" Destination must be a register!" ;
' _?reg,r/m is ?reg,r/m
: _?r/m,reg ( is the source a register? )
( -- )
direction? mod-r/m data-@ 0c0 < and dup ?clr-opstack
abort" Source must be a register!" ;
' _?r/m,reg is ?r/m,reg
: _?mem ( is one of the operands in memory? )
( -- )
mod-r/m data-@ 0bf > maybe-has-offset? 0= and dup ?clr-opstack
abort" Instruction requires a memory operand!" ;
' _?mem is ?mem
: _?reg ( are all of the operands register? )
( -- )
mod-r/m data-@ 0c0 < has-offset? or dup ?clr-opstack
abort" This instruction may only use registers!" ;
' _?reg is ?reg
: ?mem,reg ( is the instruction coded as memory,register? )
( -- )
?r/m,reg ?mem ;
: ?reg,mem ( is the instruction coded as register,memory? )
( -- )
?reg,r/m ?mem ;
: ?regexclus ( is the addressing mode exclusive? )
( -- )
rtype data-@ 0 ?nomatch ;
in-asm
: report-errors ( turn on error reporting )
['] _?params is ?params
['] _?seg is ?seg
['] _?lock is ?lock
['] _?rep is ?rep
['] _?inst-pre is ?inst-pre
['] _?operands is ?operands
['] _?opsize1 is ?opsize
['] _?adsize1 is ?adsize
['] _?short is ?short
['] _?toofar is ?toofar
['] _?unres is ?unres
['] _?noadsize is ?noadsize
['] _?toomanyops is ?toomanyops
['] _?nofar is ?nofar
['] _?match is ?match
['] _?nomatch is ?nomatch
['] _?finished is ?finished
['] _?badtype is ?badtype
['] _?badcombine is ?badcombine
['] _?notenough is ?notenough
['] _?noimmed is ?noimmed
['] _?badmode is ?badmode
['] _?reg,r/m is ?reg,r/m
['] _?r/m,reg is ?r/m,reg
['] _?mem is ?mem
['] _?reg is ?reg ;
: no-errors ( turn off error reporting )
['] noop is ?params
['] noop is ?seg
['] noop is ?lock
['] noop is ?rep
['] noop is ?inst-pre
['] noop is ?operands
['] _?opsize2 is ?opsize
['] _?adsize2 is ?adsize
['] noop is ?short
['] drop is ?toofar
['] noop is ?unres
['] noop is ?noadsize
['] drop is ?toomanyops
['] noop is ?nofar
['] 2drop is ?match
['] 2drop is ?nomatch
['] noop is ?finished
['] drop is ?badtype
['] drop is ?badcombine
['] drop is ?notenough
['] noop is ?noimmed
['] drop is ?badmode
['] noop is ?reg,r/m
['] noop is ?r/m,reg
['] noop is ?mem
['] noop is ?reg ;
( generate prefix sequences )
in-hidden
: inst, ( generate a necessary instruction prefix )
( -- )
inst-prefix data-@ ?dup if code-c, 0 inst-prefix data-! then ;
: addr, ( generate a necessary address size prefix )
( -- )
addr-prefix data-@ if 67 code-c, 0 addr-prefix data-! then ;
: data, ( generate a necessary data size prefix )
( -- )
data-prefix data-@ if 66 code-c, 0 addr-prefix data-! then ;
: seg, ( generate a necessary segment override prefix )
( -- )
seg-prefix data-@ ?dup if code-c, 0 seg-prefix data-! then ;
: generate-prefixes ( generate necessary prefixes )
( -- )
inst, addr, data, seg, ;
( the prefixes )
: seg-pre create data-, does> data-@ seg-prefix data-! ;
: inst-pre create data-, does> data-@ inst-prefix data-! ;
in-asm
2e seg-pre cs:
36 seg-pre ss:
3e seg-pre ds:
26 seg-pre es:
64 seg-pre fs:
65 seg-pre gs:
0f3 inst-pre rep
0f3 inst-pre repe
0f3 inst-pre repz
0f2 inst-pre repne
0f2 inst-pre repnz
0f0 inst-pre lock
( save the p-stack depth )
in-hidden
: save-depth ( -- )
sp@ sp-save data-! ;
: depth-change ( report on a change of depth )
sp@ sp-save data-@ swap - cell/ ;
( create an assembly mnemonic )
: compile-opcode ( compile the bytes in an opcode )
( 0 -- | a -- | x \ a -- | x \ x' \ a -- )
( os: x ... -- )
( a is the address of a two cell data structure: )
( offset 0 -- xt of the actual routine to compile the code )
( offset 1 -- parameter used to generate the code )
?dup if dup cell+ data-@ swap data-@ execute then ;
defer save-inst ( save the current instruction -- used in postfix mode )
: _save-inst ( save the current instruction, and fetch the previous one )
( also swaps instruction prefixes )
( a -- a' )
inst-save dup data-@ >r data-! r> inst-prefix sv-inst-prefix
2dup data-@ swap data-@ rot data-! swap data-! ;
' _save-inst is save-inst
in-asm
: postfix ['] _save-inst is save-inst ;
: prefix ['] noop is save-inst ;
in-hidden
: _do-opcode ( create the actual opcode, or at least call the functions )
( that do ... )
( x? \ x? \ 0|addr -- )
save-inst compile-opcode reset-for-next-instr save-depth ;
' _do-opcode is do-opcode
: opcode ( c:: parameter \ xt -- )
( r:: -- | x -- | x \ x' -- )
( r::os: x ... -- )
create data-, data-, does> do-opcode ;
( support routines for creating assembly code )
: all-except ( process all operands except one in particular )
( x \ n -- type \ mod-r/m {x!=n} | -- 0 \ 0 )
over = if drop 0 0 else <dec-reg> >r >r ?adsize ?opsize r> r> then ;
: offset8, ( create an 8 bit code-here relative offset )
( addr -- )
8b-rel register-ref code-here 1+ - dup gt-byte? ?toofar code-c, ;
: offset16, ( create a 16 bit code-here relative offset )
( addr -- )
16b-rel register-ref code-here 2+ - dup gt-word? ?toofar code-w, ;
: offset32, ( create a 32 bit code-here relative offset )
( addr -- )
32b-rel register-ref code-here 4+ - code-d, ;
: offset16/32, ( create a 16 or 32 bit code-here relative offset )
( addr \ 16bit? -- )
if offset16, else offset32, then ;
: flag-for-size-prefix ( do we need a size prefix? )
( size -- flag )
dup if dup 8bit - if default-size - else drop 0 then then ;
: check-ad-size ( check the address size )
( -- )
ad-size data-@ flag-for-size-prefix addr-prefix data-! ;
: check-dt-size ( check the operand size )
( -- )
dt-size data-@ flag-for-size-prefix data-prefix data-! ;
: check-sizes ( check the address and operand sizes )
( -- )
check-ad-size check-dt-size ;
: rtype! ( store the addressing mode type and update maxtype )
( type -- )
dup rtype data-! maxtype data-@ over < if maxtype data-! else
drop then ;
: special-process? ( do we need to specially process this register? )
( -- flag )
maxtype data-@ dup register > swap freg < and ;
: special-register? ( is this a special register? )
( -- flag )
rtype data-@ dup register > swap freg < and ;
: do-reg ( do any register addressing mode translation )
( reg \ type -- )
?regexclus rtype! do-1op-exed? if
has-mod-r/m? if
mod-r/m data-@ swap special-process? if
special-register? if
8*+ reg,r/m
else
maxtype data-@ sreg = if
c0+ swap c0-8* +
else
c0+ +
then r/m,reg
then
else
8*+ reg,r/m
then
else ( *MUST* be reg,disp or reg,immed )
c0+ reg,r/m has-mod-r/m has-immed? 0= if
has-offset
then
then
else ( first time through do-1op )
special-register? rtype data-@ sreg <> and if
8*
else ( either a general or segment register )
c0+
then has-mod-r/m r/m,reg
then mod-r/m data-! ;
: do-immed ( do an immediate addressing mode operand )
( x \ 0 -- )
drop immed-sv data-! has-immed immediate rtype! ;
: do-indire ( do an indirect addressing mode operand )
( reg -- )
has-mod-r/m? if
mod-r/m data-@ dup 0bf > if
c0-8* +
else
+
then
else
has-mod-r/m
then mod-r/m data-! has-base ;
: do-index ( do a scaled index addressing mode )
( reg -- )
has-s-i-b 8* s-i-b data-@ 8/ + s-i-b data-! has-mod-r/m? if
mod-r/m data-@ dup 0bf > if
c0-8* 4+
else
[ 7 -1 xor ] literal and 4+
then
else