add flat assembler toolchain

This commit is contained in:
2024-11-24 23:13:28 -05:00
parent 99e8e4072b
commit dbfd94ea40
302 changed files with 145599 additions and 0 deletions

View File

@ -0,0 +1,362 @@
; flat assembler interface for Unix/libc
; Copyright (c) 1999-2022, Tomasz Grysztar.
; All rights reserved.
format ELF
public main
macro ccall proc,[arg]
{ common
local size
size = 0
mov ebp,esp
if ~ arg eq
forward
size = size + 4
common
sub esp,size
end if
and esp,-16
if ~ arg eq
add esp,size
reverse
pushd arg
common
end if
call proc
mov esp,ebp }
extrn gettimeofday
section '.text' executable align 16
main:
mov ecx,[esp+4]
mov [argc],ecx
mov ebx,[esp+8]
mov [argv],ebx
push ebp
mov [stack_frame],esp
mov [con_handle],1
mov esi,_logo
call display_string
call get_params
jc information
call init_memory
mov esi,_memory_prefix
call display_string
mov eax,[memory_end]
sub eax,[memory_start]
add eax,[additional_memory_end]
sub eax,[additional_memory]
shr eax,10
call display_number
mov esi,_memory_suffix
call display_string
ccall gettimeofday,buffer,0
mov eax,dword [buffer]
mov ecx,1000
mul ecx
mov ebx,eax
mov eax,dword [buffer+4]
div ecx
add eax,ebx
mov [start_time],eax
and [preprocessing_done],0
call preprocessor
or [preprocessing_done],-1
call parser
call assembler
call formatter
call display_user_messages
movzx eax,[current_pass]
inc eax
call display_number
mov esi,_passes_suffix
call display_string
ccall gettimeofday,buffer,0
mov eax,dword [buffer]
mov ecx,1000
mul ecx
mov ebx,eax
mov eax,dword [buffer+4]
div ecx
add eax,ebx
sub eax,[start_time]
jnc time_ok
add eax,3600000
time_ok:
xor edx,edx
mov ebx,100
div ebx
or eax,eax
jz display_bytes_count
xor edx,edx
mov ebx,10
div ebx
push edx
call display_number
mov dl,'.'
call display_character
pop eax
call display_number
mov esi,_seconds_suffix
call display_string
display_bytes_count:
mov eax,[written_size]
call display_number
mov esi,_bytes_suffix
call display_string
xor al,al
jmp exit_program
information:
mov esi,_usage
call display_string
mov al,1
jmp exit_program
get_params:
mov [input_file],0
mov [output_file],0
mov [symbols_file],0
mov [memory_setting],0
mov [passes_limit],100
mov ecx,[argc]
mov ebx,[argv]
add ebx,4
dec ecx
jz bad_params
mov [definitions_pointer],predefinitions
get_param:
mov esi,[ebx]
mov al,[esi]
cmp al,'-'
je option_param
cmp [input_file],0
jne get_output_file
mov [input_file],esi
jmp next_param
get_output_file:
cmp [output_file],0
jne bad_params
mov [output_file],esi
jmp next_param
option_param:
inc esi
lodsb
cmp al,'m'
je memory_option
cmp al,'M'
je memory_option
cmp al,'p'
je passes_option
cmp al,'P'
je passes_option
cmp al,'d'
je definition_option
cmp al,'D'
je definition_option
cmp al,'s'
je symbols_option
cmp al,'S'
je symbols_option
bad_params:
stc
ret
memory_option:
cmp byte [esi],0
jne get_memory_setting
dec ecx
jz bad_params
add ebx,4
mov esi,[ebx]
get_memory_setting:
call get_option_value
or edx,edx
jz bad_params
cmp edx,1 shl (32-10)
jae bad_params
mov [memory_setting],edx
jmp next_param
passes_option:
cmp byte [esi],0
jne get_passes_setting
dec ecx
jz bad_params
add ebx,4
mov esi,[ebx]
get_passes_setting:
call get_option_value
or edx,edx
jz bad_params
cmp edx,10000h
ja bad_params
mov [passes_limit],dx
next_param:
add ebx,4
dec ecx
jnz get_param
cmp [input_file],0
je bad_params
mov eax,[definitions_pointer]
mov byte [eax],0
mov [initial_definitions],predefinitions
clc
ret
definition_option:
cmp byte [esi],0
jne get_definition
dec ecx
jz bad_params
add ebx,4
mov esi,[ebx]
get_definition:
push edi
mov edi,[definitions_pointer]
call convert_definition_option
mov [definitions_pointer],edi
pop edi
jc bad_params
jmp next_param
symbols_option:
cmp byte [esi],0
jne get_symbols_setting
dec ecx
jz bad_params
add ebx,4
mov esi,[ebx]
get_symbols_setting:
mov [symbols_file],esi
jmp next_param
get_option_value:
xor eax,eax
mov edx,eax
get_option_digit:
lodsb
cmp al,20h
je option_value_ok
cmp al,0Dh
je option_value_ok
or al,al
jz option_value_ok
sub al,30h
jc invalid_option_value
cmp al,9
ja invalid_option_value
imul edx,10
jo invalid_option_value
add edx,eax
jc invalid_option_value
jmp get_option_digit
option_value_ok:
dec esi
clc
ret
invalid_option_value:
stc
ret
convert_definition_option:
mov edx,edi
cmp edi,predefinitions+1000h
jae bad_definition_option
xor al,al
stosb
copy_definition_name:
lodsb
cmp al,'='
je copy_definition_value
cmp al,20h
je bad_definition_option
or al,al
jz bad_definition_option
cmp edi,predefinitions+1000h
jae bad_definition_option
stosb
inc byte [edx]
jnz copy_definition_name
bad_definition_option:
stc
ret
copy_definition_value:
lodsb
cmp al,20h
je definition_value_end
or al,al
jz definition_value_end
cmp edi,predefinitions+1000h
jae bad_definition_option
stosb
jmp copy_definition_value
definition_value_end:
dec esi
cmp edi,predefinitions+1000h
jae bad_definition_option
xor al,al
stosb
clc
ret
include 'system.inc'
include '..\version.inc'
_copyright db 'Copyright (c) 1999-2022, Tomasz Grysztar',0xA,0
_logo db 'flat assembler version ',VERSION_STRING,0
_usage db 0xA
db 'usage: fasm <source> [output]',0xA
db 'optional settings:',0xA
db ' -m <limit> set the limit in kilobytes for the available memory',0xA
db ' -p <limit> set the maximum allowed number of passes',0xA
db ' -d <name>=<value> define symbolic variable',0xA
db ' -s <file> dump symbolic information for debugging',0xA
db 0
_memory_prefix db ' (',0
_memory_suffix db ' kilobytes memory)',0xA,0
_passes_suffix db ' passes, ',0
_seconds_suffix db ' seconds, ',0
_bytes_suffix db ' bytes.',0xA,0
include '..\errors.inc'
include '..\symbdump.inc'
include '..\preproce.inc'
include '..\parser.inc'
include '..\exprpars.inc'
include '..\assemble.inc'
include '..\exprcalc.inc'
include '..\formats.inc'
include '..\x86_64.inc'
include '..\avx.inc'
include '..\tables.inc'
include '..\messages.inc'
section '.bss' writeable align 4
include '..\variable.inc'
argc dd ?
argv dd ?
stack_frame dd ?
memory_setting dd ?
definitions_pointer dd ?
start_time dd ?
timestamp dq ?
con_handle dd ?
displayed_count dd ?
last_displayed db ?
character db ?
preprocessing_done db ?
predefinitions rb 1000h
buffer rb 1000h

