This commit is contained in:
2024-10-24 20:58:09 -05:00
commit 8923a2006d
4 changed files with 126 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
.vscode/
tags
+22
View File
@@ -0,0 +1,22 @@
#ifndef OPCODES_H
#define OPCODES_H
#include <string.h>
#include <errno.h>
#include <stdio.h>
typedef enum {
R1 = 0, R2, R3, R4, R5, R6, R7, R8 = 7
} Registers;
typedef enum {
ADD = 0x01, SUB, MUL, DIV, AND, OR, XOR, NOT, SHL, SHR, NOP, CMP, JMP, JG, JL,
OUTB, INB, HLT, ENI, INT, LIVT, PUSHA, POPA, CALL, RET, PUSH, POP
} Mnemonic;
int IsOpcode(const char*, Mnemonic*);
int IsRegister(const char*, Registers*);
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]);
void GetRegisterText(Registers reg, char buffer[3]);
#endif
+5
View File
@@ -0,0 +1,5 @@
#include <stdio.h>
int main(int argc, char** argv) {
printf("Hello World\n");
}
+97
View File
@@ -0,0 +1,97 @@
#include "../includes/opcodes.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define OPCODECOUNT 34
struct _instruction {
char* Name;
Mnemonic Mnemonic;
};
struct _instruction instructions[OPCODECOUNT] = {
{ "copya", COPYA },
{ "copyab", COPYAB },
{ "copyra", COPYRA },
{ "copyrab", COPYRAB },
{ "copyrara", COPYRARA },
{ "copyrarab", COPYRARAB },
{ "copy", COPY },
{ "copyi", COPYI },
{ "copyb", COPYB },
{ "cmp", CMP },
{ "cmpi", CMPI },
{ "add", ADD },
{ "sub", SUB },
{ "and", AND },
{ "xor", XOR },
{ "or", OR },
{ "not", NOT },
{ "shr", SHR },
{ "shl", SHL },
{ "inc", INC },
{ "dec", DEC },
{ "push", PUSH },
{ "pop", POP },
{ "jmpi", JMPI },
{ "jmp", JMP },
{ "jz", JZ },
{ "jg", JG },
{ "jl", JL },
{ "nop", NOP },
{ "call", CALL },
{ "ret", RET }
//yld
};
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]) {
memset(buffer, '\0', 12);
for(int i = 0; i < OPCODECOUNT; i++) {
if (instructions[i].Mnemonic == mnemonic) {
strncpy(buffer, instructions[i].Name, 11);
break;
}
}
}
void GetRegisterText(Registers reg, char buffer[4]) {
memset(buffer, '\0', 4);
if (reg < R1 || reg > R8) return;
buffer[0] = 'r';
buffer[1] = reg + 49;
}
int IsOpcode(const char* text, Mnemonic* opcode) {
if (!text) return 0;
for(unsigned long i = 0; i < sizeof(instructions) / sizeof(struct _instruction); i++) {
if (strcmp(instructions[i].Name, text) == 0) {
if (opcode) *opcode = instructions[i].Mnemonic;
return 1;
}
}
return 0;
}
int IsRegister(const char* text, Registers* reg) {
if (!text) return 0;
int length = strlen(text);
Registers r = R8;
if (length != 2) return 0;
if (text[0] != 'r') return 0;
if (!isdigit(text[1])) return 0;
r = text[1] - 0x31;
if (reg) *reg = r;
return 1;
}