
; flat assembler interface for Linux
; Copyright (c) 1999-2007, Tomasz Grysztar.
; All rights reserved.

	format	ELF executable
	entry	start

segment readable executable

start:
	mov	esi,_logo
	call	display_string

	mov	[command_line],esp
	pop	eax
	lea	esp,[esp+eax*4]
	pop	eax
	pop	[environment]
	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

	mov	eax,78
	mov	ebx,buffer
	xor	ecx,ecx
	int	0x80
	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

	call	preprocessor

	cmp	[print_preprocessed_source_flg], 1
	je	display_preprocessed

	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
	mov	eax,78
	mov	ebx,buffer
	xor	ecx,ecx
	int	0x80
	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	ebx,[command_line]
	mov	[input_file],0
	mov	[output_file],0
	mov	[memory_setting],0
	mov	[passes_limit],100
	mov	ecx,[ebx]
	add	ebx,8
	dec	ecx
	jz	bad_params
      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,'e'
	je	print_preproc_option
	cmp	al,'E'
	je	print_preproc_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
	jmp	next_param
      print_preproc_option:
	mov	[print_preprocessed_source_flg], 1
      next_param:
	add	ebx,4
	dec	ecx
	jnz	get_param
	cmp	[input_file],0
	je	bad_params
	clc
	ret
      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



stdout equ 1

display_preprocessed:
		call	convert_preprocessed_source

		mov	ebx, stdout
		mov     edx,[source_start]
		mov	ecx, edi
		sub	ecx, edx
		call    write

		xor	al, al
		jmp	exit_program






convert_preprocessed_source:
	mov	esi,[memory_start]
	mov	edi,[source_start]
      .preprocessed_to_text:
	cmp	esi,[source_start]
	jae	.conversion_done
	add	esi,16
	xor	dl,dl
      .convert_preprocessed_line:
	lodsb
	cmp	al,1Ah
	je	.copy_symbol
	cmp	al,22h
	je	.copy_symbol
	cmp	al,3Bh
	je	.preprocessor_symbols
	or	al,al
	jz	.line_converted
	stosb
	xor	dl,dl
	jmp	.convert_preprocessed_line
      .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_preprocessed_line
      .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_preprocessed_line
      .preprocessor_symbols:
	mov	al,3Bh
	stosb
	jmp	.copy_symbol
      .line_converted:
	mov	ax,0A0Dh
	stosw
	jmp	.preprocessed_to_text
      .conversion_done:

	ret





include 'system.inc'

include '..\version.inc'

_copyright db 'Copyright (c) 1999-2007, 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 memory available to assembler',0xA
       db ' -p <limit>  set the maximum allowed number of passes',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 '..\expressi.inc'
include '..\preproce.inc'
include '..\parser.inc'
include '..\assemble.inc'
include '..\formats.inc'
include '..\x86_64.inc'
include '..\tables.inc'

segment readable writeable

align 4

include '..\variable.inc'

command_line dd ?
memory_setting dd ?
environment dd ?
start_time dd ?
displayed_count dd ?
last_displayed db ?
character db ?
print_preprocessed_source_flg dd 0

buffer rb 1000h
