very basic assembly

This commit is contained in:
nova 2024-02-19 22:25:25 +01:00
parent 3b094f55b8
commit 35f30e07c2
2 changed files with 64 additions and 23 deletions

42
asm_rules.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void asm_rules(char *in, char _asm[9]){
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, " ");
while(word != NULL) {
if (word[strlen(word)-1] == '\n') {
word[strlen(word)-1] = '\0'; //cleans newline from string
}
if (strcmp(word, "nop") == 0) {
//keeps the default value (0)
} else {
if (strcmp(word, "max") == 0) {
_asm[6] = 1;
//printf("%c\n", tryte[6]);
}
}
for (int i = 0; i < 9; i++) {
instruction[instruction_location] = _asm[i];
instruction_location++;
}
instruction[instruction_location] = ' ';
word = strtok(NULL, " ");
}
}

45
main.c
View File

@ -2,19 +2,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "asm_rules.c"
void bin_to_hex(char assembly_bin[], char * mem){ void trit_to_hex(char *_asm, char * mem){
char out[18]; char out[18];
char array[18];
strcpy(array, assembly_bin);
int len = strlen(assembly_bin);
int trit; int trit;
int len = strlen(_asm);
//convert assembly to formated binary representation of ternary //convert assembly to formated binary representation of ternary
for (int i = 0; i < len; i++) { for (int i = 0; i < 9; i++) {
trit = atoi(&array[strlen(array)-1]); trit = _asm[i];
//printf("%d\n",trit);
if(trit==2) { if(trit==2) {
strcat(out, "10"); strcat(out, "10");
} else if (trit==1) { } else if (trit==1) {
@ -22,10 +21,9 @@ void bin_to_hex(char assembly_bin[], char * mem){
} else { } else {
strcat(out, "00"); strcat(out, "00");
} }
array[strlen(array)-1] = '\0'; //array[strlen(array)-1] = '\0';
} }
printf("%s\n", out);
//converts formated binary to hex //converts formated binary to hex
char *a = out; char *a = out;
int num = 0; int num = 0;
@ -39,37 +37,38 @@ void bin_to_hex(char assembly_bin[], char * mem){
} }
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; char width=1;
unsigned size=0; unsigned int size=0;
fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000 ");
fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000 ");
fprintf(dest_asm,"00000 "); fprintf(dest_asm,"00000 ");
fprintf(dest_asm,"00000\n"); fprintf(dest_asm,"00000\n");
int eof;
char in_asm[50];
int addresses;
fgets(in_asm, 50, src_asm);
while(!feof(src_asm)) { while(!feof(src_asm)) {
size++;
char _asm[18];
char out[5];
fgets(_asm, 18, src_asm);
bin_to_hex(_asm, _asm); asm_rules(in_asm, out_asm);
trit_to_hex(out_asm, out_asm);
fprintf(dest_asm,"%s ", out_asm);
fprintf(dest_asm,"%s ", _asm);
// Write the hex to a file // Write the hex to a file
if (width >= 3) { if (width >= 4) {
fprintf(dest_asm,"00000\n"); fprintf(dest_asm,"\n");
width = 0; width = 0;
} }
fgets(in_asm, 50, src_asm);
width++; width++;
size++;
} }