diff --git a/a02.c b/a02.c index d019911..3a4188e 100644 --- a/a02.c +++ b/a02.c @@ -12,6 +12,9 @@ #define DEBUG FALSE int debug; //Ouput Debug Info +#define INCLEN 255 +char incpath[INCLEN]; + enum otypes {BINFIL, PRGFIL}; //Object File Types char objtyp; //Object File Type @@ -23,16 +26,18 @@ struct sym {int block; char name[MAXLBL+1]; int bytes, value, refrd;}; struct sym symbol; //Current Symbol struct sym symtbl[MAXSYM]; //Global Symbol Table int symcnt; //Number of Global Labels +int blkcnt; //Number of Block Labels Generated int blknum; //Local Label Block Number (0 = Global) char label[MAXSTR]; //Assembly Line Label char mnmnc[MAXSTR]; //Opcode Mnemonic -char oprnd[MAXSTR]; //Opcode Mnemonic +char oprnd[MAXSTR]; //Operand Text char cmmnt[MAXSTR]; //Assembly Line Comment char mcode[MAXSTR]; //Generated Bytes char strng[MAXSTR]; //Parsed String int opridx; //Index into Operand +char cpdchr; //Last Character Copied into oprnd unsigned char token, opmod; //OpCode Token, Modifier unsigned int amode; //Addressing Modes @@ -67,25 +72,52 @@ void usage(char* appnam) { exit(EXIT_FAILURE); } +/* Print Symbol Table */ +void prtsym(void) { + fprintf(lstfil, "\n%s Symbol Table\nBlock Name Size Value Rfd\n", "Global"); + for (int i=0; i 0xFF) ? 2 : 1; symbol.refrd = FALSE; + if (debug) printf("Set Symbol %s in block %d to value %d, size %d, referred=False\n", + symbol.name, symbol.block, symbol.bytes, symbol.value); } - + /* Add Character to Beginning of String */ void pfxstr(char c, char* s) { for (int i=strlen(s)+1; i; i--) @@ -172,7 +208,8 @@ int plabel(void) { /* Copy Character to Operand and Increment */ int cpychr(int c) { if (c && toupper(*linptr) != c) return FALSE; - if (opridx < MAXSTR) oprnd[opridx++] = toupper(*linptr); + cpdchr = *linptr; + if (opridx < MAXSTR) oprnd[opridx++] = toupper(cpdchr); linptr++; return TRUE; } @@ -267,10 +304,12 @@ int evlopd(int maxsiz) { if (hilo) prns = cpychr('('); result = evltrm(); if (result >= 0) - while (cpychr('+')) { + while (cpychr('+') || cpychr('-')) { + int opertr = cpdchr; int opdval = evltrm(); if (opdval < 0) break; - result += opdval; + if (opertr == '+') result += opdval; + else result -= opdval; } if (hilo) { if (result < 0) xerror("Hi/Low Operator Requires Operand", ""); @@ -386,11 +425,18 @@ void asmprc(void) { /* Assemble SUBROUTINE Pseudo-Op */ void asmsub(void) { - blknum++; + blknum = ++blkcnt; sprintf(oprnd, "%d", blknum); opridx = strlen(oprnd); if (debug) printf("Block Number set to %s\n", oprnd); } +/* Assemble ENDSUBROUTINE Pseudo-Op */ +void asmens(void) { + blknum = 0; + sprintf(oprnd, "%d", blknum); opridx = strlen(oprnd); + if (debug) printf("Block Number reset to %s\n", oprnd); +} + /* Assemble INCLUDE Pseudo-Op */ void asminf(void) { int incidx = 0; @@ -419,11 +465,12 @@ int asmpso(int dot) { if (debug) printf("Assembling Pseudo-Op %s, Token '%c'\n", mnmnc, token); switch (token) { case '=': asmequ(); break; //EQU - case 'B': asmbyt(); break; //BYTE - case 'H': asmhex(); break; //BYTE + case 'B': asmbyt(); break; //BYTE or DC + case 'H': asmhex(); break; //HEX case 'W': asmwrd(); break; //WORD - case 'F': asmfll(); break; //FILL + case 'F': asmfll(); break; //FILL or DS case 'S': asmsub(); break; //SUBRoutine + case 'M': asmens(); break; //ENDSubroutine case 'I': asminf(); break; //INCLude case '*': asmorg(); break; //ORG case 'P': asmprc(); break; //PROCessor @@ -614,6 +661,7 @@ void pcmmnt(void) { void addsym() { if (symbol.value<0) xerror("Origin Not Set", ""); memcpy(&symtbl[symcnt++], &symbol, sizeof(symbol)); + if (debug) printf("Added symbol %s in block %d to Symbol Table\n", symbol.name, symbol.block); } @@ -621,7 +669,7 @@ void addsym() { void opninc(void) { if (debug) printf("Opening Include File %s\n", incnam); if (lstfil) fputs("\n", lstfil); - incfil = opnfil(incnam, "r"); + incfil = opnfil(incnam, "r", incpath); savlno = lineno; lineno = 1; } @@ -634,7 +682,7 @@ void clsinc(void) { incfil = NULL; incnam[0] = 0; lineno = savlno; - endasm = FALSE; //Clear End Flag for Return to Maun File + endasm = FALSE; //Clear End Flag for Return to Main File } /* Assemble Input File (Two Pass) * @@ -647,7 +695,8 @@ void asmfil(int pass) { passno = pass; //Assembly Pass Number if (debug) printf("Assembling Pass %d\n", pass); lineno = 1; //Initialize Input File Line Number - blknum = 1; //Initialize Local Block Number + blkcnt = 0; //Initialize Local Block Count + blknum = 0; // and Local Block Number orgadr = -1; //Origin Address Not Set curadr = orgadr; //Set Current Address to Origin if (debug) printf("Rewinding Input File\n"); @@ -656,7 +705,7 @@ void asmfil(int pass) { if (incfil) linptr = fgets(inplin, MAXSTR, incfil); else linptr = fgets(inplin, MAXSTR, inpfil); if (endasm || linptr == NULL) {if (incfil) {clsinc(); continue;} else break;} - if (debug) printf("%05d %04X: %s", lineno, curadr, inplin); + if (debug) printf("%1d %05d %04X: %s", passno, lineno, curadr, inplin); lstadr = curadr; //Set List Address mcode[0] = 0; //Clear Generated Macbine Code plabel(); //Parse Label @@ -673,15 +722,6 @@ void asmfil(int pass) { } } -/* Print Symbol Table */ -void prtsym(void) { - fprintf(lstfil, "\n%s Symbol Table\nBlock Name Size Value Rfd\n", "Global"); - for (int i=0; i