From f2983fdabf98e1b64efd6744b332ecf1d6e78b00 Mon Sep 17 00:00:00 2001
From: xolatile <xolatile@proton.me>
Date: Tue, 23 Jul 2024 07:44:40 -0400
Subject: [PATCH 1/2] Minor fuckarounds in assembler...

---
 source/assembler.c    | 76 ++++++++++++++++++-------------------------
 test/forward_jump.eax |  3 +-
 test/proc.asm         | 62 ++++++++++++-----------------------
 test/proc.m70         | 32 ++++++++++++++++++
 4 files changed, 84 insertions(+), 89 deletions(-)
 create mode 100644 test/proc.m70

diff --git a/source/assembler.c b/source/assembler.c
index c544b21..724255c 100644
--- a/source/assembler.c
+++ b/source/assembler.c
@@ -19,8 +19,8 @@
 
 static int assemble_clean_up_queued = 0;
 
-static unsigned int   empty_count = 0;
-static unsigned int   empty_holes = 0;
+static unsigned int   empty_count = 1;
+static unsigned int   empty_holes = 1;
 static unsigned int * empty_array = NULL;
 static unsigned int * empty_imbue = NULL;
 static unsigned int * empty_store = NULL;
@@ -64,10 +64,7 @@ static void asmdirimm (int when, unsigned int size, unsigned int data) {
 static void input_at (int when, unsigned int size, unsigned int data, unsigned int base) {
 	asmdirrel (when, data);
 
-	input ((when),                  (base >>  0) & 0xff);
-	input ((when) && (size >= D16), (base >>  8) & 0xff);
-	input ((when) && (size >= D32), (base >> 16) & 0xff);
-	input ((when) && (size >= D32), (base >> 24) & 0xff);
+	input_by (when, size, base);
 }
 
 static int front (unsigned int data) {
@@ -90,11 +87,11 @@ static int near (unsigned int label) {
 	return (label && 0);
 }
 
-static void build_short (int when) {
+static void build_short_prefix (int when) {
 	input (when, 0x66);
 }
 
-static void build_long (unsigned int registers, unsigned int to, unsigned int from) {
+static void build_long_prefix (unsigned int registers, unsigned int to, unsigned int from) {
 	input (registers || to || from, 0x40 + 0x01 * to + 0x04 * from + 0x08 * registers);
 }
 
@@ -111,11 +108,9 @@ static void build_constant (int when, unsigned int size) {
 }
 
 static void build_regular (unsigned int operation, unsigned int size, unsigned int to, unsigned int destination, unsigned int from, unsigned int source) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
-	build_long (size == D64,
-	          (to   == REG) && (upper (destination)),
-	          (from == REG) && (upper (source)));
+	build_long_prefix (size == D64, (to == REG) && (upper (destination)), (from == REG) && (upper (source)));
 
 	input ((size == D8) && (to == REG)
 	      && ((from == REG) || (from == IMM))
@@ -126,9 +121,7 @@ static void build_regular (unsigned int operation, unsigned int size, unsigned i
 
 	input ((from == IMM) && (to == REG) && (destination == 0), 0x05 + 0x08 * (operation & 0x07) - 0x01 * (size == D8));
 
-	build_constant ((from == IMM)
-	               && ! ((to == REG) && (destination == 0)),
-	               size);
+	build_constant ((from == IMM) && ! ((to == REG) && (destination == 0)), size);
 
 	input (! ((from == IMM) && (to == REG) && (destination == 0)),
 	      (destination & 0x07) * ((to == REG) && (from == IMM))
@@ -153,16 +146,15 @@ static void build_regular (unsigned int operation, unsigned int size, unsigned i
 }
 
 static void build_irregular (unsigned int operation, unsigned int size, unsigned int to, unsigned int destination) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
-	build_long (size == D64, (to == REG) && (upper (destination)), 0);
+	build_long_prefix (size == D64, (to == REG) && (upper (destination)), 0);
 
 	input ((size == D8) && (to == REG) && front (destination), 0x40);
 
 	input (1, 0xf7 + 0x08 * ((operation == INC) || (operation == DEC)) - 0x01 * (size == D8));
 
 	input (to == REG, 0xc0 + 0x08 * (operation - IRREGULAR_BEGIN) + 0x01 * (destination & 0x07));
-
 	input (to == MEM, 0x05 + 0x08 * (operation - IRREGULAR_BEGIN));
 }
 
@@ -191,9 +183,9 @@ static void build_jump_if (unsigned int operation, unsigned int size, unsigned i
 }
 
 static void build_move_if (unsigned int operation, unsigned int size, unsigned int to, unsigned int destination, unsigned int from, unsigned int source) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
-	build_long (size == D64, (to   == REG) && (upper (destination)), (from == REG) && (upper (source)));
+	build_long_prefix (size == D64, (to == REG) && (upper (destination)), (from == REG) && (upper (source)));
 
 	input (1, 0x0f);
 	input (1, 0x40 + operation - MOVE_IF_BEGIN);
@@ -211,26 +203,25 @@ static void build_jump (unsigned int size, unsigned int to, unsigned int destina
 	input (to == MEM, 0xff);
 	input (to == MEM, 0x25);
 
-	input_at (to == REL, D32, destination, - (text_sector_size + 4));
-
+	input_at (to == REL, D32, destination, -(text_sector_size + 4));
 	input_at (to == MEM, D32, destination, 0x4010b0);
 }
 
 static void build_move (unsigned int size, unsigned int to, unsigned int destination, unsigned int from, unsigned int source) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
-	build_long (size == D64, (to   == REG) && (upper (destination)), (from == REG) && (upper (source)));
+	build_long_prefix (size == D64, (to == REG) && (upper (destination)), (from == REG) && (upper (source)));
 
-	input ((to == REG) && (from == REG), 0x88 + (size != D8));
-	input ((to == REG) && (from == MEM), 0x8a + (size != D8));
-	input ((to == MEM) && (from == REG), 0x88 + (size != D8));
+	input ((to == REG) && (from == REG), 0x88 + 0x01 * (size != D8));
+	input ((to == REG) && (from == MEM), 0x8a + 0x01 * (size != D8));
+	input ((to == MEM) && (from == REG), 0x88 + 0x01 * (size != D8));
 
 	build_at ((to == REG) && (from == MEM), destination);
 	build_at ((to == MEM) && (from == REG), source);
 
 	input ((to == REG) && ((from == IMM) || (from == REL)), 0xb8 + 0x01 * (destination & 0x07));
 
-	input ((to == MEM) && (from == IMM), 0xc6 + (size != D8));
+	input ((to == MEM) && (from == IMM), 0xc6 + 0x01 * (size != D8));
 	input ((to == MEM) && (from == IMM), 0x05);
 
 	input_at ((to == REG) && (from == MEM), D32,  source, 0x1000);
@@ -247,7 +238,7 @@ static void build_call (unsigned int from, unsigned int source) {
 	input (from == REL, 0xe8);
 	input (from == REG, 0xff);
 
-	input_at (from == REL, D32, source, - (text_sector_size + 4));
+	input_at (from == REL, D32, source, -(text_sector_size + 4));
 
 	input (from == REG, (0xd0 + 0x01 * (source & 0x07)));
 }
@@ -260,7 +251,7 @@ static void build_enter (unsigned int dynamic_storage, unsigned int nesting_leve
 }
 
 static void build_in_out (unsigned int move, unsigned int size, unsigned int type, unsigned int port) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
 	input (1, 0xe4 + 0x01 * (size != D8) + 0x02 * (move != OUT) + 0x08 * (type == REG));
 
@@ -268,7 +259,7 @@ static void build_in_out (unsigned int move, unsigned int size, unsigned int typ
 }
 
 static void build_pop (unsigned int size, unsigned int to, unsigned int destination) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
 	input ((to == REG) && (upper (destination)), 0x41);
 
@@ -280,7 +271,7 @@ static void build_pop (unsigned int size, unsigned int to, unsigned int destinat
 }
 
 static void build_push (unsigned int size, unsigned int from, unsigned int source) {
-	build_short (size == D16);
+	build_short_prefix (size == D16);
 
 	input ((from == REG) && (upper (source)), 0x41);
 
@@ -314,11 +305,11 @@ static void assemble_clean_up (void) {
 unsigned int    text_sector_size = 0;
 unsigned char * text_sector_byte = NULL;
 
-          int was_instruction_array_empty = 0;
+         int was_instruction_array_empty = 0;
 unsigned int text_entry_point            = 0;
 
 void assemble (unsigned int count, unsigned int * array) {
-	unsigned int index = 0;
+	unsigned int index;
 
 	if ((count == 0) || (array == NULL)) {
 		was_instruction_array_empty = 1;
@@ -335,7 +326,7 @@ void assemble (unsigned int count, unsigned int * array) {
 		assemble_clean_up_queued = !assemble_clean_up_queued;
 	}
 
-	while (index < count) {
+	for (index = 0; index < count; ++index) {
 		if (array [index] == ASMDIRREL) {
 			asmdirrel (1, array [index + 1]);
 			index += 1;
@@ -343,10 +334,9 @@ void assemble (unsigned int count, unsigned int * array) {
 			asmdirmem (1, array [index + 1]);
 			index += 1;
 		} else if (array [index] == ASMDIRIMM) {
-			unsigned int repeat = 0;
-			while (repeat < array [index + 2]) {
+			unsigned int repeat;
+			for (repeat = 0; repeat < array [index + 2]; ++repeat) {
 				asmdirimm (1, array [index + 1], array [index + 3 + repeat]);
-				++repeat;
 			}
 			index += array [index + 2] + 2;
 		} else if ((array [index] >= REGULAR_BEGIN) && (array [index] <= REGULAR_END)) {
@@ -394,24 +384,20 @@ void assemble (unsigned int count, unsigned int * array) {
 		} else {
 			return;
 		}
-
-		++index;
 	}
 
 	text_entry_point = empty_store  [0];
 
-	index = 0;
-
-	while (index < empty_holes) {
+	for (index = 1; index < empty_holes; ++index) {
 		unsigned int set = 0;
 		unsigned int get = empty_array [index];
 
 		replace ((unsigned char *) & set, & text_sector_byte [get], sizeof (set));
 
+		printf ("@%08x :%08x <> %08x + %08x -> %08x\n", get, empty_imbue [index], set, empty_store [empty_imbue [index]], set + empty_store [empty_imbue [index]]);
+
 		set += empty_store [empty_imbue [index]];
 
 		replace (& text_sector_byte [get], (unsigned char *) & set, sizeof (set));
-
-		++index;
 	}
 }
diff --git a/test/forward_jump.eax b/test/forward_jump.eax
index 72ee6b9..4ab103b 100644
--- a/test/forward_jump.eax
+++ b/test/forward_jump.eax
@@ -8,7 +8,6 @@ begin
     mov esi heyo_data
     mov edx 12
     syscall
-
   lol:
-    exit 86
+    exit 255
 end program
diff --git a/test/proc.asm b/test/proc.asm
index e02a0be..afd9307 100644
--- a/test/proc.asm
+++ b/test/proc.asm
@@ -1,45 +1,23 @@
-fast procedure heyo
-	s8 <> h = "Heyo world!\n"
-begin
-	nop mov eax 1
-	nop mov edi 1
-	nop mov esi h
-	nop mov edx 12
-	nop syscall
-end procedure
+format ELF64 executable 3
 
-fast procedure lnao
-	s8 <> l = "Lnao world!!!!\n"
-begin
-	nop mov eax 1
-	nop mov edi 1
-	nop mov esi l
-	nop mov edx 15
-	nop syscall
-end procedure
+segment readable executable
 
-fast procedure cyaa
-	s8 <> c = "Cyaa world!!!\n"
-begin
-	nop mov eax 1
-	nop mov edi 1
-	nop mov esi c
-	nop mov edx 14
-	nop syscall
-end procedure
+entry $
 
-unix program main
-	s8 <> m = "Meme world!!\n"
-begin
-	nop fastcall heyo
-	nop mov eax 1
-	nop mov edi 1
-	nop mov esi m
-	nop mov edx 13
-	nop syscall
-	nop fastcall cyaa
-	nop fastcall lnao
-	nop mov eax 60
-	nop mov edi 60
-	nop syscall
-end program
+	jmp cyaa
+
+	mov eax, 1
+	mov edi, 1
+	mov esi, heyo
+	mov edx, 12
+	syscall
+
+	cyaa:
+
+	mov eax, 60
+	mov edi, 60
+	syscall
+
+segment readable writable
+
+heyo: db 'Heyo world!', 10
diff --git a/test/proc.m70 b/test/proc.m70
new file mode 100644
index 0000000..4f21892
--- /dev/null
+++ b/test/proc.m70
@@ -0,0 +1,32 @@
+7F 45 4C 46 02 01 01 03
+00 00 00 00 00 00 00 00
+02 00 3E 00 01 00 00 00
+B0 00 40 00 00 00 00 00
+40 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 40 00 38 00
+02 00 40 00 00 00 00 00
+01 00 00 00 05 00 00 00
+00 00 00 00 00 00 00 00
+00 00 40 00 00 00 00 00
+00 00 40 00 00 00 00 00
+D4 00 00 00 00 00 00 00
+D4 00 00 00 00 00 00 00
+00 10 00 00 00 00 00 00
+01 00 00 00 06 00 00 00
+D4 00 00 00 00 00 00 00
+D4 10 40 00 00 00 00 00
+D4 10 40 00 00 00 00 00
+0C 00 00 00 00 00 00 00
+0C 00 00 00 00 00 00 00
+00 10 00 00 00 00 00 00
+E9 16 00 00 00
+B8 01 00 00 00
+BF 01 00 00 00
+BE D4 10 40 00
+BA 0C 00 00 00
+0F 05
+B8 3C 00 00 00
+BF 3C 00 00 00
+0F 05
+48 65 79 6F 20 77 6F 72 6C 64 21 0A

From 248bfc14ccb9edbfaa2e625af2ed9e5593d22d63 Mon Sep 17 00:00:00 2001
From: xolatile <xolatile@proton.me>
Date: Tue, 23 Jul 2024 07:46:26 -0400
Subject: [PATCH 2/2] Added wrong file...

---
 test/major_stress_test.eax | 110 +++++++++++++++++++++++++++++++++++++
 test/proc.m70              |  32 -----------
 2 files changed, 110 insertions(+), 32 deletions(-)
 create mode 100644 test/major_stress_test.eax
 delete mode 100644 test/proc.m70

diff --git a/test/major_stress_test.eax b/test/major_stress_test.eax
new file mode 100644
index 0000000..453a514
--- /dev/null
+++ b/test/major_stress_test.eax
@@ -0,0 +1,110 @@
+fast procedure read_character
+begin
+	mov eax 0
+	mov edi r12d
+	mov esi r13d
+	mov edx 1
+	syscall
+end procedure
+
+fast procedure write_character
+begin
+	mov eax 1
+	mov edi r12d
+	mov esi r13d
+	mov edx 1
+	syscall
+end procedure
+
+fast procedure write_string
+begin
+	mov eax 1
+	mov edi r12d
+	mov esi r13d
+	mov edx r14d
+	syscall
+end procedure
+
+fast procedure write_space
+	s8 space = " "
+begin
+	mov eax 1
+	mov edi 1
+	mov esi space
+	mov edx 1
+	syscall
+end procedure
+
+fast procedure write_line_feed
+	s8 line_feed = "\n"
+begin
+	mov eax 1
+	mov edi 1
+	mov esi line_feed
+	mov edx 1
+	syscall
+end procedure
+
+unix program main
+	s8 <> failure = "> nop file\n\0"
+	s8 <> digits  = "0123456789abcdef"
+
+	s32 file   = 0
+	s8  byte   = 0
+begin
+	pop r11
+	cmp r11 2
+	je skip
+	mov r12d 1
+	mov r13d failure
+	mov r14d 12
+	fastcall write_string
+	exit 1
+	skip:
+
+	pop r11
+	pop r11
+	mov eax 2
+	mov edi r11d
+	xor esi esi
+	xor edx edx
+	syscall
+	mov [file] eax
+
+	main_loop:
+		mov r12d [file]
+		mov r13d byte
+		call read_character
+
+		mov r10d eax
+		mov r15b [byte]
+		cmp r15b 144
+		jne main_loop_skip
+		fastcall write_line_feed
+		main_loop_skip:
+
+		mov r12d 1
+		mov r13d digits
+		sar r15d 4
+		add r13d r15d
+		call write_character
+
+		mov r12d 1
+		mov r13d digits
+		mov r15b [byte]
+		and r15d 15
+		add r13d r15d
+		fastcall write_character
+
+		fastcall write_space
+	cmp r10d 0
+	jne main_loop
+
+	fastcall write_line_feed
+
+	mov eax 3
+	mov edi [file]
+	syscall
+
+	exit 0
+end program
diff --git a/test/proc.m70 b/test/proc.m70
deleted file mode 100644
index 4f21892..0000000
--- a/test/proc.m70
+++ /dev/null
@@ -1,32 +0,0 @@
-7F 45 4C 46 02 01 01 03
-00 00 00 00 00 00 00 00
-02 00 3E 00 01 00 00 00
-B0 00 40 00 00 00 00 00
-40 00 00 00 00 00 00 00
-00 00 00 00 00 00 00 00
-00 00 00 00 40 00 38 00
-02 00 40 00 00 00 00 00
-01 00 00 00 05 00 00 00
-00 00 00 00 00 00 00 00
-00 00 40 00 00 00 00 00
-00 00 40 00 00 00 00 00
-D4 00 00 00 00 00 00 00
-D4 00 00 00 00 00 00 00
-00 10 00 00 00 00 00 00
-01 00 00 00 06 00 00 00
-D4 00 00 00 00 00 00 00
-D4 10 40 00 00 00 00 00
-D4 10 40 00 00 00 00 00
-0C 00 00 00 00 00 00 00
-0C 00 00 00 00 00 00 00
-00 10 00 00 00 00 00 00
-E9 16 00 00 00
-B8 01 00 00 00
-BF 01 00 00 00
-BE D4 10 40 00
-BA 0C 00 00 00
-0F 05
-B8 3C 00 00 00
-BF 3C 00 00 00
-0F 05
-48 65 79 6F 20 77 6F 72 6C 64 21 0A