From 35f30e07c2409cb963492e14a8ac5f1a958c7f3c Mon Sep 17 00:00:00 2001 From: nova Date: Mon, 19 Feb 2024 22:25:25 +0100 Subject: [PATCH] very basic assembly --- asm_rules.c | 42 ++++++++++++++++++++++++++++++++++++++++++ main.c | 45 ++++++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 asm_rules.c diff --git a/asm_rules.c b/asm_rules.c new file mode 100644 index 0000000..478dbc1 --- /dev/null +++ b/asm_rules.c @@ -0,0 +1,42 @@ +#include +#include +#include + +void asm_rules(char *in, char _asm[9]){ + + char src_addr[9]; + char dest_addr[9]; + char tryte[9]; + + for (int i = 0; i < 9; i++) { + _asm[i] = 0; + } + + char *instruction = (char*) malloc(10*sizeof(char)); + int instruction_location = 0; + char *word = strtok(in, " "); + + while(word != NULL) { + + if (word[strlen(word)-1] == '\n') { + word[strlen(word)-1] = '\0'; //cleans newline from string + } + + if (strcmp(word, "nop") == 0) { + //keeps the default value (0) + } else { + if (strcmp(word, "max") == 0) { + _asm[6] = 1; + //printf("%c\n", tryte[6]); + } + } + + for (int i = 0; i < 9; i++) { + instruction[instruction_location] = _asm[i]; + instruction_location++; + } + instruction[instruction_location] = ' '; + word = strtok(NULL, " "); + + } +} diff --git a/main.c b/main.c index 3032dd9..c52e5e6 100644 --- a/main.c +++ b/main.c @@ -2,19 +2,18 @@ #include #include #include +#include "asm_rules.c" -void bin_to_hex(char assembly_bin[], char * mem){ +void trit_to_hex(char *_asm, char * mem){ char out[18]; - char array[18]; - strcpy(array, assembly_bin); - int len = strlen(assembly_bin); int trit; + int len = strlen(_asm); //convert assembly to formated binary representation of ternary - for (int i = 0; i < len; i++) { + for (int i = 0; i < 9; i++) { - trit = atoi(&array[strlen(array)-1]); - + trit = _asm[i]; + //printf("%d\n",trit); if(trit==2) { strcat(out, "10"); } else if (trit==1) { @@ -22,10 +21,9 @@ void bin_to_hex(char assembly_bin[], char * mem){ } else { strcat(out, "00"); } - array[strlen(array)-1] = '\0'; + //array[strlen(array)-1] = '\0'; } - printf("%s\n", out); //converts formated binary to hex char *a = out; int num = 0; @@ -39,37 +37,38 @@ void bin_to_hex(char assembly_bin[], char * mem){ } +char out_asm[9] = {0,0,0,0,0,0,0,0,0}; int main() { - - FILE *dest_asm = fopen("assembly.txt", "w"); FILE *src_asm = fopen("src.asm", "r"); char width=1; - unsigned size=0; - + unsigned int size=0; fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000\n"); - int eof; + + char in_asm[50]; + int addresses; + fgets(in_asm, 50, src_asm); + while(!feof(src_asm)) { - size++; - char _asm[18]; - char out[5]; - fgets(_asm, 18, src_asm); - bin_to_hex(_asm, _asm); + asm_rules(in_asm, out_asm); + trit_to_hex(out_asm, out_asm); - - fprintf(dest_asm,"%s ", _asm); + fprintf(dest_asm,"%s ", out_asm); // Write the hex to a file - if (width >= 3) { - fprintf(dest_asm,"00000\n"); + if (width >= 4) { + fprintf(dest_asm,"\n"); width = 0; } + + fgets(in_asm, 50, src_asm); width++; + size++; }