1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-16 13:29:33 +00:00

a02.c02: modified and tested addsym() and prtsym()

This commit is contained in:
Curtis F Kaylor 2022-07-09 15:51:10 -04:00
parent 5748b176dc
commit 122e4fcc00

View File

@ -15,8 +15,7 @@
#include <memory.h02> #include <memory.h02>
#include <string.h02> #include <string.h02>
alias char lcltbl = $E000; //Local Symbol Table alias char symtbl = $E000; //Global Symbol Table
alias char symtbl = $E100; //Global Symbol Table
/* Address Mode Constants, Bit Masks, and Descriptions */ /* Address Mode Constants, Bit Masks, and Descriptions */
enum {ACMLT, IMMDT, ZPAGE, ZPAGX, ABSLT, ABSLX, ABSLY, IMPLD, INDCT, INDCX, INDCY, RELTV}; enum {ACMLT, IMMDT, ZPAGE, ZPAGX, ABSLT, ABSLX, ABSLY, IMPLD, INDCT, INDCX, INDCY, RELTV};
@ -76,12 +75,14 @@ char digit; //evlhex()
int term; //evltrm() int term; //evltrm()
/* Symbol Table Entries */ /* Symbol Table Entries */
struct sym {char name[8], bytes; int value;}; struct sym {char block, name[8], bytes; int value;};
struct sym symbol; //Current Symbol struct sym symbol; //Current Symbol
struct sym varble; //Symbol to Lookup struct sym varble; //Symbol to Lookup
char symcnt, lclcnt; //Number of Global, Local Symbols int symcnt; //Number of Symbols
int symptr, lclptr; //Pointer to Next Table Entry int symptr; //Pointer to Next Table Entry
char symflg, symlcl; //Valid Symbol Value, Local Symbol char symflg; //Valid Symbol Value
char blkcnt; //Number of Block Labels Generated
char blknum; //Local Label Block Number (0 = Global)
/* Input Variables */ /* Input Variables */
char inplin[128]; //Input Buffer char inplin[128]; //Input Buffer
@ -125,7 +126,7 @@ int lstadr; //List Address
/* Print Error Message and Exit * /* Print Error Message and Exit *
* Args: Y,X = Address of Eror Message */ * Args: Y,X = Address of Eror Message */
void error() { void error() {
putstr(); putstr(" ERROR"); newlin(); putstr(); putln(" ERROR");
goto exit; goto exit;
} }
@ -161,7 +162,8 @@ void init() {
orgset = #FALSE; //Origin Not Set orgset = #FALSE; //Origin Not Set
endasm = #FALSE; //End Assembly Fl endasm = #FALSE; //End Assembly Fl
symptr = &symtbl; symptr = &symtbl;
lclptr = &lcltbl; blkcnt = 1; //Top level locals are Block 1
blknum = blkcnt; //because Block 0 is a global
} }
/* Parse Word from Input Line * /* Parse Word from Input Line *
@ -248,20 +250,22 @@ void outwrd(w) {
} }
/* Print Symbol Table */ /* Print Symbol Table */
void prtsym(n, dstptr) { void prtsym() {
prtlin("SYMBOLS"); printf("%nSYMBOLS%nBLK SZ VALUE NAME%n");
for (i=0; i<n; i++) { usrptr = &symtbl;
for (j=0; j<@varble; j++) {varble[j] = *dstptr; dstptr++;} iacc(symptr); //end of symbol table for icmp()
putstr("|"); putwrd(varble.value); prtbyt(varble.bytes); prtlin(varble.name); puts(">PRTSYM: LISTING FROM $");putwrd(usrptr);
puts(" TO $"); putwrd(intacc); newlin();
//for (j=0; j<9; j++) {
while (icmp(iacc(usrptr),symptr):-) {
//iacc(usrptr); putdec(icmp(symptr));newlin();
for (i=0; i<@symbol; i++) {symbol[i] = *usrptr; usrptr++;}
putder(symbol.block);
setdst(symbol.value); printf(symbol.bytes," %d %J ");
putln(symbol.name);
} }
} }
void prtlcl() {
if (lclcnt) {
putstr("LOCAL ");
prtsym(lclcnt, &lcltbl);
}
}
/* Find Symbol in Table * /* Find Symbol in Table *
* Args: n = Number of Symbols in Table * * Args: n = Number of Symbols in Table *
@ -304,6 +308,7 @@ void plabel() {
if (wrdlen>8) error("!SYMBOL TOO LONG"); if (wrdlen>8) error("!SYMBOL TOO LONG");
setdst(label); strcpy(word); setdst(label); strcpy(word);
if (#DEBUG&2) {putstr("#FOUND "); if (local) putstr("LOCAL "); putstr("LABEL "); prtlin(label);} if (#DEBUG&2) {putstr("#FOUND "); if (local) putstr("LOCAL "); putstr("LABEL "); prtlin(label);}
symbol.block = (local) ? blknum : 0;
setdst(symbol.name);strcpy(label); setdst(symbol.name);strcpy(label);
setsym(0, curadr); symflg = orgset; setsym(0, curadr); symflg = orgset;
} else { } else {
@ -426,13 +431,12 @@ char evltrm() {
} }
char evlsym() { char evlsym() {
symlcl = cpychr('.'); //Check for Local Symbol local = cpychr('.'); //Check for Local Symbol
if (!pword()) {if (symlcl) error("INVALID SYMBOL"); else return;} if (!pword()) {if (local) error("INVALID SYMBOL"); else return;}
for (i=0; i<wrdlen; i++) { for (i=0; i<wrdlen; i++) {
if (opridx:+) {oprnd[opridx] = word[i]; opridx++;} if (opridx:+) {oprnd[opridx] = word[i]; opridx++;}
} }
if (symlcl) found = fndsym(lclcnt, &lcltbl); else found = fndsym(&symtbl);
else found = fndsym(symcnt, &symtbl);
if (found) opdval = symbol.value; if (found) opdval = symbol.value;
else error("UNDEFINED SYMBOL"); else error("UNDEFINED SYMBOL");
} }
@ -511,8 +515,7 @@ void asmhex() {
/* Assemble SUBROUTINE Pseudo-Op */ /* Assemble SUBROUTINE Pseudo-Op */
void asmsub() { void asmsub() {
prtlcl();
lclptr = &lcltbl; lclcnt = 0;
} }
/* Assemble WORD Pseudo-Op */ /* Assemble WORD Pseudo-Op */
@ -612,24 +615,20 @@ void pcmmnt() {
if (#DEBUG&2) {if (i) {putstr("#COMMENT: "); prtlin(cmmnt);} else prtlin("No Comment Found");} if (#DEBUG&2) {if (i) {putstr("#COMMENT: "); prtlin(cmmnt);} else prtlin("No Comment Found");}
} }
/* Copy Symbol Struct to Destination */ /* Append Symbol` to Symbol Table *
void cpysym() { * Uses: symbol *
* Sets: i = @symbol *
* dstptr = End of Table *
* Updates: symptr = End of Table *
* Returns: End of Table Address */
void addsym() {
if (!<symbol.value|>symbol.value) error("ORIGIN NOT SET");
dstptr = symptr;
for (i=0; i<@symbol; i++) { for (i=0; i<@symbol; i++) {
*dstptr = symbol[i]; *dstptr = symbol[i];
dstptr++; dstptr++;
} }
} symptr = dstptr;
/* Add Label to Symbol Table */
void addlcl() {
dstptr = lclptr; cpysym(); lclptr = dstptr;
lclcnt++;
}
/* Add Label to Symbol Table */
void addsym() {
dstptr = symptr; cpysym(); symptr = dstptr;
symcnt++;
} }
/* Print Listing Line */ /* Print Listing Line */
@ -655,7 +654,7 @@ void asmlin() {
pcmmnt(); //Parse Comment pcmmnt(); //Parse Comment
if (label[0]) { if (label[0]) {
if (!symflg) error("INVALID ADDRESS"); if (!symflg) error("INVALID ADDRESS");
if (local) addlcl(); else addsym(); //Add Label to Table addsym(); //Add Label to Table
} }
if (mcdidx) wrtmcd(); //Write Machine Code if (mcdidx) wrtmcd(); //Write Machine Code
prlist(); prlist();
@ -670,7 +669,7 @@ main:
asmlin(); if (endasm) break; asmlin(); if (endasm) break;
lineno++; lineno++;
} }
prtlcl(); prtsym(symcnt, &symtbl); prtsym(&symtbl);
goto exit; goto exit;
/*======================================================*/ /*======================================================*/
@ -690,8 +689,9 @@ void tlabel() {
plabel(); plabel();
if (label[0]) { if (label[0]) {
setdst(label); printf(local," LABEL=\"%s\", LOCAL=%d%n"); setdst(label); printf(local," LABEL=\"%s\", LOCAL=%d%n");
setdst(symbol.name); printf(" SYMBOL.NAME=\"%s\", "); setdst(symbol.name); printf(symbol.block, " SYMBOL.BLOCK=%d, NAME=\"%s\", ");
setdst(symbol.value); printf(symbol.bytes,"BYTES=%d, VALUE=%i%n"); setdst(symbol.value); printf(symbol.bytes,"BYTES=%d, VALUE=%i%n");
addsym(); printf(" ADDED TO TABLE, SYMPTR=$%w%n"); //dstptr set by setsym()
} }
else putln("NO LABEL FOUND"); else putln("NO LABEL FOUND");
curadr++; curadr++;
@ -715,6 +715,7 @@ tests:
curadr = orgadr; curadr = orgadr;
tlabel("FEE "); tlabel(".FI "); tlabel("FOE: "); tlabel(".FUM: "); tlabel("FEE "); tlabel(".FI "); tlabel("FOE: "); tlabel(".FUM: ");
tevtrm(" 12345"); tevtrm("%10101"); tevtrm("$1001 "); tevtrm(" 12345"); tevtrm("%10101"); tevtrm("$1001 ");
prtsym();
goto exit; goto exit;
void dolkup() { void dolkup() {