From 3b094f55b88dd3f70cf55597722bdd150da711af Mon Sep 17 00:00:00 2001 From: nova Date: Sat, 23 Dec 2023 18:22:45 +0100 Subject: [PATCH] initial commit --- main.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src.asm | 6 ++++ 2 files changed, 91 insertions(+) create mode 100644 main.c create mode 100644 src.asm diff --git a/main.c b/main.c new file mode 100644 index 0000000..3032dd9 --- /dev/null +++ b/main.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +void bin_to_hex(char assembly_bin[], char * mem){ + + char out[18]; + char array[18]; + strcpy(array, assembly_bin); + int len = strlen(assembly_bin); + int trit; + //convert assembly to formated binary representation of ternary + for (int i = 0; i < len; i++) { + + trit = atoi(&array[strlen(array)-1]); + + if(trit==2) { + strcat(out, "10"); + } else if (trit==1) { + strcat(out, "01"); + } else { + strcat(out, "00"); + } + array[strlen(array)-1] = '\0'; + } + + printf("%s\n", out); + //converts formated binary to hex + char *a = out; + int num = 0; + do { + int b = *a=='1'?1:0; + num = (num<<1)|b; + a++; + } while (*a); + //printf("%.5x\n", num); + sprintf(mem, "%.5x", num); +} + + + +int main() { + + + FILE *dest_asm = fopen("assembly.txt", "w"); + FILE *src_asm = fopen("src.asm", "r"); + char width=1; + unsigned size=0; + + + fprintf(dest_asm,"00000 "); + fprintf(dest_asm,"00000 "); + fprintf(dest_asm,"00000 "); + fprintf(dest_asm,"00000\n"); + int eof; + while(!feof(src_asm)) { + size++; + char _asm[18]; + char out[5]; + fgets(_asm, 18, src_asm); + + bin_to_hex(_asm, _asm); + + + fprintf(dest_asm,"%s ", _asm); + // Write the hex to a file + if (width >= 3) { + fprintf(dest_asm,"00000\n"); + width = 0; + } + width++; + + } + + printf("\n%u trytes", size); + + + // Close the file + fclose(dest_asm); + fclose(src_asm); + + return 0; +} + diff --git a/src.asm b/src.asm new file mode 100644 index 0000000..23df217 --- /dev/null +++ b/src.asm @@ -0,0 +1,6 @@ +222222222 +111111111 +000000000 +012012012 +210210210 +121212121