tern-assembler/main.c

78 lines
1.2 KiB
C
Raw Normal View History

2023-12-23 18:22:45 +01:00
#include <stdio.h>
#include <stdlib.h>
2024-02-19 22:25:25 +01:00
#include "asm_rules.c"
2023-12-23 18:22:45 +01:00
2024-02-29 22:53:11 +01:00
//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;
}
2023-12-23 18:22:45 +01:00
//convert assembly to formated binary representation of ternary
2024-02-29 22:53:11 +01:00
for (int i = 0; i < 27; i++) {
2023-12-23 18:22:45 +01:00
2024-02-29 22:53:11 +01:00
printf("%c",_asm[i]);
if(_asm[i]==2) {
strcat(tryte, "10");
} else if (_asm[i]==1) {
strcat(tryte, "01");
2023-12-23 18:22:45 +01:00
} else {
2024-02-29 22:53:11 +01:00
strcat(tryte, "00");
2023-12-23 18:22:45 +01:00
}
}
2024-02-29 22:53:11 +01:00
//todo: convert tryte to hex
//*_out = (char*)calloc(8,sizeof(int));
char *a = tryte;
2023-12-23 18:22:45 +01:00
int num = 0;
do {
int b = *a=='1'?1:0;
num = (num<<1)|b;
a++;
} while (*a);
2024-02-29 22:53:11 +01:00
//sprintf(_out, "%x", num);
2023-12-23 18:22:45 +01:00
}
2024-02-29 22:53:11 +01:00
2023-12-23 18:22:45 +01:00
int main() {
FILE *dest_asm = fopen("assembly.txt", "w");
FILE *src_asm = fopen("src.asm", "r");
2024-02-19 22:25:25 +01:00
unsigned int size=0;
2023-12-23 18:22:45 +01:00
fprintf(dest_asm,"00000\n");
2024-02-29 22:53:11 +01:00
char in_asm[54];
char out_asm[27];
fgets(in_asm, 54, src_asm);
char *_out;
2023-12-23 18:22:45 +01:00
2024-02-19 22:25:25 +01:00
while(!feof(src_asm)) {
2023-12-23 18:22:45 +01:00
2024-02-19 22:25:25 +01:00
asm_rules(in_asm, out_asm);
2024-02-29 22:53:11 +01:00
trit_to_hex(out_asm, _out);
2023-12-23 18:22:45 +01:00
2024-02-29 22:53:11 +01:00
fprintf(dest_asm,"%s ", _out);
2023-12-23 18:22:45 +01:00
// Write the hex to a file
2024-02-19 22:25:25 +01:00
2024-02-29 22:53:11 +01:00
fgets(in_asm, 54, src_asm);
free(_out);
2024-02-19 22:25:25 +01:00
size++;
2023-12-23 18:22:45 +01:00
}
printf("\n%u trytes", size);
// Close the file
fclose(dest_asm);
fclose(src_asm);
return 0;
}