tern-assembler/main.c
2024-02-29 22:53:11 +01:00

78 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "asm_rules.c"
//void trit_to_hex(char *_asm, char **_out){
void trit_to_hex(char *_asm, char _out[27]){
char tryte[54];
for (int i = 0; i < 54; i++) {
tryte[i] = 0;
}
//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);
//sprintf(_out, "%x", num);
}
int main() {
FILE *dest_asm = fopen("assembly.txt", "w");
FILE *src_asm = fopen("src.asm", "r");
unsigned int size=0;
fprintf(dest_asm,"00000\n");
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);
fprintf(dest_asm,"%s ", _out);
// Write the hex to a file
fgets(in_asm, 54, src_asm);
free(_out);
size++;
}
printf("\n%u trytes", size);
// Close the file
fclose(dest_asm);
fclose(src_asm);
return 0;
}