Esempio n. 1
0
// Check table
void tablecheck(char *inFile) {
	
	FILE *tableFile = fopen(inFile, "rb");
	static int const lineCount = 14;

	// Initialize counters
	int i;

	char *lineBuffer = malloc(200 * sizeof (char));

	if (tableFile == NULL) {
		printError(1);
	}

	for (i = 0; i < lineCount; i++) {
		fgets(lineBuffer, 200, tableFile);

		if (fDebug == 1) {
			debug(1, lineBuffer);
		}

		if (strncmp(lineBuffer, "IP=", 3) == 0) {			
			verifyLine(lineBuffer, 1);
		}
		else if (strncmp(lineBuffer, "E=", 2) == 0) {
			verifyLine(lineBuffer, 2);
		}
		else if (strncmp(lineBuffer, "P=", 2) == 0) {
			verifyLine(lineBuffer, 3);
		}
		else if (strncmp(lineBuffer, "S1=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S2=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S3=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S4=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S5=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S6=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S7=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "S8=", 3) == 0) {
			verifyLine(lineBuffer, 4);

		}
		else if (strncmp(lineBuffer, "V=",2) == 0) {
			verifyLine(lineBuffer, 12);

		}
		else if (strncmp(lineBuffer, "PC1=", 4) == 0) {
			verifyLine(lineBuffer, 13);

		}
		else if (strncmp(lineBuffer, "PC2=", 4) == 0) {
			verifyLine(lineBuffer, 14);

		}
		else {
			printError(2);
		}
		free(lineBuffer);
		
	}

	fclose(tableFile);
}
Esempio n. 2
0
//Does the assembly code verification, counting the instructions and data number.
int checksAssembly()
{
    FILE *file = NULL;
    char *line;
    char aux[300];

    //Number of lines
    int lineNumber = 1;
    //Counts the program lines(only instructions, directives and labels)
    int lineCount = 0;
    //Tells which part of the reader program is
    int module = 0, end = 0, pseg = 0, dseg = 0;
    //Save the directive code
    int directiveCode = -2;

    int instructionState = 0;

    file = fopen(urlInputFile, "r");

    while(fgets(aux, sizeof(aux), file)!= NULL)
    {
        //Verifies the line
        line = verifyLine(aux);

        //If is anything
        if(line!=NULL)
        {
            //Reads the directive code
            directiveCode = getDirective(line);

            //Asserts that the first instructions are the .module
            if(lineCount == 0 && directiveCode != 0){
                informError(1, lineNumber);
                fclose(file);
                return 1;
            }
            //If is .module
            if(directiveCode == 0){
                module = 1;
                end = 0;
            }
            //If is .pseg
            else if(directiveCode == 1){
                pseg = 1;
                dseg = 0;
            }
            //If is .dseg
            else if(directiveCode == 2){
            	if(pseg == 0){
            		informError(6, lineNumber);
            		fclose(file);
                	return 1;
				}
                pseg = 0;
                dseg = 1;
            }
            //If is .end
            else if(directiveCode == 4){
                module = 0;
                end = 1;
                lineCount = 0;
            }

            //If is a wrong directive
            else if(directiveCode == -2){
                informError(3, lineNumber);
                fclose(file);
                return 1;
            }

            //If is a instruction increments the number of instruction
            if(pseg == 1 && module == 1 && line[0] != '.' && directiveCode != 3){
                addCommand(line, lineNumber);
                numberOfInstructions++;
            }

            else if(pseg == 1 && module == 1 && line[0] == '.' && directiveCode == 3){
                informError(4, lineNumber);
                fclose(file);
                return 1;
            }

            //If is a word increments the number of instruction
            if(dseg == 1 && module == 1 && directiveCode == 3){
                addCommand(line, lineNumber);
                numberOfData++;
            }

            else if(dseg == 1 && module == 1 && directiveCode == -1){
                informError(5, lineNumber);
                fclose(file);
                return 1;
            }

            //Checks if the memory(64KB -> 16384 32-bits address) was exceeded
            if((numberOfData + numberOfInstructions) > 16384){
                informError(2, lineNumber);
                fclose(file);
                return 1;
            }

            if(end != 1)
                lineCount++;
        }
        lineNumber++;
    }
    fclose(file);
    return 0;
}