Compare commits
11 Commits
6a7f2a5098
...
main
Author | SHA1 | Date | |
---|---|---|---|
258b25a829 | |||
c325e645f3 | |||
bf787c2d6d | |||
46aa1e9ce9 | |||
763c447cf5 | |||
bfd7fd3016 | |||
f428423c49 | |||
fb59048075 | |||
1db3a98e13 | |||
feb34cd409 | |||
67c050abbc |
6
Makefile
6
Makefile
@ -1,7 +1,5 @@
|
|||||||
all:
|
all:
|
||||||
gcc ./main.c -std=c99
|
gcc ./main.c -std=c99 -o tasm
|
||||||
./a.out
|
|
||||||
|
|
||||||
d:
|
d:
|
||||||
gcc -g -std=c99 ./main.c
|
gcc -g -std=c99 ./main.c -o tasm
|
||||||
gdb ./a.out
|
|
||||||
|
85
asm_rules.c
85
asm_rules.c
@ -1,38 +1,72 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void asm_rules(char *in, char _asm[27]){
|
void trit_flip(char *in, char *instruction) {
|
||||||
|
for (int i = 0; i<27; i++) {
|
||||||
for (int i=0; i<27; i++) {
|
in[i] = instruction[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int asm_rules(char *in, char *_asm){
|
||||||
|
|
||||||
|
for (int i = 0; i < 27; i++) {
|
||||||
_asm[i] = 0;
|
_asm[i] = 0;
|
||||||
}
|
}
|
||||||
|
int pass = 0;
|
||||||
|
|
||||||
char *word = strtok(in, " ");
|
char *word = strtok(in, " ");
|
||||||
|
|
||||||
while(word != NULL) {
|
while(word != NULL) {
|
||||||
|
|
||||||
if (word[strlen(word)-1] == '\n') {
|
|
||||||
word[strlen(word)-1] = '\0'; //cleans newline from string
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(word, "nop") == 0) {
|
if (strcmp(word, "nop") == 0) {
|
||||||
//keeps the default value (0)
|
//keeps the default value (0) if nop
|
||||||
|
} else if (word[0] == ';') {
|
||||||
|
//comments
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
if (strcmp(word, "max") == 0) {
|
//cases make larger checks harder to read
|
||||||
_asm[6] = 1;
|
if (strstr(word, "max")) {
|
||||||
} else if (strcmp(word, "min") == 0) {
|
trit_flip(_asm, "000000+00000+");
|
||||||
_asm[6] = 2;
|
} else if (strstr(word, "min")) {
|
||||||
} else if (strcmp(word, "any") == 0) {
|
trit_flip(_asm, "000000+00000-");
|
||||||
_asm[6] = 2;
|
} else if (strstr(word, "any")) {
|
||||||
_asm[5] = 1;
|
trit_flip(_asm, "000000+000000");
|
||||||
} else if (strcmp(word, "cons") == 0) {
|
} else if (strstr(word, "cons")) {
|
||||||
_asm[6] = 1;
|
trit_flip(_asm, "000000+0000+-");
|
||||||
_asm[5] = 1;
|
} else if (strstr(word, "add")) {
|
||||||
|
trit_flip(_asm, "000000+000+-+");
|
||||||
|
} else if (strstr(word, "sub")) {
|
||||||
|
trit_flip(_asm, "000000+000--+");
|
||||||
|
} else if (strstr(word, "mul")) {
|
||||||
|
trit_flip(_asm, "000000+000+0+");
|
||||||
|
} else if (strstr(word, "div")) {
|
||||||
|
trit_flip(_asm, "000000+000+0-");
|
||||||
|
} else if (strstr(word, "dec")) {
|
||||||
|
trit_flip(_asm, "000000+0000--");
|
||||||
|
} else if (strstr(word, "inc")) {
|
||||||
|
trit_flip(_asm, "000000+0000+-");
|
||||||
|
} else if (strstr(word, "iou")) {
|
||||||
|
trit_flip(_asm, "000000-0000--");
|
||||||
|
} else if (strstr(word, "ieq")) {
|
||||||
|
trit_flip(_asm, "000000-0000-+");
|
||||||
|
} else if (strstr(word, "jmp")) {
|
||||||
|
trit_flip(_asm, "000000-000000");
|
||||||
|
} else if (strstr(word, "jec")) {
|
||||||
|
trit_flip(_asm, "000000-00000+");
|
||||||
|
} else if (strstr(word, "mov")) {
|
||||||
|
trit_flip(_asm, "000000000000+");
|
||||||
|
} else if (strstr(word, "Nst")) { //underflow test
|
||||||
|
trit_flip(_asm, "-------------");
|
||||||
|
} else if (strstr(word, "Pst")) { //overflow test
|
||||||
|
trit_flip(_asm, "+++++++++++++");
|
||||||
}
|
}
|
||||||
else if (word[0] == '$') {
|
if(word[0] == 'i') {
|
||||||
//constants
|
//integer
|
||||||
|
_asm[5] = '0';
|
||||||
} else if (word[0] == '@') {
|
} else if(word[0] == 'f') {
|
||||||
//address location
|
//floating
|
||||||
|
_asm[5] = '-';
|
||||||
|
}
|
||||||
|
if (word[0] == '$' || word[0] == '@') {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,4 +74,7 @@ void asm_rules(char *in, char _asm[27]){
|
|||||||
|
|
||||||
word = strtok(NULL, " ");
|
word = strtok(NULL, " ");
|
||||||
}
|
}
|
||||||
|
pass = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
218
main.c
218
main.c
@ -1,78 +1,19 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "asm_rules.c"
|
#include "asm_rules.c"
|
||||||
|
|
||||||
void bin_to_hex(char *tryte, int *hex) {
|
void bin_to_hex(char *tryte, int *hex);
|
||||||
char *a = tryte;
|
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out);
|
||||||
int num = 0;
|
int params(int argc, char **argv);
|
||||||
do {
|
|
||||||
int b = *a=='1'?1:0;
|
|
||||||
num = (num<<1)|b;
|
|
||||||
a++;
|
|
||||||
} while (*a);
|
|
||||||
*hex = num;
|
|
||||||
//printf("%d\n",num);
|
|
||||||
|
|
||||||
}
|
FILE *dest_asm;
|
||||||
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
|
FILE *src_asm;
|
||||||
|
int main(int argc, char **argv) {
|
||||||
char tryte[54];
|
int p = params(argc, argv);
|
||||||
char t0[18];
|
if (p==1) {
|
||||||
char t1[18];
|
return 1;
|
||||||
char t2[18];
|
|
||||||
char t3[18];
|
|
||||||
for (int i = 0; i < 18; i++) {
|
|
||||||
t0[i] = 0;
|
|
||||||
t1[i] = 0;
|
|
||||||
t2[i] = 0;
|
|
||||||
t3[i] = 0;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 54; i++) {
|
|
||||||
tryte[i] = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//convert assembly to formated binary representation of ternary
|
unsigned int size=0; //keeps the size of the assembled program
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int j = 0;
|
|
||||||
for (int i = 0; i < 18; i++) {
|
|
||||||
t0[i] = tryte[j];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 12; i++) {
|
|
||||||
t1[i] = tryte[j];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 12; i++) {
|
|
||||||
t2[i] = tryte[j];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
FILE *dest_asm = fopen("assembly.txt", "w");
|
|
||||||
FILE *src_asm = fopen("src.asm", "r");
|
|
||||||
unsigned int size=0;
|
|
||||||
|
|
||||||
fprintf(dest_asm,"00000\n");
|
|
||||||
char in_asm[64];
|
char in_asm[64];
|
||||||
char out_asm[27];
|
char out_asm[27];
|
||||||
|
|
||||||
@ -81,32 +22,31 @@ int main() {
|
|||||||
int hex_in1;
|
int hex_in1;
|
||||||
int hex_out;
|
int hex_out;
|
||||||
|
|
||||||
fgets(in_asm, 64, src_asm);
|
while(fgets(in_asm, sizeof(in_asm), src_asm)) {
|
||||||
|
|
||||||
|
|
||||||
while(!feof(src_asm)) {
|
|
||||||
hex_op = 0;
|
hex_op = 0;
|
||||||
hex_in0 = 0;
|
hex_in0 = 0;
|
||||||
hex_in1 = 0;
|
hex_in1 = 0;
|
||||||
hex_out = 0;
|
hex_out = 0;
|
||||||
|
|
||||||
asm_rules(in_asm, out_asm);
|
if (in_asm[strlen(in_asm)-1] == '\n') {
|
||||||
asm_to_hex(out_asm, &hex_op, &hex_in0, &hex_in1, &hex_out);
|
in_asm[strlen(in_asm)-1] = '\0'; //cleans newline from string
|
||||||
|
}
|
||||||
|
|
||||||
printf("%.5x %.5x %.5x %.5x\n", hex_op, hex_in0, hex_in1, hex_out);
|
if (!(in_asm[0] == '\0')){ // if in_asm is an empty, skip
|
||||||
|
asm_rules(in_asm, out_asm);
|
||||||
fprintf(dest_asm,"%.5x ", hex_op);
|
asm_to_hex(out_asm, &hex_op, &hex_in0, &hex_in1, &hex_out);
|
||||||
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);
|
|
||||||
size++;
|
|
||||||
|
|
||||||
|
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("\n%u trytes", size);
|
printf("\nfilesize: %u trytes\n\n", size);
|
||||||
|
|
||||||
|
|
||||||
// Close the file
|
// Close the file
|
||||||
@ -116,3 +56,109 @@ int main() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int params(int argc, char **argv){
|
||||||
|
//{{{
|
||||||
|
printf("%s\n", *argv);
|
||||||
|
char in = 0;
|
||||||
|
char out = 0;
|
||||||
|
char ret = 1;
|
||||||
|
if (argc > 1) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user