106 lines
2.9 KiB
C
106 lines
2.9 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#include "Instruc.h"
|
||
|
|
||
|
void printBits(size_t const size, void const * const ptr)
|
||
|
{
|
||
|
unsigned char *b = (unsigned char*) ptr;
|
||
|
unsigned char byte;
|
||
|
int i, j;
|
||
|
|
||
|
for (i = size-1; i >= 0; i--) {
|
||
|
for (j = 7; j >= 0; j--) {
|
||
|
byte = (b[i] >> j) & 1;
|
||
|
printf("%u", byte);
|
||
|
}
|
||
|
}
|
||
|
puts("");
|
||
|
}
|
||
|
|
||
|
long parseInst(char* instruction, char* argA, char* argB) {
|
||
|
long binaryInstruction;
|
||
|
|
||
|
char a[100], b[100];
|
||
|
|
||
|
strcpy(a, argA);
|
||
|
strcpy(b, argB);
|
||
|
|
||
|
if (!strcmp(instruction, "MOV")) binaryInstruction = 1;
|
||
|
else if (!strcmp(instruction, "ADD")) binaryInstruction = 2;
|
||
|
else if (!strcmp(instruction, "SUB")) binaryInstruction = 3;
|
||
|
else if (!strcmp(instruction, "JMP")) binaryInstruction = 4;
|
||
|
else if (!strcmp(instruction, "JMZ")) binaryInstruction = 5;
|
||
|
else if (!strcmp(instruction, "JMG")) binaryInstruction = 6;
|
||
|
else if (!strcmp(instruction, "DJZ")) binaryInstruction = 7;
|
||
|
else if (!strcmp(instruction, "CMP")) binaryInstruction = 8;
|
||
|
else if (!strcmp(instruction, "DAT")) binaryInstruction = 0;
|
||
|
binaryInstruction = binaryInstruction << 2;
|
||
|
|
||
|
if (strlen(a) > 1) {
|
||
|
if (a[0] == '@') {
|
||
|
binaryInstruction += 2;
|
||
|
a[0] = ' ';
|
||
|
} else if (a[0] != '#') {
|
||
|
binaryInstruction += 1;
|
||
|
} else a[0] = ' ';
|
||
|
}
|
||
|
binaryInstruction = binaryInstruction << 2;
|
||
|
|
||
|
if (strlen(b) > 1 && strcmp(instruction, "DAT")) {
|
||
|
if (b[0] == '@') {
|
||
|
binaryInstruction += 2;
|
||
|
b[0] = ' ';
|
||
|
} else if (b[0] != '#') {
|
||
|
binaryInstruction += 1;
|
||
|
} else b[0] = ' ';
|
||
|
}
|
||
|
binaryInstruction = binaryInstruction << 28;
|
||
|
|
||
|
int da = atoi(a) % 8000;
|
||
|
int db = atoi(b) % 8000;
|
||
|
|
||
|
while (da < 0) da += 8000;
|
||
|
while (db < 0) db += 8000;
|
||
|
|
||
|
if (strlen(a) > 1) binaryInstruction += (da & 268435455);
|
||
|
binaryInstruction = binaryInstruction << 28;
|
||
|
if (strlen(b) > 1) binaryInstruction += (db & 268435455);
|
||
|
|
||
|
return binaryInstruction;
|
||
|
}
|
||
|
|
||
|
int convertFile(char* filename, char* destination) {
|
||
|
FILE* sourceFile = fopen(filename, "r");
|
||
|
FILE* destFile = fopen(destination, "w");
|
||
|
|
||
|
char funcName[4], argA[10], argB[10];
|
||
|
long code;
|
||
|
|
||
|
fseek(sourceFile, 0L, SEEK_SET);
|
||
|
|
||
|
if (sourceFile && destFile) {
|
||
|
while (!feof(sourceFile)) {
|
||
|
fscanf(sourceFile, "%s", funcName);
|
||
|
|
||
|
strcpy(argA, "");
|
||
|
strcpy(argB, "");
|
||
|
|
||
|
if (!strcmp(funcName, "JMP")) {
|
||
|
fscanf(sourceFile, "%s", argA);
|
||
|
} else if (!strcmp(funcName, "DAT")) {
|
||
|
fscanf(sourceFile, "%s", argB);
|
||
|
} else {
|
||
|
fscanf(sourceFile, "%s %s", argA, argB);
|
||
|
}
|
||
|
|
||
|
code = parseInst(funcName, argA, argB);
|
||
|
fprintf(destFile, "%ld\n", code);
|
||
|
}
|
||
|
} else return 0;
|
||
|
|
||
|
fclose(sourceFile);
|
||
|
fclose(destFile);
|
||
|
}
|