Work in progress on finishing encodings...

This commit is contained in:
xolatile 2024-07-23 20:17:44 -04:00
parent daeaace9f8
commit a125c2be2b
5 changed files with 348 additions and 287 deletions

@ -175,7 +175,9 @@ static void build_special_2 (unsigned int operation) {
input_by (1, D16, data [operation - SPECIAL_2_BEGIN]); 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) && (size == D32), 0x0f);
input (far (location), 0x80 + operation - JUMP_IF_BEGIN); input (far (location), 0x80 + 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 == REG) && (from == MEM), destination);
build_at ((to == MEM) && (from == REG), source); 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 == REG) && ((from == IMM) || (from == REL)), 0xb8 + 0x01 * (destination & 0x07));
input ((to == MEM) && (from == IMM), 0xc6 + 0x01 * (size != D8)); 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_at ((to == MEM) && (from == IMM), D32, destination, 0x1000);
input_by ((to == MEM) && (from == IMM), size, source); input_by ((to == MEM) && (from == IMM), size, source);
input_at ((to == REG) && (from == REL), D32, source, 0x4010b0); 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) { 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]); build_special_2 (array [index + 0]);
index += 0; index += 0;
} else if ((array [index] >= JUMP_IF_BEGIN) && (array [index] <= JUMP_IF_END)) { } else if ((array [index] >= JUMP_IF_BEGIN) && (array [index] <= JUMP_IF_END)) {
build_jump_if (array [index + 0], array [index + 1], array [index + 2]); build_jump_if (array [index + 0], array [index + 1], array [index + 2], array [index + 3]);
index += 2; index += 3;
} else if ((array [index] >= MOVE_IF_BEGIN) && (array [index] <= MOVE_IF_END)) { } 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]); build_move_if (array [index + 0], array [index + 1], array [index + 2], array [index + 3], array [index + 4], array [index + 5]);
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]); build_push (array [index + 1], array [index + 2], array [index + 3]);
index += 3; index += 3;
} else { } else {
return; exit (array [index]);
} }
} }

193
test/major_stress_test.asm Normal file

@ -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

@ -1,48 +1,48 @@
fast procedure read_character fast procedure read_character
begin begin
mov eax 0 nop mov eax 0
mov edi r12d nop mov edi r12d
mov esi r13d nop mov esi r13d
mov edx 1 nop mov edx 1
syscall nop syscall
end procedure end procedure
fast procedure write_character fast procedure write_character
begin begin
mov eax 1 nop mov eax 1
mov edi r12d nop mov edi r12d
mov esi r13d nop mov esi r13d
mov edx 1 nop mov edx 1
syscall nop syscall
end procedure end procedure
fast procedure write_string fast procedure write_string
begin begin
mov eax 1 nop mov eax 1
mov edi r12d nop mov edi r12d
mov esi r13d nop mov esi r13d
mov edx r14d nop mov edx r14d
syscall nop syscall
end procedure end procedure
fast procedure write_space fast procedure write_space
s8 space = ' ' s8 space = ' '
begin begin
mov eax 1 nop mov eax 1
mov edi 1 nop mov edi 1
mov esi space nop mov esi space
mov edx 1 nop mov edx 1
syscall nop syscall
end procedure end procedure
fast procedure write_line_feed fast procedure write_line_feed
s8 line_feed = '\n' s8 line_feed = '\n'
begin begin
mov eax 1 nop mov eax 1
mov edi 1 nop mov edi 1
mov esi line_feed nop mov esi line_feed
mov edx 1 nop mov edx 1
syscall nop syscall
end procedure end procedure
unix program main unix program main
@ -52,59 +52,63 @@ unix program main
s32 file = 0 s32 file = 0
s8 byte = 0 s8 byte = 0
begin begin
pop r11 nop nop nop
cmp r11 2
je skip
mov r12d 1
mov r13d failure
mov r14d 12
fastcall write_string
exit 1
skip:
pop r11 nop pop r11
pop r11 nop cmp r11 2
mov eax 2 nop je skip
mov edi r11d nop mov r12d 1
xor esi esi nop mov r13d failure
xor edx edx nop mov r14d 12
syscall nop fastcall write_string
mov [file] eax 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: main_loop:
mov r12d [file] nop mov r12d [file]
mov r13d byte nop mov r13d byte
fastcall read_character 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 nop fastcall write_line_feed
mov r15b [byte]
cmp r15b 144
jne main_loop_skip
fastcall write_line_feed
main_loop_skip:
mov r12d 1 nop mov eax 3
mov r13d digits nop mov edi [file]
sar r15d 4 nop syscall
add r13d r15d
fastcall write_character
mov r12d 1 nop exit 0
mov r13d digits
mov r15b [byte]
and r15d 15
add r13d r15d
fastcall write_character
fastcall write_space nop nop nop
cmp r10d 0
jne main_loop
fastcall write_line_feed
mov eax 3
mov edi [file]
syscall
exit 0
end program end program

