void get_OPformat(void){ char *temp; OPCODE_extended = 0; // Initialization extended tag /* if OPCODE is extended format */ if(OPCODE[0] == '+'){ temp = &OPCODE[1]; strcpy(OPCODE, temp); OPCODE_extended = 1; } /* Search OPTAB, if OPCODE is found, then */ if(search_OPTAB(&instPtr)){ OPCODE_format = inst_Length(instPtr); } /* else if OPCODE is "BYTE" */ else if(!strcmp(OPCODE, "BYTE")){ OPCODE_format = 5; } /* else if OPCODE is "WORD" */ else if(!strcmp(OPCODE, "WORD")){ OPCODE_format = 6; } /* else if OPCODE is "RESB" */ else if(!strcmp(OPCODE, "RESB")){ OPCODE_format = 0; } /* else if OPCODE is "RESW" */ else if(!strcmp(OPCODE, "RESW")){ OPCODE_format = 0; } /* else if OPCODE is "BASE" */ else if(!strcmp(OPCODE, "BASE")){ OPCODE_format = 7; } /* else if OPCODE is "START" */ else if(!strcmp(OPCODE, "START")){ OPCODE_format = 0; } /* else if OPCODE is "END" */ else if(!strcmp(OPCODE, "END")){ OPCODE_format = -0; } /* else (undefined OPCODE) */ else{ printf("ERROR. %s is undefined OPCODE.\n", OPCODE); exit(0); } return ; }
//Pass 1 void PASS_ONE() { FILE * fp1,*fp2; //file pointers char line[100], s1[100], s2[100], s3[100], s4[100],startingADDRESS[5], saveLOCCTR[5]; int t,flag_SYMTAB, flag_OPTAB; printf("Enter the input file name :"); scanf("%s",in_file); //input file name fp1 = fopen(in_file, "r"); //corresponding file opened if(fp1 == NULL) //if '.txt' not in entered file name { strcat(in_file, ".txt"); //concatenating '.txt' fp1 = fopen(in_file, "r"); if(fp1 == NULL) { printf("Could not open source file!!\n"); exit(0); } } strcpy(out_file, in_file); out_file[strlen(out_file)-4] = '\0'; strcat(out_file, "-intermediate.txt"); //to get intermediate file name fp2 = fopen(out_file, "w"); if(fp2 == NULL) { printf("Could not open Intermediate file!!\n"); exit(0); } //fgets(line, 100, fp1);//For column headers //Read first line fgets(line, 100 ,fp1); //reading a line of the input t = sscanf(line,"%s %s %s %s",s1,s2,s3,s4); //t stores the number of words read if(t==2) //two words read { strcpy(s3, s2); //shifting the words read strcpy(s2, s1); strcpy(s1, ""); } char temp[10]; if(strcmp(s2,"START")==0) //start line { strcpy(LOCCTR,s3); make_length(LOCCTR,temp,4); line[strlen(line)-1] = '\0'; if(t==1) fprintf(fp2, "%s %s %s",temp,line," Syntax error : less arguments"); else if(t>3) fprintf(fp2, "%s %s %s",temp,line," Syntax error : more arguments"); else fprintf(fp2, "%s %s",temp,line);//Write to Intermediate file fprintf(fp2,"\n"); fgets(line, 100 ,fp1);//Read next line t = sscanf(line,"%s %s %s %s",s1,s2,s3,s4); if(t==2) { strcpy(s3, s2); strcpy(s2, s1); strcpy(s1, ""); } } else //if START was not found { strcpy(LOCCTR, "0000"); printf("Syntax Error : NO START found!!\n"); } strcpy(startingADDRESS, LOCCTR); while(!feof(fp1) && (strcmp(s2, "END")!=0)) { flag_SYMTAB = 0;flag_OPTAB = 0; strcpy(saveLOCCTR, LOCCTR); if(line[0] != '#')//Not a comment line { if(t==3)//Symbol exists in Label Field { if(search_SYMTAB(s1))//Label exists in SYMTAB flag_SYMTAB = 1; else insert_SYMTAB(s1,LOCCTR); } strcpy(saveLOCCTR, LOCCTR); char * OPCODE = search_OPTAB(s2); if(OPCODE != NULL) increment_LOCCTR(3); else if(strcmp(s2, "WORD") == 0) //WORD detected increment_LOCCTR(3); else if(strcmp(s2, "RESW") == 0) //RESW detected increment_LOCCTR(3 * atoi(s3)); else if(strcmp(s2, "RESB") == 0) //RESB detected increment_LOCCTR(atoi(s3)); else if(strcmp(s2, "BYTE") == 0) //BYTE detected increment_LOCCTR(find_length_BYTE(s3)); else { increment_LOCCTR(3); flag_OPTAB = 1;//OPCODE not found and is not an assembler directive } } make_length(saveLOCCTR,temp,4); strcpy(saveLOCCTR,temp); char write[100];//Line to write in Intermediate file line[strlen(line)-1] = '\0';//Remove newline character from line read so taht it writes error on same line if(flag_OPTAB == 0 ) { if(flag_SYMTAB == 0) sprintf(write, "%s %s",saveLOCCTR, line); else sprintf(write,"%s %s %s",saveLOCCTR, line, "Duplicate Symbol"); } else { if(flag_SYMTAB == 0) sprintf(write,"%s %s %s",saveLOCCTR, line, "Invalid Operation Code"); else sprintf(write,"%s %s %s %s",saveLOCCTR, line, "Duplicate Symbol", "Invalid Operation Code"); } if(t==1) fprintf(fp2, "%s %s",write," Syntax error : less arguments"); else if(t>3) fprintf(fp2, "%s %s",write," Syntax error : more arguments"); else fprintf(fp2, "%s",write);//Write to Intermediate file fprintf(fp2,"\n"); //Read next line fgets(line, 100, fp1); t = sscanf(line,"%s %s %s %s",s1,s2,s3,s4); if(t==2) { strcpy(s3, s2); strcpy(s2, s1); strcpy(s1, ""); } } make_length(LOCCTR,temp,4); //Write last line to Intermediate file fprintf(fp2, "%s %s",temp, line); fclose(fp1); fclose(fp2); //Computing Program Length int start, location,l; char length[5]; sscanf(startingADDRESS, "%x", &start); sscanf(LOCCTR, "%x", &location); l = location - start; itoa(l, length, 16); //getting the length of the code in a string strcpy(out_file, in_file); out_file[strlen(out_file)-4] = '\0'; strcat(out_file, "-length.txt"); fp2 = fopen(out_file, "w"); if(fp2 != NULL) fprintf(fp2, "%s", length); fclose(fp2); //closing file pointer }
//pass II void PASS_TWO() { FILE * fp1,*fp2,*fp3; char line[100], s1[100], s2[100], s3[100], s4[100], startADDRESS[5], length[5], name[100]; //s1 is Location, s2 is Label, s3 is Opcode, s4 is Operand char * symbolADDRESS; char OBJECTCODE[100] ; int t,flag_SYMTAB,main_flag = 0,linecounter = 0; printf("Enter the intermediate file name :"); scanf("%s",im_file); fp1 = fopen(im_file, "r"); //opening intermediate file if(fp1 == NULL) { strcat(im_file, ".txt"); fp1 = fopen(im_file, "r"); if(fp1 == NULL) { printf("Could not open Intermediate file!!\n"); exit(0); } } loadSYMTAB_from_file(); //loading symbol table strcpy(obj_file, im_file); obj_file[strlen(obj_file)-17] = '\0'; strcat(obj_file, "-object.txt"); fp2 = fopen(obj_file, "w"); if(fp2 == NULL) { printf("Could not open Object file!!\n"); exit(0); } //Read first line fgets(line, 100 ,fp1); linecounter++; t = sscanf(line,"%s %s %s %s",s1,s2,s3,s4); if(t==3) { strcpy(s4, s3); strcpy(s3, s2); } if(strcmp(s3,"START")==0) { strcpy(startADDRESS, s1); strcpy(name, s2);//s2 != s3 fgets(line, 100 ,fp1);//Read next line linecounter++; t = sscanf(line,"%s %s %s %s",s1,s2,s3,s4); if(t==3) { strcpy(s4, s3); strcpy(s3, s2); } } else { strcpy(startADDRESS, "0000"); strcpy(name, "UNNAMED"); } //Getting Program Length char S[100]; strcpy(S, im_file); S[strlen(S)-17] = '\0'; strcat(S, "-length.txt"); fp3 = fopen(S, "r"); fscanf(fp3, "%s",length); fclose(fp3); //Writing Header Record char temp1[7], temp2[7]; make_length(startADDRESS, temp1, 6); make_length(length, temp2, 6); fprintf(fp2,"H|%s|%s|%s",name,temp1,temp2); fprintf(fp2,"\n"); //Initialse text record fprintf(fp2, "T|%s|%s",temp1,length); while(!feof(fp1) && (strcmp(s3, "END")!=0)) { flag_SYMTAB = 0; if(s2[0] != '#')//Not a comment line { char * OPCODE = search_OPTAB(s3); if(OPCODE != NULL)//OPCODE found in OPTAB { if(s4!=NULL)//There is a symbol in operand field { symbolADDRESS = search_SYMTAB(s4); if(symbolADDRESS != NULL) make_length(symbolADDRESS, temp1, 4); if(symbolADDRESS == NULL)//Operand does not exist in SYMTAB { printf("Line %d : Error : Symbol is not defined\n",linecounter); main_flag = 1; flag_SYMTAB = 1; } } else//No symbol in operand field strcpy(symbolADDRESS,"0000"); //Assemble object code instruction strcpy(OBJECTCODE, OPCODE); if(symbolADDRESS != NULL) strcat(OBJECTCODE, temp1); } else if(OPCODE==NULL && (strcmp(s3, "BYTE") == 0)) convert_BYTE(s4,OBJECTCODE); else if(OPCODE==NULL && (strcmp(s3, "WORD") == 0)) convert_WORD(s4,OBJECTCODE); else//OPCODE is RESW or RESB strcpy(OBJECTCODE, ""); if(strcmp(OBJECTCODE,"") != 0) fprintf(fp2, "|%s",OBJECTCODE);//Add object code to text } //Read next line fgets(line, 100, fp1); linecounter++; t = sscanf(line,"%s %s %s %s",s1,s2,s3,s4); if(t==3) { strcpy(s4, s3); strcpy(s3, s2); } } //Write End Record fprintf(fp2,"\n"); make_length(startADDRESS, temp1, 6); fprintf(fp2,"E|%s",temp1); fclose(fp1); fclose(fp2); strcpy(obj_file, im_file); obj_file[strlen(obj_file)-17] = '\0'; strcat(obj_file, "-object.txt"); if(main_flag == 1) //error in file remove(obj_file); //removing the object file as error has been detected }
void pass1(void){ char line[100]; char orig_line[100]; //SICXE_OP *instPtr; /* Read first input line */ fgets(line, 100, source_file); strcpy(orig_line, line); get_format(line); /* if OPCODE = 'START' then */ if(!strcmp(OPCODE, "START")){ /* Save #[OPERAND] as starting address (Relocation -> start at 0)*/ /* Initialize LOCCTR to starting address */ LOCCTR = 0; // Enable to be Relocation /* Write line to intermedite file */ insert_to_file(orig_line); line_Counter++; /* Read next line */ fgets(line, 100, source_file); strcpy(orig_line, line); get_format(line); } else{ /* Initialize LOCCTR to 0 */ LOCCTR = 0; } /* while OPCODE != 'END' do */ while(strcmp(OPCODE, "END")){ print_type = 1; // Normal PREV_LOCCTR = LOCCTR; /* if this is not a comment line then */ if(1){ /* if there is a symbol in the LABEL field then */ if(SYMBOL_ox == 1){ insert_SYMTAB(SYMBOL); } /* search OPTAB for OPCODE, if found then */ if(search_OPTAB(&instPtr)){ /* add instruction length to LOCCTR */ LOCCTR += (OPCODE_format); } /* else if OPCODE = 'WORD' */ else if(!strcmp(OPCODE, "WORD")){ /* add 3 to LOCCTR */ LOCCTR += 3; } /* else if OPCODE = 'RESW' */ else if(!strcmp(OPCODE, "RESW")){ /* add 3 * #[OPERAND] to LOCCTR */ LOCCTR += (3 * atoi(OPERAND1)); } /* else if OPCODE = 'RESB' */ else if(!strcmp(OPCODE, "RESB")){ /* add #[OPERAND] to LOCCTR */ LOCCTR += atoi(OPERAND1); } /* else if OPCODE = 'BYTE' */ else if(!strcmp(OPCODE, "BYTE")){ /* find length of constant in bytes */ /* add length to LOCCTR */ LOCCTR += (byte_Length()); } /* else if OPCODE = 'BASE' */ else if(!strcmp(OPCODE, "BASE")){ /* find length of constant in bytes */ /* add length to LOCCTR */ print_type = 2; // Directive } /* else */ else{ /* Set EORROR flag (invalid operation code) */ printf("EORROR! %s is Invalid Operation Code.\n", OPCODE); exit(0); } } /* Write line to intermediate file */ insert_to_file(orig_line); /* Read next line */ fgets(line, 100, source_file); strcpy(orig_line, line); get_format(line); line_Counter++; } /* Write last line to intermediate file */ print_type = 2; // Directive insert_to_file(orig_line); /* Save (LOCCTR - starting address) as program length */ program_Length = (LOCCTR - 0); return ; }