major refactor (I'm starting to understand things

This commit is contained in:
2025-06-21 18:51:49 -04:00
parent 9bc1e59bdb
commit bcbdd2c0a2
2 changed files with 123 additions and 85 deletions

View File

@ -287,7 +287,7 @@ def_Slice Byte
;region Math ;region Math
; returns: raccumulator = U64 ; returns: raccumulator = U64
; Usage ; Usage
%macro min_S64 2 %macro min_S64 2
mov raccumulator, %1 mov raccumulator, %1
cmp raccumulator, %2 cmp raccumulator, %2
@ -360,6 +360,15 @@ str8_to_cstr_capped:
;region WinAPI ;region WinAPI
%define MS_INVALID_HANDLE_VALUE -1
%define MS_FILE_ATTRIBUTE_NORMAL 0x00000080
%define MS_FILE_SHARE_READ 0x00000001
%define MS_GENERIC_READ 0x80000000
%define MS_OPEN_EXISTING 3
%define MS_STD_OUTPUT_HANDLE 11
%define wapi_shadow_space 32
; kernel32.lib ; kernel32.lib
; Console IO ; Console IO
extern GetStdHandle extern GetStdHandle
@ -370,40 +379,58 @@ extern CreateFileA
extern GetFileSizeEx extern GetFileSizeEx
extern GetLastError extern GetLastError
extern ReadFile extern ReadFile
extern WriteFileA
; Process API ; Process API
extern ExitProcess extern ExitProcess
%define MS_INVALID_HANDLE_VALUE -1 struc wapi_ctbl
%define MS_FILE_ATTRIBUTE_NORMAL 0x00000080 .shadow: resb 32 ; 32 bytes for RCX, RDX, R8, R9 home
%define MS_FILE_SHARE_READ 0x00000001 endstruc
%define MS_GENERIC_READ 0x80000000
%define MS_OPEN_EXISTING 3
%define MS_STD_OUTPUT_HANDLE 11
%define wapi_shadow_width 48 struc GetStdHandle_ctbl
%macro wapi_shadow_space 0 .shadow: resb 32
push rstack_base_ptr endstruc
mov rstack_base_ptr, rstack_ptr
sub rstack_ptr, wapi_shadow_width
%endmacro
%define wapi_arg4_offset 28
%define wapi_arg5_offset 32
%define wapi_CreateFileA_dwCreationDisposition 32 ; rcx: hConsoleOutput
%define wapi_CreateFileA_dwFlagsAndAttributes 40 ; rdx: lpBuffer
%define wapi_CreateFileA_hTemplateFile 48 ; r8: nNumberOfCharsToWrite
; r9: lpNumberOfCharsWritten
; s1: lpReserved
struc WriteConsoleA_ctbl
.shadow: resq 4
.lpReserved: resq 1
.lpNumberOfCharsWritten: resq 1
._pad_: resq 1
endstruc
%define wapi_ReadFile_lpOverlapped 32 struc CloseHandle_ctbl
.shadow: resb 32
.
endstruc
%define wapi_write_console_written_chars r9 ; r8: dwShareMode
%macro wapi_write_console 2 ; r9: lpSecurityAttributes
mov rcounter,[%1] ; Console Handle ; s1: dwCreationDisposition
lea rdata, [%2] ; Slice_Str8.Ptr ; s2: dwFlagsAndAttributes
mov r8_32, %2 %+ _len ; Slice_Str8.Len ; s3: hTemplateFile
lea r9, [rstack_ptr + wapi_arg4_offset] ; Written chars ; NOTE: Even though the first two are DWORDs, on the stack they each
mov qword [rstack_ptr + wapi_arg5_offset], 0 ; Reserved (must be 0) ; occupy a full 8-byte slot in the x64 ABI.
call WriteConsoleA struc CreateFileA_ctbl
%endmacro .shadow: resb 32
.dwCreationDisposition: resb 8
.dwFlagsAndAttributes: resb 8
.hTemplateFile: resb 8
endstruc
; rcx: hfile
; rdx: lpbuffer
; r8: nNumberOfBytesToWrite
; r9: lpNumberOfBytesWritten
; s1: lpOverlapped
struc WriteFileA_ctbl
.shadow: resb 32
.lpOverlapped: resb 8
endstruc
section .data section .data
std_out_hndl dq 0 std_out_hndl dq 0
@ -474,12 +501,12 @@ file_read_contents:
mov r8, [backing + Slice_Byte.len] mov r8, [backing + Slice_Byte.len]
mov r9, [result + FileOpInfo.content + Slice_Byte.len] mov r9, [result + FileOpInfo.content + Slice_Byte.len]
assert_cmp jle, r9, r8 assert_cmp jle, r9, r8
jg .error_exit jg .error_close_handle
; MS_BOOL get_size_failed = ! raccumulator ; MS_BOOL get_size_failed = ! raccumulator
; if (get_size_failed) goto .error_exit ; if (get_size_failed) goto .error_exit
assert_cmp jne, raccumulator, false assert_cmp jne, raccumulator, false
je .error_exit je .error_close_handle
; push r14 ; file_size ; push r14 ; file_size
%define file_size r14d %define file_size r14d
@ -502,7 +529,7 @@ file_read_contents:
; if (read_failed) goto .error_exit ; if (read_failed) goto .error_exit
mov r9, qword [result + FileOpInfo.content + Slice_Byte.len] mov r9, qword [result + FileOpInfo.content + Slice_Byte.len]
assert_cmp je, file_size, r9d assert_cmp je, file_size, r9d
jne .error_exit jne .error_close_handle
; CloseHandle(id_file) ; CloseHandle(id_file)
wapi_shadow_space wapi_shadow_space
@ -515,6 +542,13 @@ file_read_contents:
mov [result + FileOpInfo.content + Slice_Byte.ptr], raccumulator mov [result + FileOpInfo.content + Slice_Byte.ptr], raccumulator
jmp .cleanup jmp .cleanup
.error_close_handle:
; CloseHandle(id_file)
wapi_shadow_space
mov rcounter, rbase
call CloseHandle
stack_pop
.error_exit: .error_exit:
; result = {} ; result = {}
mov qword [result + FileOpInfo.content + Slice_Byte.ptr], 0 mov qword [result + FileOpInfo.content + Slice_Byte.ptr], 0
@ -544,37 +578,36 @@ section .data
section .text section .text
global main global main
main: main:
wapi_shadow_space stack_push GetStdHandle_ctbl_size
; Setup stdout handle mov rcounter_32, -MS_STD_OUTPUT_HANDLE
mov rcounter_32, -MS_STD_OUTPUT_HANDLE
call GetStdHandle call GetStdHandle
mov [std_out_hndl], raccumulator mov [std_out_hndl], raccumulator
stack_pop stack_pop
%push proc_scope %push proc_scope
; dbg_wipe_gprs ; dbg_wipe_gprs
%push calling %push calling
%assign stack_offset 0 %assign stack_offset 0
stack_slice Slice_Byte, local_backing stack_slice Slice_Byte, local_backing
stack_push stack_offset ; stack local_backing : Slice_byte stack_push stack_offset ; stack local_backing : Slice_byte
mov qword [local_backing + Slice_Byte.ptr], read_mem ; local_backing.ptr = read_mem.ptr mov qword [local_backing + Slice_Byte.ptr], read_mem ; local_backing.ptr = read_mem.ptr
mov qword [local_backing + Slice_Byte.len], Mem_128k_size ; local_backing.len = Mem_128k_size mov qword [local_backing + Slice_Byte.len], Mem_128k_size ; local_backing.len = Mem_128k_size
lea rcounter, file ; rcounter = file.ptr lea rcounter, file ; rcounter = file.ptr
mov rdata, [path_hello_files_asm + Str8.ptr] ; rdata = path_hello_files.ptr mov rdata, [path_hello_files_asm + Str8.ptr] ; rdata = path_hello_files.ptr
mov r8, [path_hello_files_asm + Str8.len] ; r9 = path_hello_files.len mov r8, [path_hello_files_asm + Str8.len] ; r8 = path_hello_files.len
lea r9, [local_backing] lea r9, [local_backing] ; r9 = & local_backing
call file_read_contents ; read_file_contents(rcounter, rdata, r8, r9) call file_read_contents ; read_file_contents(rcounter, rdata, r8, r9)
stack_pop stack_pop
%pop calling %pop calling
wapi_shadow_space stack_push WriteConsoleA_ctbl_size ; frame WriteConoleA
mov rcounter, [std_out_hndl] mov rcounter, [std_out_hndl] ; rcounter = std_out_hndl
lea rdata, [file + FileOpInfo.content + Slice_Byte.ptr] lea rdata, [file + FileOpInfo.content + Slice_Byte.ptr] ; rdata = file.content.ptr
mov r8_32, [file + FileOpInfo.content + Slice_Byte.len] mov r8_32, [file + FileOpInfo.content + Slice_Byte.len] ; r8 = file.content.len
lea r9, [rstack_ptr + wapi_arg4_offset] lea r9, [rstack_ptr + WriteFileA_ctbl.lpNumberOfBytesWritten] ; r9 = & stack.ptr[WriteFileA.ctbl.lpNumberOfBytesWritten]
mov qword [rstack_ptr + wapi_arg5_offset], 0 mov qword [rstack_ptr + WriteFileA_ctbl.lpReserved], nullptr ; stack.ptr[.ctbl.lpRserved] = nullptr
call WriteConsoleA call WriteConsoleA
stack_pop stack_pop
; Exit program ; Exit program
wapi_shadow_space wapi_shadow_space

View File

@ -79,41 +79,46 @@ breakpoint:
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:444:1" source_location: "hello_files.asm:469:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:475:1" source_location: "hello_files.asm:474:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:506:1" source_location: "hello_files.asm:494:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:501:1" source_location: "hello_files.asm:500:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:495:1" source_location: "hello_files.asm:504:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:470:1" source_location: "hello_files.asm:516:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:456:1" source_location: "hello_files.asm:510:1"
hit_count: 1 hit_count: 0
} }
breakpoint: breakpoint:
{ {
source_location: "hello_files.asm:571:1" source_location: "hello_files.asm:443:1"
hit_count: 1
}
breakpoint:
{
source_location: "hello_files.asm:455:1"
hit_count: 0 hit_count: 0
} }