View File

@ -0,0 +1,410 @@
; flat assembler interface for Unix/libc
; Copyright (c) 1999-2022, Tomasz Grysztar.
; All rights reserved.
extrn malloc
extrn free
extrn getenv
extrn fopen
extrn fclose
extrn fread
extrn fwrite
extrn fseek
extrn ftell
extrn time
extrn exit
extrn 'write' as libc_write
init_memory:
mov eax,esp
and eax,not 0FFFh
add eax,1000h-10000h
mov [stack_limit],eax
mov ecx,[memory_setting]
shl ecx,10
jnz allocate_memory
mov ecx,1000000h
allocate_memory:
mov [memory_setting],ecx
ccall malloc,ecx
or eax,eax
jz out_of_memory
mov [additional_memory],eax
add eax,[memory_setting]
mov [memory_end],eax
mov eax,[memory_setting]
shr eax,2
add eax,[additional_memory]
mov [additional_memory_end],eax
mov [memory_start],eax
ret
exit_program:
movzx eax,al
push eax
ccall free,[additional_memory]
pop eax
ccall exit,eax
mov esp,[stack_frame]
pop ebp
ret
get_environment_variable:
ccall getenv,esi
mov esi,eax
or eax,eax
jz no_environment_variable
copy_variable_value:
lodsb
cmp edi,[memory_end]
jae out_of_memory
stosb
or al,al
jnz copy_variable_value
dec edi
ret
no_environment_variable:
stosb
dec edi
ret
open:
push esi edi ebp
call adapt_path
ccall fopen,buffer,open_mode
pop ebp edi esi
or eax,eax
jz file_error
mov ebx,eax
clc
ret
adapt_path:
mov esi,edx
mov edi,buffer
copy_path:
lods byte [esi]
cmp al,'\'
jne path_char_ok
mov al,'/'
path_char_ok:
stos byte [edi]
or al,al
jnz copy_path
cmp edi,buffer+1000h
ja out_of_memory
ret
create:
push esi edi ebp
call adapt_path
ccall fopen,buffer,create_mode
pop ebp edi esi
or eax,eax
jz file_error
mov ebx,eax
clc
ret
close:
ccall fclose,ebx
ret
read:
push ebx ecx edx esi edi ebp
ccall fread,edx,1,ecx,ebx
pop ebp edi esi edx ecx ebx
cmp eax,ecx
jne file_error
clc
ret
file_error:
stc
ret
write:
push ebx ecx edx esi edi ebp
ccall fwrite,edx,1,ecx,ebx
pop ebp edi esi edx ecx ebx
cmp eax,ecx
jne file_error
clc
ret
lseek:
push ebx
movzx eax,al
ccall fseek,ebx,edx,eax
test eax,eax
jnz lseek_error
mov ebx,[esp]
ccall ftell,ebx
pop ebx
clc
ret
lseek_error:
pop ebx
stc
ret
display_string:
lodsb
or al,al
jz string_displayed
mov dl,al
call display_character
jmp display_string
string_displayed:
ret
display_character:
mov [character],dl
ccall libc_write,[con_handle],character,1
ret
display_number:
push ebx
mov ecx,1000000000
xor edx,edx
xor bl,bl
display_loop:
div ecx
push edx
cmp ecx,1
je display_digit
or bl,bl
jnz display_digit
or al,al
jz digit_ok
not bl
display_digit:
mov dl,al
add dl,30h
push ebx ecx
call display_character
pop ecx ebx
digit_ok:
mov eax,ecx
xor edx,edx
mov ecx,10
div ecx
mov ecx,eax
pop eax
or ecx,ecx
jnz display_loop
pop ebx
ret
display_user_messages:
mov [displayed_count],0
call show_display_buffer
cmp [displayed_count],0
je line_break_ok
cmp [last_displayed],0Ah
je line_break_ok
mov dl,0Ah
call display_character
line_break_ok:
ret
display_block:
jecxz block_displayed
add [displayed_count],ecx
mov al,[esi+ecx-1]
mov [last_displayed],al
display_characters:
lodsb
mov dl,al
push ecx esi
call display_character
pop esi ecx
loop display_characters
block_displayed:
ret
fatal_error:
mov [con_handle],2
mov esi,error_prefix
call display_string
pop esi
call display_string
mov esi,error_suffix
call display_string
mov al,0FFh
jmp exit_program
assembler_error:
mov [con_handle],2
call display_user_messages
mov ebx,[current_line]
test ebx,ebx
jz display_error_message
push dword 0
get_error_lines:
mov eax,[ebx]
cmp byte [eax],0
je get_next_error_line
push ebx
test byte [ebx+7],80h
jz display_error_line
mov edx,ebx
find_definition_origin:
mov edx,[edx+12]
test byte [edx+7],80h
jnz find_definition_origin
push edx
get_next_error_line:
mov ebx,[ebx+8]
jmp get_error_lines
display_error_line:
mov esi,[ebx]
call display_string
mov esi,line_number_start
call display_string
mov eax,[ebx+4]
and eax,7FFFFFFFh
call display_number
mov dl,']'
call display_character
pop esi
cmp ebx,esi
je line_number_ok
mov dl,20h
call display_character
push esi
mov esi,[esi]
movzx ecx,byte [esi]
inc esi
call display_block
mov esi,line_number_start
call display_string
pop esi
mov eax,[esi+4]
and eax,7FFFFFFFh
call display_number
mov dl,']'
call display_character
line_number_ok:
mov esi,line_data_start
call display_string
mov esi,ebx
mov edx,[esi]
call open
mov al,2
xor edx,edx
call lseek
mov edx,[esi+8]
sub eax,edx
jz line_data_displayed
push eax
xor al,al
call lseek
mov ecx,[esp]
mov edx,[additional_memory]
lea eax,[edx+ecx]
cmp eax,[additional_memory_end]
ja out_of_memory
call read
call close
pop ecx
mov esi,[additional_memory]
get_line_data:
mov al,[esi]
cmp al,0Ah
je display_line_data
cmp al,0Dh
je display_line_data
cmp al,1Ah
je display_line_data
or al,al
jz display_line_data
inc esi
loop get_line_data
display_line_data:
mov ecx,esi
mov esi,[additional_memory]
sub ecx,esi
call display_block
line_data_displayed:
mov esi,lf
call display_string
pop ebx
or ebx,ebx
jnz display_error_line
cmp [preprocessing_done],0
je display_error_message
mov esi,preprocessed_instruction_prefix
call display_string
mov esi,[current_line]
add esi,16
mov edi,[additional_memory]
xor dl,dl
convert_instruction:
lodsb
cmp al,1Ah
je copy_symbol
cmp al,22h
je copy_symbol
cmp al,3Bh
je instruction_converted
stosb
or al,al
jz instruction_converted
xor dl,dl
jmp convert_instruction
copy_symbol:
or dl,dl
jz space_ok
mov byte [edi],20h
inc edi
space_ok:
cmp al,22h
je quoted
lodsb
movzx ecx,al
rep movsb
or dl,-1
jmp convert_instruction
quoted:
mov al,27h
stosb
lodsd
mov ecx,eax
jecxz quoted_copied
copy_quoted:
lodsb
stosb
cmp al,27h
jne quote_ok
stosb
quote_ok:
loop copy_quoted
quoted_copied:
mov al,27h
stosb
or dl,-1
jmp convert_instruction
instruction_converted:
xor al,al
stosb
mov esi,[additional_memory]
call display_string
mov esi,lf
call display_string
display_error_message:
mov esi,error_prefix
call display_string
pop esi
call display_string
mov esi,error_suffix
call display_string
mov al,2
jmp exit_program
make_timestamp:
ccall time,timestamp
mov eax,dword [timestamp]
mov edx,dword [timestamp+4]
ret
open_mode db 'r',0
create_mode db 'w',0
error_prefix db 'error: ',0
error_suffix db '.'
lf db 0xA,0
line_number_start db ' [',0
line_data_start db ':',0xA,0
preprocessed_instruction_prefix db 'processed: ',0