-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTM.c
More file actions
4058 lines (3745 loc) · 158 KB
/
Copy pathSTM.c
File metadata and controls
4058 lines (3745 loc) · 158 KB
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
//-----------------------------------------------------------
// Dr. Art Hanna
// Simple Target Machine (STM) for SPL featuring a two-pass
// assembler and simulation of a stack-based ISA
// 9.25.2018 Added CONCATS (concatenate strings)
// 9.15.2020 Added escape sequence codes to <string> and <character>
// literal scanning in GetNextToken()
#define VERSION "November, 2023"
// STM.c
//-----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#define SOURCELINELENGTH 512
#define LINESPERPAGE 55
#define EOPC (1)
#define EOLC (2)
#define FF 0X0C
#define SIZEOFIDENTIFIERTABLE 500
#define MAXIMUMLENGTHIDENTIFIER 64
#define HIBYTE(word) ((0XFF00u & word) >> 8)
#define LOBYTE(word) ((0X00FFu & word) )
#define TORF(word) ((word == 0XFFFFu) ? 'T' : ((word == 0X0000u)? 'F' : '?'))
#define SIGNED(x) (((x) <= 0X7FFF) ? (int) (x) : ( (int) ((x)-65536)) )
#define UNSIGNED(x) (((x) <= 32767) ? (WORD) (x) : ((WORD) ((x)-65536)) )
typedef unsigned char BYTE;
typedef unsigned short int WORD;
/*
=====================================================================
STM assembly-language grammar expressed in BNF
=====================================================================
<STMProgram> ::= { [ <statement> ] EOLC }* EOPC
<statement> ::= <instruction> [ <comment> ]
| <comment>
<instruction> ::= [ <identifier> ] <HWMnemonic> [ <operand> ]
| [ <identifier> ] ORG <I16>
| <identifier> EQU (( <W16> | * ))
| [ <identifier> ] RW [ <I16> ]
| [ <identifier> ] DW <W16>
| [ <identifier> ] DS <string>
<HWMnemonic> ::= || See list of H/W operations
<operand> ::= <memory>
| #<W16>
| <A16> || for jump/CALL instructions *ONLY*
<memory> ::= #<W16> || mode = 0X00, immediate
| <A16> || mode = 0X01, memory direct
| @<A16> || mode = 0X02, memory indirect
| $<A16> || mode = 0X03, memory indexed
| SP:<I16> || mode = 0X04, SP-relative direct
| @SP:<I16> || mode = 0X05, SP-relative indirect
| $SP:<I16> || mode = 0X06, SP-relative indexed
| FB:<I16> || mode = 0X07, FB-relative direct
| @FB:<I16> || mode = 0X08, FB-relative indirect
| $FB:<I16> || mode = 0X09, FB-relative indexed
| SB:<I16> || mode = 0X0A, SB-relative direct
| @SB:<I16> || mode = 0X0B, SB-relative indirect
| $SB:<I16> || mode = 0X0C, SB-relative indexed
<W16> ::= (( <I16> | <F16> | true | false | <character> | <identifier> ))
<A16> ::= <identifier>
<identifier> ::= <letter> { (( <letter> | <dit> | _ )) }*
<I16> ::= [ (( + | - )) ] (( 0D <dit> { <dit> }*
| 0X <hit> { <hit> }*
| 0B <bit> { <bit> }* ))
<F16> ::= [ (( + | - )) ] 0F <dit> { <dit> }* . <dit> { <dit> }* [ E [ - ] <dit> { <dit> }* ]
<bit> ::= 0 | 1
<dit> ::= <bit> | 2 | 3| 4| 5 | 6 | 7 | 8 | 9
<hit> ::= <dit> | A | B | C | D | E | F
<character> ::= '<ASCIICharacter>' || *Note* use ''' to embed one ', no escape sequences supported
<string> ::= "{<ASCIICharacter>}*" || *Note* escape sequences \n,\t,\v,\b,\\,\" are supported
<letter> ::= A | B | ... | Z | a | b | ... | z
<ASCIICharacter> ::= || Every printable ASCII character in range [ ' ','~' ]
<comment> :: ; {<ASCIICharacter>}*
=====================================================================
STM Instruction Set Architecture (ISA)
=====================================================================
OpCode Assembly Syntax Instruction Format Semantics (meaning at run-time)
------ ------------------ ------------------ ------------------------------------------------------------------
0X00 NOOP OpCode Do nothing
0X01 PUSH memory OpCode:mode:O16 Push word memory[EA] on run-time stack
0X02 PUSHA memory OpCode:mode:O16 Push EA on run-time stack
0X03 POP memory OpCode:mode:O16 Pop run-time stack and store word in memory[EA]
0X04 DISCARD #W16 OpCode:O16 Discard O16U words from top of run-time stack
0X05 SWAP OpCode Pop RHS,LHS; push RHS,LHS
0X06 MAKEDUP OpCode Read TOS; push TOS (duplicate TOS)
0X07 PUSHSP OpCode Push SP
0X08 PUSHFB OpCode Push FB
0X09 PUSHSB OpCode Push SB
0X0A POPSP OpCode Pop SP
0X0B POPFB OpCode Pop FB
0X0C POPSB OpCode Pop SB
0X0D SETAAE memory OpCode:mode:O16 Pop key,value; add (key,value) to array in memory[EA] (Note 5)
0X0E GETAAE memory OpCode:mode:O16 Pop key; find (key,value) in array in memory[EA]; push value (Note 6)
0X0F ADRAAE memory OpCode:mode:O16 Pop key; find (key,value) in array in memory[EA]; push address
of value (Note 7)
0X10 COPYAA OpCode Pop RHS,LHS; memory[LHS+2*i] = memory[RHS+2*i], i in
[ 0,2*capacity+1 ] (Note 9)
0X11 SETSE memory OpCode:mode:O16 Pop character,index; store character in
memory[EA+4+2*(index-1)] (Note 8)
0X12 GETSE memory OpCode:mode:O16 Pop index; push memory[EA+4+2*(index-1)] (Note 8)
0X13 ADRSE memory OpCode:mode:O16 Pop index; push address (EA+4+2*(index-1)) (Note 8)
0X14 ADDSE memory OpCode:mode:O16 Pop character; store character in memory[EA+4+2*length];
increment length (Note 8)
0X15 COPYS OpCode Pop RHS,LHS; memory[LHS+2*i] = memory[RHS+2*i], i in
[ 0,capacity+1 ] (Note 9)
0X1D CONCATS OpCode Pop RES,RHS,LHS; memory[RES] = memory[LHS] concatenate memory[RHS];
push RES (Note 12)
0X16 SETAE memory OpCode:mode:O16 Pop value,index(n),index(n-1),...,index(1); store value at offset
in array (Note 10)
0X17 GETAE memory OpCode:mode:O16 Pop index(n),index(n-1),...,index(1); push value found at offset
in array (Note 10)
0X18 ADRAE memory OpCode:mode:O16 Pop index(n),index(n-1),...,index(1); push address of value found
at offset in array (Note 10)
0X19 COPYA OpCode Pop RHS,LHS; memory[LHS+2*i] = memory[RHS+2*i], i in
[ 0,2*n+capacity ] (Note 9)
0X1A GETAN memory OpCode:mode:O16 Push n (# of dimensions)
0X1B GETALB memory OpCode:mode:O16 Pop dimension # i; push lower-bound, LBi (Note 11)
0X1C GETAUB memory OpCode:mode:O16 Pop dimension # i; push upper-bound, UBi (Note 11)
0X20 ADDI OpCode Pop RHS,LHS; push integer ( LHS+RHS )
0X21 ADDF OpCode Pop RHS,LHS; push float ( LHS+RHS )
0X22 SUBI OpCode Pop RHS,LHS; push integer ( LHS-RHS )
0X23 SUBF OpCode Pop RHS,LHS; push float ( LHS-RHS )
0X24 MULI OpCode Pop RHS,LHS; push integer ( LHS*RHS )
0X25 MULF OpCode Pop RHS,LHS; push float ( LHS*RHS )
0X26 DIVI OpCode Pop RHS,LHS; push integer ( LHS÷RHS )
0X27 DIVF OpCode Pop RHS,LHS; push float ( LHS÷RHS )
0X28 REMI OpCode Pop RHS,LHS; push integer ( LHS rem RHS )
0X29 POWI OpCode Pop RHS,LHS; push integer pow(LHS,RHS)
0X2A POWF OpCode Pop RHS,LHS; push float pow(LHS,RHS)
0X2B NEGI OpCode Pop RHS; push integer -RHS
0X2C NEGF OpCode Pop RHS; push float -RHS
0X2D AND OpCode Pop RHS,LHS; push boolean ( LHS and RHS )
0X2E NAND OpCode Pop RHS,LHS; push boolean ( LHS nand RHS )
0X2F OR OpCode Pop RHS,LHS; push boolean ( LHS or RHS )
0X30 NOR OpCode Pop RHS,LHS; push boolean ( LHS nor RHS )
0X31 XOR OpCode Pop RHS,LHS; push boolean ( LHS xor RHS )
0X32 NXOR OpCode Pop RHS,LHS; push boolean ( LHS nxor RHS )
0X33 NOT OpCode Pop RHS; push boolean ( not RHS )
0X34 BITAND OpCode Pop RHS,LHS; push boolean ( LHS bitwise-and RHS )
0X35 BITNAND OpCode Pop RHS,LHS; push boolean ( LHS bitwise-nand RHS )
0X36 BITOR OpCode Pop RHS,LHS; push boolean ( LHS bitwise-or RHS )
0X37 BITNOR OpCode Pop RHS,LHS; push boolean ( LHS bitwise-nor RHS )
0X38 BITXOR OpCode Pop RHS,LHS; push boolean ( LHS bitwise-xor RHS )
0X39 BITNXOR OpCode Pop RHS,LHS; push boolean ( LHS bitwise-nxor RHS )
0X3A BITNOT OpCode Pop RHS; push boolean ( bitwise-not RHS )
0X3B BITSL #W16 OpCode:O16 Pop LHS; push ( LHS shifted-left O16U bits )
0X3C BITLSR #W16 OpCode:O16 Pop LHS; push ( LHS logically shifted-right O16U bits )
0X3D BITASR #W16 OpCode:O16 Pop LHS; push ( LHS arithmetically shifted-right O16U bits )
0X60 CITOF OpCode Pop integer RHS; push float RHS (integer-to-float)
0X61 CFTOI OpCode Pop float RHS; push integer RHS (float-to-integer)
0X70 CMPI OpCode Pop RHS,LHS; set LEG in FLAGS based on ( LHS ? RHS ) (integer)
0X71 CMPF OpCode Pop RHS,LHS; set LEG in FLAGS based on ( LHS ? RHS ) (float)
0X72 SETNZPI OpCode Set NZP in FLAGS based on sign of TOS (integer)
0X73 SETNZPF OpCode Set NZP in FLAGS based on sign of TOS (float)
0X74 SETT OpCode Set T in FLAGS based on true/false value of TOS (boolean)
0X80 JMP A16 OpCode:O16 PC <- O16U
0X81 JMPL A16 OpCode:O16 if ( L ) PC <- O16U
0X82 JMPE A16 OpCode:O16 if ( E ) PC <- O16U
0X83 JMPG A16 OpCode:O16 if ( G ) PC <- O16U
0X84 JMPLE A16 OpCode:O16 if ( L or E ) PC <- O16U
0X85 JMPNE A16 OpCode:O16 if ( L or G ) PC <- O16U (JMPLG)
0X86 JMPGE A16 OpCode:O16 if ( G or E ) PC <- O16U
0X87 JMPN A16 OpCode:O16 if ( N ) PC <- O16U
0X88 JMPNN A16 OpCode:O16 if ( not N ) PC <- O16U
0X89 JMPZ A16 OpCode:O16 if ( Z ) PC <- O16U
0X8A JMPNZ A16 OpCode:O16 if ( not Z ) PC <- O16U
0X8B JMPP A16 OpCode:O16 if ( P ) PC <- O16U
0X8C JMPNP A16 OpCode:O16 if ( not P ) PC <- O16U
0X8D JMPT A16 OpCode:O16 if ( T ) PC <- O16U
0X8E JMPNT A16 OpCode:O16 if ( not T ) PC <- O16U (JMPF)
0XA0 CALL A16 OpCode:O16 Push PC; PC <- O16U
0XA1 RETURN OpCode Pop PC
0XFF SVC #W16 OpCode:O16 Execute service request O16U (parameters are passed on run-time stack)
*Notes*
Note 1: Opcode is an 8-bit code used to uniquely identify each STM machine instruction.
Note 2: mode is an 8-bit code used to specify a memory operand's addressing mode.
Note 3: O16 is the instruction's operand field and is interpreted as either a 16-bit two's complement
integer, O16, or as a 16-bit unsigned integer, O16U. The interpretation used depends on the OpCode.
Note 4: EA is the effective address of a memory word. The EA is computed using O16U and the
instruction's addressing mode.
Note 5: When (key,value) pair is found, the existing value is replaced with the value popped
from run-time stack. When (key,value) pair is not found, the pair is stored in the next available
(key,value) slot. A fatal run-time error occurs when (key,value) pair is not found and (size = capacity).
Note 6: A fatal run-time error occurs when (key,value) pair is not found.
Note 7: When (key,value) pair is not found, the pair is stored in the next available (key,value) slot.
A fatal run-time error occurs when (key,value) pair is not found and (size = capacity).
Note 8: A STM string is composed of 2+capacity words of contiguous memory. Word 1 is the length of
the string (the number of characters contained in the string); word 2 is the string's capacity; and
words 3-to-(capacity+2) are reserved for the string's characters. When empty, length = 0, otherwise
(1 <= length <= capacity). A fatal error occurs when (1 <= index <= length) is not true for SETSE,
GETSE, and ADRSE. A fatal error occurs when (length = capacity) when ADDSE begins execution.
Note 9: Assumes that memory block pointed to by LHS is large enough to accommodate structure stored
in memory block pointed to by RHS.
Note 10: A fatal error occurs when the following equation is not true for SETAE, GETAE, and ADRAE.
((LB1 <= index1 <= UB1) AND (LB2 <= index2 <= UB2) AND...AND (LBn <= indexn <= UBn))
Note 11: A fatal error occurs when dimension # i is not in [ 1,n ].
Note 12: Assumes that memory block pointed to by RES is large enough to accommodate the concatenation
of strings pointed to by LHS and RHS. A fatal error occurs when
(length-of-LHS + length-of-RHS) > capacity-of-RES
*/
//-----------------------------------------------------------
typedef enum
//-----------------------------------------------------------
{
// Pseudo-terminals
IDENTIFIER,
INTEGER,
FLOAT,
CHARACTER,
STRING,
EOPTOKEN,
EOLTOKEN,
UNKNOWN,
// Punctuation
POUND,
ATSIGN,
COLON,
DOLLAR,
ASTERISK,
// Assembler mnemonics
ORG,
EQU,
DW,
DS,
RW,
// Boolean literals
FALSE,
TRUE,
// Registers
SP,
FB,
SB,
// H/W mnemonics
NOOP,
PUSH,
PUSHA,
POP,
DISCARD,
SWAP,
MAKEDUP,
PUSHSP,
PUSHFB,
PUSHSB,
POPSP,
POPFB,
POPSB,
SETAAE,
GETAAE,
ADRAAE,
COPYAA,
SETSE,
GETSE,
ADRSE,
ADDSE,
COPYS,
CONCATS,
SETAE,
GETAE,
ADRAE,
COPYA,
GETAN,
GETALB,
GETAUB,
ADDI,
ADDF,
SUBI,
SUBF,
MULI,
MULF,
DIVI,
DIVF,
REMI,
POWI,
POWF,
NEGI,
NEGF,
AND,
NAND,
OR,
NOR,
XOR,
NXOR,
NOT,
BITAND,
BITNAND,
BITOR,
BITNOR,
BITXOR,
BITNXOR,
BITNOT,
BITSL,
BITLSR,
BITASR,
CITOF,
CFTOI,
CMPI,
CMPF,
SETNZPI,
SETNZPF,
SETT,
JMP,
JMPL,
JMPE,
JMPG,
JMPLE,
JMPNE,
JMPGE,
JMPN,
JMPNN,
JMPZ,
JMPNZ,
JMPP,
JMPNP,
JMPT,
JMPNT,
CALL,
RETURN,
SVC
} TOKENTYPE;
typedef enum { NONE,MEMORY,A16,IMMW16 } OPERANDTYPE;
//-----------------------------------------------------------
typedef struct
//-----------------------------------------------------------
{
int opCode;
int sizeInBytes;
char mnemonic[8+1]; // *LOOK* increase size for longer-than-8-character mnemonics
TOKENTYPE token;
OPERANDTYPE operandType;
} HWOPERATIONRECORD;
//-----------------------------------------------------------
const HWOPERATIONRECORD HWOperationTable[] =
//-----------------------------------------------------------
{
{ 0X00,1,"NOOP" ,NOOP ,NONE },
{ 0X01,4,"PUSH" ,PUSH ,MEMORY },
{ 0X02,4,"PUSHA" ,PUSHA ,MEMORY },
{ 0X03,4,"POP" ,POP ,MEMORY },
{ 0X04,3,"DISCARD" ,DISCARD ,IMMW16 },
{ 0X05,1,"SWAP" ,SWAP ,NONE },
{ 0X06,1,"MAKEDUP" ,MAKEDUP ,NONE },
{ 0X07,1,"PUSHSP" ,PUSHSP ,NONE },
{ 0X08,1,"PUSHFB" ,PUSHFB ,NONE },
{ 0X09,1,"PUSHSB" ,PUSHSB ,NONE },
{ 0X0A,1,"POPSP" ,POPSP ,NONE },
{ 0X0B,1,"POPFB" ,POPFB ,NONE },
{ 0X0C,1,"POPSB" ,POPSB ,NONE },
{ 0X0D,4,"SETAAE" ,SETAAE ,MEMORY },
{ 0X0E,4,"GETAAE" ,GETAAE ,MEMORY },
{ 0X0F,4,"ADRAAE" ,ADRAAE ,MEMORY },
{ 0X10,1,"COPYAA" ,COPYAA ,NONE },
{ 0X11,4,"SETSE" ,SETSE ,MEMORY },
{ 0X12,4,"GETSE" ,GETSE ,MEMORY },
{ 0X13,4,"ADRSE" ,ADRSE ,MEMORY },
{ 0X14,4,"ADDSE" ,ADDSE ,MEMORY },
{ 0X15,1,"COPYS" ,COPYS ,NONE },
{ 0X1D,1,"CONCATS" ,CONCATS ,NONE },
{ 0X16,4,"SETAE" ,SETAE ,MEMORY },
{ 0X17,4,"GETAE" ,GETAE ,MEMORY },
{ 0X18,4,"ADRAE" ,ADRAE ,MEMORY },
{ 0X19,1,"COPYA" ,COPYA ,NONE },
{ 0X1A,4,"GETAN" ,GETAN ,MEMORY },
{ 0X1B,4,"GETALB" ,GETALB ,MEMORY },
{ 0X1C,4,"GETAUB" ,GETAUB ,MEMORY },
{ 0X20,1,"ADDI" ,ADDI ,NONE },
{ 0X21,1,"ADDF" ,ADDF ,NONE },
{ 0X22,1,"SUBI" ,SUBI ,NONE },
{ 0X23,1,"SUBF" ,SUBF ,NONE },
{ 0X24,1,"MULI" ,MULI ,NONE },
{ 0X25,1,"MULF" ,MULF ,NONE },
{ 0X26,1,"DIVI" ,DIVI ,NONE },
{ 0X27,1,"DIVF" ,DIVF ,NONE },
{ 0X28,1,"REMI" ,REMI ,NONE },
{ 0X29,1,"POWI" ,POWI ,NONE },
{ 0X2A,1,"POWF" ,POWF ,NONE },
{ 0X2B,1,"NEGI" ,NEGI ,NONE },
{ 0X2C,1,"NEGF" ,NEGF ,NONE },
{ 0X2D,1,"AND" ,AND ,NONE },
{ 0X2E,1,"NAND" ,NAND ,NONE },
{ 0X2F,1,"OR" ,OR ,NONE },
{ 0X30,1,"NOR" ,NOR ,NONE },
{ 0X31,1,"XOR" ,XOR ,NONE },
{ 0X32,1,"NXOR" ,NXOR ,NONE },
{ 0X33,1,"NOT" ,NOT ,NONE },
{ 0X34,1,"BITAND" ,BITAND ,NONE },
{ 0X35,1,"BITNAND" ,BITNAND ,NONE },
{ 0X36,1,"BITOR" ,BITOR ,NONE },
{ 0X37,1,"BITNOR" ,BITNOR ,NONE },
{ 0X38,1,"BITXOR" ,BITXOR ,NONE },
{ 0X39,1,"BITNXOR" ,BITNXOR ,NONE },
{ 0X3A,1,"BITNOT" ,BITNOT ,NONE },
{ 0X3B,3,"BITSL" ,BITSL ,IMMW16 },
{ 0X3C,3,"BITLSR" ,BITLSR ,IMMW16 },
{ 0X3D,3,"BITASR" ,BITASR ,IMMW16 },
{ 0X60,1,"CITOF" ,CITOF ,NONE },
{ 0X61,1,"CFTOI" ,CFTOI ,NONE },
{ 0X70,1,"CMPI" ,CMPI ,NONE },
{ 0X71,1,"CMPF" ,CMPF ,NONE },
{ 0X72,1,"SETNZPI" ,SETNZPI ,NONE },
{ 0X73,1,"SETNZPF" ,SETNZPF ,NONE },
{ 0X74,1,"SETT" ,SETT ,NONE },
{ 0X80,3,"JMP" ,JMP ,A16 },
{ 0X81,3,"JMPL" ,JMPL ,A16 },
{ 0X82,3,"JMPE" ,JMPE ,A16 },
{ 0X83,3,"JMPG" ,JMPG ,A16 },
{ 0X84,3,"JMPLE" ,JMPLE ,A16 },
{ 0X85,3,"JMPNE" ,JMPNE ,A16 },
{ 0X86,3,"JMPGE" ,JMPGE ,A16 },
{ 0X87,3,"JMPN" ,JMPN ,A16 },
{ 0X88,3,"JMPNN" ,JMPNN ,A16 },
{ 0X89,3,"JMPZ" ,JMPZ ,A16 },
{ 0X8A,3,"JMPNZ" ,JMPNZ ,A16 },
{ 0X8B,3,"JMPP" ,JMPP ,A16 },
{ 0X8C,3,"JMPNP" ,JMPNP ,A16 },
{ 0X8D,3,"JMPT" ,JMPT ,A16 },
{ 0X8E,3,"JMPNT" ,JMPNT ,A16 },
{ 0XA0,3,"CALL" ,CALL ,A16 },
{ 0XA1,1,"RETURN" ,RETURN ,NONE },
{ 0XFF,3,"SVC" ,SVC ,IMMW16 }
};
//-----------------------------------------------------------
// GLOBAL VARIABLES
//-----------------------------------------------------------
FILE *SOURCE,*LOG;
char sourceFileName[SOURCELINELENGTH+1];
char sourceLine[SOURCELINELENGTH+1],nextCharacter;
int sourceLineIndex;
bool atEOP;
// ******* identifierTable
typedef struct
{
int value;
int numberOfDefinitions;
char identifier[MAXIMUMLENGTHIDENTIFIER+1];
} IDENTIFIERTABLERECORD;
int sizeOfIdentifierTable;
IDENTIFIERTABLERECORD identifierTable[SIZEOFIDENTIFIERTABLE+1];
// ******* mainMemory
BYTE mainMemory[0XFFFF+1];
// ******* syntaxErrors (up to 10 for each source line)
int numberOfSyntaxErrors;
char syntaxErrors[10][80+1];
//-----------------------------------------------------------
void ProcessRunTimeError(const char error[],bool isFatalError)
//-----------------------------------------------------------
{
fprintf(LOG ,"Run-time error %s\n",error); fflush(LOG);
printf("Run-time error %s\n",error);
if ( isFatalError )
{
fclose(LOG);
system("PAUSE");
exit(1);
}
}
//-----------------------------------------------------------
int main()
//-----------------------------------------------------------
{
void DoPass1();
void DoPass2(bool *noSyntaxErrors);
void ExecuteProgram();
char fullFileName[SOURCELINELENGTH+1];
bool noSyntaxErrors;
printf("Version %s\n\n",VERSION);
printf("Source filename? "); scanf("%s",sourceFileName);
strcpy(fullFileName,sourceFileName);
strcat(fullFileName,".stm");
if ( (SOURCE = fopen(fullFileName,"r")) == NULL )
{
printf("Error opening source file %s\n",fullFileName);
system("PAUSE");
exit( 1 );
}
strcpy(fullFileName,sourceFileName);
strcat(fullFileName,".log");
if ( (LOG = fopen(fullFileName,"w")) == NULL )
{
printf("Error opening log file %s\n",fullFileName);
system("PAUSE");
exit( 1 );
}
printf("Log file is %s\n",fullFileName);
/*
Assemble source program then, if no syntax errors are discovered
during translation execute resulting machine program
*/
DoPass1();
DoPass2(&noSyntaxErrors);
fclose(SOURCE);
if ( noSyntaxErrors )
ExecuteProgram();
else
printf("Source file contains syntax errors\n");
fclose(LOG);
system("PAUSE");
return( 0 );
}
//-----------------------------------------------------------
void DoPass1()
//-----------------------------------------------------------
{
void GetNextToken(TOKENTYPE *token,char lexeme[]);
void GetNextCharacter();
void ReadSourceLine();
void DefineIdentifierInTable(const char lexeme[],int value);
bool IdentifierIsInTable(const char lexeme[]);
int FindIdentifierInTable(const char lexeme[]);
int ATOI16(const char lexeme[]);
WORD ATOF16(const char lexeme[]);
TOKENTYPE token;
char lexeme[SOURCELINELENGTH+1];
int LC;
bool defineLineLabel;
char labelLexeme[SOURCELINELENGTH+1];
int labelValue;
sizeOfIdentifierTable = 0;
LC = 0X0000;
/*
Each statement *MUST BE* wholly contained on a single source line.
Read through source file line-by-line to build identifierTable.
Ignore *ALL* syntax errors because they will be "caught by" pass #2.
*/
ReadSourceLine();
while ( !atEOP )
{
GetNextCharacter();
GetNextToken(&token,lexeme);
if ( token != EOLTOKEN )
{
if ( token == IDENTIFIER )
{
defineLineLabel = true;
strcpy(labelLexeme,lexeme);
labelValue = LC; // default, with labelValue changed by ORG and EQU instructions
GetNextToken(&token,lexeme);
}
else
defineLineLabel = false;
switch ( token )
{
case ORG:
GetNextToken(&token,lexeme);
if ( token == INTEGER )
labelValue = LC = ATOI16(lexeme);
else // *ERROR*
labelValue = LC = 0X0000;
break;
case EQU:
GetNextToken(&token,lexeme);
switch ( token )
{
case INTEGER:
labelValue = ATOI16(lexeme);
break;
case FLOAT:
labelValue = ATOF16(lexeme);
break;
case TRUE:
labelValue = 0XFFFFu;
break;
case FALSE:
labelValue = 0X0000u;
break;
case CHARACTER:
labelValue = (WORD) lexeme[0];
break;
case IDENTIFIER:
if ( IdentifierIsInTable(lexeme) )
labelValue = identifierTable[FindIdentifierInTable(lexeme)].value;
else
defineLineLabel = false;
break;
case ASTERISK:
labelValue = LC;
break;
default:
defineLineLabel = false;
break;
}
break;
case RW:
GetNextToken(&token,lexeme);
if ( token == INTEGER )
LC += 2*ATOI16(lexeme);
else // *ERROR* unless token is EOLTOKEN
LC += 2;
break;
case DW: // *always* only one word of object
LC += 2;
break;
case DS: // 2-byte UNICODE characters (prepend length and capacity)
GetNextToken(&token,lexeme);
if ( token == STRING )
LC += 2*((int) strlen(lexeme)+2);
else // *ERROR*
LC += 2;
break;
default: // should be a hardware operation token
{
bool found = false;
int i = 0;
while ( (i <= (sizeof(HWOperationTable)/sizeof(HWOPERATIONRECORD))-1) && !found )
if ( token != HWOperationTable[i].token )
i++;
else
{
LC += HWOperationTable[i].sizeInBytes;
found = true;
}
if ( !found ) // *ERROR*
LC += 1;
}
break;
}
/*
When first token is an identifier add its lexeme and its value
to the identifierTable if it's not already there.
*/
if ( defineLineLabel )
DefineIdentifierInTable(labelLexeme,labelValue);
}
// Ignore remainder of source line
ReadSourceLine();
}
}
//-----------------------------------------------------------
void DoPass2(bool *noSyntaxErrors)
//-----------------------------------------------------------
{
void GetNextToken(TOKENTYPE *token,char lexeme[]);
void GetNextCharacter();
void ReadSourceLine();
void ListTopOfPageHeader(int *pageNumber,int *lines);
void RecordSyntaxError(const char syntaxError[]);
int ATOI16(const char lexeme[]);
WORD ParseW16(TOKENTYPE *token,char lexeme[]);
WORD ParseA16(TOKENTYPE *token,char lexeme[]);
bool IdentifierIsInTable(const char lexeme[]);
int FindIdentifierInTable(const char lexeme[]);
void WriteBYTEToMainMemory(int address,BYTE byte);
TOKENTYPE token;
char lexeme[SOURCELINELENGTH+1];
bool lineIsLabeled;
BYTE objectCode[256+1];
int objectBytes;
int oldLC,LC,lineNumber,linesOnPage,pageNumber;
int i,j;
*noSyntaxErrors = true;
LC = 0X0000;
lineNumber = 0;
pageNumber = 0;
ListTopOfPageHeader(&pageNumber,&linesOnPage);
/*
Each statement *MUST BE* wholly contained on a single source line.
Read through source file line-by-line to assemble into object code.
*/
rewind(SOURCE);
atEOP = false;
ReadSourceLine(); lineNumber++;
while ( !atEOP )
{
oldLC = LC;
numberOfSyntaxErrors = 0;
objectBytes = 0;
GetNextCharacter();
GetNextToken(&token,lexeme);
if ( token != EOLTOKEN )
{
// Is first token an identifier? If so, is it multiply defined?
if ( token == IDENTIFIER )
{
int index = FindIdentifierInTable(lexeme);
if ( identifierTable[index].numberOfDefinitions != 1 )
RecordSyntaxError("Multiply-defined identifier");
GetNextToken(&token,lexeme);
lineIsLabeled = true;
}
else
lineIsLabeled = false;
switch ( token )
{
case IDENTIFIER: // assume misspelled hardware mnemonic, so insert a NOOP
LC++;
objectCode[1] = 0X00u; objectBytes = 1;
RecordSyntaxError("Misplaced identifier");
GetNextToken(&token,lexeme);
break;
case UNKNOWN: // does not affect LC
objectBytes = 0;
RecordSyntaxError("Unknown token");
GetNextToken(&token,lexeme);
break;
/*
<instruction> ::= [ <identifier> ] ORG <I16>
| <identifier> EQU (( <W16> | * ))
| [ <identifier> ] RW [ <I16> ]
| [ <identifier> ] DW <W16>
| [ <identifier> ] DS <string>
*/
case ORG:
objectBytes = 0;
GetNextToken(&token,lexeme);
if ( token == INTEGER )
oldLC = LC = ATOI16(lexeme);
else
{
RecordSyntaxError("ORG statement <I16> operand missing");
oldLC = LC = 0X0000;
}
GetNextToken(&token,lexeme);
break;
case EQU:
if ( !lineIsLabeled )
RecordSyntaxError("EQU statement must be labeled");
objectBytes = 0;
GetNextToken(&token,lexeme);
if ( token == ASTERISK )
GetNextToken(&token,lexeme);
else
ParseW16(&token,lexeme);
break;
case RW:
GetNextToken(&token,lexeme);
objectBytes = 0;
if ( token == INTEGER )
{
LC += 2*ATOI16(lexeme);
GetNextToken(&token,lexeme);
}
else if ( token == EOLTOKEN )
LC += 2;
else
{
RecordSyntaxError("Invalid RW statement <I16> operand");
LC += 2;
GetNextToken(&token,lexeme);
}
break;
case DW: // one word of object
{
WORD W16;
objectBytes = 2;
GetNextToken(&token,lexeme);
W16 = ParseW16(&token,lexeme);
objectCode[1] = HIBYTE(W16);
objectCode[2] = LOBYTE(W16);
}
break;
case DS:
GetNextToken(&token,lexeme);
if ( token == STRING )
{
objectBytes = 0;
// length
objectCode[++objectBytes] = HIBYTE((WORD) strlen(lexeme));
objectCode[++objectBytes] = LOBYTE((WORD) strlen(lexeme));
// capacity
objectCode[++objectBytes] = HIBYTE((WORD) strlen(lexeme));
objectCode[++objectBytes] = LOBYTE((WORD) strlen(lexeme));
for (i = 0; i <= (int) strlen(lexeme)-1; i++)
{
objectCode[++objectBytes] = 0X00u;
objectCode[++objectBytes] = (BYTE) lexeme[i];
}
}
else
{
objectBytes = 2;
RecordSyntaxError("DS statement operand must be string");
objectCode[1] = 0X00u;
objectCode[2] = 0X00u;
}
GetNextToken(&token,lexeme);
break;
/*
<instruction> ::= [ <identifier> ] <HWMnemonic> [ <operand> ]
<operand> ::= <memory>
| #<W16>
| <A16>
<memory> ::= #<W16> || mode = 0X00, immediate
| <A16> || mode = 0X01, memory direct
| @<A16> || mode = 0X02, memory indirect
| $<A16> || mode = 0X03, memory indexed
| SP:<I16> || mode = 0X04, SP-relative direct
| @SP:<I16> || mode = 0X05, SP-relative indirect
| $SP:<I16> || mode = 0X06, SP-relative indexed
| FB:<I16> || mode = 0X07, FB-relative direct
| @FB:<I16> || mode = 0X08, FB-relative indirect
| $FB:<I16> || mode = 0X09, FB-relative indexed
| SB:<I16> || mode = 0X0A, SB-relative direct
| @SB:<I16> || mode = 0X0B, SB-relative indirect
| $SB:<I16> || mode = 0X0C, SB-relative indexed
<W16> ::= (( <I16> | <F16> | true | false | <character> | <identifier> ))
<A16> ::= <identifier>
<identifier> ::= <letter> { (( <letter> | <dit> | _ )) }*
*/
default:
{
// find hardware token in HWOperationTable
TOKENTYPE operation = token;
bool found = false;
int i = 0;
while ( (i <= (sizeof(HWOperationTable)/sizeof(HWOPERATIONRECORD))-1) && !found )
if ( operation != HWOperationTable[i].token )
i++;
else
found = true;
if ( !found )
{
RecordSyntaxError("Invalid hardware mnemonic");
LC += 1;
}
else
{
objectCode[1] = HWOperationTable[i].opCode;
switch ( HWOperationTable[i].operandType )
{
case NONE:
objectBytes = 1;
GetNextToken(&token,lexeme);
break;
case MEMORY:
GetNextToken(&token,lexeme);
switch ( token )
{
case POUND:
{
WORD W16;
if ( (operation == POP) || (operation == PUSHA) )
RecordSyntaxError("POP and PUSHA cannot have an immediate operand");
objectCode[2] = 0;
GetNextToken(&token,lexeme);
W16 = ParseW16(&token,lexeme);
objectCode[3] = HIBYTE(W16);
objectCode[4] = LOBYTE(W16);
}
break;
case IDENTIFIER:
{
WORD A16;
objectCode[2] = 1;
A16 = ParseA16(&token,lexeme);
objectCode[3] = HIBYTE(A16);
objectCode[4] = LOBYTE(A16);
}
break;
case ATSIGN:
{
WORD A16;
GetNextToken(&token,lexeme);
if ( token == IDENTIFIER )
{
objectCode[2] = 2;
A16 = ParseA16(&token,lexeme);
objectCode[3] = HIBYTE(A16);
objectCode[4] = LOBYTE(A16);
}
else
{
TOKENTYPE R = token;
if ( !((R == SP) || (R == FB) || (R == SB)) )
{
RecordSyntaxError("Expecting register SP, FB, or SB");
R = SP;
}
GetNextToken(&token,lexeme);
if ( token != COLON )
RecordSyntaxError("Expecting :");
GetNextToken(&token,lexeme);
if ( token != INTEGER )
{
RecordSyntaxError("Expecting <I16>");
objectCode[2] = 5;
objectCode[3] = 0X00u;
objectCode[4] = 0X00u;
}
else
{
switch ( R )
{
case SP:
objectCode[2] = 5;
break;
case FB:
objectCode[2] = 8;
break;
case SB:
objectCode[2] = 11;
break;
}
objectCode[3] = HIBYTE(ATOI16(lexeme));
objectCode[4] = LOBYTE(ATOI16(lexeme));
}
GetNextToken(&token,lexeme);
}
}
break;
case DOLLAR:
{
GetNextToken(&token,lexeme);
if ( token == IDENTIFIER )
{
WORD A16;
objectCode[2] = 3;
A16 = ParseA16(&token,lexeme);
objectCode[3] = HIBYTE(A16);
objectCode[4] = LOBYTE(A16);