85 lines
1.4 KiB
C
85 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include "asm_rules.c"
|
|
|
|
void trit_to_hex(char *_asm, char * mem){
|
|
|
|
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';
|
|
}
|
|
|
|
//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);
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
while(!feof(src_asm)) {
|
|
|
|
asm_rules(in_asm, out_asm);
|
|
trit_to_hex(out_asm, out_asm);
|
|
|
|
fprintf(dest_asm,"%s ", out_asm);
|
|
// Write the hex to a file
|
|
if (width >= 4) {
|
|
fprintf(dest_asm,"\n");
|
|
width = 0;
|
|
}
|
|
|
|
fgets(in_asm, 50, src_asm);
|
|
width++;
|
|
size++;
|
|
|
|
}
|
|
|
|
printf("\n%u trytes", size);
|
|
|
|
|
|
// Close the file
|
|
fclose(dest_asm);
|
|
fclose(src_asm);
|
|
|
|
return 0;
|
|
}
|
|
|