; =====================================================================================================================
; = Bin2Asm - Binary to ASM Converter - (C) 1999 Teraphy / Phrozen Crew - http://teraphy.cjb.net
; =====================================================================================================================

.486
.model		flat, stdcall
; -------- Include Files and Libraries --------------------------------------------------------------------------------
include		windows.inc
include		elib32.inc
includelib	elib32.lib
include		kernel32.inc
includelib	kernel32.lib
include		user32.inc
includelib	user32.lib
; -------- Initialised Data -------------------------------------------------------------------------------------------
.data
szUsage		db "Usage: Bin2Asm InFile OutFile Label",0
sBinToAsm	db "; Bin2Asm v1.1  (C) Teraphy",CrLf
sBinToAsmSize	EQU $-sBinToAsm
sLabelByte	db " Label Byte"
sLabelByteSize	EQU $-sLabelByte
sSize		db "Size EQU $-"
sSizeSize	EQU $-sSize
sDD		db 9,"DD "
sDDSize		EQU 4
sDB		db 9,"DB "
sDBSize		EQU 4
sDDVal		db "0%.8lXh"
sDDValSize	EQU 10
sDBVal		db "0%.2lXh"
sDBValSize	EQU 4
sComma		db ","
sCommaSize	EQU 1
sCrLf		db CrLf
sCrLfSize	EQU 2
; -------- UnInitialised Data -----------------------------------------------------------------------------------------
.data?
hMem		dd ?
hInFile		dd ?
hOutFile	dd ?
dSize		dd ?
bytes_read	dd ?
SaveVal		db ?
FirstTime	db ?
szInFile	db 128 dup (?)
szOutFile	db 128 dup (?)
szLabel		db 128 dup (?)
szBuffer	db 256 dup (?)
; -------- Program Code -----------------------------------------------------------------------------------------------
.code
start:
; -------- Print Message ----------------------------------------------------------------------------------------------
	PrintM	"Bin2Asm v1.1    (C) 1999 Teraphy / PhrozenCrew",CrLf
; -------- Get Input File ---------------------------------------------------------------------------------------------
	invoke	GetCLineArg, addr szInFile, 1
	.IF eax != CLINE_OK
		invoke	Print, addr szUsage
		Exit	-1
	.ENDIF
; -------- Get Output File --------------------------------------------------------------------------------------------
	invoke	GetCLineArg, addr szOutFile, 2
	.IF eax != CLINE_OK
		invoke	Print, addr szUsage
		Exit	-1
	.ENDIF
; -------- Get Label --------------------------------------------------------------------------------------------------
	invoke	GetCLineArg, addr szLabel, 3
	.IF eax != CLINE_OK
		invoke	Print, addr szUsage
		Exit	-1
	.ENDIF
; -------- Open Input File --------------------------------------------------------------------------------------------
	invoke	CreateFile, addr szInFile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
	.IF eax == INVALID_HANDLE_VALUE
		PrintM	"Error: Input File Not Found"
		Exit	-1
	.ENDIF
	mov	hInFile, eax
; -------- Create Output File -----------------------------------------------------------------------------------------
	invoke	CreateFile, addr szOutFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
	.IF eax == INVALID_HANDLE_VALUE
		invoke	CloseHandle, hInFile
		PrintM	"Error: Could'nt Create Output File"
		Exit	-1
	.ENDIF
	mov	hOutFile,eax
; -------- Allocate Memory --------------------------------------------------------------------------------------------
	invoke	GetFileSize, hInFile, NULL
	mov	dSize, eax
	invoke	GlobalAlloc, GPTR, eax
	mov	hMem, eax
; -------- Read Input File --------------------------------------------------------------------------------------------
	invoke	ReadFile, hInFile, hMem, dSize, addr bytes_read, NULL
; -------- Write File Header ------------------------------------------------------------------------------------------
	invoke	WriteFile, hOutFile, addr sBinToAsm, sBinToAsmSize, addr bytes_read, NULL
	invoke	lstrlen, addr szLabel
	invoke	WriteFile, hOutFile, addr szLabel, eax, addr bytes_read, NULL
	invoke	WriteFile, hOutFile, addr sLabelByte, sLabelByteSize, addr bytes_read, NULL
; -------- Process Read Data Block and Save DWords to File ------------------------------------------------------------
	mov	esi, hMem
	mov	ecx, dSize
	shr	ecx, 2
	.IF ecx == 0
		jmp DWord_Done
	.ENDIF
	mov	SaveVal,5
@@:
	push	ecx
	.IF SaveVal == 5
		mov	SaveVal,0
		invoke	WriteFile, hOutFile, addr sCrLf, sCrLfSize, addr bytes_read, NULL
		invoke	WriteFile, hOutFile, addr sDD, sDDSize, addr bytes_read, NULL
	.ELSE
		invoke	WriteFile, hOutFile, addr sComma, sCommaSize, addr bytes_read, NULL
	.ENDIF
	lodsd
	invoke	wsprintf, addr szBuffer, addr sDDVal, eax
	invoke	WriteFile, hOutFile, addr szBuffer, sDDValSize, addr bytes_read, NULL
	inc	SaveVal
	pop	ecx
	dec	ecx
	jz	DWord_Done
	jmp	@B
DWord_Done:
; -------- Process Read Data Block and Save Remaining Byte(s) to File -------------------------------------------------
	mov	eax, dSize
	shr	eax, 2
	shl	eax, 2
	mov	ecx, dSize
	sub	ecx, eax
	.IF ecx == 0
		jmp Done
	.ENDIF
	mov	FirstTime,1
	push	ecx
	invoke	WriteFile, hOutFile, addr sCrLf, sCrLfSize, addr bytes_read, NULL
	invoke	WriteFile, hOutFile, addr sDB, sDBSize, addr bytes_read, NULL
	pop	ecx
@@:
	push	ecx
	.IF FirstTime == 0
		invoke	WriteFile, hOutFile, addr sComma, sCommaSize, addr bytes_read, NULL
	.ELSE
		mov	FirstTime,0
	.ENDIF
	xor	eax,eax
	lodsb
	invoke	wsprintf, addr szBuffer, addr sDBVal, eax
	invoke	WriteFile, hOutFile, addr szBuffer, sDBValSize, addr bytes_read, NULL
	inc	SaveVal
	pop	ecx
	dec	ecx
	jz	Done
	jmp	@B
Done:
; -------- Write File Endings -----------------------------------------------------------------------------------------
	invoke	WriteFile, hOutFile, addr sCrLf, sCrLfSize, addr bytes_read, NULL
	invoke	lstrlen, addr szLabel
	invoke	WriteFile, hOutFile, addr szLabel, eax, addr bytes_read, NULL
	invoke	WriteFile, hOutFile, addr sSize, sSizeSize, addr bytes_read, NULL
	invoke	lstrlen, addr szLabel
	invoke	WriteFile, hOutFile, addr szLabel, eax, addr bytes_read, NULL
; -------- Close Files ------------------------------------------------------------------------------------------------
	invoke	CloseHandle, hInFile
	invoke	CloseHandle, hOutFile
; -------- Free Memory ------------------------------------------------------------------------------------------------
	invoke	GlobalFree, hMem
; -------- Show Done Message ------------------------------------------------------------------------------------------
	PrintM	"Completed Sucessfully!"
; -------- Exit Program -----------------------------------------------------------------------------------------------
	invoke	ExitProcess, 0
end start