This commit is contained in:
nova 2024-02-29 22:53:11 +01:00
parent 25efa4317e
commit 997ca65f08
3 changed files with 57 additions and 60 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
all:
gcc ./main.c -std=c99
./a.out
d:
gcc -g -std=c99 ./main.c
gdb ./a.out

View File

@ -1,19 +1,8 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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, " ");
}
}

73
main.c
View File

@ -1,73 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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++;
}