initial commit

This commit is contained in:
nova 2023-12-23 18:22:45 +01:00
parent 2b23ed0832
commit 3b094f55b8
2 changed files with 91 additions and 0 deletions

85
main.c Normal file
View File

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

6
src.asm Normal file
View File

@ -0,0 +1,6 @@
222222222
111111111
000000000
012012012
210210210
121212121