From a125c2be2bd8a5e2c59011d0734d9d81d3d67187 Mon Sep 17 00:00:00 2001 From: xolatile Date: Tue, 23 Jul 2024 20:17:44 -0400 Subject: [PATCH] Work in progress on finishing encodings... --- source/assembler.c | 16 ++- test/major_stress_test.asm | 193 +++++++++++++++++++++++++++++++++++++ test/major_stress_test.eax | 150 ++++++++++++++-------------- test/nop.asm | 88 +++++------------ test/nop.eax | 188 +++++++++--------------------------- 5 files changed, 348 insertions(+), 287 deletions(-) create mode 100644 test/major_stress_test.asm diff --git a/source/assembler.c b/source/assembler.c index 41fa9e6..09d038f 100644 --- a/source/assembler.c +++ b/source/assembler.c @@ -175,10 +175,12 @@ static void build_special_2 (unsigned int operation) { input_by (1, D16, data [operation - SPECIAL_2_BEGIN]); } -static void build_jump_if (unsigned int operation, unsigned int size, unsigned int location) { +static void build_jump_if (unsigned int operation, unsigned int size, unsigned int ignore, unsigned int location) { + (void) ignore; + input (far (location) && (size == D32), 0x0f); - input (far (location), 0x80 + operation - JUMP_IF_BEGIN); + input (far (location), 0x80 + operation - JUMP_IF_BEGIN); input (near (location), 0x70 + operation - JUMP_IF_BEGIN); } @@ -219,6 +221,8 @@ static void build_move (unsigned int size, unsigned int to, unsigned int destina build_at ((to == REG) && (from == MEM), destination); build_at ((to == MEM) && (from == REG), source); + build_co ((to == REG) && (from == REG), destination, source); + input ((to == REG) && ((from == IMM) || (from == REL)), 0xb8 + 0x01 * (destination & 0x07)); input ((to == MEM) && (from == IMM), 0xc6 + 0x01 * (size != D8)); @@ -230,6 +234,8 @@ static void build_move (unsigned int size, unsigned int to, unsigned int destina input_at ((to == MEM) && (from == IMM), D32, destination, 0x1000); input_by ((to == MEM) && (from == IMM), size, source); input_at ((to == REG) && (from == REL), D32, source, 0x4010b0); + + input_by ((to == REG) && (from == IMM) && (size == D64), D32, 0); } static void build_call (unsigned int from, unsigned int source) { @@ -352,8 +358,8 @@ void assemble (unsigned int count, unsigned int * array) { build_special_2 (array [index + 0]); index += 0; } else if ((array [index] >= JUMP_IF_BEGIN) && (array [index] <= JUMP_IF_END)) { - build_jump_if (array [index + 0], array [index + 1], array [index + 2]); - index += 2; + build_jump_if (array [index + 0], array [index + 1], array [index + 2], array [index + 3]); + index += 3; } else if ((array [index] >= MOVE_IF_BEGIN) && (array [index] <= MOVE_IF_END)) { build_move_if (array [index + 0], array [index + 1], array [index + 2], array [index + 3], array [index + 4], array [index + 5]); index += 5; @@ -382,7 +388,7 @@ void assemble (unsigned int count, unsigned int * array) { build_push (array [index + 1], array [index + 2], array [index + 3]); index += 3; } else { - return; + exit (array [index]); } } diff --git a/test/major_stress_test.asm b/test/major_stress_test.asm new file mode 100644 index 0000000..453bc11 --- /dev/null +++ b/test/major_stress_test.asm @@ -0,0 +1,193 @@ +format ELF64 executable 3 + +segment executable readable + +entry main + +read_character: + nop +mov eax, 0 + nop +mov edi, r12d + nop +mov esi, r13d + nop +mov edx, 1 + nop +syscall + ret + +write_character: + nop +mov eax, 1 + nop +mov edi, r12d + nop +mov esi, r13d + nop +mov edx, 1 + nop +syscall + ret + +write_string: + nop +mov eax, 1 + nop +mov edi, r12d + nop +mov esi, r13d + nop +mov edx, r14d + nop +syscall + ret + +write_space: + nop +mov eax, 1 + nop +mov edi, 1 + nop +mov esi, space + nop +mov edx, 1 + nop +syscall + ret + +write_line_feed: + nop +mov eax, 1 + nop +mov edi, 1 + nop +mov esi, line_feed + nop +mov edx, 1 + nop +syscall + ret + +main: + nop +nop +nop + + nop +pop r11 + nop +cmp r11, 2 + nop +je skip + nop +mov r12d, 1 + nop +mov r13d, failure + nop +mov r14d, 12 + nop +call write_string + nop +mov eax, 60 +mov edi, 1 +syscall + nop +skip: + + nop +pop r11 + nop +pop r11 + nop +mov eax, 2 + nop +mov edi, r11d + nop +xor esi, esi + nop +xor edx, edx + nop +syscall + nop +mov dword[filaa], eax + + main_loop: + nop +mov r12d, [filaa] + nop +mov r13, bytaa + nop +call read_character +nop + nop +mov r10d, eax + nop +mov r15b, [bytaa] + nop +cmp r15b, 144 + nop +jne main_loop_skip + nop +call write_line_feed + nop +main_loop_skip: +nop + nop +mov r12d, 1 + nop +mov r13d, digits + nop +sar r15d, 4 + nop +add r13d, r15d + nop + call write_character +nop + nop +mov r12d, 1 + nop +mov r13d, digits + nop +mov r15b, [bytaa] + nop +and r15d, 15 + nop +add r13d, r15d + nop + call write_character +nop + nop + call write_space + nop +cmp r10d, 0 + nop +jne main_loop + + nop + call write_line_feed + + nop +mov eax, 3 + nop +mov edi, [filaa] + nop +syscall + + nop +mov eax, 60 +mov edi, 0 +syscall + + nop +nop +nop + +segment readable writable + +space db ' ' +line_feed db '\n' +failure db "> nopfilaa", 10 +digits db "0123456789abcdef" +filaa dd 0 +bytaa db 0 diff --git a/test/major_stress_test.eax b/test/major_stress_test.eax index 1bd7a51..989e630 100644 --- a/test/major_stress_test.eax +++ b/test/major_stress_test.eax @@ -1,48 +1,48 @@ fast procedure read_character begin - mov eax 0 - mov edi r12d - mov esi r13d - mov edx 1 - syscall + nop mov eax 0 + nop mov edi r12d + nop mov esi r13d + nop mov edx 1 + nop syscall end procedure fast procedure write_character begin - mov eax 1 - mov edi r12d - mov esi r13d - mov edx 1 - syscall + nop mov eax 1 + nop mov edi r12d + nop mov esi r13d + nop mov edx 1 + nop syscall end procedure fast procedure write_string begin - mov eax 1 - mov edi r12d - mov esi r13d - mov edx r14d - syscall + nop mov eax 1 + nop mov edi r12d + nop mov esi r13d + nop mov edx r14d + nop syscall end procedure fast procedure write_space s8 space = ' ' begin - mov eax 1 - mov edi 1 - mov esi space - mov edx 1 - syscall + nop mov eax 1 + nop mov edi 1 + nop mov esi space + nop mov edx 1 + nop 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 + nop mov eax 1 + nop mov edi 1 + nop mov esi line_feed + nop mov edx 1 + nop syscall end procedure unix program main @@ -52,59 +52,63 @@ unix program main 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: + nop nop nop - pop r11 - pop r11 - mov eax 2 - mov edi r11d - xor esi esi - xor edx edx - syscall - mov [file] eax + nop pop r11 + nop cmp r11 2 + nop je skip + nop mov r12d 1 + nop mov r13d failure + nop mov r14d 12 + nop fastcall write_string + nop exit 1 + nop skip: + + nop pop r11 + nop pop r11 + nop mov eax 2 + nop mov edi r11d + nop xor esi esi + nop xor edx edx + nop syscall + nop mov [file] eax main_loop: - mov r12d [file] - mov r13d byte - fastcall read_character + nop mov r12d [file] + nop mov r13d byte + nop fastcall read_character +nop + nop mov r10d eax + nop mov r15b [byte] + nop cmp r15b 144 + nop jne main_loop_skip + nop fastcall write_line_feed + nop main_loop_skip: +nop + nop mov r12d 1 + nop mov r13d digits + nop machine 0x04ffc141 end machine + nop add r13d r15d + nop fastcall write_character +nop + nop mov r12d 1 + nop mov r13d digits + nop mov r15b [byte] + nop and r15d 15 + nop add r13d r15d + nop fastcall write_character +nop + nop fastcall write_space + nop cmp r10d 0 + nop jne main_loop - mov r10d eax - mov r15b [byte] - cmp r15b 144 - jne main_loop_skip - fastcall write_line_feed - main_loop_skip: + nop fastcall write_line_feed - mov r12d 1 - mov r13d digits - sar r15d 4 - add r13d r15d - fastcall write_character + nop mov eax 3 + nop mov edi [file] + nop syscall - mov r12d 1 - mov r13d digits - mov r15b [byte] - and r15d 15 - add r13d r15d - fastcall write_character + nop exit 0 - fastcall write_space - cmp r10d 0 - jne main_loop - - fastcall write_line_feed - - mov eax 3 - mov edi [file] - syscall - - exit 0 + nop nop nop end program diff --git a/test/nop.asm b/test/nop.asm index c0ad6fd..e217a4c 100644 --- a/test/nop.asm +++ b/test/nop.asm @@ -4,6 +4,30 @@ segment executable readable entry R_ENTRY_POINT +R_F2_READ_CHARACTER: + mov rax, R_SYSTEM_CALL_READ + mov rdi, r12 + mov rsi, r13 + mov rdx, 1 + syscall + ret + +R_F2_WRITE_CHARACTER: + mov rax, R_SYSTEM_CALL_WRITE + mov rdi, r12 + mov rsi, r13 + mov rdx, 1 + syscall + ret + +R_F3_WRITE_STRING: + mov rax, R_SYSTEM_CALL_WRITE + mov rdi, r12 + mov rsi, r13 + mov rdx, r14 + syscall + ret + R_ENTRY_POINT: ; Main function start. pop r11 ; Pop argument count from the stack to r13 register. @@ -36,13 +60,7 @@ R_ENTRY_POINT: ; Write new line if byte equals "nop" instruction. mov r10, rax ; Store stop signal to r10. - nop - nop - nop mov r15b, [R_D1_BYTE] ; Store byte data to r15b. - nop - nop - nop cmp r15b, 144 ; Compare byte to "nop" instruction. jne MAIN_LOOP_SKIP ; Skip if not "nop" instruction. mov r12, R_STANDARD_OUTPUT @@ -58,13 +76,7 @@ R_ENTRY_POINT: mov r12, R_STANDARD_OUTPUT mov r13, R_A1_DIGITS - nop - nop - nop mov r15b, [R_D1_BYTE] - nop - nop - nop and r15, 15 add r13, r15 call R_F2_WRITE_CHARACTER @@ -90,42 +102,12 @@ R_ENTRY_POINT: syscall ; Main function end. -R_F2_READ_CHARACTER: - mov rax, R_SYSTEM_CALL_READ - mov rdi, r12 - mov rsi, r13 - mov rdx, 1 - syscall - ret - -R_F2_WRITE_CHARACTER: - mov rax, R_SYSTEM_CALL_WRITE - mov rdi, r12 - mov rsi, r13 - mov rdx, 1 - syscall - ret - -R_F3_WRITE_STRING: - mov rax, R_SYSTEM_CALL_WRITE - mov rdi, r12 - mov rsi, r13 - mov rdx, r14 - syscall - ret - segment readable writable ; Program variables and constants. R_D8_FILE dq 0 -nop -nop -nop R_D1_BYTE db 0 -nop -nop -nop R_A1_DIGITS db '0123456789ABCDEF', 0 R_D1_SPACE db 32 R_D1_NEW_LINE db 10 @@ -151,25 +133,3 @@ R_STANDARD_OUTPUT = 1 R_SUCCESS = 0 R_FAILURE = 1 - -;~90_ -;~90_ -;~90_41_80_FF_ -;~90_75_13_49_C7_C4_01_00_00_00_49_C7_C5_2E_12_40_00_E8_A8_00_00_00_49_C7_C4_01_00_00_00_49_C7_C5_1C_12_40_00_49_C1_FF_04_4D_01_FD_E8_8E_00_00_00_49_C7_C4_01_00_00_00_49_C7_C5_1C_12_40_00_ -;~90_ -;~90_ -;~90_44_8A_3D_AB_10_00_00_ -;~90_ -;~90_ -;~90_49_83_E7_0F_4D_01_FD_E8_67_00_00_00_49_C7_C4_01_00_00_00_49_C7_C5_2D_12_40_00_E8_54_00_00_00_49_83_FA_00_0F_85_66_FF_FF_FF_49_C7_C4_01_00_00_00_49_C7_C5_2E_12_40_00_E8_37_00_00_00_48_C7_C0_03_00_00_00_48_8B_3D_53_10_00_00_0F_05_48_C7_C0_3C_00_00_00_48_C7_C7_00_00_00_00_0F_05_48_C7_C0_00_00_00_00_4C_89_E7_4C_89_EE_48_C7_C2_01_00_00_00_0F_05_C3_48_C7_C0_01_00_00_00_4C_89_E7_4C_89_EE_48_C7_C2_01_00_00_00_0F_05_C3_48_C7_C0_01_00_00_00_4C_89_E7_4C_89_EE_4C_89_F2_0F_05_C3_00_00_00_00_00_00_00_00_ -;~90_ -;~90_ -;~90_ -;~[1:R_D1_BYTE]=249=F9 -;~90_ -;~90_ -;~90_49_83_E7_0F_4D_01_FD_E8_67_00_00_00_49_C7_C4_01_00_00_00_49_C7_C5_2D_12_40_00_E8_54_00_00_00_49_83_FA_00_0F_85_66_FF_FF_FF_49_C7_C4_01_00_00_00_49_C7_C5_2E_12_40_00_E8_37_00_00_00_48_C7_C0_03_00_00_00_48_8B_3D_53_10_00_00_0F_05_48_C7_C0_3C_00_00_00_48_C7_C7_00_00_00_00_0F_05_48_C7_C0_00_00_00_00_4C_89_E7_4C_89_EE_48_C7_C2_01_00_00_00_0F_05_C3_48_C7_C0_01_00_00_00_4C_89_E7_4C_89_EE_48_C7_C2_01_00_00_00_0F_05_C3_48_C7_C0_01_00_00_00_4C_89_E7_4C_89_EE_4C_89_F2_0F_05_C3_00_00_00_00_00_00_00_00_ -;~90_ -;~90_ -;~90_ -;~[1:R_D1_BYTE]=171=AB diff --git a/test/nop.eax b/test/nop.eax index d2988f1..f7a01cf 100644 --- a/test/nop.eax +++ b/test/nop.eax @@ -1,154 +1,52 @@ -//////////////////////////////////////////////////////////////////////////////// -// nop -//////////////////////////////////////////////////////////////////////////////// - -u32 system_call_read = 0 -u32 system_call_write = 1 -u32 system_call_open = 2 -u32 system_call_close = 3 - -u32 standard_input = 0 -u32 standard_output = 1 - -//////////////////////////////////////////////////////////////////////////////// - -fast -procedure print - in u64 message +fast procedure getchar begin - // Not sure about casting... - until [message] = 0 repeat - fastcall write standard_output message 1 - inc message - end repeat + nop mov eax 0 + nop mov edi 0 + nop mov esi r13d + nop mov edx 1 + nop syscall end procedure -//////////////////////////////////////////////////////////////////////////////// - -fast -procedure fatal - in u64 message +fast procedure putchar begin - fastcall print message - exit 1 + nop mov eax 1 + nop mov edi 1 + nop mov esi r13d + nop mov edx 1 + nop syscall end procedure -//////////////////////////////////////////////////////////////////////////////// - -fast -procedure read - in s32 file - in u64 data - in u64 size - u8 <> fatal_message = "> failed to read from the file!\n\0" -begin - mov eax system_call_read - mov edi file - mov rsi data - mov rdx size - syscall - - if eax = -1 then - fastcall fatal fatal_message - end if -end procedure - -//////////////////////////////////////////////////////////////////////////////// - -fast -procedure write - in s32 file - in u64 data - in u64 size - u8 <> fatal_message = "> failed to write to the file!\n\0" -begin - mov eax system_call_write - mov edi file - mov rsi data - mov rdx size - syscall - - if eax = -1 then - fastcall fatal fatal_message - end if -end procedure - -//////////////////////////////////////////////////////////////////////////////// - -fast -procedure open - in u64 name - in s32 mode - in s32 file -begin - mov eax system_call_open - mov rdi name - mov esi mode - syscall - - if eax = -1 then - fastcall fatal "> failed to open the file!\n\0" - end if - - mov [file] eax -end procedure - -//////////////////////////////////////////////////////////////////////////////// - -fast -procedure close - in s32 file -begin - mov eax system_call_close - mov edi file - syscall - - if eax = -1 then - fastcall fatal "> failed to close the file!\n\0" - end if -end procedure - -//////////////////////////////////////////////////////////////////////////////// - unix program main - s32 file = 0 - u8 byte = 0 - - s8 <> digits = "0123456789abcdef\0" - s8 space = 32 - s8 new_line = 10 + s8 <> digits = "0123456789ABCDEF" + s8 byte = 0 + s8 space = ' ' + s8 line = '\n' begin - if argc = 2 then - fastcall fatal "> argument count must be 2!\n\0" - end if - - fastcall open argv 1 0 file - - until r10 = 0 repeat - fastcall read standard_input byte 1 - // When EOF is reached 'rax', then 'r10' is 0. - mov r10 rax - - if u8 [byte] = 0x90 then - fastcall print "\n\0" - end if - - mov r12 standard_output - mov r13 digits - sar r15 4 - add r13 r15 - fastcall write r12 r13 1 - - mov r12 standard_output - mov r13 digits - mov r15b [byte] - and r15 15 - add r13 r15 - fastcall write r12 r13 1 - - fastcall print " \0" - end repeat - - fastcall print "\n\0" - fastcall close file + main_loop: + nop mov r13d byte + nop fastcall getchar + nop mov r10d eax // Store stop signal to r10. + nop mov r15b [byte] // Store byte data to r15b. + nop cmp r15b 144 // Compare byte to "nop" instruction. + nop jne main_loop_skip // Skip if not "nop" instruction. + nop mov r13d line + nop fastcall putchar + main_loop_skip: + nop mov r13d digits + nop machine 0x04ffc149 end machine + nop add r13d r15d + nop fastcall putchar + nop mov r13d digits + nop mov r15b [byte] + nop and r15d 15 + nop add r13d r15d + nop fastcall putchar + nop mov r13d space + nop fastcall putchar + nop cmp r10d 0 // Compare stop signal to 0. + nop jne main_loop // Loop back if not equal. + nop mov r12d 1 + nop mov r13d line + nop fastcall putchar + nop exit 0 end program