@ -4,6 +4,30 @@ segment executable readable
entry R_ENTRY_POINT 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: R_ENTRY_POINT:
; Main function start. ; Main function start.
pop r11 ; Pop argument count from the stack to r13 register. 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. ; Write new line if byte equals "nop" instruction.
mov r10, rax ; Store stop signal to r10. mov r10, rax ; Store stop signal to r10.
nop
nop
nop
mov r15b, [R_D1_BYTE] ; Store byte data to r15b. mov r15b, [R_D1_BYTE] ; Store byte data to r15b.
nop
nop
nop
cmp r15b, 144 ; Compare byte to "nop" instruction. cmp r15b, 144 ; Compare byte to "nop" instruction.
jne MAIN_LOOP_SKIP ; Skip if not "nop" instruction. jne MAIN_LOOP_SKIP ; Skip if not "nop" instruction.
mov r12, R_STANDARD_OUTPUT mov r12, R_STANDARD_OUTPUT
@ -58,13 +76,7 @@ R_ENTRY_POINT:
mov r12, R_STANDARD_OUTPUT mov r12, R_STANDARD_OUTPUT
mov r13, R_A1_DIGITS mov r13, R_A1_DIGITS
nop
nop
nop
mov r15b, [R_D1_BYTE] mov r15b, [R_D1_BYTE]
nop
nop
nop
and r15, 15 and r15, 15
add r13, r15 add r13, r15
call R_F2_WRITE_CHARACTER call R_F2_WRITE_CHARACTER
@ -90,42 +102,12 @@ R_ENTRY_POINT:
syscall syscall
; Main function end. ; 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 segment readable writable
; Program variables and constants. ; Program variables and constants.
R_D8_FILE dq 0 R_D8_FILE dq 0
nop
nop
nop
R_D1_BYTE db 0 R_D1_BYTE db 0
nop
nop
nop
R_A1_DIGITS db '0123456789ABCDEF', 0 R_A1_DIGITS db '0123456789ABCDEF', 0
R_D1_SPACE db 32 R_D1_SPACE db 32
R_D1_NEW_LINE db 10 R_D1_NEW_LINE db 10
@ -151,25 +133,3 @@ R_STANDARD_OUTPUT = 1
R_SUCCESS = 0 R_SUCCESS = 0
R_FAILURE = 1 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

@ -1,154 +1,52 @@
//////////////////////////////////////////////////////////////////////////////// fast procedure getchar
// 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
begin begin
// Not sure about casting... nop mov eax 0
until [message] = 0 repeat nop mov edi 0
fastcall write standard_output message 1 nop mov esi r13d
inc message nop mov edx 1
end repeat nop syscall
end procedure end procedure
//////////////////////////////////////////////////////////////////////////////// fast procedure putchar
fast
procedure fatal
in u64 message
begin begin
fastcall print message nop mov eax 1
exit 1 nop mov edi 1
nop mov esi r13d
nop mov edx 1
nop syscall
end procedure 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 unix program main
s32 file = 0 s8 <> digits = "0123456789ABCDEF"
u8 byte = 0 s8 byte = 0
s8 space = ' '
s8 <> digits = "0123456789abcdef\0" s8 line = '\n'
s8 space = 32
s8 new_line = 10
begin begin
if argc = 2 then main_loop:
fastcall fatal "> argument count must be 2!\n\0" nop mov r13d byte
end if nop fastcall getchar
nop mov r10d eax // Store stop signal to r10.
fastcall open argv 1 0 file nop mov r15b [byte] // Store byte data to r15b.
nop cmp r15b 144 // Compare byte to "nop" instruction.
until r10 = 0 repeat nop jne main_loop_skip // Skip if not "nop" instruction.
fastcall read standard_input byte 1 nop mov r13d line
// When EOF is reached 'rax', then 'r10' is 0. nop fastcall putchar
mov r10 rax main_loop_skip:
nop mov r13d digits
if u8 [byte] = 0x90 then nop machine 0x04ffc149 end machine
fastcall print "\n\0" nop add r13d r15d
end if nop fastcall putchar
nop mov r13d digits
mov r12 standard_output nop mov r15b [byte]
mov r13 digits nop and r15d 15
sar r15 4 nop add r13d r15d
add r13 r15 nop fastcall putchar
fastcall write r12 r13 1 nop mov r13d space
nop fastcall putchar
mov r12 standard_output nop cmp r10d 0 // Compare stop signal to 0.
mov r13 digits nop jne main_loop // Loop back if not equal.
mov r15b [byte] nop mov r12d 1
and r15 15 nop mov r13d line
add r13 r15 nop fastcall putchar
fastcall write r12 r13 1 nop exit 0
fastcall print " \0"
end repeat
fastcall print "\n\0"
fastcall close file
end program end program