cleanup
This commit is contained in:
parent
25efa4317e
commit
997ca65f08
7
Makefile
Normal file
7
Makefile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
all:
|
||||||
|
gcc ./main.c -std=c99
|
||||||
|
./a.out
|
||||||
|
|
||||||
|
d:
|
||||||
|
gcc -g -std=c99 ./main.c
|
||||||
|
gdb ./a.out
|
37
asm_rules.c
37
asm_rules.c
@ -1,19 +1,8 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void asm_rules(char *in, char _asm[9]){
|
void asm_rules(char *in, char _asm[27]){
|
||||||
|
|
||||||
char src_addr[9];
|
|
||||||
char dest_addr[9];
|
|
||||||
char tryte[9];
|
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++) {
|
|
||||||
_asm[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *instruction = (char*) malloc(10*sizeof(char));
|
|
||||||
int instruction_location = 0;
|
|
||||||
char *word = strtok(in, " ");
|
char *word = strtok(in, " ");
|
||||||
|
|
||||||
while(word != NULL) {
|
while(word != NULL) {
|
||||||
@ -26,17 +15,25 @@ void asm_rules(char *in, char _asm[9]){
|
|||||||
//keeps the default value (0)
|
//keeps the default value (0)
|
||||||
} else {
|
} else {
|
||||||
if (strcmp(word, "max") == 0) {
|
if (strcmp(word, "max") == 0) {
|
||||||
_asm[6] = 1;
|
_asm[6] = 1;
|
||||||
//printf("%c\n", tryte[6]);
|
} 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] = 2;
|
||||||
|
_asm[5] = 1;
|
||||||
|
} else if (strcmp(word, "cons")) {
|
||||||
|
_asm[6] = 1;
|
||||||
|
_asm[5] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++) {
|
|
||||||
instruction[instruction_location] = _asm[i];
|
|
||||||
instruction_location++;
|
|
||||||
}
|
|
||||||
instruction[instruction_location] = ' ';
|
|
||||||
word = strtok(NULL, " ");
|
word = strtok(NULL, " ");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
73
main.c
73
main.c
@ -1,73 +1,66 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "asm_rules.c"
|
#include "asm_rules.c"
|
||||||
|
|
||||||
void trit_to_hex(char *_asm, char * mem){
|
//void trit_to_hex(char *_asm, char **_out){
|
||||||
|
void trit_to_hex(char *_asm, char _out[27]){
|
||||||
|
|
||||||
char out[18];
|
char tryte[54];
|
||||||
int trit;
|
for (int i = 0; i < 54; i++) {
|
||||||
int len = strlen(_asm);
|
tryte[i] = 0;
|
||||||
//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
|
//convert assembly to formated binary representation of ternary
|
||||||
char *a = out;
|
for (int i = 0; i < 27; i++) {
|
||||||
|
|
||||||
|
printf("%c",_asm[i]);
|
||||||
|
if(_asm[i]==2) {
|
||||||
|
strcat(tryte, "10");
|
||||||
|
} else if (_asm[i]==1) {
|
||||||
|
strcat(tryte, "01");
|
||||||
|
} else {
|
||||||
|
strcat(tryte, "00");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//todo: convert tryte to hex
|
||||||
|
//*_out = (char*)calloc(8,sizeof(int));
|
||||||
|
char *a = tryte;
|
||||||
int num = 0;
|
int num = 0;
|
||||||
do {
|
do {
|
||||||
int b = *a=='1'?1:0;
|
int b = *a=='1'?1:0;
|
||||||
num = (num<<1)|b;
|
num = (num<<1)|b;
|
||||||
a++;
|
a++;
|
||||||
} while (*a);
|
} while (*a);
|
||||||
//printf("%.5x\n", num);
|
|
||||||
sprintf(mem, "%.5x", num);
|
//sprintf(_out, "%x", num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char out_asm[9] = {0,0,0,0,0,0,0,0,0};
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
FILE *dest_asm = fopen("assembly.txt", "w");
|
FILE *dest_asm = fopen("assembly.txt", "w");
|
||||||
FILE *src_asm = fopen("src.asm", "r");
|
FILE *src_asm = fopen("src.asm", "r");
|
||||||
char width=1;
|
|
||||||
unsigned int size=0;
|
unsigned int size=0;
|
||||||
|
|
||||||
fprintf(dest_asm,"00000 ");
|
|
||||||
fprintf(dest_asm,"00000 ");
|
|
||||||
fprintf(dest_asm,"00000 ");
|
|
||||||
fprintf(dest_asm,"00000\n");
|
fprintf(dest_asm,"00000\n");
|
||||||
|
char in_asm[54];
|
||||||
char in_asm[50];
|
char out_asm[27];
|
||||||
int addresses;
|
|
||||||
fgets(in_asm, 50, src_asm);
|
fgets(in_asm, 54, src_asm);
|
||||||
|
char *_out;
|
||||||
|
|
||||||
while(!feof(src_asm)) {
|
while(!feof(src_asm)) {
|
||||||
|
|
||||||
asm_rules(in_asm, out_asm);
|
asm_rules(in_asm, out_asm);
|
||||||
trit_to_hex(out_asm, out_asm);
|
trit_to_hex(out_asm, _out);
|
||||||
|
|
||||||
fprintf(dest_asm,"%s ", out_asm);
|
fprintf(dest_asm,"%s ", _out);
|
||||||
// Write the hex to a file
|
// Write the hex to a file
|
||||||
if (width >= 4) {
|
|
||||||
fprintf(dest_asm,"\n");
|
|
||||||
width = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fgets(in_asm, 50, src_asm);
|
fgets(in_asm, 54, src_asm);
|
||||||
width++;
|
free(_out);
|
||||||
size++;
|
size++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user