From 0c6ab27f4304f3f9727792de1cb262dccf198c21 Mon Sep 17 00:00:00 2001 From: xolatile Date: Thu, 21 Nov 2024 13:22:58 -0500 Subject: [PATCH] Wrote bit test instructions... --- documentation/TODO.md | 1 + source/arena.h | 6 +- source/assembler.c | 149 ++++++++++++++++++++++++++++++++---------- source/assembler.h | 4 +- test/heyo.eax | 3 + 5 files changed, 124 insertions(+), 39 deletions(-) diff --git a/documentation/TODO.md b/documentation/TODO.md index aa7a793..392052f 100644 --- a/documentation/TODO.md +++ b/documentation/TODO.md @@ -1,6 +1,7 @@ + terrible infinit error on ../debug/comment\_as\_space.eax - X: Implement other utility instructions. +- X: Implement ELF object files and symbols. - X: Implement BMI1 and BMI2 into assembler. - X: Write 'cpuid' tool to check for extensions. - X: Check out AES and SHA integration. diff --git a/source/arena.h b/source/arena.h index 2d14d0a..73191e4 100644 --- a/source/arena.h +++ b/source/arena.h @@ -3,16 +3,16 @@ #include +// We only allocate memory, program is guaranteed to terminate. extern void * aalloc(size_t size); #endif #ifdef ARENA_IMPLEMENTATION -#include - #ifndef ARENA_CHUNK -#define ARENA_CHUNK (4096) +// What memory chunk size would be enough for everyone...? +#define ARENA_CHUNK (640 * 1024) #endif static struct { diff --git a/source/assembler.c b/source/assembler.c index 94326eb..b7fb755 100644 --- a/source/assembler.c +++ b/source/assembler.c @@ -5,14 +5,23 @@ #include #define DOUBLE_BEGIN (ADD) +#define DOUBLE_END (CMP) #define SINGLE_BEGIN (INC) -#define STATIC_1_BEGIN (NOP) -#define STATIC_2_BEGIN (SYSCALL) -#define JUMP_IF_BEGIN (JO) -#define MOVE_IF_BEGIN (CMOVO) -#define SET_IF_BEGIN (SETO) +#define SINGLE_END (IDIV) #define FLOAT_BEGIN (FADD) +#define FLOAT_END (FDIVR) #define SHIFT_BEGIN (ROL) +#define SHIFT_END (SAR) +#define STATIC_1_BEGIN (NOP) +#define STATIC_1_END (PUSHF) +#define STATIC_2_BEGIN (SYSCALL) +#define STATIC_2_END (FCOS) +#define JUMP_IF_BEGIN (JO) +#define JUMP_IF_END (JG) +#define MOVE_IF_BEGIN (CMOVO) +#define MOVE_IF_END (CMOVG) +#define SET_IF_BEGIN (SETO) +#define SET_IF_END (SETG) #if DEBUG == 1 @@ -59,8 +68,16 @@ static const char * operation_name [] = { "sete", "setne", "setbe", "seta", "sets", "setns", "setpe", "setpo", "setl", "setge", "setle", "setg", - "bswap", "bsf", "bsr", "loop", - "loope", "loopne" + "lea", "cbw", "cwd", "cdq", + "cmc", "clc", "cld", "cli", + "movbe", "stc", "std", "sti", + "test", "ud2", "xadd", "xchg", + "bt", "bts", "btr", "btc", + "bsf", "bsr", "bswap", + "loop", "loope", "loopne", + "rep", "repe", "repne", + "ins", "outs", "lods", "stos", + "movs", "cmps", "scas" }; #endif @@ -81,11 +98,14 @@ static int near(int label) { return label && 0; } static int transfer(int to, int from) { return (to == REG) && (from == REG); } static int import (int to, int from) { return (to == REG) && (from == MEM); } +static int attach (int to, int from) { return (to == REG) && (from == IMM); } static int export (int to, int from) { return (to == MEM) && (from == REG); } static int assign (int to, int from) { return (to == MEM) && (from == IMM); } -static int attach (int to, int from) { return (to == REG) && (from == IMM); } static int relate (int to, int from) { return (to == REG) && (from == REL); } +static int absolute (void) { return (0x400000 - text_sector_size - 4); } +static int relative (void) { return (0x1000 - text_sector_size - 4); } + static void replace(char * destination, char * source, int size) { @@ -142,17 +162,6 @@ static void long_prefix(int size, 0x08 * long_size); } -static void modify_registers(int to, - int destination, - int from, - int source) { - // Refactor. - inset(transfer(to, from), - 0xc0 + - 0x01 * (destination & 7) + - 0x08 * (source & 7)); -} - static void modify_memory(int operation, int to, int from) { @@ -161,6 +170,17 @@ static void modify_memory(int operation, 0x08 * operation * (assign(to, from))); } +static int mc0(int code, + int base) { + return (0xc0 + + 0x01 * (code % 8) + + 0x08 * (base % 8)); +} + +static int m05(int code) { + return (0x05 + 0x08 * code); +} + static int store_relative(const int * restrict array) { const int relative = array[1]; @@ -209,9 +229,9 @@ static int build_double(const int * restrict array) { const int source = array[5]; const int offset = text_sector_size + 4; - debug_error(size > D64, "size : double = %i; -- XBA\n", size); - debug_error(to > MEM, "to : double = %i; -- XBA\n", to); - debug_error(from > IMM, "from : double = %i; -- XBA\n", from); + debug_error(size > D64, "@rsize : double = %i;@-\n", size); + debug_error(to > MEM, "@rto : double = %i;@-\n", to); + debug_error(from > IMM, "@rfrom : double = %i;@-\n", from); debug_print("@y%s@- @b%s@- @c%s@- %i @c%s@- %i", operation_name [operation], @@ -251,7 +271,7 @@ static int build_double(const int * restrict array) { 0x04 * (assign(to, from)) + 0xc0 * (attach(to, from))); - modify_registers(to, destination, from, source); + inset(transfer(to, from), mc0(destination, source)); modify_memory(destination, to, from); modify_memory(source, to, from); @@ -277,8 +297,8 @@ static int build_single(const int * restrict array) { const int destination = array[3]; const int offset = text_sector_size + 4; - debug_error(size > D64, "size : single = %i; -- XBA\n", size); - debug_error(to > MEM, "to : single = %i; -- XBA\n", to); + debug_error(size > D64, "@rsize : single = %i;@-\n", size); + debug_error(to > MEM, "@rto : single = %i;@-\n", to); debug_print("@y%s@- @b%s@- @c%s@- %i", operation_name [operation], @@ -388,7 +408,7 @@ static int build_move_if(const int * restrict array) { inset(1, 0x0f); inset(1, 0x40 + operation - MOVE_IF_BEGIN); - modify_registers(to, destination, from, source); + inset(transfer(to, from), mc0(destination, source)); modify_memory(destination, to, from); @@ -453,9 +473,9 @@ static int build_move(const int * restrict array) { const int extension = array[6]; const int offset = 0x1000 - text_sector_size - 4; - debug_error(size > D64, "size : move = %i; -- XBA\n", size); - debug_error(to > MEM, "to : move = %i; -- XBA\n", to); - debug_error(from > IMM, "from : move = %i; -- XBA\n", from); + debug_error(size > D64, "@rsize : move = %i;@-\n", size); + debug_error(to > MEM, "@rto : move = %i;@-\n", to); + debug_error(from > IMM, "@rfrom : move = %i;@-\n", from); debug_print("@ymov@- @b%s@- @c%s@- %i @c%s@- %i %i", size_name [size], @@ -476,7 +496,7 @@ static int build_move(const int * restrict array) { modify_memory(destination, to, from); modify_memory(source, to, from); - modify_registers(to, destination, from, source); + inset(transfer(to, from), mc0(destination, source)); inset((to == REG) && ((from == IMM) || (from == REL)), 0xb0 + 0x08 * (size != D8) + 0x01 * (destination & 7)); @@ -673,7 +693,6 @@ static int build_swap(const int * restrict array) { return 3; } -// Forward or reverse...?! static int build_bit_scan(const int * restrict array) { const int operation = array[0]; const int size = array[1]; @@ -694,7 +713,7 @@ static int build_bit_scan(const int * restrict array) { inset_immediate(1, D16, 0xbc0f + 0x100 * (operation == BSR)); - modify_registers(REG, destination, from, source); + inset(transfer(REG, from), mc0(destination, source)); inset(from == MEM, 0x05 + 0x08 * destination); @@ -703,6 +722,53 @@ static int build_bit_scan(const int * restrict array) { return 5; } +static int build_bit_test(const int * restrict array) { + const int operation = array[0]; + const int size = array[1]; + const int to = array[2]; + const int destination = array[3]; + const int from = array[4]; + const int source = array[5]; + + const int offset = operation - BT; + + debug_error(size > D64, "@rsize : bit test = %i;@-\n", size); + debug_error(to > MEM, "@rto : bit test = %i;@-\n", to); + debug_error(from > IMM, "@rfrom : bit test = %i;@-\n", from); + + debug_error(size == D8, "@rbit test ! size D8@-\n"); + debug_error(from == MEM, "@rbit test ! from MEM@-\n"); + debug_error(source >= 64, "@rbit test ! source %i@-\n", source); + + debug_print("@y%s@- @b%s@- @c%s@- %i @c%s@- %i", + operation_name [operation], + size_name [size], + operand_name [to], + destination, + operand_name [from], + source); + + short_prefix(size); + + long_prefix(size, to, destination, from, source); + + inset(1, 0x0f); + + inset(attach (to, from) || assign(to, from), 0xba); + inset(transfer(to, from) || export(to, from), 0xa3 + 0x08 * offset); + + inset (export (to, from), m05(to)); + inset (assign (to, from), m05(4 + offset)); + inset (attach (to, from), mc0(destination, 4 + offset)); + inset (transfer(to, from), mc0(destination, source)); + + inset_memory(from == MEM, D32, source, relative()); + + inset(1, source); + + return 5; +} + static int build_loop(const int * restrict array) { const int operation = array[0]; const int location = array[3]; @@ -757,8 +823,23 @@ static int (*build_instruction[])(const int * restrict array) = { build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, build_set_if, - build_swap, build_bit_scan, build_bit_scan, build_loop, - build_loop, build_loop + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + //~LEA, CBW, CWD, CDQ, + //~CMC, CLC, CLD, CLI, + //~MOVBE, STC, STD, STI, + //~TEST, UD2, XADD, XCHG, + build_bit_test, build_bit_test, build_bit_test, build_bit_test, + build_bit_scan, build_bit_scan, build_swap, + build_loop, build_loop, build_loop, + NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL + //~REP, REPE, REPNE, + //~INS, OUTS, LODS, STOS, + //~MOVS, CMPS, SCAS }; int main_entry_point = 0; diff --git a/source/assembler.h b/source/assembler.h index af58697..189d7ae 100644 --- a/source/assembler.h +++ b/source/assembler.h @@ -78,11 +78,11 @@ enum { SETS, SETNS, SETPE, SETPO, SETL, SETGE, SETLE, SETG, LEA, CBW, CWD, CDQ, - BT, BTC, BTR, BTS, CMC, CLC, CLD, CLI, MOVBE, STC, STD, STI, TEST, UD2, XADD, XCHG, - BSWAP, BSF, BSR, + BT, BTS, BTR, BTC, + BSF, BSR, BSWAP, LOOP, LOOPE, LOOPNE, REP, REPE, REPNE, INS, OUTS, LODS, STOS, diff --git a/test/heyo.eax b/test/heyo.eax index 76a8868..8a35fc4 100644 --- a/test/heyo.eax +++ b/test/heyo.eax @@ -24,5 +24,8 @@ begin fastcall cyaa fastcall heyo fastcall cyaa + loop heyo + loope heyo + loopne heyo exit 60 end program