86 lines
1.4 KiB
C
86 lines
1.4 KiB
C
#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;
|
|
}
|
|
|