fixed input/output of files, parsing needs additional work

This commit is contained in:
Josh Ashton
2023-10-17 19:42:45 -06:00
parent bf8e88a7e8
commit 01d483cf8d
3 changed files with 19 additions and 45 deletions
+19 -47
View File
@@ -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);
printHex(initParse(file));
printf("\n\n");
writeHex(initParse(file));
return 0;
}
struct instruction * head = initParse(file);
return printHex(head);
//if(outputFile != NULL) return writeHex(head, outputFile);
//else return printHex(head);
}
/**
// 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.
+1
View File
@@ -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
+1
View File
@@ -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