working main function

This commit is contained in:
nova 2024-03-12 10:18:56 +01:00
parent 5a0c4f70f1
commit 6a7f2a5098
2 changed files with 41 additions and 32 deletions

View File

@ -2,6 +2,9 @@
void asm_rules(char *in, char _asm[27]){
for (int i=0; i<27; i++) {
_asm[i] = 0;
}
char *word = strtok(in, " ");
@ -15,23 +18,24 @@ void asm_rules(char *in, char _asm[27]){
//keeps the default value (0)
} else {
if (strcmp(word, "max") == 0) {
_asm[6] = 1;
} else if (strcmp(word, "min")) {
_asm[6] = 2;
} else if (strcmp(word, "min")) {
_asm[6] = 2;
} else if (strcmp(word, "xor")) {
//_asm[5] = 1;
for (int i = 0; i < 27; i++) {
_asm[i]=2;
}
} else if (strcmp(word, "any")) {
_asm[6] = 1;
} else if (strcmp(word, "min") == 0) {
_asm[6] = 2;
} else if (strcmp(word, "any") == 0) {
_asm[6] = 2;
_asm[5] = 1;
} else if (strcmp(word, "cons")) {
} else if (strcmp(word, "cons") == 0) {
_asm[6] = 1;
_asm[5] = 1;
}
else if (word[0] == '$') {
//constants
} else if (word[0] == '@') {
//address location
}
}
word = strtok(NULL, " ");

45
main.c
View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include "asm_rules.c"
void bin_to_hex(char *tryte, char *hex) {
void bin_to_hex(char *tryte, int *hex) {
char *a = tryte;
int num = 0;
do {
@ -9,11 +9,11 @@ void bin_to_hex(char *tryte, char *hex) {
num = (num<<1)|b;
a++;
} while (*a);
sprintf(hex, "%x", num);
printf("%d\n", num);
*hex = num;
//printf("%d\n",num);
}
void trit_to_bin(char *_asm, char opcode, char in0, char in1, char out){
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
char tryte[54];
char t0[18];
@ -47,23 +47,23 @@ void trit_to_bin(char *_asm, char opcode, char in0, char in1, char out){
t0[i] = tryte[j];
j++;
}
for (int i = 0; i < 18; i++) {
for (int i = 0; i < 12; i++) {
t1[i] = tryte[j];
j++;
}
for (int i = 0; i < 18; i++) {
for (int i = 0; i < 12; i++) {
t2[i] = tryte[j];
j++;
}
for (int i = 0; i < 18; i++) {
for (int i = 0; i < 12; i++) {
t3[i] = tryte[j];
j++;
}
bin_to_hex(t0, &opcode);
bin_to_hex(t1, &in0);
bin_to_hex(t2, &in1);
bin_to_hex(t3, &out);
bin_to_hex(t0, opcode);
bin_to_hex(t1, in0);
bin_to_hex(t2, in1);
bin_to_hex(t3, out);
}
@ -76,24 +76,29 @@ int main() {
char in_asm[64];
char out_asm[27];
char hex_op[5];
char hex_in0[5];
char hex_in1[5];
char hex_out[5];
int hex_op;
int hex_in0;
int hex_in1;
int hex_out;
fgets(in_asm, 64, src_asm);
while(!feof(src_asm)) {
hex_op = 0;
hex_in0 = 0;
hex_in1 = 0;
hex_out = 0;
asm_rules(in_asm, out_asm);
trit_to_bin(out_asm, *hex_op, *hex_in0, *hex_in1, *hex_out);
asm_to_hex(out_asm, &hex_op, &hex_in0, &hex_in1, &hex_out);
printf("%.5x %.5x %.5x %.5x\n", hex_op, hex_in0, hex_in1, hex_out);
fprintf(dest_asm,"%.5s ", hex_op);
fprintf(dest_asm,"%.5s ", hex_in0);
fprintf(dest_asm,"%.5s ", hex_in1);
fprintf(dest_asm,"%.5s \n", hex_out);
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
fgets(in_asm, 64, src_asm);