From 67c050abbc5b62c294607c874a8dcb3fc279d54f Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 17 Mar 2024 17:16:23 +0100 Subject: [PATCH] command line arguments, cleanup --- Makefile | 6 ++---- main.c | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 1e82d76..4f4d699 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,5 @@ all: - gcc ./main.c -std=c99 - ./a.out + gcc ./main.c -std=c99 -o tasm d: - gcc -g -std=c99 ./main.c - gdb ./a.out + gcc -g -std=c99 ./main.c -o tasm diff --git a/main.c b/main.c index 5193ea5..8d04f3e 100644 --- a/main.c +++ b/main.c @@ -67,12 +67,36 @@ void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){ } -int main() { - FILE *dest_asm = fopen("assembly.txt", "w"); - FILE *src_asm = fopen("src.asm", "r"); - unsigned int size=0; +int main(int argc, char **argv) { - fprintf(dest_asm,"00000\n"); + FILE *dest_asm; + FILE *src_asm; + if (argc > 2) { + for (int i = 1 ; i < argc; i++) { + if (strcmp("-i", argv[i]) == 0 || strcmp("--input", argv[i]) == 0) { + src_asm = fopen(argv[i+1], "r"); + i=i+1; + } else if (strcmp("-o", argv[i]) == 0 || strcmp("--output", argv[i]) == 0) { + dest_asm = fopen(argv[i+1], "w"); + i=i+1; + } else if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) { + printf("Usage: tasm [options] file...\n"); + printf("Options:\n"); + printf("-h,--help Prints this information\n"); + printf("-i,--input Path of input file\n"); + printf("-o,--output Path of output file\n"); + return 0; + } else { + printf("unknown argument: %s\n", argv[i]); + return 1; + } + } + } else { + printf("tasm: no input file\n"); + return 1; + } + + unsigned int size=0; //keeps the size of the assembled program char in_asm[64]; char out_asm[27]; @@ -81,10 +105,9 @@ int main() { int hex_in1; int hex_out; - fgets(in_asm, 64, src_asm); - while(!feof(src_asm)) { + while(fgets(in_asm, sizeof(in_asm), src_asm)) { hex_op = 0; hex_in0 = 0; hex_in1 = 0; @@ -101,7 +124,6 @@ int main() { fprintf(dest_asm,"%.5x \n", hex_out); // Write the hex to a file - fgets(in_asm, 64, src_asm); size++; }