// asmmain.cpp --- main program assembler, tsaiwn@csie.nctu.edu.tw /******************************* *** SISC Simple Assebler @CopyLeft by tsaiwn@csie.nctu.edu.tw for UNIX user: gcc asmmain.cpp asmutil.cpp asmpass1.cpp asmpass2.cpp asmutil2.cpp ./a.out ********** for PC user using TC++3.0 and/or other IDE: open a project and then add the above 5 "*.cpp" files into the project Note that do NOT add the ".h" file like asmprot.h *********************************/ #define MAXSYM 999 #include #include #include "asmprot.h" int lineNumber = 0; int numInst = 0; /* number of instructions */ int pc; /*program counter*/ char token[20][38]; /* for tokens */ char comment[80]; FILE* fpIn, *fpOut; char bufIn[80], bufOut[80]; char symbolTable[MAXSYM][10]; //////////////////////////////////////////////////////// // 注意, 以下的 opTable 應該參考課本 SIC 或 SIC/XE 的指令修正 char opTable[][6]={ // each item has max of 5 chars (5+1 == 6) "NOP", "LOAD", "LDI", "STORE", "MOVE", "ADD", "FADD", "OR", "AND", "XOR", "ROT", "BR", "HALT","XXXXX","XXXXX","XXXXX", "GETC", "PUTC", "GETI", "PUTI", "GETS", "PUTS", "CMP", "JLT", "JEQ", "JGT" }; int numOp = sizeof(opTable) / sizeof opTable[0]; int numSym=0; // number of items in Symbol table /*** 以上也要在 asmprot.h 宣告 ***/ char fileIn[80], fileOut[80]; int main() { /** printf("numOp=%d\n", numOp); **/ fprintf(stderr, "Filename of ASM source: "); fgets(fileIn, sizeof(fileIn), stdin); myChop(fileIn); fprintf(stderr, "output Filename for OBJ code: "); fgets(fileOut, sizeof(fileOut), stdin); myChop(fileOut); if( strcmp(fileIn,fileOut) == 0) { fprintf(stderr, "%!Filename of OBJ can not equale to Input filename\n"); return -1; // tell him error } fpIn = fopen(fileIn, "rt"); if(! fpIn){ fprintf(stderr, "Open source error: %s\n", fileIn); return -1; // open error } while(!feof(fpIn)){ if(getNextLine(fpIn, bufIn)){ printf("%4.3d %s\n", ++lineNumber, bufIn); }else break; //EOF } fclose(fpIn); /**********/ fpIn = fopen(fileIn, "rt"); fpOut = fopen(fileOut, "at"); // 或是用 "wt" printf("\n===Assembling %s ...\n", fileIn); fprintf(fpOut, ";;;===Assembling %s ...\n", fileIn); lineNumber = 0; if(pass1(fpIn, fpOut) == 0){ /**********/ fclose(fpIn); fpIn = fopen(fileIn, "rt"); pass2(fpIn, fpOut); }else{ printf("Error found at Pass1.\n"); } printf("=== Thank you for using the assembler.\n"); fclose(fpIn); fclose(fpOut); /**********/ }//main(