Question
Help! I'm writing an assembler to take in assembly code and write out the machine code but I keep getting the error Invalid file path.
Help! I'm writing an assembler to take in assembly code and write out the machine code but I keep getting the error "Invalid file path". I don't want to use hashmaps anymore, I want to use arrays only but I'm not sure how to switch from hashmaps to array
#include
#include
#include
int RegisterValidity(char*); int main(int argc, char **argv) { StrMap *sm; char buf[300]; sm = sm_new(10); int result;
if(argc == 1){ printf("No input given. "); return 0; }else if (argc > 1 && argc <= 3){
FILE *f; f = fopen(argv[1], "r");
if (f == NULL){ printf("Invalid file or path. "); exit(0); }
char write[2048]; //char array line by line int mark = 0; int firstChar = 1;//to denote first line char
while(fgets(write, 2048, f) != NULL) { // Checking if label is invalid char* token = strtok(write," \t "); const char *special_characters = "`~@#$%^&*()+=<>,.?/{}[]|";
while (token) { if(mark != firstChar) { if(strcmp(token, "beq") != 0 && strcmp(token, "add") != 0 && strcmp(token, "nand") && strcmp(token, "lw")!= 0 && strcmp(token, "sw") != 0 && strcmp(token, "jalr")!=0 && strcmp(token, "halt") != 0 && strcmp(token, "noop")!= 0 && strcmp(token, ".fill") != 0) {
// Checking for invalid labels -- Duplicates if(sm_exists(sm, token) == 1) { fprintf(stderr, "Duplicate labels cannot be entered. "); return 0; }
char str[128]; sprintf(str, "%d", mark); sm_put(sm, opcode, str); }//inner statement firstChar = mark; //so mark and firstChar only diverge for first token in a line } //printf("%s ", token); token = strtok(NULL, " \t "); } mark++; }//while fseek(f, 0, SEEK_SET);
char parse[2048]; int instructions[mark]; //integer array of instructions int linecounter = 0;//counts lines int inst;
while(fgets(parse, 2048, f) != NULL) { char* tok = strtok(parse, " \t "); if(sm_exists(sm, tok)!=0 || tok == NULL){ tok = strtok(NULL, " \t "); }// if first tok is a label, move on
if(strcmp(tok, "add")==0) { inst = 0 << 22; tok = strtok(NULL, " \t "); if(RegisterValidity(tok) == 0) {return 0;} inst = atoi(tok) | inst; tok = strtok(NULL, " \t "); if(RegisterValidity(tok) == 0) {return 0;} inst = atoi(tok)<<19 | inst; tok = strtok(NULL, " \t "); if(RegisterValidity(tok) == 0) {return 0;} inst = atoi(tok)<<16 | inst; instructions[linecounter] = inst; }
if(strcmp(tok, "nand")==0) { inst = 1 << 22; tok = strtok(NULL, " \t "); if(RegisterValidity(tok) == 0) {return 0;} inst = atoi(tok) | inst; tok = strtok(NULL, " \t "); if(RegisterValidity(tok) == 0) {return 0;} inst = atoi(tok)<<19 | inst; tok = strtok(NULL, " \t "); if(RegisterValidity(tok) == 0) {return 0;} inst = atoi(tok)<<16 | inst; instructions[linecounter] = inst; }
while(tok) { tok = strtok(NULL," \t "); } linecounter++; } fclose(f);
if(argc == 3) { //write out results to input file FILE *fp; fp = fopen(argv[2], "w"); int i = 0; while(i < linecounter) { fprintf(fp, "%d ", instructions[i]); i++; } fclose(fp); } else { int i = 0; while(i < linecounter) { printf("%d ", instructions[i]); i++; } }//print to file
} else { printf("Only enter two file inputs. "); } sm_delete(sm); return 0; }//main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started