#include #include #include int searchOptab(char opcode[10]) { FILE *optab; char op[10]; int value; optab = fopen("optab.txt", "r"); if (optab == NULL) { printf("Error in opening optab file."); exit(1); } while (fscanf(optab, "%s%d", op, &value) != EOF) { if (strcmp(op, opcode) == 0) { fclose(optab); return value; } } fclose(optab); return -1; } int searchSymtab(char label[10]) { FILE *symtab; char sym[10]; int value; symtab = fopen("symtab.txt", "r"); if (symtab == NULL) { printf("Error in opening symtab file."); exit(1); } while (fscanf(symtab, "%s%d", sym, &value) != EOF) { if (strcmp(sym, label) == 0) { fclose(symtab); return value; } } fclose(symtab); return -1; } int main() { FILE *intermediate, *output, *header, *text, *end; char label[10], opcode[10], operand[10], obcode[10], obcodeTe[100] = "", obcodeCopy[10] = ""; int LOCCTR, symaddr, opaddr, i, sa, leng, lenObCode = 0, flag = 0, flag2 = 0, opc; intermediate = fopen("intermediate.txt", "r"); output = fopen("output.txt", "w"); header = fopen("header.txt", "w"); text = fopen("text.txt", "w"); end = fopen("end.txt", "w"); if (intermediate == NULL || output == NULL || header == NULL || text == NULL || end == NULL) { printf("Error in opening file."); return 1; } fscanf(intermediate,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0){ fprintf(header,"H^00%s^00%s",label,operand); fprintf(text,"T^00%s",operand); fprintf(end,"E^00%s",operand); fprintf(output,"\t%s\t%s\t%s\n",label,opcode,operand); sa=atoi(operand); } while (strcmp(opcode,"END")!=0){ fscanf(intermediate,"%d%s%s%s",&LOCCTR,label,opcode,operand); if(strcmp(label,"**")==0){ opaddr=searchOptab(opcode); if(opaddr!=-1){ symaddr=searchSymtab(operand); if(symaddr!=-1){ sprintf(obcode,"%02d%d",opaddr,symaddr); lenObCode+=3; } strcat(obcodeTe,"^"); strcat(obcodeTe,obcode); } } else if(strcmp(opcode,"BYTE")==0){ if(flag==0){ fprintf(text,"^%02d%s",lenObCode,obcodeTe); strcpy(obcodeTe,""); lenObCode=0; flag=1; flag2=1; } for(i=strlen(operand);i<(6+3);i++){ strcat(obcodeCopy,"0"); } for(i=2;i17){ fprintf(text,"^%02d%s",lenObCode,obcodeTe); strcpy(obcodeTe,""); lenObCode=0; flag2=1; } } } if(lenObCode>0){ fprintf(text,"^%02d%s",lenObCode,obcodeTe); } leng=LOCCTR-sa; fclose(intermediate); fclose(output); fclose(header); fclose(text); fclose(end); printf("Output generated successfully"); return 0; }