tern-assembler/main.c

165 lines
3.4 KiB
C

#include <stdio.h>
#include "asm_rules.c"
void bin_to_hex(char *tryte, int *hex);
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out);
int params(int argc, char **argv);
FILE *dest_asm;
FILE *src_asm;
int main(int argc, char **argv) {
int p = params(argc, argv);
if (p==1) {
return 1;
}
unsigned int size=0; //keeps the size of the assembled program
char in_asm[64];
char out_asm[27];
int hex_op;
int hex_in0;
int hex_in1;
int hex_out;
while(fgets(in_asm, sizeof(in_asm), src_asm)) {
hex_op = 0;
hex_in0 = 0;
hex_in1 = 0;
hex_out = 0;
if (in_asm[strlen(in_asm)-1] == '\n') {
in_asm[strlen(in_asm)-1] = '\0'; //cleans newline from string
}
if (!(in_asm[0] == '\0')){ // if in_asm is an empty, skip
asm_rules(in_asm, out_asm);
asm_to_hex(out_asm, &hex_op, &hex_in0, &hex_in1, &hex_out);
printf("%.7x %.7x %.7x %.7x\n", hex_op, hex_in0, hex_in1, hex_out);
// Write the hex to a file
fprintf(dest_asm,"%.7x ", hex_op);
fprintf(dest_asm,"%.7x ", hex_in0);
fprintf(dest_asm,"%.7x ", hex_in1);
fprintf(dest_asm,"%.7x \n", hex_out);
size = size + 2; //1 word = 2trytes
}
}
printf("\nfilesize: %u trytes\n\n", size);
// Close the file
fclose(dest_asm);
fclose(src_asm);
return 0;
}
int params(int argc, char **argv){
//{{{
printf("%s\n", *argv);
char in = 0;
char out = 0;
char ret = 1;
if (argc > 2) {
for (int i = 1 ; i < argc; i++) {
if (strcmp("-i", argv[i]) == 0 || strcmp("--input", argv[i]) == 0) {
if (argv[i+1]==NULL) {
//empty
} else {
in = 1;
if((src_asm = fopen(argv[i+1],"r"))!=NULL) {
//src_asm = fopen(argv[i+1], "r");
i=i+1;
ret = 0;
} else {
printf("tasm: file '%s' does not exist \n", argv[i+1]);
ret = 1;
}
}
} else if (strcmp("-o", argv[i]) == 0 || strcmp("--output", argv[i]) == 0) {
if (argv[i+1]==NULL) {
//empty
} else {
dest_asm = fopen(argv[i+1], "w");
i=i+1;
out = 1;
ret = 0;
}
} else if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
printf("Usage: tasm [options] file...\n");
printf("Options:\n");
printf("-h,--help Prints this information\n");
printf("-i,--input <arg> Path of input file\n");
printf("-o,--output <arg> Path of output file\n");
} else {
printf("unknown argument: %s\n", argv[i]);
}
}
}
if (in == 0) {
printf("tasm: no input file\n");
ret = 1;
} else {
if (out == 0) {
printf("tasm: no output file, using default: output\n\n");
dest_asm = fopen("output", "w");
}
}
return ret;
//}}}
}
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
char tryte[54];
char t0[27];
char t1[26];
char t2[26];
char t3[26];
for (int i = 0; i < 18; i++) {
t0[i] = 0;
t1[i] = 0;
t2[i] = 0;
t3[i] = 0;
}
tryte[0] = '\0';
//convert assembly to formated binary representation of ternary
int i = 0;
do {
if(_asm[i]=='-') {
strcat(tryte, "10");
} else if (_asm[i]=='+') {
strcat(tryte, "01");
} else {
strcat(tryte, "00");
}
i++;
} while (i<13);
int j = 0;
for (int i = 0; i < 27; i++) {
t0[i] = tryte[j];
j++;
}
bin_to_hex(t0, opcode);
bin_to_hex(t1, in0);
bin_to_hex(t2, in1);
bin_to_hex(t3, out);
}
void bin_to_hex(char *tryte, int *hex) {
for (int i = 0; i < 26; ++i){
if (tryte[i] == '1' ){
*hex = (*hex << 1)|1;
} else {
*hex = (*hex << 1)|0;
}
}
}