Page 1 of 1

CODE NEED TO BE MODIFIED. I NEED HELP ON HOW TO PUT IN THE INPUT FILE. I WANT THE INPUT FILE SPECIFICALLY BE CALLED "INP

Posted: Tue Jul 12, 2022 8:17 am
by answerhappygod
CODE NEED TO BE MODIFIED. I NEED HELP ON HOW TO PUT INTHE INPUT FILE. I WANT THE INPUT FILE SPECIFICALLY BE CALLED"INPUT.TXT" AND THAT IT CAN BE READ TO PROCESS AND GIVE THE OUTPUTBELOW MENTIONED. NO COPYING AND PASTING WILL RECIEVE A DOWNVOTE!JUST NEED TO CHANGE AND ADD A COUPLE OF CODE TO BE ABLE TO READ THEINPUT FILE FOR C PROGRAM, NOT ASM FILE.
Input needs to be like this:
your simulator must run from the command line with a singleinput file as a parameter to main. This filewill contain a sequence of instructions for your simulator to storein “Instruction Memory” and then runvia the fetch/execute cycle. In the input file each instruction isrepresented with two integers: the first onerepresents the opcode and the second one a memory address or adevice number depending on theinstruction.
Example:Input File5 5 //IN 56 7 //OUT 73 0 //STORE 05 5 //IN 56 7 //OUT 73 1 //STORE 11 0 //LOAD 04 1 //SUB 13 0 //STORE 06 7 //OUT 71 1 //LOAD 16 7 //OUT 77 0 //END
Output needs to be like this:
Your simulator should provide output according to the inputfile. Along with this output your programshould provide status messages identifying details on the workingsof your simulator. Output text doesnot have to reflect my example word-for-word, but please providedetail on the program as it runs in areadable format that does not conflict with the actual output ofyour simulator. After each instruction printthe current state of the Program Counter, Accumulator, and DataMemory. The INPUT instruction is theonly one that should prompt an interaction from the user.
Example:
Assembling Program...Program Assembled.Run.PC = 10 | A = NULL | DM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]/* input value */XPC = 11 | A = X | DM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]/* outputting accumulator to screen */XPC = 12 | A = X | DM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* storing accumulator to memory location 0 */PC = 13 | A = X | DM = [X, 0, 0, 0, 0, 0, 0, 0, 0, 0]... etcProgram complete
Here is my code so far:
// #define _CRT_SECURE_NO_WARNINGS#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>
// Enumeration for stateenum State {LOAD = 1,ADD = 2,STORE = 3,SUB = 4,IN = 5,OUT = 6,END = 7,JMP = 8,SKIPZ = 9};
// Creating Instruction structuretypedef struct {int opCode; // to store opcodeint deviceOrAddress; // address of the operation} Instruction;
// Global variableint CODESIZE = 13;
// Tiny Machine Architecture variablesint pc = 10; // for program counter
int ir = 0; // for instruction registerint mar = 0; // to store address of memoryint dataMemory[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int mdr = 0; // to store data of memoryint ac = 0; // accumulator
// function for print the tinny architecutervariblesvoid printVaribles() {int i;printf("PC: %d | A: %d | MEM: [", pc, ac);// print data in memoryfor (i = 0; i < 9; i += 1) {printf("%d,", dataMemory);}printf("]\n");}
// Function used to count the amount of lines in text"in.txt"int getNumberOfLineInFile(FILE *fp) {char buffer[10];int count = 0;
// check if file pointer is validif (fp == NULL) {printf("File is not opening...\n");// TIP: For errors you should exit with EXIT_FAILURE statusexit(EXIT_FAILURE);}
// caluclate the number of linewhile (fgets(buffer, 10, fp) != NULL) {// increament countcount++;}
// close file after readingfclose(fp);
// return countreturn count;}
// Function used to parse instruction into machinecodevoid tinyMachineSimulator(int opCode, int b) {switch (opCode) {case LOAD:printf("/* Loading from address [%d]... */\n", b);ir = b;mar = ir;mdr = dataMemory[mar];ac = mdr;// Print value in tinny macine variblesprintVaribles();pc += 1;printf("/* PC <- PC + 1 */\n");printf("/* PC <- PC + 1 */\n");printf("/* MAR <- IR.ADDR */\n");printf("/* MDR <- MEM[MAR] */\n");printf("/* A <- MDR */\n");break;
case ADD:printf("/Adding accumulator and value obtain from address [%d]/\n",b);ir = b;mar = ir;mdr = dataMemory[mar];ac += mdr;// Print value in tinny macine variblesprintVaribles();pc += 1;break;
case STORE:printf("/* storing accumulator to memory location 0 */\n");mdr = ac;ir = b;mar = ir;dataMemory[mar] = mdr;// Print value in tinny macine variblesprintVaribles();pc += 1;break;
case SUB:printf("/* Subtracting memory address value [%d] fromaccumulator*/\n", b);ir = b;mar = ir;mdr = dataMemory[mar];ac -= mdr;// Print value in tinny macine variblesprintVaribles();pc += 1;break;
case IN:printf("/Please input value:/\n");scanf("%d", &ac);// Print value in tinny macine variblesprintVaribles();pc += 1;break;
case OUT:printf("/*Accumulator current value = %d */\n", ac);// Print value in tinny macine variblesprintVaribles();pc += 1;break;
case END:printf("Program complete\n");exit(1);
case JMP:// *Jump to addressprintf("/Setting program counter to %d/\n", b);pc = b;// Print value in tinny macine variblesprintVaribles();break;
case SKIPZ:// Check if accumulator is 0,printf("/Skipping the next instruction/\n");// if it is skip next instructionif (ac == 0) {// increament PC by 2pc += 2;} else {// otherwise// increament PC by 1pc += 1;}// Print value in tinny macine variblesprintVaribles();break;
default:printf("/There was an error parsing that opcode! Exitingprogram/\n");exit(0);}}
// driver functionint main(int argc, char *argv[]) {// NOTE: validate number of command line argumentsif (argc < 2) {printf("Filename is required in command line arguments.\n");exit(EXIT_FAILURE);}
// This is where we get the number of lines in thecode// This is not required// CODESIZE = getNumberOfLineInFile(fopen(argv[1],"r"));
// This creates array for storing instructionsInstruction programMemory[CODESIZE];
// Reading a text file line by lineFILE *fp = fopen(argv[1], "r");// check if file was successfully openedif (fp == NULL) {printf("File is not opening...\n");// TIP: For errors you should exit with EXIT_FAILURE statusexit(EXIT_FAILURE);}
char buffer[50];int i = 0;// read line from inputfile and check wether each line contains twovalues// NOTE: You should specify the maximum number of characters to beread// not the number of lineswhile (fgets(buffer, sizeof(buffer), fp) != NULL) {// remove the trailing newline from the bufferbuffer[strlen(buffer) - 1] = '\0';// check if this is an empty lineif (strlen(buffer) == 0)continue;
// validate the format of the inputif (!(isdigit(buffer[0]) && isspace(buffer[1]) &&isdigit(buffer[2])))continue;
// parse the values// TIP: A digit can be converted to an int by subtracting '0'programMemory.opCode = buffer[0] - '0';programMemory.deviceOrAddress = buffer[2] - '0';// increment the indexi++;}
// Optional: Print the content read from filefor (int j = 0; j < i; j++) {printf("[DEBUG] programMemory[%d] = {opCode: %d, deviceOrAddress:%d}\n", j,programMemory[j].opCode, programMemory[j].deviceOrAddress);}
// Need to close the filefclose(fp);
// Display outputprintf("Tiny Machine Simulator\n");printf("Assembling program...\n");printf("Program assembled. Run.\n");
// Print intial value in tinny macine variblesprintVaribles();
// Get the instruction from programMemoryfor (i = (pc / 10) - 1; i < sizeof(programMemory); i += 1){// call function and passing programMemory value into our parserfunctiontinyMachineSimulator(programMemory.opCode,programMemory.deviceOrAddress);}
printf("Program concluded...\n");// system("pause");return 0;}