Added barebones memory addressing, testing in progress...

This commit is contained in:
xolatile
2024-07-13 18:31:49 -04:00
parent 05400a303b
commit 2e06b65f58
7 changed files with 328 additions and 200 deletions

@ -1,6 +1,7 @@
#include "assembler.h"
#include <stdlib.h>
#include <string.h>
#define REGULAR_BEGIN (ADD)
#define REGULAR_END (CMP)
@ -23,11 +24,12 @@
#define MOVE_IF_COUNT (MOVE_IF_END - MOVE_IF_BEGIN + 1)
// Regulates displacement, immediate, label, variable, constant, string data.
static next holes;
static next * empty_from;
static next * empty_to;
static next * imbue_with;
static next * imbue_size;
#define TESTING_FOR_NOW (144)
static next empty_count = 0;
static next empty_holes = 0;
static next empty_array [TESTING_FOR_NOW] = { 0 };
static next empty_imbue [TESTING_FOR_NOW] = { 0 };
static next empty_store [TESTING_FOR_NOW] = { 0 };
// Main function.
static void place (form when,
@ -46,24 +48,45 @@ static void print (form when,
place ((when != 0) && (size >= D16), (byte) ((data >> 8) & 0xff));
place ((when != 0) && (size >= D32), (byte) ((data >> 16) & 0xff));
place ((when != 0) && (size >= D32), (byte) ((data >> 24) & 0xff));
/* 64-BIT SUPPORT */
}
static void asmdirrel (next data) {
empty_array [empty_holes] = text_sector_size;
empty_imbue [empty_holes] = data;
empty_holes += 1;
}
static void asmdirmem (void) {
empty_store [empty_count] = text_sector_size;
empty_count += 1;
}
static void delay (form when,
size_index size,
next data) {
/* */
empty_from [holes] = text_sector_size;
imbue_with [holes] = data;
imbue_size [holes] = size;
place ((when != 0) && (size >= D8), (byte) 0x00);
place ((when != 0) && (size >= D16), (byte) 0x40);
place ((when != 0) && (size >= D32), (byte) 0x10);
place ((when != 0) && (size >= D32), (byte) 0x00);
/* NO DEREFERENCING */
/* 64-BIT SUPPORT */
place ((when != 0) && (size >= D8), (byte) ((data >> 0) & 0xff));
place ((when != 0) && (size >= D16), (byte) ((data >> 8) & 0xff));
place ((when != 0) && (size >= D32), (byte) ((data >> 16) & 0xff));
place ((when != 0) && (size >= D32), (byte) ((data >> 24) & 0xff));
asmdirrel (data);
}
empty_to [holes] = text_sector_size;
static void asmdirimm (size_index size,
next redo,
next data) {
/* */
next i;
holes += when;
for (i = 0; i < redo; ++i) {
print (1, size, data);
}
}
static form front (form data) { return ((data >= 4) && (data <= 7)); }
@ -132,9 +155,9 @@ static void build_regular (operation_index operation,
// 40>front
place ((size == D8) && (to == REG) && ((from == REG) || (from == IMM))
&& (((front (destination) && lower (source))
|| (lower (destination) && front (source))) ||
((to == REG) && (from == IMM) && front (destination))),
&& (((front ((form) destination) && lower ((form) source))
|| (lower ((form) destination) && front ((form) source))) ||
((to == REG) && (from == IMM) && front ((form) destination))),
(byte) 0x40);
place ((from == IMM) && (to == REG) && (destination == 0),
@ -179,7 +202,7 @@ static void build_irregular (operation_index operation,
(to == REG) && (upper ((form) destination)), 0);
// 40>front
place ((size == D8) && (to == REG) && front (destination), (byte) 0x40);
place ((size == D8) && (to == REG) && front ((form) destination), (byte) 0x40);
place (1, (byte) (0xf7
+ 0x08 * ((operation == INC) || (operation == DEC))
@ -289,13 +312,14 @@ static void build_move (size_index size,
build_register_redirection ((to == MEM) && (from == REG), source);
place ((to == REG) && (from == IMM), (byte) (0xb8
+ 0x08 * (size != D8)
//~+ 0x08 * (size != D8)
+ 0x01 * (destination & 0x07)));
place ((to == MEM) && (from == IMM), (byte) (0xc6 + (size != D8)));
place ((to == MEM) && (from == IMM), (byte) (0x05));
delay ((to == REG) && (from == MEM), D32, (next) ~0);
delay ((to == REG) && (from == MEM), D32, source);
delay ((to == REG) && (from == REL), D32, source);
print ((to == REG) && (from == IMM), size, source);
delay ((to == MEM) && (from == REG), D32, (next) ~0);
delay ((to == MEM) && (from == IMM), D32, (next) ~0);
@ -318,7 +342,18 @@ void assemble (next count,
}
for (index = 0; index < count; ++index) {
if ((array [index] >= REGULAR_BEGIN)
if (array [index] == ASMDIRREL) {
asmdirrel (array [index + 1]);
index += 1;
} else if (array [index] == ASMDIRMEM) {
asmdirmem ();
index += 0;
//~} else if (array [index] == ASMDIRIMM) {
//~asmdirimm (array [index + 1], array [index + 2],
//~array [index + 3]);
//~index += 3;
//~} else if (array [index] == ASMDIRREP) {
} else if ((array [index] >= REGULAR_BEGIN)
&& (array [index] <= REGULAR_END)) {
build_regular (array [index + 0], array [index + 1],
array [index + 2], array [index + 3],
@ -335,6 +370,7 @@ void assemble (next count,
index += 0;
} else if ((array [index] >= SPECIAL_2_BEGIN)
&& (array [index] <= SPECIAL_2_END)) {
printf ("spc\n");
build_special_2 (array [index + 0]);
index += 0;
} else if ((array [index] >= JUMP_IF_BEGIN)
@ -353,12 +389,22 @@ void assemble (next count,
array [index + 3]);
index += 3;
} else if (array [index] == MOV) {
printf ("mov\n");
build_move (array [index + 1], array [index + 2],
array [index + 3], array [index + 4],
array [index + 5]);
index += 5;
} else {
exit (EXIT_FAILURE); // For debugging only!
exit (array [index]); // For debugging only!
}
}
return;
for (index = 0; index < empty_holes; ++index) {
next set = 0, get = empty_array [index];
memcpy (& set, & text_sector_byte [get], sizeof (set));
set += empty_store [empty_imbue [index]];
memcpy (& text_sector_byte [get], & set, sizeof (set));
}
}

@ -16,7 +16,10 @@ typedef enum {
} type_index;
typedef enum {
RECORD,
ASMDIRMEM, // Assembler directive: >LABEL '"goto" clabel;'
ASMDIRREL, // Assembler directive: LABEL: 'clabel:'
ASMDIRIMM, // Assembler directive: D8-D64 {REDO} {DATA}
ASMDIRREP, // Assembler directive: ...
/* HIGHLY EXPERIMENTAL CODE ABOVE... */
ADD, OR, ADC, SBB,
AND, SUB, XOR, CMP,

@ -9,9 +9,8 @@ entry $
mov esi, heyo
mov edx, 12
syscall
mov eax, 60
mov edi, 0
mov edi, 60
syscall
segment readable writable

@ -1,17 +1,14 @@
unix program heyo_world
s8 [] heyo = "Heyo world!\n"
begin
mov eax 1
mov edi 1
mov esi 4198610
mov esi heyo
mov edx 12
syscall
mov eax 60
mov edi 0
mov edi 60
syscall
end program
/*

175
test/nop.asm Normal file

@ -0,0 +1,175 @@
format ELF64 executable 3
segment executable readable
entry R_ENTRY_POINT
R_ENTRY_POINT:
; Main function start.
pop r11 ; Pop argument count from the stack to r13 register.
cmp r11, 2 ; Compare argument count to argument 0.
je SKIP ; Skip if equal, write message and exit if unequal.
mov r12, R_STANDARD_OUTPUT
mov r13, R_A1_FAILURE_ARGUMENT_COUNT
mov r14, R_S8_FAILURE_ARGUMENT_COUNT
call R_F3_WRITE_STRING
mov rax, R_SYSTEM_CALL_EXIT
mov rdi, R_FAILURE
syscall
SKIP:
; Open default file in read only mode.
pop r11 ; Ignore argument 0.
pop r11 ; Pop argument 1 from the stack to r12 register.
mov rax, R_SYSTEM_CALL_OPEN
mov rdi, r11
xor rsi, rsi
xor rdx, rdx
syscall
mov [R_D8_FILE], rax
MAIN_LOOP:
; Read 1 byte from default file.
mov r12, [R_D8_FILE]
mov r13, R_D1_BYTE
call R_F2_READ_CHARACTER
; 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
mov r13, R_D1_NEW_LINE
call R_F2_WRITE_CHARACTER
MAIN_LOOP_SKIP:
mov r12, R_STANDARD_OUTPUT
mov r13, R_A1_DIGITS
sar r15, 4
add r13, r15
call R_F2_WRITE_CHARACTER
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
mov r12, R_STANDARD_OUTPUT
mov r13, R_D1_SPACE
call R_F2_WRITE_CHARACTER
cmp r10, 0 ; Compare stop signal to 0.
jne MAIN_LOOP ; Loop back if not equal.
mov r12, R_STANDARD_OUTPUT
mov r13, R_D1_NEW_LINE
call R_F2_WRITE_CHARACTER
; Close default file.
mov rax, R_SYSTEM_CALL_CLOSE
mov rdi, [R_D8_FILE]
syscall
mov rax, R_SYSTEM_CALL_EXIT
mov rdi, R_SUCCESS
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
; Program strings and string sizes.
R_A1_FAILURE_ARGUMENT_COUNT db 'Failure: Argument count must be one.', 10, 0
R_S8_FAILURE_ARGUMENT_COUNT = $-R_A1_FAILURE_ARGUMENT_COUNT
; GNU/Linux OS system call enumeration.
R_SYSTEM_CALL_READ = 0
R_SYSTEM_CALL_WRITE = 1
R_SYSTEM_CALL_OPEN = 2
R_SYSTEM_CALL_CLOSE = 3
R_SYSTEM_CALL_EXIT = 60
; GNU/Linux OS standard input and output descriptors and exit codes.
R_STANDARD_INPUT = 0
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

@ -4,182 +4,15 @@ segment readable executable
entry $
fff:
nop
mov eax, 1
nop
mov edi, 1
nop
mov esi, heyo
nop
mov edx, 12
nop
syscall
nop
nop
nop
mov eax, 1
nop
mov edi, 1
nop
mov esi, cyaa
nop
mov edx, 12
nop
syscall
nop
nop
nop
mov eax, 1
nop
mov edi, 1
nop
mov esi, neko
nop
mov edx, 12
nop
syscall
nop
nop
nop
mov rax, 1122334455667788h
nop
mov rcx, 1122334455667788h
nop
mov rdx, 1122334455667788h
nop
mov rbx, 1122334455667788h
nop
mov rsp, 1122334455667788h
nop
mov rbp, 1122334455667788h
nop
mov rsi, 1122334455667788h
nop
mov rdi, 1122334455667788h
nop
mov r8, 1122334455667788h
nop
mov r9, 1122334455667788h
nop
mov r10, 1122334455667788h
nop
mov r11, 1122334455667788h
nop
mov r12, 1122334455667788h
nop
mov r13, 1122334455667788h
nop
mov r14, 1122334455667788h
nop
mov r15, 1122334455667788h
nop
nop
nop
mov eax, 60
nop
mov edi, 0
nop
mov edi, 60
syscall
nop
jmp fff
nop
jmp nnn
nop
nop
nop
nnn:
segment readable writable
heyo: db "heyo world!", 10
cyaa: db "cyaa world!", 10
neko: db "neko world!", 10
;~176 / 00B0 ; header
;~504 / 01F8 ; executable
;~292 / 0124 ; readable
;~36 / 0024 ; writable
;~328 / 0148 ; executable - header
;~7F_45_4C_46_02_01_01_03_
;~00_00_00_00_00_00_00_00_ ; 0
;~02_00_3E_00_01_00_00_00_
;~B0_00_40_00_00_00_00_00_ ; header + hardcore
;~40_00_00_00_00_00_00_00_
;~00_00_00_00_00_00_00_00_ ; 0
;~00_00_00_00_40_00_38_00_
;~02_00_40_00_00_00_00_00_
;~01_00_00_00_05_00_00_00_ ; text
;~00_00_00_00_00_00_00_00_ ; 0
;~00_00_40_00_00_00_00_00_ ; hardcore
;~00_00_40_00_00_00_00_00_ ; hardcore
;~D4_01_00_00_00_00_00_00_ ; executable - writable
;~D4_01_00_00_00_00_00_00_ ; executable - writable
;~00_10_00_00_00_00_00_00_ ; softcore
;~01_00_00_00_06_00_00_00_ ; data
;~D4_01_00_00_00_00_00_00_ ; executable - writable
;~D4_11_40_00_00_00_00_00_ ; hardcore + softcore + executable - writable
;~D4_11_40_00_00_00_00_00_ ; hardcore + softcore + executable - writable
;~24_00_00_00_00_00_00_00_ ; writable
;~24_00_00_00_00_00_00_00_ ; writable
;~00_10_00_00_00_00_00_00_ ; softcore
;~90_B8_01_00_00_00_
;~90_BF_01_00_00_00_
;~90_BE_D4_11_40_00_
;~90_BA_0C_00_00_00_
;~90_0F_05_
;~90_
;~90_
;~90_B8_01_00_00_00_
;~90_BF_01_00_00_00_
;~90_BE_E0_11_40_00_
;~90_BA_0C_00_00_00_
;~90_0F_05_
;~90_
;~90_
;~90_B8_01_00_00_00_
;~90_BF_01_00_00_00_
;~90_BE_EC_11_40_00_
;~90_BA_0C_00_00_00_
;~90_0F_05_
;~90_
;~90_
;~90_48_B8_88_77_66_55_44_33_22_11_
;~90_48_B9_88_77_66_55_44_33_22_11_
;~90_48_BA_88_77_66_55_44_33_22_11_
;~90_48_BB_88_77_66_55_44_33_22_11_
;~90_48_BC_88_77_66_55_44_33_22_11_
;~90_48_BD_88_77_66_55_44_33_22_11_
;~90_48_BE_88_77_66_55_44_33_22_11_
;~90_48_BF_88_77_66_55_44_33_22_11_
;~90_49_B8_88_77_66_55_44_33_22_11_
;~90_49_B9_88_77_66_55_44_33_22_11_
;~90_49_BA_88_77_66_55_44_33_22_11_
;~90_49_BB_88_77_66_55_44_33_22_11_
;~90_49_BC_88_77_66_55_44_33_22_11_
;~90_49_BD_88_77_66_55_44_33_22_11_
;~90_49_BE_88_77_66_55_44_33_22_11_
;~90_49_BF_88_77_66_55_44_33_22_11_
;~90_
;~90_
;~90_B8_3C_00_00_00_
;~90_BF_00_00_00_00_
;~90_0F_05_
;~90_E9_E2_FE_FF_FF_
;~90_EB_03_
;~90_
;~90_
;~90_
;~68_65_79_6F_20_77_6F_72_6C_64_21_0A_
;~63_79_61_61_20_77_6F_72_6C_64_21_0A_
;~6E_65_6B_6F_20_77_6F_72_6C_64_21_0A_
heyo: db "Heyo world!", 10

75
tool/tester.c Normal file

@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#include "../source/assembler.h"
#include "../source/assembler.c"
#include "../source/unix.h"
#include "../source/unix.c"
static unsigned int array [] = {
NOP, MOV, D32, REG, R0, IMM, 1,
NOP, MOV, D32, REG, R7, IMM, 1,
//~NOP, MOV, D32, REG, R6, REL, 0,
NOP, MOV, D32, REG, R6, IMM, 0x004010DA,
NOP, MOV, D32, REG, R2, IMM, 12,
NOP, SYSCALL,
NOP, MOV, D32, REG, R0, IMM, 60,
NOP, MOV, D32, REG, R7, IMM, 60,
NOP, SYSCALL
//~ASMDIRMEM, 0,
//~ASMDIRIMM, D8, 12,
//~72, 101, 121, 111, 32, 119, 111, 114, 108, 100, 10, 0
};
int main (void) {
FILE * file = NULL;
text_sector_byte = malloc (1024UL * 1024UL * sizeof (* text_sector_byte));
file = fopen ("run_me_please", "w+");
elf_main_header (1, 1, 1, 0);
elf_text_sector (34);
elf_data_sector (34, 12);
fwrite (elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
fwrite (elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
fwrite (elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
printf ("> %u\n", (unsigned int) (sizeof (array) / sizeof (array [0])));
assemble ((unsigned int) (sizeof (array) / sizeof (array [0])), array);
fwrite (text_sector_byte, 1UL, (size_t) text_sector_size, file);
fwrite ("Heyo world!\n", 1UL, 12, file);
free (text_sector_byte);
fclose (file);
return (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
D2 00 00 00 00 00 00 00 D2 00 00 00 00 00 00 00
00 10 00 00 00 00 00 00 01 00 00 00 06 00 00 00
D2 00 00 00 00 00 00 00 D2 10 40 00 00 00 00 00
D2 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
C0 01 00 00 00
C7 01 00 00 00
C6 00 10 40 00
C2 0C 00 00 00
0F 05
C0 3C 00 00 00
C7 3C 00 00 00
0F 05
48 65 79 6F 20 77 6F 72 6C 64 21 0A
*/