; ############################################################################
; ## Filename     :   Parser.txt                                            ##
; ## Description  :   Byte Parser.                                          ##
; ## Author       :   CrackZ                                                ##
; ## Creation Date:   29/02/2000                                            ##
; ## Last Modified:   10/07/2000                                            ##
; ##									    ##
; ## 29/04/2000 - Recoded to .exe to make use of DPMI (memory allocation).  ##
; ## 10/07/2000 - Fixed a very stupid bug.                                  ##
; ############################################################################

; tasm32 /zi Parser.txt
; tlink32 /v Parser
; Stubit Parser.exe

.386
.model flat, stdcall

.data

AskFileName	db 0dh,0ah,'[?] Insert the name of the file to write : ','$'
CouldNotWrite	db 0dh,0ah,'[i] Unable to write the file.','$'
FileHandle	dd 0h
FileNotOpened	db 0dh,0ah,'[i] File could not be opened, exiting.','$'
FileWrite	db 15 dup('$')
FileSize	dd 0h
FileSizex8	dd 0h
GetLabelName	db 0dh,0ah,'[?] Please insert your desired label name (max. 10 chars) : ','$'
InputBuffer	db 15 dup('$')
LabelName	db 15 dup('$')
LinearAddress	dd 0h
LineCounter	db 10h
MemoryHandle	dd 0h
O_Logo		db 0dh,0ah,'----------------------------------------'
		db 0dh,0ah,'** Byte Parser By CrackZ - 25/04/2000 **'
		db 0dh,0ah,'----------------------------------------'
		db 0dh,0ah,'$'
O_Msg		db 0dh,0ah,'[?] Please insert name of file to parse : ','$'
StartWriteAddr	dd 0h
SuccessMsg	db 0dh,0ah,'[i] All Done, parsed file successfully written.','$'
WriteHandle	dd 0h

.code

Start:	push ds
	pop es
	lea edx, O_Logo
	mov ah, 9
	int 21h
	lea edx, O_Msg
	mov ah, 9
	int 21h
	lea edx, InputBuffer
	mov ah, 0ah
	int 21h
	call OpenFile
	call AllocMem
	call ReadFile
	call ProcLabelName
	xor eax, eax
	xor ebx, ebx
	mov ecx, [FileSize]
	xor edx, edx
	mov esi, [LinearAddress]
	call Parser
	call Create_And_Write_File
	call FreeMem
	lea edx, SuccessMsg
	mov ah, 9
	int 21h
	jmp endprg

; ############################################################################
; ## Functions                                                              ##
; ############################################################################

AllocMem PROC uses ebx esi edi
	mov eax, 0501h ; Allocate Memory Block.
	mov ebx, [FileSize]
	shl ebx, 3h ; Multiply FileSize by 8.
	mov [FileSizex8], ebx
	mov ecx, ebx
	and ebx, 0FFFF0000h
	shr ebx, 10h
	and ecx, 0FFFFh
	int 31h
	shl ebx, 10h
	add bx, cx
	mov [LinearAddress], ebx
	shl esi, 10h
	add si, di
	mov [MemoryHandle], esi
	xor eax, eax
	mov ecx, [FileSizex8]
	mov edi, [LinearAddress]
	repz stosb
	ret
AllocMem ENDP

Create_And_Write_File:
	lea edx, AskFileName
	mov ah, 9
	int 21h
	lea edx, FileWrite
	mov ah, 0ah
	int 21h
	lea edx, FileWrite+2
	movzx ebx, byte ptr [FileWrite+1]
	mov byte ptr [ebx.edx], 0h
	mov eax, 3C00h
	int 21h
	jnc Create_And_Write_File2
	lea edx, CouldNotWrite
	mov ah, 9
	int 21h
	jmp endprg

Create_And_Write_File2:
	movzx eax, ax
	mov [WriteHandle], eax
	xchg eax, ebx
	mov eax, [LinearAddress]
	mov ecx, [FileSize]
	add eax, ecx
	sub edi, eax
	xchg edi, ecx
	mov edx, eax
	mov eax, 4000h
	int 21h
	mov eax, 3E00h
	int 21h
	mov ebx, [FileHandle]
	int 21h
	ret		

FreeMem PROC uses ebx esi edi
	mov eax, 0502h
	xor esi, esi
	xor edi, edi
	mov esi, [MemoryHandle]
	mov edi, esi
	and esi, 0FFFF0000h
	shr esi, 10h
	and edi, 0FFFFh	
	int 31h
	ret
FreeMem ENDP

M_ASCII:
	cmp al, 9h
	jbe M_ASCIINum
	add al, 37h
	ret
	
M_ASCIINum:
	add al, 30h
	ret

OpenFile:
	lea edx, InputBuffer+2
	movzx ebx, byte ptr [InputBuffer+1]
	mov byte ptr [ebx.edx], 0h
	mov eax, 3d02h
	int 21h
	jnc OpenFile2
	lea edx, FileNotOpened
	mov ah, 9
	int 21h
	jmp endprg

OpenFile2:
	movzx eax, ax
	mov [FileHandle], eax
	xchg eax, ebx
	mov eax, 4202h
	xor ecx, ecx
	xor edx, edx
	int 21h
	shld edx, edx, 10h
	movzx eax, ax
	or eax, edx
	mov [FileSize], eax
	mov eax, 4200h
	xor ecx, ecx
	xor edx, edx
	int 21h
	ret

ProcLabelName:
	lea edx, GetLabelName
	mov ah, 9
	int 21h	
	lea edx, LabelName
	mov ah, 0ah
	int 21h
	mov ecx, [FileSize]
	mov edi, [LinearAddress]
	add edi, ecx
	mov [StartWriteAddr], edi	
	movzx ecx, byte ptr [LabelName+1]
	lea esi, LabelName+2
	repz movsb
	mov dword ptr [edi], 20626409h
	add edi, 4h
	ret

ReadFile:
	mov eax, 3F00h
	mov ebx, [FileHandle]
	mov ecx, [FileSize]
	mov edx, [LinearAddress]
	int 21h
	ret


; ############################################################################
; ## Main Parsing Funtion                                                   ##
; ############################################################################

Parser:	mov bl, byte ptr [esi]
	ror ebx, 4h
	mov al, bl
	rol ebx, 4h
	and bl, 0fh
	call M_ASCII
	cmp al, 41h
	jae ParserAddByte

ParserWrite:
	mov [edi], al
	mov al, bl
	call M_ASCII
	inc edi
	mov [edi], al
	inc edi
	mov word ptr [edi], 2C68h
	add edi, 2h
	dec ecx
	jz ParserFinished
	dec byte ptr [LineCounter]
	jz ParserNewLine
	inc esi
	jmp Parser

ParserAddByte:
	mov byte ptr [edi], 30h
	inc edi
	jmp ParserWrite	

ParserFinished:
	mov byte ptr [edi], 30h
	inc edi
	ret

ParserNewLine:
	dec edi
	mov dword ptr [edi], 09090A0Dh
	mov dword ptr [edi+4], 20206264h
	add edi, 7h
	inc esi
	mov byte ptr [LineCounter], 10h
	jmp Parser

; ############################################################################

endprg:	mov eax,4C00h
	int 21h

END Start