New assembler stuff...

This commit is contained in:
xolatile 2024-07-02 17:26:33 -04:00
parent 925672eed0
commit a00f0cd6cd
2 changed files with 110 additions and 100 deletions

@ -9,69 +9,114 @@ static void byte_push (byte data) {
byte_count += 1;
}
static form lower (form data) { return ((data >= 0) && (data <= 7)); }
static form upper (form data) { return ((data >= 8) && (data <= 15)); }
static inline form valid (form data) { return ((data => 0) && (data <= 15)); }
static inline form lower (form data) { return ((data => 0) && (data <= 7)); }
static inline form upper (form data) { return ((data => 8) && (data <= 15)); }
static void format_prefix (size_index size,
type_index type,
form destination,
form source) {
byte format = 0X00;
//~static inline void format_prefix (size_index size,
//~type_index destination,
//~type_index source) {
//~if (size == SIZE_16B) {
//~byte_push (0X66);
//~} else if (size == SIZE_64B) {
//~byte format = 0X48;
//~if (destination == TYPE_REGISTER) {
//~format += upper (destination) * 0X01;
//~}
//~if (source == TYPE_REGISTER) {
//~format += upper (source) * 0X04;
//~}
//~byte_push (format);
//~} else {
//~return;
//~}
//~}
if (size == size_16b) byte_push (0X66);
//~static inline void format_rex_prefix (size_index size,
//~type_index destination,
//~type_index source) {
//~byte format = 0X40;
if (size == size_64b) format += 0X48;
if (size == size_32b) format += 0X48;
if (size == size_64b) format += 0X48;
if (size == size_64b) format += 0X48;
//~byte_push (format);
//~}
if (upper (destination)) format += 0X01;
if (upper (source)) format += 0X04;
static inline void format_register_direction (size_index size,
form to,
form from) {
byte format = 0XC0;
error (valid (to), "Invalid destination register index.");
error (valid (from), "Invalid source register index.");
format += 1 * (to % 8);
format += 8 * (from % 8);
byte_push (format);
}
static void format_operation (size_index size,
type_index type,
form destination,
form source) {
byte format = 0X00;
static inline void format_register_indirection (size_index size,
form to) {
byte format = 0XF0;
switch (type) {
case type_register_register: format += 0X30; break;
case type_register_variable: format += 0X32; break;
case type_register_constant: format += 0X80; break;
case type_variable_register: format += 0X30; break;
case type_variable_constant: format += 0X80; break;
default: exit (EXIT_FAILURE);
}
error (valid (to), "Invalid destination register index.");
if (size != size_8b) format += 0X01;
if (type == type_register_constant) format -= (destination == 0) * 0X3A;
format += to % 8;
byte_push (format);
}
static void format_modifier (size_index size,
type_index type,
form destination,
form source) {
byte format = 0X00;
static inline void format_register_redirection (size_index size,
form direction) {
byte format = 0X05;
error (valid (direction), "Invalid direction register index.");
format += 8 * (direction % 8);
byte_push (format);
}
void assemble_xor (size_index size,
type_index type,
form destination,
form source) {
if (size == size_256b) exit (EXIT_FAILURE);
if (size == size_128b) exit (EXIT_FAILURE);
//~static inline void format_operation (size_index size,
//~type_index type,
//~form destination,
//~form source) {
//~byte format = 0X00;
format_prefix (size, type, destination, source);
format_operation (size, type, destination, source);
format_modifier (size, type, destination, source);
//~switch (type) {
//~case type_register_register: format += 0X30; break;
//~case type_register_variable: format += 0X32; break;
//~case type_register_constant: format += 0X80; break;
//~case type_variable_register: format += 0X30; break;
//~case type_variable_constant: format += 0X80; break;
//~default: exit (EXIT_FAILURE);
//~}
// ...
}
//~if (size != size_8b) format += 0X01;
//~if (type == type_register_constant) format -= (destination == 0) * 0X3A;
//~byte_push (format);
//~}
//~static inline void format_modifier (size_index size,
//~type_index type,
//~form destination,
//~form source) {
//~byte format = 0X00;
//~byte_push (format);
//~}
//~static void assemble_xor (size_index size,
//~type_index type,
//~form destination,
//~form source) {
//~if (size == size_256b) exit (EXIT_FAILURE);
//~if (size == size_128b) exit (EXIT_FAILURE);
//~format_prefix (size, type, destination, source);
//~format_operation (size, type, destination, source);
//~format_modifier (size, type, destination, source);
//~// ...
//~}

@ -4,73 +4,38 @@
#include <stdlib.h>
typedef enum {
size_256b, size_128b, size_64b,
size_32b, size_16b, size_8b,
SIZE_256B, SIZE_128B, SIZE_64B,
SIZE_32B, SIZE_16B, SIZE_8B,
} size_index;
typedef enum {
type_register_register, type_register_variable, type_register_constant,
type_variable_register, type_variable_constant,
TYPE_REGISTER, TYPE_VARIABLE, TYPE_CONSTANT,
} type_index;
typedef enum {
operation_move, operation_add, operation_subtract,
operation_multiply, operation_divide, operation_modulus,
operation_compare, operation_jump, operation_xor,
operation_and, operation_or, operation_not,
operation_is, operation_above, operation_below,
operation_if, operation_increment, operation_decrement,
operation_system, operation_call, operation_return,
operation_enter, operation_leave, operation_exit,
OPERATION_MOVE, OPERATION_ADD, OPERATION_SUBTRACT,
OPERATION_MULTIPLY, OPERATION_DIVIDE, OPERATION_MODULUS,
OPERATION_COMPARE, OPERATION_JUMP, OPERATION_XOR,
OPERATION_AND, OPERATION_OR, OPERATION_NOT,
OPERATION_IS, OPERATION_ABOVE, OPERATION_BELOW,
OPERATION_IF, OPERATION_INCREMENT, OPERATION_DECREMENT,
OPERATION_SYSTEM, OPERATION_CALL, OPERATION_RETURN,
OPERATION_ENTER, OPERATION_LEAVE, OPERATION_EXIT,
} operation_index;
typedef enum {
operand_rax, operand_rcx, operand_rdx,
operand_rbx, operand_rsi, operand_rdi,
operand_rsp, operand_rbp, operand_r8,
operand_r9, operand_r10, operand_r11,
operand_r12, operand_r13, operand_r14,
operand_r15,
} operand_64b_index;
typedef enum {
operand_eax, operand_ecx, operand_edx,
operand_ebx, operand_esi, operand_edi,
operand_esp, operand_ebp, operand_r8d,
operand_r9d, operand_r10d, operand_r11d,
operand_r12d, operand_r13d, operand_r14d,
operand_r15d,
} operand_32b_index;
typedef enum {
operand_ax, operand_cx, operand_dx,
operand_bx, operand_si, operand_di,
operand_sp, operand_bp, operand_r8w,
operand_r9w, operand_r10w, operand_r11w,
operand_r12w, operand_r13w, operand_r14w,
operand_r15w,
} operand_16b_index;
typedef enum {
operand_al, operand_cl, operand_dl,
operand_bl, operand_sil, operand_dil,
operand_spl, operand_bpl, operand_r8b,
operand_r9b, operand_r10b, operand_r11b,
operand_r12b, operand_r13b, operand_r14b,
operand_r15b,
} operand_8b_index;
typedef enum {
command_operation, command_size, command_operand_d,
command_operand_s,
command_length,
} command_index;
OPERAND_REGISTER_0, OPERAND_REGISTER_1, OPERAND_REGISTER_2,
OPERAND_REGISTER_3, OPERAND_REGISTER_4, OPERAND_REGISTER_5,
OPERAND_REGISTER_6, OPERAND_REGISTER_7, OPERAND_REGISTER_8,
OPERAND_REGISTER_9, OPERAND_REGISTER_A, OPERAND_REGISTER_B,
OPERAND_REGISTER_C, OPERAND_REGISTER_D, OPERAND_REGISTER_E,
OPERAND_REGISTER_F, OPERAND_REFERENCE, OPERAND_DEREFERENCE,
} operand_index;
typedef signed int form;
typedef unsigned int next;
typedef unsigned char byte;
void assemble_xor (size_index size, type_index type, form destination, form source);
// size, operation, type, operand/form, type, operand/form
#endif