forked from privat/pep8term
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasem8.cpp
More file actions
4652 lines (4437 loc) · 159 KB
/
asem8.cpp
File metadata and controls
4652 lines (4437 loc) · 159 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
// File: asem8.cpp
// Assembler for the Pep/8 computer as described in "Computer Systems",
// Fourth edition, J. Stanley Warford, Jones and Bartlett, Publishers,
// 2010. ISBN 978-0-7637-7144-7
// Pepperdine University, Malibu, CA 90265
// Stan.Warford@pepperdine.edu
// Released under the GNU General Public License without warrenty
// as described in http://www.gnu.org/copyleft/gpl.html
// Version history:
/*
Written by Luciano d'Ilori.
Version 8.17
Fixed a bug in the translation of hex constants and eliminated the warnings
due to lack of default cases in switch statements and lack of possible return
values in nonvoid functions.
Warford, 8 February 2015
Version 8.16
Increased LINE_LENGTH from 128 to 1024 to allow longer lines in the source
file, and CODE_MAX_SIZE from 4096 to 32768 to allow longer object programs
to be assembled.
Warford, 14 February 2010
Version 8.15
Fixed a bug in vGetToken to allow underscore characters _ in identifiers.
Warford, 31 March 2009
Version 8.14
Fixed a bug in vDecToHexWord for decimal values greater than MAX_DEC.
Warford, 31 December 2005
Version 8.13
Comments too long are now truncated instead of causing fatal error.
Warford, 04 February 2005
Version 8.12
Replaced depricated libraries. Added namespace std.
Warford, 01 Februrary 2005
Version 8.11
Now allowing leading zeros in decimal constants.
Version 8.1
Added functionality so user can define certain mnemonics.
Modified assembler syntax to allow certain non-printable characters.
Updated instruction set.
Version 8.04
Fixed indexed addressing bug.
<,x> now adds 1 or 5 to opcode depending on instruction
Version 8.03
Allows .EQUATE with char and string constants.
Has more detailed error messages.
*/
/*Constants are in capital letters.
Function references in comments do not list any possible parameters the functions may have.
Most functions begin with the lowercase first letter as that of the type they return.
Most variables begin with the lowercase first letter as that of their type (ex. int iDec).
For the most part, numbers other than 0 and 1 are either constants, array indices,
or are given explanations.*/
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <ctype.h>
#include <string>
#include <string.h>
#include <libgen.h>
using namespace std;
/*Constants*/
const int IDENT_LENGTH=8; /*Maximum identifier length*/
const int HEX_LENGTH=4; /*Length of a hexadecimal string*/
const int BYTE_LENGTH=2; /*Length of byte is 2 hex digits*/
const int WORD_LENGTH=4; /*Length of word is 4 hex digits*/
const int CHAR_LENGTH=4; /*Can be \x with 2 hex digits*/
const int DEC_LENGTH=6; /*-32768(6 characters) to 65535*/
const int COMMENT_LENGTH=65; /*Maximum comment length for empty lines*/
const int COMMENT_LENGTH_NONEMPTY=35; /*Maximum comment length for nonempty lines*/
const int COMMENT_LENGTH_NONEMPTY_NO_SYMBOLS=44; /*Maximum comment length for nonempty lines*/
const int STRING_LENGTH=96; /*Maximum string length*/
const int STRING_OPRND_LENGTH=4; /*string operands can be up to 4 hex digits long*/
const int ADDR_MODE_LENGTH=3; /*Maximum length of addr. mode (eg. sxf)*/
const int MAX_LINES=4096; /*pACode[MAX_LINES], pAMnemon[MAX_LINES]*/
const int BYTE=1; /*A byte is 1 byte*/
const int WORD=2; /*A word is 2 bytes*/
const int UNARY=1; /*A unary instruction takes up 1 byte*/
const int NONUNARY=3; /*A nonunary instruction takes up 3 bytes*/
const int OBJ_FILE_LINE_LENGTH=16; /*Number of bytes per line in object(hex) file*/
const int OBJ_CODE_LENGTH=6; /*Number of characters per line for object code in asem listing*/
const int IMMEDIATE=1;/*2^0. Powers of two to represent addressing mode bitset*/
const int DIRECT=2;/*2^1*/
const int INDIRECT=4;/*2^2*/
const int STACK_RELATIVE=8;/*2^3*/
const int STACK_RELATIVE_DEFERRED=16;/*2^4*/
const int INDEXED=32;/*2^5*/
const int STACK_INDEXED=64;/*2^6*/
const int STACK_INDEXED_DEFERRED=128;/*2^7*/
const int HEX=16; /*vGenerateHexCode() and vDecToHexWord()*/
const int HEX2=256; /*vDecToHexWord() for value of second most significant bit in hex*/
const int HEX3=4096; /*vDecToHexWord() for value of most significant bit in hex*/
const int OPERAND_SPACES=14; /*Number of charcter spaces in assembler listing for operands*/
const int ADDR_LENGTH=4; /*Length of addresses (2 bytes or 4 hex digits)*/
const int MAX_ADDR=65535; /*Maximum address location*/
const int MAX_BYTE=255; /*Maximum decimal value for a byte*/
const int MAX_DEC=65535; /*Maximum decimal value*/
const int MIN_BYTE=-256; /*Minimum decimal value for a byte*/
const int MIN_DEC=-32768; /*Minimum decimal value*/
const int LINE_LENGTH=1024; /*Maximum length of a line of code*/
const int CODE_MAX_SIZE=32768; /*Maximum number of bytes of code*/
const int FILE_NAME_LENGTH=255; /*255 characters maximum in a file name. Matches maximum file name length for ext[2-4] filesystems*/
const int FILE_PATH_LENGTH=4096; /*4096 characters maximum in a file's full path. Matches maximum length for ext[2-4] filesystems*/
const int UNIMPLEMENTED_INSTRUCTIONS = 8; /*Number of unimplemented mnemonics*/
const int UNARY_TRAPS = 4; /*Number of unimplemented mnemonics guaranteed to be unary*/
/*Enumerated Types*/
/*All possible mnemonics*/
enum Mnemon{
eM_STOP, eM_RETTR, eM_MOVSPA, eM_MOVFLGA, eM_BR, eM_BRLE, eM_BRLT, eM_BREQ,
eM_BRNE, eM_BRGE, eM_BRGT, eM_BRV, eM_BRC, eM_CALL, eM_NOTA, eM_NOTX, eM_NEGA,
eM_NEGX, eM_ASLA, eM_ASLX, eM_ASRA, eM_ASRX, eM_ROLA, eM_ROLX, eM_RORA,
eM_RORX, eM_CHARI, eM_CHARO, eM_RET0, eM_RET1, eM_RET2, eM_RET3, eM_RET4,
eM_RET5, eM_RET6, eM_RET7, eM_ADDSP, eM_SUBSP, eM_ADDA, eM_ADDX, eM_SUBA,
eM_SUBX, eM_ANDA, eM_ANDX, eM_ORA, eM_ORX, eM_CPA, eM_CPX, eM_LDA, eM_LDX,
eM_LDBYTEA, eM_LDBYTEX, eM_STA, eM_STX, eM_STBYTEA, eM_STBYTEX, eM_UNIMP0,
eM_UNIMP1, eM_UNIMP2, eM_UNIMP3, eM_UNIMP4, eM_UNIMP5, eM_UNIMP6, eM_UNIMP7, eM_EMPTY
};
/*All possible dot commands*/
enum DotCommand{
eD_BLOCK, eD_ADDRSS, eD_ASCII, eD_BURN, eD_BYTE, eD_EQUATE, eD_WORD, eD_END,
eD_EMPTY
};
/*Tokens found by vGetToken()*/
enum Key{
eT_ADDRMODE, eT_CHARCONSTANT, eT_COMMENT, eT_DECCONSTANT, eT_DOTCOMMAND,
eT_EMPTY, eT_HEXCONSTANT, eT_IDENTIFIER, eT_STRING, eT_SYMBOL, eT_INVALID,
eT_INVALIDADDR, eT_INVALIDCHAR, eT_INVALIDCOMMENT, eT_INVALIDDEC, eT_INVALIDDOTCOMMAND,
eT_INVALIDHEX, eT_INVALIDSTRING
};
/*States for finite state machine of vGetToken()*/
enum State{
eS_START, eS_ADDR, eS_ADDRs, eS_ADDRsx, eS_CHAR1, eS_CHAR2, eS_CHARBASH,
eS_CHARBYTE, eS_COMMENT, eS_DEC, eS_DOT1, eS_DOT2, eS_HEX1, eS_HEX2,
eS_IDENT, eS_SIGN, eS_STRING, eS_STRINGBASH, eS_STRINGBYTE, eS_STOP
};
/*States for finite state machine of vProcessSourceLine()*/
enum ParseState{
ePS_START, ePS_COMMENT, ePS_SYMBOLDEC, ePS_INSTRUCTION, ePS_OPRNDSPECDEC,
ePS_OPRNDSPECHEX, ePS_OPRNDSPECCHAR, ePS_OPRNDSPECSTRING, ePS_OPRNDSPECSYM,
ePS_DOTCOMMAND, ePS_ASCII, ePS_EQUATE, ePS_CLOSE, ePS_FINISH
};
/*Global Records*/
/*Contains .EQUATE symbols to be used in a linked list*/
struct sEquateNode{
char cSymValue[ADDR_LENGTH + 1]; /*Value of symbol*/
char cSymID[IDENT_LENGTH + 1]; /*Symbol identification*/
sEquateNode* pNext;
};
/*Record for symbol declarations*/
struct sSymbolNode{
char cSymValue[ADDR_LENGTH + 1]; /*Value of symbol*/
int iLine;
char cSymID[IDENT_LENGTH + 1]; /*Symbol name*/
sSymbolNode* pNext; /*Pointer to next sSymbolNode in the linked list*/
};
/*Record for symbol output declarations*/
struct sSymbolOutputNode{
int iLine;
char cSymID[IDENT_LENGTH + 1]; /*Symbol name*/
sSymbolOutputNode* pNext; /*Pointer to next sSymbolOutputNode in the linked list*/
};
/*Record for symbol declarations*/
struct sUndeclaredsSymbolNode{
char cSymValue[ADDR_LENGTH + 1]; /*Value of symbol*/
int iLine;
char cSymID[IDENT_LENGTH + 1]; /*Symbol identification*/
sUndeclaredsSymbolNode* pNext; /*Pointer to next sSymbolNode in the linked list*/
};
/*Contains information about comments to be used in a linked list*/
struct sCommentNode{
int iLine;
bool bNonemptyLine;
char cComment[COMMENT_LENGTH + 1];
sCommentNode* pNext;
};
/*Contains information about user-defined instructions*/
struct sUnimplementedMnemonNode{
char cID[IDENT_LENGTH + 1]; //Name of unimplemented opcode
int iAddrMode;
};
/*Global Variables (part 1)*/
ifstream in_file;
ofstream out_file;
char cLine[LINE_LENGTH]; /*Array of characters for a line of code*/
int iLineIndex; /*Index of line array*/
int iSecPassCodeIndex=0; /*Used in second pass of assembly to account for symbols*/
int iCurrentAddress=0; /*Keeps track of the current address*/
sSymbolNode* pSymbol; /*Pointer to linked list of sSymbolNodes*/
sSymbolOutputNode* pSymbolOutput; /*Pointer to linked list of sSymbolNodes for output*/
sUndeclaredsSymbolNode* pUndeclaredSym; /*Pointer to linked list of sUndeclaredsSymbolNodes*/
sCommentNode* pComment=NULL; /*Pointer to first sCommentNode of the comment linked list*/
sEquateNode* pEquate=NULL; /*Pointer to first sEquateNode of the .EQUATE linked list*/
char cDotTable[eD_EMPTY + 1][IDENT_LENGTH + 1]; /*Used for vLookUpDot()*/
char cMnemonTable [eM_EMPTY + 1][IDENT_LENGTH + 1]; /*Used for vLookUpMnemon()*/
int iHexOutputBuffer=0; /*Used for object code output for 16 bytes per line*/
bool bIsAscii=false; /*Keeps track of whether previous token was .ASCII pseudo-op*/
int iBurnStart=0; /*Used first to store the value of the operand of a .BURN*/
/*and then to store the value of where the first byte of code should be written.*/
int iBurnAddr=0; /*Used to store the address of a .BURN line*/
int iBurnCounter=0; /*Keeps track of number of .BURNs used in the program.*/
sUnimplementedMnemonNode sUnimpMnemon [UNIMPLEMENTED_INSTRUCTIONS]; /*Array of unimplemented mnemon nodes*/
/* Global variables continued after class ACode declaration*/
/*Utility functions*/
bool bIsHex(char& ch){
if (('a'<=ch) && (ch<='f')){
ch=toupper(ch);
return true;
}
else if ((isdigit(ch)) || (('A'<=ch) && (ch<='F')))
return true;
else
return false;
}
/*Stores the next line of assembly language code to be translated in global cLine[].*/
void vGetLine(){
in_file.getline(cLine, LINE_LENGTH);
if ((!in_file.eof ()) && (in_file.gcount()>0))
cLine[in_file.gcount() - 1]='\n';
else{
cLine[in_file.gcount()]='\n';
}
iLineIndex=0;
}
/*Gets the next character to be processed by vGetToken().*/
void vAdvanceInput (char& ch){
ch=cLine[iLineIndex++];
}
/*Backs up the input to the current character to be processed by vGetToken().*/
void vBackUpInput (){
iLineIndex--;
}
/*Outputs the version number of the Pep/8 assembler*/
void vVersionNumber (){
cerr << "Pep/8 Assembler, version Unix 8.17" << endl;
}
/*Converts a hexadecimal number to a decimal number and returns the decimal number.*/
int iHexToDec (char ch){
switch (ch){
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
case 'A': return 10; break;
case 'B': return 11; break;
case 'C': return 12; break;
case 'D': return 13; break;
case 'E': return 14; break;
case 'F': return 15; break;
default: return -1; // Should not occur
}
}
/*Converts a decimal number(0-15) to a hexadecimal number(0-F) and returns the hex number.*/
char cDecToHex (int i){
switch (i){
case 0: return '0'; break;
case 1: return '1'; break;
case 2: return '2'; break;
case 3: return '3'; break;
case 4: return '4'; break;
case 5: return '5'; break;
case 6: return '6'; break;
case 7: return '7'; break;
case 8: return '8'; break;
case 9: return '9'; break;
case 10: return 'A'; break;
case 11: return 'B'; break;
case 12: return 'C'; break;
case 13: return 'D'; break;
case 14: return 'E'; break;
case 15: return 'F'; break;
default: return ' '; // Should not occur
}
}
/*Converts a hexadecimal byte to a positive decimal integer*/
//int iHexByteToDecInt (char cHex[HEX_LENGTH + 1]){
// return HEX * iHexToDec(cHex[0]) + iHexToDec(cHex[1]);
//}
/*Converts a hexadecimal word to a positive decimal integer*/
int iHexWordToDecInt (char cHex[HEX_LENGTH + 1]){
return HEX3 * iHexToDec(cHex[0]) + HEX2 * iHexToDec(cHex[1]) + HEX * iHexToDec(cHex[2]) + iHexToDec(cHex[3]);
}
/*Converts a decimal value between -256 to 255 to a hexadecimal array of characters*/
void vDecToHexByte (int iDec, char cHex[BYTE_LENGTH + 1]){
if (iDec<0){
iDec=iDec + MAX_BYTE + 1;
}
cHex[0]=cDecToHex(iDec / HEX);
cHex[1]=cDecToHex(iDec % HEX);
cHex[2]='\0';
}
/*Converts a decimal value between -32768 and 65535 to a hexadecimal array of characters*/
void vDecToHexWord (int iDec, char cHex[ADDR_LENGTH + 1]){
int iFirstInt;
int iSecondInt;
int iThirdInt;
int iFourthInt;
if (iDec<0){
iDec=iDec + MAX_DEC + 1;
}
if (iDec>MAX_DEC){
iDec=iDec - (MAX_DEC + 1);
}
iFirstInt=iDec / HEX3;
iSecondInt=(iDec - HEX3 * iFirstInt) / HEX2;
iThirdInt=(iDec - HEX3 * iFirstInt - iSecondInt * HEX2) / HEX;
iFourthInt=(iDec - HEX3 * iFirstInt - iSecondInt * HEX2 - iThirdInt * HEX);
cHex[0]=cDecToHex(iFirstInt);
cHex[1]=cDecToHex(iSecondInt);
cHex[2]=cDecToHex(iThirdInt);
cHex[3]=cDecToHex(iFourthInt);
cHex[4]='\0';
}
/*Converts an array of char (decimal constant) to int using FSM implementation.*/
int iCharToInt (char ch []){
enum CIState {eCIS_START, eCIS_iSign, eCIS_INTEGER};
CIState state=eCIS_START;
int iSign;
int intValue;
int i=0;
while (ch[i]!='\0'){
switch (state) {
case eCIS_START:
if (isdigit (ch[i])){
intValue=ch[i] - '0';
iSign=1;
state= eCIS_INTEGER;
}
else if (ch[i] == '-'){
iSign=-1;
state=eCIS_iSign;
}
else if (ch[i] == '+'){
iSign=1;
state=eCIS_iSign;
}
break;
case eCIS_iSign:
if (isdigit(ch[i])){
intValue=ch[i] - '0';
state=eCIS_INTEGER;
}
break;
case eCIS_INTEGER:
if (isdigit (ch[i])){
intValue=10 * intValue + ch[i] - '0';
}
break;
};
i++;
}
return iSign * intValue;
}
/*Converts an addressing mode to its decimal equivalent.*/
int iAddrModeValue (char addrMode[], bool noAddrModeRequired){
if ((addrMode[0]=='i') || (addrMode[0]=='\0')){
return 0;
}
else if (addrMode[0]=='d'){
return 1;
}
else if (addrMode[0]=='n'){
return 2;
}
else if (addrMode[0]=='x'){
if (noAddrModeRequired)/*Branch instructions use a different bit than other instructions to indicate indexed addressing.*/
return 1;
else
return 5;
}
else if (addrMode[0]=='s'){
if (addrMode[1]=='\0'){
return 3;
}
else if (addrMode[1]=='f'){
return 4;
}
else if (addrMode[1]=='x'){
if (addrMode[2]=='\0'){
return 6;
}
else if (addrMode[2]=='f'){
return 7;
}
}
}
return -1; // Should not occur
}
/*True when cAddrMode is a valid addressing mode for a particular instruction.*/
bool bSearchAddrModes (char cAddrMode[], int iAddrMode){
if (iAddrMode==255)
return true;
else if (iAddrMode==0)
return false;
else if (cAddrMode[0]=='i')
return ((iAddrMode & IMMEDIATE) != 0);
else if (cAddrMode[0]=='d')
return ((iAddrMode & DIRECT) != 0);
else if (cAddrMode[0]=='n')
return ((iAddrMode & INDIRECT) != 0);
else if (cAddrMode[0]=='x')
return ((iAddrMode & INDEXED) != 0);
else if (cAddrMode[0]=='s'){
if (cAddrMode[1]=='x'){
if (cAddrMode[2]=='f')
return ((iAddrMode & STACK_INDEXED_DEFERRED) != 0);
else
return ((iAddrMode & STACK_INDEXED) != 0);
}
else if (cAddrMode[1]=='f')
return ((iAddrMode & STACK_RELATIVE_DEFERRED) != 0);
else
return ((iAddrMode & STACK_RELATIVE) != 0);
}
return false; // Should not occur
}
/*Gives cValue[] the cValue of the symbol named in cID[]*/
/*assertion: symbol has been defined*/
void vGetSymbolValue(char cID[], char cValue[]){
sSymbolNode* pTemp=pSymbol;
while (pTemp!=NULL){
if (strcmp(cID, pTemp->cSymID) == 0){
strncpy(cValue, pTemp->cSymValue, HEX_LENGTH + 1);
return;
}
pTemp=pTemp->pNext;
}
}
/*Continues the output following the first line of object code in the assembler*/
/*listing when a .BLOCK command occurs when more than 3 bytes are reserved.*/
void vDotBlockOutputContinued (int iDec){
int iLineCounter=0;
int i;
out_file << endl << " ";/*6 spaces*/
for (i=0; i<iDec; i++){
if (iLineCounter == OBJ_CODE_LENGTH){
out_file << " " << endl << " ";/*6 spaces*/
iLineCounter=0;
}
out_file << "00";
iLineCounter+=2;
}
for (i=iLineCounter; i<=OBJ_CODE_LENGTH; i++){
out_file << " ";
}
}
/*Continues the output following the first line of object code in the assembler*/
/*listing when a .ASCII command occurs when more than 3 bytes are reserved.*/
void vDotAsciiOutputContinued (char cStr[], const int objLength){
int i=OBJ_CODE_LENGTH; /*First 6 have already been output in vGenerateHexCode()*/
int iLineCounter=0;
while (i< objLength){
if (iLineCounter>=OBJ_CODE_LENGTH){
out_file << " " << endl << " ";/*6 spaces*/
iLineCounter=0;
}
out_file << cStr[i++];
out_file << cStr[i++];
iLineCounter+=2; /*A character in ascii takes 2 hex digits*/
}
for (i=iLineCounter; i<=OBJ_CODE_LENGTH; i++){
out_file << " ";
}
}
/*Gets a line from the trap file*/
void vGetTrapLine (int iLine){
char ch;
int i = 0;
bool bNoPrevI=true;
bool bNoPrevD=true;
bool bNoPrevN=true;
bool bNoPrevS=true;
bool bNoPrevSF=true;
bool bNoPrevX=true;
bool bNoPrevSX=true;
bool bNoPrevSXF=true;
sUnimpMnemon[iLine].iAddrMode=0;
vGetLine();
vAdvanceInput(ch);
while (i<IDENT_LENGTH && !isspace(ch)){
sUnimpMnemon[iLine].cID[i++] = toupper(ch);
vAdvanceInput(ch);
}
sUnimpMnemon[iLine].cID[i]='\0';
while (!isspace(ch)){
vAdvanceInput(ch);
}
while (isspace(ch) && ch!='\n'){
vAdvanceInput(ch);
}
if (iLine>=UNARY_TRAPS && ch!='\n'){
do{ /* while ch != '\n' */
if(toupper(ch)=='I' && bNoPrevI){
bNoPrevI=false;
sUnimpMnemon[iLine].iAddrMode+=IMMEDIATE;
vAdvanceInput(ch);
}
else if(toupper(ch)=='D' && bNoPrevD){
bNoPrevD=false;
sUnimpMnemon[iLine].iAddrMode+=DIRECT;
vAdvanceInput(ch);
}
else if(toupper(ch)=='N' && bNoPrevN){
bNoPrevN=false;
sUnimpMnemon[iLine].iAddrMode+=INDIRECT;
vAdvanceInput(ch);
}
else if(toupper(ch)=='X' && bNoPrevX){
bNoPrevX=false;
sUnimpMnemon[iLine].iAddrMode+=INDEXED;
vAdvanceInput(ch);
}
else if(toupper(ch)=='S'){
vAdvanceInput(ch);
if(toupper(ch)=='X'){
vAdvanceInput(ch);
if(toupper(ch)=='F' && bNoPrevSXF){
bNoPrevSXF=false;
sUnimpMnemon[iLine].iAddrMode+=STACK_INDEXED_DEFERRED;
vAdvanceInput(ch);
}
else if(bNoPrevSX){
bNoPrevSX=false;
sUnimpMnemon[iLine].iAddrMode+=STACK_INDEXED;
}
}
else if(toupper(ch)=='F' && bNoPrevSF){
bNoPrevSF=false;
sUnimpMnemon[iLine].iAddrMode+=STACK_RELATIVE_DEFERRED;
vAdvanceInput(ch);
}
else if(bNoPrevS){
bNoPrevS=false;
sUnimpMnemon[iLine].iAddrMode+=STACK_RELATIVE;
}
}
while(!isspace(ch)){
vAdvanceInput(ch);
}
while(isspace(ch) && ch!='\n'){
vAdvanceInput(ch);
}
}while (ch !='\n');
}
}
/*Buffers for assembler listing*/
/*Buffer for spaces in symbol column in assembler listing*/
void vSymbolBuffer (char cSym[]){
out_file << ":";
int i=0;
while (cSym[i]!='\0'){
i++;
}
for (int j=i; j<IDENT_LENGTH; j++){
out_file << " ";
}
}
/*Buffer for spaces in symbol column in symbol table*/
void vSymbolListingBuffer (char cSym[]){
int i=0;
while (cSym[i]!='\0'){
i++;
}
for (int j=i; j<=IDENT_LENGTH; j++){
out_file << " ";
}
}
/*Buffer for spaces in mnemon column in assembler listing for cDot commands*/
void vDotCommandBuffer (char cDot[]){
int i=0;
while (cDot[i]!='\0'){
i++;
}
for (int j=i; j<IDENT_LENGTH - 1; j++){
out_file << " ";
}
}
/*Buffer for spaces in mnemon column in assembler listing for cMnemon*/
void vMnemonBuffer (char cMnemon[]){
int i=0;
while (cMnemon[i]!='\0'){
i++;
}
for (int j=i; j<IDENT_LENGTH; j++){
out_file << " ";
}
}
/*Buffer for spaces in operand column in assembler listing to work with*/
/*operands and addr. modes of various lengths.*/
void vOperandBuffer(char cOperand[], char cAddrMode[], bool bDecSym){
int iTemp=OPERAND_SPACES;
if (!bDecSym)
iTemp-=2;/*0x is 2 spaces*/
if (cAddrMode[0]!='\0')
iTemp--; /*comma is one space*/
int i=0;
while (cOperand[i++]!='\0'){
iTemp--;
}
i=0;
while (cAddrMode[i++]!='\0'){
iTemp--;
}
for (int j=iTemp; j>0; j--){
out_file << " ";
}
}
/*Buffer for spaces in operand column in assembler listing for*/
/*operands with no addr. mode*/
void vOperandBuffer(char cOperand[], bool bDecSym){
int iTemp=OPERAND_SPACES;
if (!bDecSym)
iTemp-=2;
int i=0;
while (cOperand[i++]!='\0'){
iTemp--;
}
for (int j=iTemp; j>0; j--){
out_file << " ";
}
}
/*Outputs spaces for a blank address column in the assembler listing*/
void vBlankAddressColumn (){
out_file << " ";/*6 spaces*/
}
/*Outputs spaces for a blank object code column in the assembler listing*/
void vBlankObjCodeColumn (){
out_file << " ";/*7 spaces*/
}
/*Outputs spaces for a blank symbol column in the assembler listing*/
void vBlankSymbolColumn (){
out_file << " ";/*8 spaces*/
}
void vOutputSymbolDecs (){
if (pSymbol!=NULL){
if ((pSymbolOutput!=NULL) && (pSymbolOutput->iLine == iSecPassCodeIndex)){
out_file << pSymbolOutput->cSymID;
vSymbolBuffer (pSymbolOutput->cSymID);
pSymbolOutput=pSymbolOutput->pNext;
}
else
vBlankSymbolColumn ();
}
}
/*Buffer for the object file for the loader*/
void vHexOutputBufferLoader (){
if (iHexOutputBuffer == OBJ_FILE_LINE_LENGTH - 1){
out_file << endl;
iHexOutputBuffer=0;
}
else{
out_file << " ";
iHexOutputBuffer++;
}
}
/*Abstract Token Class*/
class AToken {
public:
virtual Key kTokenType ()=0;
virtual ~AToken() { };
};
class TAddress : public AToken{
private:
char cAddrValue[ADDR_MODE_LENGTH + 1];
public:
TAddress (char str[]) { strncpy (cAddrValue, str, ADDR_MODE_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cAddrValue, ADDR_MODE_LENGTH + 1); }
Key kTokenType () { return eT_ADDRMODE; }
};
class TCharConstant : public AToken{
private:
char cCharValue[CHAR_LENGTH + 1];
char cCharByteValue[BYTE_LENGTH +1];
public:
TCharConstant (char str[]) {
strncpy (cCharValue, str, CHAR_LENGTH + 1);
if (str[0]=='\\'){
if (str[1]=='x'){
cCharByteValue[0]=str[2];
cCharByteValue[1]=str[3];
cCharByteValue[2]='\0';
}
else if ((str[1]=='\'') || (str[1]=='\"') || (str[1]=='\\'))
vDecToHexByte(static_cast <int> (str[1]), cCharByteValue);
else if (str[1]=='b')
vDecToHexByte(static_cast <int> ('\b'), cCharByteValue);
else if (str[1]=='f')
vDecToHexByte(static_cast <int> ('\f'), cCharByteValue);
else if (str[1]=='n')
vDecToHexByte(static_cast <int> ('\n'), cCharByteValue);
else if (str[1]=='r')
vDecToHexByte(static_cast <int> ('\r'), cCharByteValue);
else if (str[1]=='t')
vDecToHexByte(static_cast <int> ('\t'), cCharByteValue);
else
vDecToHexByte(static_cast <int> ('\v'), cCharByteValue);
}
else
vDecToHexByte(static_cast <int> (str[0]), cCharByteValue);
}
void vGetValue (char str[]) { strncpy (str, cCharValue, CHAR_LENGTH + 1); }
void vGetByteValue (char str[]) { strncpy (str, cCharByteValue, BYTE_LENGTH + 1); }
int iLength() { return 1; }/*a char constant is 1 char long*/
Key kTokenType () { return eT_CHARCONSTANT; }
};
class TComment : public AToken{
private:
char cCommentValue[COMMENT_LENGTH + 1];
public:
TComment (char str[]) { strncpy (cCommentValue, str, COMMENT_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cCommentValue, COMMENT_LENGTH + 1); }
Key kTokenType () { return eT_COMMENT; }
};
class TDecConstant : public AToken{
private:
char cDecValue[DEC_LENGTH + 1];
public:
TDecConstant (char str[]) { strncpy (cDecValue, str, DEC_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cDecValue, DEC_LENGTH + 1); }
Key kTokenType () { return eT_DECCONSTANT; }
};
class TDotCommand : public AToken{
private:
char cDotValue[IDENT_LENGTH + 1];
public:
TDotCommand (char str[]) { strncpy (cDotValue, str, IDENT_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cDotValue, IDENT_LENGTH + 1); }
Key kTokenType () { return eT_DOTCOMMAND; }
};
class TEmpty : public AToken{
public:
Key kTokenType () { return eT_EMPTY; }
};
class THexConstant : public AToken{
private:
char cHexValue[HEX_LENGTH + 1];
public:
THexConstant (char str[]) { strncpy (cHexValue, str, HEX_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cHexValue, HEX_LENGTH + 1); }
Key kTokenType () { return eT_HEXCONSTANT; }
};
class TIdentifier : public AToken{
private:
char cIdentValue[IDENT_LENGTH + 1];
public:
TIdentifier (char str[]) { strncpy (cIdentValue, str, IDENT_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cIdentValue, IDENT_LENGTH + 1); }
Key kTokenType () { return eT_IDENTIFIER; }
};
class TString : public AToken{
private:
char cStringValue[STRING_LENGTH + 1];
char cStringByteValue[2 * STRING_LENGTH +1];/*each char is 2 hex digits*/
char cTempByteValue[BYTE_LENGTH + 1];
int i;/*string length*/
int j;/*object length (how many hex digits)*/
public:
TString (char str[]) {
strncpy (cStringValue, str, STRING_LENGTH + 1);
i=0; j=0;
do{
if (str[i] == '\\') {
int k=i+1;
if (str[k] == 'x') {
i+=2;
cStringByteValue[j++]=str[i++];
cStringByteValue[j++]=str[i++];
}
else if ((str[k] == '\'') || (str[k] == '\"') || (str[k] == '\\')){
vDecToHexByte(static_cast <int> (str[k]), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
else if (str[k] == 'b'){
vDecToHexByte(static_cast <int> ('\b'), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
else if (str[k] == 'f'){
vDecToHexByte(static_cast <int> ('\f'), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
else if (str[k] == 'n'){
vDecToHexByte(static_cast <int> ('\n'), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
else if (str[k] == 'r'){
vDecToHexByte(static_cast <int> ('\r'), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
else if (str[k] == 't'){
vDecToHexByte(static_cast <int> ('\t'), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
else{
vDecToHexByte(static_cast <int> ('\v'), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
i+=2;
}
}
else{
vDecToHexByte(static_cast <int> (str[i++]), cTempByteValue);
cStringByteValue[j++]=cTempByteValue[0];
cStringByteValue[j++]=cTempByteValue[1];
}
}
while (str[i]!='\0');
cStringByteValue[j]='\0';
}
void vGetValue (char str[]) { strncpy (str, cStringValue, STRING_LENGTH + 1); }
void vGetObjValue (char str[], const int length) {
strncpy (str, cStringByteValue, length);
str[length]='\0';
}
int iLength() { return i; }
int iObjLength() { return j; }
Key kTokenType () { return eT_STRING; }
};
class TSymbol : public AToken{
private:
char cSymbolValue[IDENT_LENGTH + 1];
public:
TSymbol (char str[]) { strncpy (cSymbolValue, str, IDENT_LENGTH + 1); }
void vGetValue (char str[]) { strncpy (str, cSymbolValue, IDENT_LENGTH + 1); }
Key kTokenType () { return eT_SYMBOL; }
};
class TInvalid : public AToken{
public:
Key kTokenType () { return eT_INVALID; }
};
class TInvalidAddr : public AToken{
public:
Key kTokenType () { return eT_INVALIDADDR; }
};
class TInvalidChar : public AToken{
public:
Key kTokenType () { return eT_INVALIDCHAR; }
};
class TInvalidComment : public AToken{
public:
Key kTokenType () { return eT_INVALIDCOMMENT; }
};
class TInvalidDec : public AToken{
public:
Key kTokenType () { return eT_INVALIDDEC; }
};
class TInvalidDotCommand : public AToken{
public:
Key kTokenType () { return eT_INVALIDDOTCOMMAND; }
};
class TInvalidHex : public AToken{
public:
Key kTokenType () { return eT_INVALIDHEX; }
};
class TInvalidString : public AToken{
public:
Key kTokenType () { return eT_INVALIDSTRING; }
};
/*Abstract Mnemonic Class*/
class AMnemon{
public:
virtual int iOpCode ()=0; /*Returns the integer operation code of a given mnemonic*/
virtual void vMnemonOutput ()=0; /*Outputs the mnemonic for the assembler listing*/
virtual bool bNoAddrModeRequired ()=0; /*For nonunary instructions, true when no addressing mode is required*/
virtual bool bValidAddrMode (char ch[])=0;/*True when given addressing mode is valid for the instruction*/
virtual bool bIsUnary ()=0;/*True when the instruction is unary*/
virtual ~AMnemon() { };
};
class UnaryOp : public AMnemon{
public:
bool bValidAddrMode (char ch[]) {return false; }
bool bIsUnary () {return true; }
bool bNoAddrModeRequired () {return false; }
};
class BranchOp : public AMnemon{
public:
bool bValidAddrMode (char ch[]) {return ((ch[0] == 'i') || (ch[0] == 'x')); }
bool bIsUnary () {return false; }
bool bNoAddrModeRequired () {return true; }
};
class GeneralOp : public AMnemon{
public:
bool bValidAddrMode (char ch[]) {return true; }
bool bIsUnary () {return false; }
bool bNoAddrModeRequired () {return false; }
};
class InputOp : public AMnemon{
public:
bool bValidAddrMode (char ch[]) {return (ch[0] != 'i'); }
bool bIsUnary () {return false; }
bool bNoAddrModeRequired () {return false; }
};
class Stop : public UnaryOp{
public:
int iOpCode () { return 0; }
void vMnemonOutput () { out_file << "STOP "; }
};
class Rettr : public UnaryOp{
public:
int iOpCode () { return 1; }
void vMnemonOutput () { out_file << "RETTR "; }
};
class Movspa : public UnaryOp{
public: