//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 }
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 ; }