diff --git a/src/main.c b/src/main.c index c2e29ec..4924fb4 100644 --- a/src/main.c +++ b/src/main.c @@ -26,68 +26,40 @@ int error(char * msg) { return 1; } -int printHex(struct instruction* current) { +void printHex(struct instruction* current) { while(current->nextInstruction != NULL) { printf("0x%s ", convert(current)); current = current->nextInstruction; } - free(current); - return 1; } -int writeHex(struct instruction * current) { +void writeHex(struct instruction * current) { + char output[25]; + printf("What is the output file name (ends in .hex): "); + scanf("%s", output); + + FILE * file = fopen(output, "w"); + if(file == NULL) printf("error!"); while(current->nextInstruction != NULL) { - printf("0x%s ", convert(current)); + fprintf(file, "0x%s ", convert(current)); current = current->nextInstruction; } - return 1; // TODO: Need to write the output to the file. } -int main(int argc, char* args[]) { - if(argc < 2) return error("There must be at least one argument."); - if(strcmp(args[1], "-h") == 0 || strcmp(args[1], "--help") == 0) return help(); - +int main() { char * file; - //FILE * outputFile; + char * output; - // If the only argument is the input file, then set the FILE pointer. - if(argc == 2) { - printf("only 1 true arg, %s", args[1]); - file = args[1]; - } + printf("Welcome to the MIPS assembly to hexadecimal converter!\nPlease enter the input filename: "); + scanf("%s", file); - struct instruction * head = initParse(file); + printHex(initParse(file)); + printf("\n\n"); + writeHex(initParse(file)); - return printHex(head); - //if(outputFile != NULL) return writeHex(head, outputFile); - //else return printHex(head); + return 0; } - - - /** - // If there are 5 arguments total, then we should have the flag for the hex output file in position 4. - } else if(argc == 5) { - printf("4 true args, %s, %s", args[3], args[4]); - if(strcmp(args[3], "-x") == 0 || strcmp(args[3], "--hex") == 0) - outputFile= fopen(args[4], "w"); - else return error("invalid argument format."); - - // Catch if the file does not exist/cannot be opened. - if(outputFile == NULL) return error("output file cannot be created or written. check permissions."); - } - - // If there are at least 3 arguments, then we should have the flag for the assembly input file in position 1. - if(argc >= 3) { - printf("at least 2 true args, %s, %s", args[1], args[2]); - if(strcmp(args[1], "--asm") == 0 || strcmp(args[1], "-a") == 0) { - file = fopen(args[2], "r"); - outputFile = NULL; - } else return error("invalid argument format."); - } - */ - - // Catch if the file does not exist/cannot be opened. diff --git a/src/output.hex b/src/output.hex index e69de29..b0b5cc9 100644 --- a/src/output.hex +++ b/src/output.hex @@ -0,0 +1 @@ +0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000024 0x00000020 0x00000020 0x00000020 0x00000020 0x00000024 0x00000020 0x00000024 0x00000020 0x00000020 0x00000020 0x00000024 0x00000020 0x00000024 0x00000020 0x0000F821 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x012A5820 0x012A5820 0x012A5820 0x01014023 0x01014020 0x01014020 0x01014020 0x0000F821 \ No newline at end of file diff --git a/src/test.hex b/src/test.hex new file mode 100644 index 0000000..b0b5cc9 --- /dev/null +++ b/src/test.hex @@ -0,0 +1 @@ +0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000020 0x00000024 0x00000020 0x00000020 0x00000020 0x00000020 0x00000024 0x00000020 0x00000024 0x00000020 0x00000020 0x00000020 0x00000024 0x00000020 0x00000024 0x00000020 0x0000F821 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x0000F820 0x012A5820 0x012A5820 0x012A5820 0x01014023 0x01014020 0x01014020 0x01014020 0x0000F821 \ No newline at end of file