//asmpass1.cpp -- by tsaiwn@csie.nctu.edu.tw /************* Pass1 (把程式看第一遍叫做 Pass 1) 主要是要建立 Symbol table 請參考課本Chapter2, P.50 (ORG 相當於課本的 START) 不過假指令部份先認 ORG, END 就好, DW, DB, RESW, RESB 就隨便你啦 當然也要先看看 asmprot.h 和主程式中define的資料結構 **假指令 pseudo instruction : 又叫做 Directive (指示) ************/ #include #include #include "asmprot.h" int pass1(FILE* fpIn, FILE* fpOut) { int ntok, lineNumber=0, k; pc = 0; while(!feof(fpIn)){ // read File line by line until EOF if(getNextLine(fpIn, bufIn) == 0) break; //EOF lineNumber++; if(bufIn[0] == ';' ){ // is comment? // 為簡單起見, 規定第一格寫 ';' 表示注解 comment printf("%4.3d %s\n", lineNumber, bufIn); continue; } if(bufIn[0] == 0) continue; // empty Line (NULL String) //NOT a comment ntok = myToken(bufIn, token); // see asmutil.cpp /*********************/ /* OPCODE 是否為 ORG ? 是則要調整 pc 值 */ /** 用 strcmp( str1, str2) 來比較字串 **/ /** if(strcmp(str1,str2) == 0 ) { 字串str1 和 str2 相等 } **/ /*********************/ //.. todo printf("%4.3d ", lineNumber); if( ! isalpha(bufIn[0]) ){ // 沒有 Label , Label 以字母開頭 printf("\t"); }else{ // 有 Label, 要加入 symbolTable[] ; see "asmprot.h" //.. todo } /* if(! isalpha( ... 有 Label */ /*********************/ // pc 需要 加 2 嗎? 此 line 有沒有 指令? // 有指令或要 生出 Data 的才會影響 PC program Counter // todo... for(k=0; k< ntok; k++){ // print all tokens printf("\t%s", token[k]); } if(comment[0]) printf(" %s", comment); // if any comment printf("\n"); }// while not EOF return 0; /* indicates no error */ }//pass1(