From 18088691d7d364560bba0f25aa35f700e0783a8e Mon Sep 17 00:00:00 2001 From: Curtis F Kaylor Date: Thu, 14 Nov 2019 11:15:51 -0500 Subject: [PATCH] Updated and Debugged A02 Assembler --- a02.c | 158 +++++++++++++++++++++++++++++++++++++++++++--------------- a02.h | 123 +++++++++++++++++++-------------------------- 2 files changed, 169 insertions(+), 112 deletions(-) diff --git a/a02.c b/a02.c index 7c77403..238961a 100644 --- a/a02.c +++ b/a02.c @@ -18,9 +18,9 @@ int orgadr; //Origin Address int curadr; //Current Address int lstadr; //List Address -struct sym {int block; char name[MAXSYM]; int bytes, value;}; +struct sym {int block; char name[MAXLBL+1]; int bytes, value;}; struct sym symbol; //Current Symbol -struct sym symtbl[MAXGLB]; //Global Symbol Table +struct sym symtbl[MAXSYM]; //Global Symbol Table int symcnt; //Number of Global Labels int blknum; //Local Label Block Number (0 = Global) @@ -43,14 +43,17 @@ char *linptr; //Pointer into Input Buffer int lineno; //Input File Line Number int opridx; //Index into Operand int passno; //Assembler Pass Number (1 or 2) +int savlno; //Line Number (Saved) char prgnam[256]; //Assembler Path and Name (from Command Line) char inpnam[256]; //Input File Name char outnam[256]; //Output File Name char lstnam[256]; //List File Name +char incnam[256]; //Include File Name FILE *inpfil; //Input File Pointer FILE *outfil; //Output File Pointer FILE *lstfil; //List File Pointer +FILE *incfil; //Include File Pointer /* Print Error Message and Exit */ void xerror(char* format, char *s) { @@ -59,6 +62,14 @@ void xerror(char* format, char *s) { exit(EXIT_FAILURE); } +/* Open File with Error Checking */ +FILE * opnfil(char* name, char* mode) { + if (DEBUG) printf("Opening file '%s' with mode '%s'\n", name, mode); + FILE *fp = fopen(name, mode); + if (!fp) xerror("Error Opening File '%s'\n", name); + return fp; +} + /* Skip Character in Input Line * * Args: c - Character to Skip * * Updates: linptr */ @@ -81,7 +92,7 @@ void skpspc(void) { int pword(int skip, char* word) { int wrdlen = 0; if (skip) skpspc(); - while (isalnum(*linptr)) { + while (isalnum(*linptr) || *linptr == '_') { word[wrdlen++] = toupper(*linptr); linptr++; } @@ -122,6 +133,7 @@ int plabel(void) { if (label[0] && fndsym(block, label)) xerror("Duplicate Label %s Encountered\n", label); if (DEBUG) printf("Initializing Symbol %s\n", label); symbol.block = block; + if (strlen(label) > MAXLBL) xerror("Label %s Too Long\n", label); strcpy(symbol.name, label); setsym(curadr, 0); } @@ -135,7 +147,8 @@ int plabel(void) { /* Copy Character to Operand and Increment */ int cpychr(int c) { if (c && toupper(*linptr) != c) return FALSE; - oprnd[opridx++] = toupper(*linptr++); + if (opridx < MAXSTR) oprnd[opridx++] = toupper(*linptr); + linptr++; return TRUE; } @@ -189,7 +202,7 @@ struct sym *evlsym() { char name[MAXSTR]; int block = (cpychr('.')) ? blknum : 0; pword(TRUE, name); - for (int i=0; name[i]; i++) oprnd[opridx++] = name[i]; + for (int i=0; name[i]; i++) if (opridx