Refactored usual names for assembly enumerations...

This commit is contained in:
xolatile 2024-07-03 10:27:47 -04:00
parent 1e8489879e
commit 90e74fc189
3 changed files with 80 additions and 107 deletions

View File

@ -7,23 +7,12 @@
int main (void) {
token_array = malloc (144UL * sizeof (* token_array));
assemble (OPERATION_ADD_F, SIZE_64B,
TYPE_REGISTER, OPERAND_REGISTER_1,
TYPE_REGISTER, OPERAND_REGISTER_2);
assemble (ADC, D64, REG, R1, REG, R2);
assemble (ADC, D32, REG, R1, MEM, 12);
assemble (ADC, D16, MEM, 12, REG, R0);
assemble (ADC, D8, REG, R3, IMM, 0X77);
assemble (OPERATION_ADD_F, SIZE_32B,
TYPE_REGISTER, OPERAND_REGISTER_1,
TYPE_VARIABLE, 12);
assemble (OPERATION_ADD_F, SIZE_16B,
TYPE_VARIABLE, 12,
TYPE_REGISTER, OPERAND_REGISTER_A);
assemble (OPERATION_ADD_F, SIZE_8B,
TYPE_REGISTER, OPERAND_REGISTER_3,
TYPE_CONSTANT, 0X77);
for (int index = 0; index < token_count; ++index) {
for (unsigned int index = 0; index < token_count; ++index) {
printf ("%02X \n", token_array [index]);
}

View File

@ -1,5 +1,9 @@
#include "assembler.h"
typedef signed int form;
typedef unsigned int next;
typedef unsigned char byte;
static next token_count = 0;
static byte * token_array = NULL;
@ -24,42 +28,26 @@ void format_long_prefix (size_index size,
form destination,
type_index from,
form source) {
token_print (0X40 +
0X01 * ((to == TYPE_REGISTER) && (upper (destination))) +
0X04 * ((from == TYPE_REGISTER) && (upper (source))) +
0X08 * (size == SIZE_64B));
token_print (0X40 +
0X01 * ((to == REG) && (upper (destination))) +
0X04 * ((from == REG) && (upper (source))) +
0X08 * (size == D64));
}
static inline
void format_register_direction (size_index size,
form destination,
form source) {
token_print (0XC0 +
0X01 * (destination % 8) +
0X08 * (source % 8));
void format_register_direction (form destination,
form source) {
token_print (0XC0 + 0X01 * (destination % 8) + 0X08 * (source % 8));
}
/*
static inline
void format_register_indirection (size_index size,
form destination) {
byte format = 0XF0;
format += destination % 8;
token_print (format);
}
*/
static inline
void format_register_redirection (size_index size,
form direction) {
token_print (0X05 +
0X08 * (direction % 8));
void format_register_redirection (form direction) {
token_print (0X05 + 0X08 * (direction % 8));
}
static inline
void format_constant (size_index size) {
token_print (0X80 +
0X01 * (size != SIZE_8B));
token_print (0X80 + 0X01 * (size != D8));
}
static inline
@ -67,38 +55,26 @@ void format_regular_instruction (byte format,
size_index size,
type_index to,
form destination,
type_index from,
form source) {
//~format += 0X01 * (size != SIZE_8B);
//~format += 0X02 * ((to == TYPE_REGISTER) && (from == TYPE_VARIABLE));
//~format += 0XC0 * ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT));
//~format += 0X04 * ((to == TYPE_VARIABLE) && (from == TYPE_CONSTANT));
//~if ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)) {
//~format += destination % 8;
//~}
//~token_print (format);
type_index from) {
token_print (format +
destination % 8 * ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)) +
0X01 * (size != SIZE_8B) +
0X02 * ((to == TYPE_REGISTER) && (from == TYPE_VARIABLE)) +
0XC0 * ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)) +
0X04 * ((to == TYPE_VARIABLE) && (from == TYPE_CONSTANT)));
destination % 8 * ((to == REG) && (from == IMM)) +
0X01 * (size != D8) +
0X02 * ((to == REG) && (from == MEM)) +
0X04 * ((to == MEM) && (from == IMM)) +
0XC0 * ((to == REG) && (from == IMM)));
}
static inline
void assemble_enter (form dynamic_storage,
form nesting_level) {
token_print (0XC8);
token_print ((dynamic_storage / 1) % 256); // FIX LATER
token_print ((dynamic_storage / 256) % 256);
token_print ((nesting_level / 1) % 256);
token_print ((byte) (dynamic_storage / 1) % 256); // FIX LATER
token_print ((byte) (dynamic_storage / 256) % 256);
token_print ((byte) (nesting_level / 1) % 256);
}
static inline void assemble_leave (void) { token_print (0XC9); }
static inline void assemble_system_call (void) { token_print (0X0F); token_print (0X05); }
static inline void assemble_system_call (void) { token_print (0X0F); token_print (0X05); } // FIX LATER
static inline void assemble_system_return (void) { token_print (0X0F); token_print (0X07); }
static inline void assemble_system_enter (void) { token_print (0X0F); token_print (0X34); }
static inline void assemble_system_exit (void) { token_print (0X0F); token_print (0X35); }
@ -113,43 +89,43 @@ void assemble (operation_index operation,
form source) {
byte code = 0X00;
if (size == SIZE_16B) {
if (size == D16) {
format_short_prefix ();
}
if ((size == SIZE_64B)
|| ((to == TYPE_REGISTER) && (upper (destination)))
|| ((from == TYPE_REGISTER) && (upper (source)))) {
if ((size == D64)
|| ((to == REG) && (upper (destination)))
|| ((from == REG) && (upper (source)))) {
format_long_prefix (size, to, destination, from, source);
}
if (from == TYPE_CONSTANT) {
if (from == IMM) {
format_constant (size);
}
switch (operation) {
case OPERATION_ADD: code = 0X00; break;
case OPERATION_OR: code = 0X08; break;
case OPERATION_ADD_F: code = 0X10; break;
case OPERATION_SUBTRACT_F: code = 0X18; break;
case OPERATION_AND: code = 0X20; break;
case OPERATION_SUBTRACT: code = 0X28; break;
case OPERATION_XOR: code = 0X30; break;
case OPERATION_COMPARE: code = 0X38; break;
default: break;
case ADD: code = 0X00; break;
case OR: code = 0X08; break;
case ADC: code = 0X10; break;
case SBB: code = 0X18; break;
case AND: code = 0X20; break;
case SUB: code = 0X28; break;
case XOR: code = 0X30; break;
case CMP: code = 0X38; break;
default: break;
}
format_regular_instruction (code, size, to, destination, from, source);
format_regular_instruction (code, size, to, destination, from);
if ((to == TYPE_REGISTER) && (from == TYPE_REGISTER)) {
format_register_direction (size, destination, source);
if ((to == REG) && (from == REG)) {
format_register_direction (destination, source);
}
if ((to == TYPE_REGISTER) && (from == TYPE_VARIABLE)) {
format_register_redirection (size, destination);
if ((to == REG) && (from == MEM)) {
format_register_redirection (destination);
}
if ((to == TYPE_VARIABLE) && (from == TYPE_REGISTER)) {
format_register_redirection (size, source);
if ((to == MEM) && (from == REG)) {
format_register_redirection (source);
}
}

View File

@ -4,37 +4,45 @@
#include <stdlib.h>
typedef enum {
SIZE_256B, SIZE_128B, SIZE_64B,
SIZE_32B, SIZE_16B, SIZE_8B,
D64, D32, D16, D8,
} size_index;
typedef enum {
TYPE_REGISTER, TYPE_VARIABLE, TYPE_CONSTANT,
NIL, REG, MEM, IMM,
} 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_ADD_F, OPERATION_SUBTRACT_F,
ADD, OR, ADC, SBB,
AND, SUB, XOR, CMP,
MUL, DIV, IMUL, IDIV,
INC, DEC, NOT, NEG,
ENTER, LEAVE, CALL, RET,
SYSENTER, SYSEXIT, SYSCALL, SYSRET,
JMP, JPE, JS, JPO,
JE, JNE, JZ, JNZ,
JA, JNA, JB, JNB,
MOV, CMOVPE, CMOVS, CMOVPO,
CMOVE, CMOVNE, CMOVZ, CMOVNZ,
CMOVA, CMOVNA, CMOVB, CMOVNB,
LOCK, HLT, IN, OUT,
PUSH, POP, BSWAP, TEST,
RCL, RCR, ROL, ROR,
SHL, SHR, SAL, SAR,
REP, REPE, REPNE, REPZ,
LOOP, LOOPE, LOOPNE, PAUSE,
XADD, XCHG, LEA, POPCNT,
INT, BSF, BSR, BOUND,
FADD, FSUB, FMUL, FDIV,
FNOP, FXAM, FABS, FSCALE,
FSIN, FCOS, FSQRT, FCHS,
FXCH, FREM, FLDPI, FLDZ,
} operation_index;
typedef enum {
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,
R0, R1, R2, R3,
R4, R5, R6, R7,
R8, R9, R10, R11,
R12, R13, R14, R15,
} operand_index;
typedef signed int form;
typedef unsigned int next;
typedef unsigned char byte;
#endif