-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASSEMBLE.CPP
More file actions
1045 lines (926 loc) · 36.8 KB
/
Copy pathASSEMBLE.CPP
File metadata and controls
1045 lines (926 loc) · 36.8 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
/***********************************************************************
*
* ASSEMBLE.CPP
* Assembly Routines for 68000 Assembler
*
* Function: processFile()
* Assembles the input file. For each pass, the function
* passes each line of the input file to assemble() to be
* assembled. The routine also makes sure that errors are
* printed on the screen and listed in the listing file
* and keeps track of the error counts and the line
* number.
*
* assemble()
* Assembles one line of assembly code. The line argument
* points to the line to be assembled, and the errorPtr
* argument is used to return an error code via the
* standard mechanism. The routine first determines if the
* line contains a label and saves the label for later
* use. It then calls instLookup() to look up the
* instruction (or directive) in the instruction table. If
* this search is successful and the parseFlag for that
* instruction is TRUE, it defines the label and parses
* the source and destination operands of the instruction
* (if appropriate) and searches the flavor list for the
* instruction, calling the proper routine if a match is
* found. If parseFlag is FALSE, it passes pointers to the
* label and operands to the specified routine for
* processing.
*
* Usage: processFile()
*
* assemble(line, errorPtr)
* char *line;
* int *errorPtr;
*
* Author: Paul McKee
* ECE492 North Carolina State University
* Date: 12/13/86
Modified: Charles Kelly
Monroe County Community College
http://www.monroeccc.edu/ckelly
IFxx <string1>,<string2>
<statements>
ENDC
The condition xx is either C or NC. IFC means if compare. IFNC means
if not compare. If the condition is true the following statements
are included in the program.
Another syntax is:
IFxx <expression>
<statements>
ENDC
The condition xx is either: EQ (equal to), NE (not equal to),
LT (less than), LE (less than or equal to),
GT (greater than), GE (greater than or equal to)
The expression is compared with 0. If the condition is true the
following statements are included in the program.
IFARG n
<statements>
ENDC
If the argument number n exists the following statements in the macro
are included in the program.
ENDC ends the conditional section.
************************************************************************/
// include <vcl.h>
#include <stdio.h>
#include <ctype.h>
#include "asm.h"
// include "assembleS.h"
// include "mainS.h"
// include "textS.h"
// include "editorOptions.h"
#include <fcntl.h>
//#include <unistd.h>
extern int loc; // The assembler's location counter
extern int sectionLoc[16]; // section locations
extern int sectI; // current section
extern bool offsetMode; // True when processing Offset directive // was conflicting with extern int offsetMode!
extern bool showEqual; // true to display equal after address in listing
extern char pass; // pass counter
extern bool pass2; // Flag set during second pass
extern bool endFlag; // Flag set when the END directive is encountered
extern bool continuation; // TRUE if the listing line is a continuation
extern char empty[]; // used in conditional assembly
extern int lineNum;
extern int lineNumL68;
extern int errorCount, warningCount;
extern char line[256]; // Source line
extern FILE *inFile; // Input file
extern FILE *listFile; // Listing file
extern FILE *objFile; // Object file
extern FILE *errFile; // error message file
extern FILE *tmpFile; // temp file
extern int labelNum; // macro label \@ number
extern bool xrefFlag; // True if a cross-reference is desired
extern bool CEXflag; // True is Constants are to be EXpanded
extern bool BITflag; // True to assemble bitfield instructions
extern char lineIdent[]; // "mmm" used to identify macro in listing
//extern char arguments[MAX_ARGS][ARG_SIZE+1]; // macro arguments
extern bool CREflag, MEXflag, SEXflag; // assembler directive flags
extern bool noENDM; // set true if no ENDM in macro
extern int macroNestLevel; // count nested macro calls
extern char buffer[256]; //ck used to form messages for display in windows
extern char numBuf[20];
extern char globalLabel[SIGCHARS+1];
extern int includeNestLevel; // count nested include directives
extern char includeFile[256]; // name of current include file
extern bool includedFileError; // true if include error message displayed
extern unsigned int stcLabelI; // structured if label number
extern unsigned int stcLabelW; // structured while label number
extern unsigned int stcLabelR; // structured repeat label number
extern unsigned int stcLabelF; // structured for label number
extern unsigned int stcLabelD; // structured dbloop label number
bool skipList; // true to skip listing line
bool skipCond; // true conditionally skips lines
bool printCond; // true to print condition on listing line
bool skipCreateCode; // true to skip calling createCode during macro processing
const int MAXT = 128; // maximum number of tokens
const int MAX_SIZE = 512; // maximun size of input line
char *token[MAXT]; // pointers to tokens
char tokens[MAX_SIZE]; // place tokens here
char *tokenEnd[MAXT]; // where tokens end in source line
int nestLevel = 0; // nesting level of conditional directives
extern bool mapROM; // memory map flags
extern bool mapRead;
extern bool mapProtected;
extern bool mapInvalid;
extern bool isRelative;
// RA extern stack<int,vector<int> > stcStack;
extern std::stack<int> stcStack;
// RA extern stack<char, vector<char> > dbStack;
extern std::stack<char> dbStack;
// RA extern stack<String, vector<String> > forStack;
extern std::stack<string> forStack;
//--- added by RA --------------------------------------------
#ifndef ChangeFileExt
string ChangeFileExt(string in, const string& newExt) {
string s=in;
string::size_type i = s.rfind('.', s.length());
if (i != string::npos) {
s.replace(i, newExt.length(), newExt);
}
else {
s.append(newExt);
}
return s;
}
#endif
//--- added by github.com/dmo2118
void REMOVECR(char *line)
{
size_t len = strnlen(line, 256);
if (len >= 2) {
char *end = line + len;
if (end[-2] == '\r' && end[-1] == '\n') {
end[-2] = '\n';
end[-1] = 0;
}
}
}
//------------------------------------------------------------
//------------------------------------------------------------
// Assemble source file
int assembleFile(char fileName[], char tempName[], AnsiString workName)
{
AnsiString outName;
try {
tmpFile = fopen(tempName, "w+");
if (!tmpFile) {
sprintf(buffer,"Error creating temp file.");
// Application->MessageBox(buffer, "Error", MB_OK);
fprintf(stderr,"%s\n",buffer);
return SEVERE;
}
inFile = fopen(fileName, "r");
if (!inFile) {
// Application->MessageBox("Error reading source file.", "Error", MB_OK);
fprintf(stderr,"%s\n",buffer);
return SEVERE;
}
// if generate listing is checked then create .L68 file
// if (Options->chkGenList->Checked) {
if (listFlag) { // RA
outName = ChangeFileExt(workName, ".L68");
initList((char *)outName.c_str()); //RA // initialize list file
}
// if Object file flag then create .S68 file (S-Record)
if (objFlag) {
outName = ChangeFileExt(workName, ".S68");
if(initObj(outName.c_str()) != NORMAL) // RA // if error initializing object file
objFlag = false; // disable object file creation
}
// if binary file flag then create .bin file
if (objFlag) {
outName = ChangeFileExt(workName, ".bin");
if (initBin(outName.c_str()) != NORMAL) // RA // if error initializing binary file
binFlag = false; // disable object file creation
}
SetBasePathForFile(fileName);
// Assemble the file
processFile();
// flush any pending space at end of file (e.g. DS.B)
output(0, 0);
// Close files and print error and warning counts
fclose(inFile);
fclose(tmpFile);
finishList();
if (objFlag)
finishObj();
if (binFlag)
finishBin();
clearSymbols(); //ck clear symbol table memory
// clear stacks used in structured assembly
while(stcStack.empty() == false)
stcStack.pop();
while(dbStack.empty() == false)
dbStack.pop();
while(forStack.empty() == false)
forStack.pop();
// // minimize message area if no errors or warnings
// if (warningCount == 0 && errorCount == 0) {
// TTextStuff *Active = (TTextStuff*)Main->ActiveMDIChild; //grab active mdi child
// Active->Messages->Height = 7;
// }
// AssemblerBox->lblStatus->Caption = IntToStr(warningCount);
// AssemblerBox->lblStatus2->Caption = IntToStr(errorCount);
// if(errorCount == 0 && errorCount == 0)
// {
// AssemblerBox->cmdExecute->Enabled = true;
// }
}
catch( ... ) {
sprintf(buffer, "ERROR: An exception occurred in routine 'assembleFile'. \n");
printError(NULL, EXCEPTION, 0);
return 0; // RA
}
return NORMAL;
}
int strcap(char *d, char *s)
{
bool capFlag;
try {
capFlag = true;
while (*s) {
if (capFlag)
*d = toupper(*s);
else
*d = *s;
if (*s == '\'')
capFlag = !capFlag;
d++;
s++;
}
*d = '\0';
}
catch( ... ) {
sprintf(buffer, "ERROR: An exception occurred in routine 'strcap'. \n");
printError(NULL, EXCEPTION, 0);
return 0; // RA
}
return NORMAL;
}
char *skipSpace(char *p)
{
try {
while (isspace((unsigned char)*p))
p++;
return p;
}
catch( ... ) {
sprintf(buffer, "ERROR: An exception occurred in routine 'skipSpace'. \n");
printError(NULL, EXCEPTION, 0);
return NULL;
}
}
// continue assembly process by reading source file and sending each
// line to assemble()
// does 2 passes from here
int processFile()
{
int error;
try {
offsetMode = false; // clear flags
showEqual = false;
pass2 = false;
macroNestLevel = 0; // count nested macro calls
noENDM = false; // set to true if no ENDM in macro
includedFileError = false; // true if include error message displayed
mapROM = false; // memory map flags
mapRead = false;
mapProtected = false;
mapInvalid = false;
for (pass = 0; pass < 2; pass++) {
globalLabel[0] = '\0'; // for local labels
labelNum = 0; // macro label \@ number
// evalNumber() contains error code that depends on the range of these numbers
stcLabelI = 0x00000000; // structured if label number
stcLabelW = 0x10000000; // structured while label number
stcLabelF = 0x20000000; // structured for label number
stcLabelR = 0x30000000; // structured repeat label number
stcLabelD = 0x40000000; // structured dbloop label number
includeNestLevel = 0; // count nested include directives
includeFile[0] = '\0'; // name of current include file
loc = 0;
for (int i=0; i<16; i++) // clear section locations
sectionLoc[i] = 0;
sectI = 0; // current section
lineNum = 1;
lineNumL68 = 1;
endFlag = false;
isRelative = true;
errorCount = warningCount = 0;
skipCond = false; // true conditionally skips lines in code
while(!endFlag && fgets(line, 256, inFile)) {
// RA - not sure I still need this.
// Handle MSDOS/Win line endings by chomping the CR in the CRLF.
REMOVECR(line);
error = OK;
continuation = false;
skipList = false;
printCond = false; // true to print condition on listing line
skipCreateCode = false;
assemble(line, &error); // assemble one line of code
lineNum++;
}
if (!pass2) {
pass2 = true;
// ************************************************************
// ******************** STARTING PASS 2 *********************
// ************************************************************
} else { // pass2 just completed
if(!endFlag) { // if no END directive was found
error = END_MISSING;
warningCount++;
printError(listFile, error, lineNum);
}
}
rewind(inFile);
}
}
catch( ... ) {
sprintf(buffer, "ERROR: An exception occurred in routine 'processFile'. \n");
printError(NULL, EXCEPTION, 0);
return 0; // RA
}
return NORMAL;
}
// Conditionally Assemble one line of code
int assemble(char *line, int *errorPtr)
{
exprVal value;
value.value = 0;
bool backRef = false;
int error2Ptr = 0;
char capLine[256];
char *p;
bool comment; // true when line is comment
try {
//printf("loc: %d\n", loc);
if (pass2 && listFlag)
listLoc();
// RA don't to_upper if we see INCLUDE or INCBIN
#ifdef _MSC_VER
strcap(capLine, line);
#else
if ( NULL==strcasestr(line, "INCLUDE") && NULL==strcasestr(line, "INCBIN"))
strcap(capLine, line);
else
{
//fprintf(listFile,"found INCLUDE or INCLUDEBIN, copying line to: %s :::\n",line); // RA debug
strncpy(capLine, line, 255);
}
#endif
p = skipSpace(capLine); // skip leading white space
tokenize(capLine, (char *)", \t\n\r", token, tokens); // RA // tokenize line
if (*p == '*' || *p == ';') // if comment
comment = true;
else
comment = false;
if (comment) // if comment
if (pass2 && listFlag) {
listLine(line, lineIdent);
return NORMAL;
}
// conditional assembly for all code
// DEBUG //fprintf(listFile,"tokens 0 1 2 3 :::(%s):: :::(%s)::: :::(%s)::: :::(%s):::\n",token[0], token[1], token[2], token[3]); //RA debug
// ----- IFC -----
if(!(stricmp(token[1], "IFC"))) { // if IFC opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (stricmp(token[2], token[3])) { // If IFC strings don't match
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
printCond = true;
}
// ----- IFNC -----
} else if(!(stricmp(token[1], "IFNC"))) { // if IFNC opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[3] == empty) { // if IFNC arguments missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
if (!(stricmp(token[2], token[3]))) { // if IFNC strings match
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- IFEQ -----
} else if(!(stricmp(token[1], "IFEQ"))) { // if IFEQ opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[2] == empty) { // if argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
eval(token[2], &value, &backRef, &error2Ptr);
if (error2Ptr < ERRORN && value.value != 0) { // if not condition
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- IFNE -----
} else if(!(stricmp(token[1], "IFNE"))) { // if IFNE opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[2] == empty) { // if argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
eval(token[2], &value, &backRef, &error2Ptr);
if (error2Ptr < ERRORN && value.value == 0) { // if not condition
skipCond = true; // skip lines in macro
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- IFLT -----
} else if(!(stricmp(token[1], "IFLT"))) { // if IFLT opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[2] == empty) { // if argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
eval(token[2], &value, &backRef, &error2Ptr);
if (error2Ptr < ERRORN && value.value >= 0) { // if not condition
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- IFLE -----
} else if(!(stricmp(token[1], "IFLE"))) { // if IFLE opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[2] == empty) { // if argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
eval(token[2], &value, &backRef, &error2Ptr);
if (error2Ptr < ERRORN && value.value > 0) { // if not condition
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- IFGT -----
} else if(!(stricmp(token[1], "IFGT"))) { // if IFGT opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[2] == empty) { // if argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
eval(token[2], &value, &backRef, &error2Ptr);
if (error2Ptr < ERRORN && value.value <= 0) { // if not condition
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- IFGE -----
} else if(!(stricmp(token[1], "IFGE"))) { // if IFGE opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (skipCond)
nestLevel++; // nest level of skip
else {
if (token[2] == empty) { // if argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
eval(token[2], &value, &backRef, &error2Ptr);
if (error2Ptr < ERRORN && value.value < 0) { // if not condition
skipCond = true; // conditionally skip lines
nestLevel++; // nest level of skip
}
}
printCond = true;
}
// ----- ENDC -----
} else if(!(stricmp(token[1], "ENDC"))) { // if ENDC opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (nestLevel > 0)
nestLevel--; // decrease nesting level
if (nestLevel == 0) {
skipCond = false; // stop skipping lines
} else
printCond = false;
} else if (!skipCond && !skipCreateCode) { // else, if not skip condition and not skip create
createCode(capLine, errorPtr);
}
// display and list errors and source line
if (pass2) {
if (*errorPtr > MINOR)
errorCount++;
else if (*errorPtr > WARNING)
warningCount++;
printError(listFile, *errorPtr, lineNum);
if (printCond && !skipList)
{
listCond(skipCond);
listLine(line, lineIdent);
} else if ( (listFlag && (!skipCond && !skipList)) || *errorPtr > WARNING)
listLine(line, lineIdent);
}
}
catch( ... ) {
NEWERROR(*errorPtr, EXCEPTION);
sprintf(buffer, "ERROR: An exception occurred in routine 'assemble'. \n");
return 0; // RA
}
return NORMAL;
}
exprVal LocExpr()
{
exprVal expr;
expr.value = loc;
expr.isRelative = isRelative;
return expr;
}
// create machine code for instruction
int createCode(char *capLine, int *errorPtr) {
instruction *tablePtr;
flavor *flavorPtr;
opDescriptor source, dest;
char *p, *start, label[SIGCHARS+1], size, f;
bool sourceParsed, destParsed;
unsigned short mask, i;
p = start = skipSpace(capLine); // skip leading spaces and tabs
if (*p && *p != '*' && *p != ';') { // if line not empty and not comment
// if first char is not alpha . or _
if( !isalpha(*p) && *p != '.' && *p != '_')
NEWERROR(*errorPtr,ILLEGAL_SYMBOL);
// assume the line starts with a label
i = 0;
do {
if (i < SIGCHARS) // only first SIGCHARS of label are used
label[i++] = *p;
p++;
} while (isalnum(*p) || *p == '.' || *p == '_' || *p == '$');
label[i] = '\0'; // end label string with null
if (i >= SIGCHARS)
NEWERROR(*errorPtr, LABEL_TOO_LONG);
// if next character is space AND the label was at the start of the line
// OR the label ends with ':'
if ((isspace((unsigned char)*p) && start == capLine) || *p == ':') {
if (*p == ':') // if label ends with :
p++; // skip it
p = skipSpace(p); // skip trailing spaces
if (*p == '*' || *p == ';' || !*p) { // if the next char is '*' or ';' or end of line
define(label, LocExpr(), pass2, true, errorPtr); // add label to list of labels
return NORMAL;
}
} else {
p = start; // reset p to start of line
label[0] = '\0'; // clear label
}
p = instLookup(p, &tablePtr, &size, errorPtr);
if (*errorPtr > SEVERE)
return NORMAL;
p = skipSpace(p);
if (tablePtr->parseFlag) {
// Move location counter to a word boundary and fix
// the listing before assembling an instruction
if (loc & 1) {
loc++;
listLoc();
}
if (*label)
define(label, LocExpr(), pass2, true, errorPtr);
if (*errorPtr > SEVERE)
return NORMAL;
sourceParsed = destParsed = false;
flavorPtr = tablePtr->flavorPtr;
for (f = 0; (f < tablePtr->flavorCount); f++, flavorPtr++) {
if (!sourceParsed && flavorPtr->source) {
p = opParse(p, &source, errorPtr); // parse source
if (*errorPtr > SEVERE)
return NORMAL;
if (flavorPtr && flavorPtr->exec == bitField) { // if bitField instruction
p = skipSpace(p); // skip spaces after source operand
if (*p != ',') { // if not Dn,addr{offset:width}
p = fieldParse(p, &source, errorPtr); // parse {offset:width}
if (*errorPtr > SEVERE)
return NORMAL;
}
}
sourceParsed = true;
}
if (!destParsed && flavorPtr->dest) { // if destination needs parsing
p = skipSpace(p); // skip spaces after source operand
if (*p != ',') {
NEWERROR(*errorPtr, COMMA_EXPECTED);
return NORMAL;
}
p++; // skip over comma
p = skipSpace(p); // skip spaces before destination operand
p = opParse(p, &dest, errorPtr); // parse destination
if (*errorPtr > SEVERE)
return NORMAL;
if (flavorPtr && flavorPtr->exec == bitField &&
flavorPtr->source == DnDirect) // if bitField instruction Dn,addr{offset:width}
{
p = skipSpace(p); // skip spaces after destination operand
if (*p != '{') {
NEWERROR(*errorPtr, BAD_BITFIELD);
return NORMAL;
}
p = fieldParse(p, &dest, errorPtr);
if (*errorPtr > SEVERE)
return NORMAL;
}
if (!isspace((unsigned char)*p) && *p) { // if next character is not whitespace
NEWERROR(*errorPtr, SYNTAX);
return NORMAL;
}
destParsed = true;
}
if (!flavorPtr->source) {
mask = pickMask( (int) size, flavorPtr, errorPtr);
// The following line calls the function defined for the current
// instruction as a flavor in instTable[]
(*flavorPtr->exec)(mask, (int) size, &source, &dest, errorPtr);
return NORMAL;
}
else if ((source.mode & flavorPtr->source) && !flavorPtr->dest) {
if (*p!='{' && !isspace((unsigned char)*p) && *p) {
NEWERROR(*errorPtr, SYNTAX);
return NORMAL;
}
mask = pickMask( (int) size, flavorPtr, errorPtr);
// The following line calls the function defined for the current
// instruction as a flavor in instTable[]
(*flavorPtr->exec)(mask, (int) size, &source, &dest, errorPtr);
return NORMAL;
}
else if (source.mode & flavorPtr->source
&& dest.mode & flavorPtr->dest) {
mask = pickMask( (int) size, flavorPtr, errorPtr);
// The following line calls the function defined for the current
// instruction as a flavor in instTable[]
(*flavorPtr->exec)(mask, (int) size, &source, &dest, errorPtr);
return NORMAL;
}
}
NEWERROR(*errorPtr, INV_ADDR_MODE);
} else {
// The following line calls the function defined for the current
// instruction as a flavor in instTable[]
(*tablePtr->exec)( (int) size, label, p, errorPtr);
return NORMAL;
}
}
return NORMAL;
}
//-------------------------------------------------------
// parse {offset:width}
char *fieldParse(char *p, opDescriptor *d, int *errorPtr)
{
exprVal offset, width;
bool backRef;
d->field = 0;
if (*p != '{') {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
p++; // skip '{'
p = skipSpace(p);
// parse offset
if ((p[0] == 'D') && isRegNum(p[1])) { // if offset in data register
d->field |= 0x0800; // set Do to 1 for Dn offset
d->field |= ((p[1] - '0') << 6); // put reg number in bits[8:6]
p+=2; // skip Dn
} else { // else offset is immediate
if (p[0] == '#')
p++; // skip '#'
p = eval(p, &offset, &backRef, errorPtr); // read offset number
if (*errorPtr > SEVERE || offset.isRelative) {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
if (!backRef)
NEWERROR(*errorPtr, INV_FORWARD_REF);
if (offset.value < 0 || offset.value > 31) {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
d->field |= offset.value << 6; // put offset in bits[10:6]
}
p = skipSpace(p);
if (*p != ':') {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
p++; // skip ':'
p = skipSpace(p);
// parse width
if ((p[0] == 'D') && isRegNum(p[1])) { // if width in data register
d->field |= 0x0020; // set Dw to 1 for Dn width
d->field |= (p[1] - '0'); // put reg number in bits[2:0]
p+=2; // skip Dn
} else { // else width is immediate
if (p[0] == '#')
p++; // skip '#'
p = eval(p, &width, &backRef, errorPtr); // read width number
if (*errorPtr > SEVERE || width.isRelative) {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
if (!backRef)
NEWERROR(*errorPtr, INV_FORWARD_REF);
if (width.value < 1 || width.value > 32) {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
if (width.value == 32) // 0 specifies a field width of 32
width.value = 0;
d->field |= width.value; // put width in bits[4:0]
}
if (*p != '}') {
NEWERROR(*errorPtr, BAD_BITFIELD);
return p;
}
p++; // skip '}'
return p;
}
//-------------------------------------------------------
int pickMask(int size, flavor *flavorPtr, int *errorPtr)
{
if (!size || size & flavorPtr->sizes)
if (size & (BYTE_SIZE | SHORT_SIZE))
return flavorPtr->bytemask;
else if (!size || size == WORD_SIZE)
return flavorPtr->wordmask;
else
return flavorPtr->longmask;
NEWERROR(*errorPtr, INV_SIZE_CODE);
return flavorPtr->wordmask;
}
//---------------------------------------------------
// Tokenize a string to tokens.
// Each element of token[] is a pointer to the corresponding token in
// tokens. token[0] is always reserved for the label if any. A value
// of empty in token[] indicates no token.
// Each token is null terminated.
// Items inside parenthesis ( ) are one token
// Items inside single quotes ' ' are one token
// Parameters:
// instr = the string to tokenize
// delim = string of delimiter characters
// (spaces are not default delimiters)
// period delimiters are included in the start of the next token
// token[] = pointers to tokens
// tokens = new string full of tokens
// Returns number of tokens extracted.
int tokenize(char* instr, char* delim, char *token[], char* tokens){
int i, size, tokN = 0, tokCount = 0;
char* start;
int parenCount;
bool dotDelimiter;
bool quoted = false;
dotDelimiter = (strchr(delim, '.')); // set true if . is a delimiter
// clear token pointers
for (i=0; i<MAXT; i++) {
token[i] = empty; // this makes the pointer point to empty
tokenEnd[i] = NULL; // clear positions
}
start = instr;
while(*instr && isspace((unsigned char)*instr)) // skip leading spaces
instr++;
if (*instr != '*' && *instr != ';') { // if not comment line
if (start != instr) // if no label
tokN = 1;
size = 0;
while (*instr && tokN < MAXT && size < MAX_SIZE) { // while tokens remain
parenCount = 0;
token[tokN] = &tokens[size]; // pointer to token
//while (*instr && strchr(delim, *instr)) // skip leading delimiters
while(*instr && isspace((unsigned char)*instr)) // skip leading spaces
instr++;
if (*instr == '\'' && *(instr+1) == '\'') { // if argument starts with '' (NULL)
tokens[size++] = '\0';
instr+=2;
}
if (dotDelimiter && *instr == '.') { // if . delimiter
tokens[size++] = *instr++; // start token with .
}
// while more chars AND (not delimiter OR inside parens) AND token size limit not reached OR quoted
while (*instr && (!(strchr(delim, *instr)) || parenCount > 0 || quoted) && (size < MAX_SIZE-1) ) {
if (*instr == '\'') // if found '
if (quoted)
quoted = false;
else
quoted = true;
if (*instr == '(') // if found (
parenCount++;
else if (*instr == ')')
parenCount--;
tokens[size++] = *instr++;
}
tokens[size++] = '\0'; // terminate
tokenEnd[tokN] = instr; // save token end position in source line
if (*instr && (!dotDelimiter || *instr != '.')) // if not . delimiter
instr++; // skip delimiter
tokCount++; // count tokens
tokN++; // next token index
//while (*instr && strchr(delim, *instr)) // skip trailing delimiters
while (*instr && isspace((unsigned char)*instr)) // skip trailing spaces *ck 12-10-2005
instr++;
}
}
return tokCount;
}
#ifndef main
void help(void)
{
fprintf(stderr, "asy68k cli 68000 assembler Version %s based on source code from http://www.easy68k.com/\n", VERSION);
fprintf(stderr, "Distributed under the GNU General Public Use License. The software is not warranted in any way. Use at your own risk.\n");
fprintf(stderr, "\nPorted to the *nix CLI so it can be used in a Makefile workflow w/o WINE by Ray Arachelian May 2019\n\n");
fprintf(stderr,"Usage:\n asy68k {options} file1.X68 {file2.X68} ... {fileN.X68}\n\n");
fprintf(stderr,"(Options with \"default:\" are enabled, use --no-{option} to turn off, i.e. --no-list)\n"
"--list default: produce listing (file.L68)\n"
"--object default: produce S-Record object code file (file.S68)\n"
"--bitfields default: assemble bitfield instructions\n"
"--warnings default: show warnings in listing file\n"
"--symbols add symbol table to listing file\n"
"--macroexpand expand macros in listing file\n"
"--structureexpand expand structures in code listing file\n"
"--expandconstants expand constants in listing file\n"
"\n");
}
// stolen from mainS.cpp
int main(int argc, char *argv[])
{
int i,s;
string sourceFile, tempFile;
if (argc == 1) {help(); exit(0);}
listFlag = true; // True if a listing is desired
objFlag = true; // True if an S-Record object code file is desired
binFlag = true; // True to generate output binary file
CEXflag = false; // True is Constants are to be EXpanded
BITflag = true; // True to assemble bitfield instructions
CREflag = true; // true adds symbol table to listing