119 lines
2.5 KiB
C
Raw Normal View History

2023-12-23 18:22:45 +01:00
#include <stdio.h>
2024-02-19 22:25:25 +01:00
#include "asm_rules.c"
2023-12-23 18:22:45 +01:00
2024-03-12 10:18:56 +01:00
void bin_to_hex(char *tryte, int *hex) {
2024-03-11 23:09:47 +01:00
char *a = tryte;
long num = 0;
2024-03-11 23:09:47 +01:00
do {
long b = *a=='1'?1:0;
2024-03-11 23:09:47 +01:00
num = (num<<1)|b;
a++;
} while (*a);
2024-03-12 10:18:56 +01:00
*hex = num;
2024-03-11 23:09:47 +01:00
}
2024-03-12 10:18:56 +01:00
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
2024-02-29 22:53:11 +01:00
char tryte[108] = "\0";
char t0[26] = "\0";
char t1[26] = "\0";
char t2[26] = "\0";
char t3[26] = "\0";
2023-12-23 18:22:45 +01:00
int size = sizeof(_asm)/sizeof(_asm[0]);
2023-12-23 18:22:45 +01:00
//convert assembly to formated binary representation of ternary
for (int i = 0; i < sizeof(_asm)/4; i++) {
2023-12-23 18:22:45 +01:00
2024-03-11 23:09:47 +01:00
//printf("%c",_asm[i]);
if(_asm[i]=='-') {
2024-02-29 22:53:11 +01:00
strcat(tryte, "10");
} else if (_asm[i]=='+') {
2024-02-29 22:53:11 +01:00
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
2024-03-12 10:18:56 +01:00
bin_to_hex(t0, opcode);
bin_to_hex(t1, in0);
bin_to_hex(t2, in1);
bin_to_hex(t3, out);
2023-12-23 18:22:45 +01:00
}
2024-03-17 17:16:23 +01:00
int main(int argc, char **argv) {
FILE *dest_asm;
FILE *src_asm;
if (argc > 2) {
for (int i = 1 ; i < argc; i++) {
if (strcmp("-i", argv[i]) == 0 || strcmp("--input", argv[i]) == 0) {
src_asm = fopen(argv[i+1], "r");
i=i+1;
} else if (strcmp("-o", argv[i]) == 0 || strcmp("--output", argv[i]) == 0) {
dest_asm = fopen(argv[i+1], "w");
i=i+1;
} 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");
return 0;
} else {
printf("unknown argument: %s\n", argv[i]);
return 1;
}
}
} else {
printf("tasm: no input file\n");
return 1;
}
2023-12-23 18:22:45 +01:00
2024-03-17 17:16:23 +01:00
unsigned int size=0; //keeps the size of the assembled program
2024-03-11 23:09:47 +01:00
char in_asm[64];
2024-02-29 22:53:11 +01:00
char out_asm[27];
2024-03-12 10:18:56 +01:00
int hex_op;
int hex_in0;
int hex_in1;
int hex_out;
2024-03-11 23:09:47 +01:00
2024-03-17 17:16:23 +01:00
while(fgets(in_asm, sizeof(in_asm), src_asm)) {
2024-03-12 10:18:56 +01:00
hex_op = 0;
hex_in0 = 0;
hex_in1 = 0;
hex_out = 0;
2023-12-23 18:22:45 +01:00
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);
2024-03-11 23:09:47 +01:00
printf("%.5x %.5x %.5x %.5x\n", hex_op, hex_in0, hex_in1, hex_out);
2023-12-23 18:22:45 +01:00
fprintf(dest_asm,"%.5x ", hex_op);
fprintf(dest_asm,"%.5x ", hex_in0);
fprintf(dest_asm,"%.5x ", hex_in1);
fprintf(dest_asm,"%.5x \n", hex_out);
// Write the hex to a file
2024-02-19 22:25:25 +01:00
size = size + 3; //1 word = 3trytes
}
2023-12-23 18:22:45 +01:00
}
printf("\n%u trytes\n\n", size);
2023-12-23 18:22:45 +01:00
// Close the file
fclose(dest_asm);
fclose(src_asm);
return 0;
}