diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1e82d76 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: + gcc ./main.c -std=c99 + ./a.out + +d: + gcc -g -std=c99 ./main.c + gdb ./a.out diff --git a/asm_rules.c b/asm_rules.c index 478dbc1..5a6a3f5 100644 --- a/asm_rules.c +++ b/asm_rules.c @@ -1,19 +1,8 @@ -#include #include -#include -void asm_rules(char *in, char _asm[9]){ +void asm_rules(char *in, char _asm[27]){ - 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) { @@ -26,17 +15,25 @@ void asm_rules(char *in, char _asm[9]){ //keeps the default value (0) } else { if (strcmp(word, "max") == 0) { - _asm[6] = 1; - //printf("%c\n", tryte[6]); + _asm[6] = 1; + } else if (strcmp(word, "min")) { + _asm[6] = 2; + } else if (strcmp(word, "min")) { + _asm[6] = 2; + } else if (strcmp(word, "xor")) { + //_asm[5] = 1; + for (int i = 0; i < 27; i++) { + _asm[i]=2; + } + } else if (strcmp(word, "any")) { + _asm[6] = 2; + _asm[5] = 1; + } else if (strcmp(word, "cons")) { + _asm[6] = 1; + _asm[5] = 1; } } - 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 c52e5e6..027d0be 100644 --- a/main.c +++ b/main.c @@ -1,73 +1,66 @@ #include #include -#include -#include #include "asm_rules.c" -void trit_to_hex(char *_asm, char * mem){ +//void trit_to_hex(char *_asm, char **_out){ +void trit_to_hex(char *_asm, char _out[27]){ - char out[18]; - int trit; - int len = strlen(_asm); - //convert assembly to formated binary representation of ternary - for (int i = 0; i < 9; i++) { - - trit = _asm[i]; - //printf("%d\n",trit); - if(trit==2) { - strcat(out, "10"); - } else if (trit==1) { - strcat(out, "01"); - } else { - strcat(out, "00"); - } - //array[strlen(array)-1] = '\0'; + char tryte[54]; + for (int i = 0; i < 54; i++) { + tryte[i] = 0; } - //converts formated binary to hex - char *a = out; + //convert assembly to formated binary representation of ternary + for (int i = 0; i < 27; i++) { + + printf("%c",_asm[i]); + if(_asm[i]==2) { + strcat(tryte, "10"); + } else if (_asm[i]==1) { + strcat(tryte, "01"); + } else { + strcat(tryte, "00"); + } + } + + //todo: convert tryte to hex + //*_out = (char*)calloc(8,sizeof(int)); + char *a = tryte; 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); + + //sprintf(_out, "%x", num); } -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 int size=0; - fprintf(dest_asm,"00000 "); - fprintf(dest_asm,"00000 "); - fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000\n"); - - char in_asm[50]; - int addresses; - fgets(in_asm, 50, src_asm); + char in_asm[54]; + char out_asm[27]; + + fgets(in_asm, 54, src_asm); + char *_out; while(!feof(src_asm)) { asm_rules(in_asm, out_asm); - trit_to_hex(out_asm, out_asm); + trit_to_hex(out_asm, _out); - fprintf(dest_asm,"%s ", out_asm); + fprintf(dest_asm,"%s ", _out); // Write the hex to a file - if (width >= 4) { - fprintf(dest_asm,"\n"); - width = 0; - } - fgets(in_asm, 50, src_asm); - width++; + fgets(in_asm, 54, src_asm); + free(_out); size++; }