;EasyCodeName=BinToAsci64,1
;Programmed by Pavlů Zdeněk
;www.zpavlu.cz
;29.08.2021 - 19:17:04

;Tuto proceduru FpuFLtoA napsal Raymond Filiatreault, prosinec 2002
;a upraven v dubnu 2003. Drobná chyba byla opravena v červenci 2003.
;Upraveno v březnu 2004, aby se předešlo případné ztrátě dat z FPU.
;Proceduru upravil Zdeněk Pavlů v roce 2021 pro JWasm X64.
;Podrobný popis v programu BinToAsci32.

;This procedure FpuFLtoA was written by Raymond Filiatreault, December 2002
; and modified April 2003. A minor flaw was corrected in July 2003.
; Modified March 2004 to avoid any potential data loss from the FPU.
;The procedure was modified by Zdeněk Pavlů in 2021 for JWasm X64.
;Description in the BinToAsci32 program.

.Const
	STR_REG     Equ   0
	SRC1_FPU    EQU   1
	SRC1_REAL   Equ   2
	SRC2_DMEM   Equ   1024
	STR_SCI     Equ   32768
	SRC2_DIMM   Equ   2048

	FOREGROUND_BLUE			Equ		1H	;modrá barva textu
	FOREGROUND_GREEN		Equ		2H	;zelená barvy textu
	FOREGROUND_RED			Equ		4H	;červená barva textu
	FOREGROUND_INTENSITY	Equ		8H	;zvýšená intenzita barvy textu
	FOREGROUND_WHITE		Equ		FOREGROUND_BLUE + FOREGROUND_GREEN + FOREGROUND_RED
	Barva1					Equ		FOREGROUND_WHITE + FOREGROUND_INTENSITY		;barva bílá výrazná
	Barva2					Equ		FOREGROUND_RED + FOREGROUND_INTENSITY		;červená výrazná
	Barva3					Equ		FOREGROUND_GREEN + FOREGROUND_INTENSITY		;zzelená výrazná


.Data?
	inbuf					DB 100 Dup (?)	;prostor pro 100 byte
	ResultAsci 				DB 200 Dup (?)

	;poloha kursoru
	COOR	Struc
				X	DW	?
				Y	DW	?
	COOR EndS
	CRD		COOR	<?>


.Data

	hInst		HINSTANCE	NULL
	Output_Handle	DQ			NULL		;handle bufferu obrazovky konzoly, do tohoto budeme zapisovat (console screen buffer handle, here we will write)
	Input_Handle	DQ			NULL
	;pro testy 
	REalx4   		Real4    	314.15926535897932384
	Realx8			Real8    	0.0000031415926535897932384
	Realx10			Real10	  	13141.59265358979323846
	REalx4c   		Real4    	0.0003141592653589793238426433832795
	Realx8c			Real8    	3141592653589.793238426433832795
	Realx10c		Real10	  	-31415926.535897932384626433832795
	Dword4			DWord		314.15926535897932384
	Qword8			QWord		314.15926535897932384
	TByte1			TByte		-314159265358979.32384626433832795

	Result 			Real10 		0.0

;	Str1Cz			DB			"Ukázky převodu reálného čísla na Asci.", 0
	Str1Cz			DB			"Examples of real number conversion to Asci.", 0
	Str2Cz			DB			"Real10 To String --> ", 0
	Str3Cz			DB			"Real8  To String --> ", 0
	Str4Cz			DB			"Real4  To String --> ", 0
	Str5Cz			DB			"DWord  To String --> ", 0
	Str6Cz			DB			"QWord  To String --> ", 0
	Str7Cz			DB			"TByte  To String --> ", 0
;	Str8Cz			DB			"Konec ukázky převodu Real4, Real8, Real10, Dword, Qword a TByte.", 0
	Str8Cz			DB			"End of Real4, Real8, Real10, Dword, Qword and TByte conversion example. ", 0AH, 0DH, 0
;	Str9Cz			DB			"Ukončete stiskem ENTER.", 0
	Str9Cz			DB			"Exit by pressing ENTER.", 0
;	titl1			DB			"Prevod BinToAsci64", 0
	titl1			DB			"Convert BinToAsci64", 0

.Code

	FpuFLtoA64 Proto :QWord,:DWord,:QWord,:DWord


;Makro na nastavení pozice kurzoru, barvy písma a výpis písma (Macro to set cursor position, font color and font listing)
Set_Kurzor Macro OutHandle:Req, pX:Req, pY:Req, Color:Req, Text:Req
	Mov CRD.X, pX
	Mov CRD.Y, pY
	Invoke SetConsoleCursorPosition, OutHandle, CRD
	Invoke SetConsoleTextAttribute, Output_Handle, Color
	Lea Rsi, Text
	Invoke lstrlen, Rsi
    Mov Rdi, Rax
    Invoke WriteConsoleA, Output_Handle, Rsi, Edi, Addr dwWritten, 0
EndM

;Makro pro zjednodušení definování řetězce (Macro to simplify string definition)
CStr Macro Text:VarArg
	local szText
	   .const
	szText  db Text,0
	   .code
	   exitm <offset szText>
EndM

start Proc
	Local lpBuff[20]:Byte
	Local lpOuput:QWord

	Invoke GetModuleHandle, NULL
	Mov hInst, Rax
	Invoke Main
	Invoke GetStdHandle, STD_INPUT_HANDLE
	Invoke ReadConsole, Rax, Addr lpBuff, 15, Addr lpOuput, NULL
	Invoke ExitProcess, 0
	Ret
start EndP

Main Proc Frame Uses  Rsi Rdi
	Local dwWritten:DWord
    Invoke SetConsoleOutputCP, 1250				;nastavení národního prostředí CZ ((locale setting CZ))
    Invoke SetConsoleTitle, Addr titl1

    Invoke GetStdHandle, STD_OUTPUT_HANDLE      ;(console screen buffer handle, here we will write)
    Mov [Output_Handle], Rax

    Set_Kurzor Output_Handle, 2, 1, Barva3, Str1Cz
    Set_Kurzor Output_Handle, 1, 3, Barva1, Str2Cz
    Invoke FpuFLtoA64, Addr Realx10, 16, Addr ResultAsci, STR_SCI OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 3, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 1, 5, Barva1, Str3Cz
	Invoke Real8ToReal10, Realx8
    Invoke FpuFLtoA64, Addr Result, 10, Addr ResultAsci, STR_SCI OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 5, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 1, 7, Barva1, Str4Cz
	Invoke Real4ToReal10, REalx4
    Invoke FpuFLtoA64, Addr Result, 6, Addr ResultAsci, STR_SCI OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 7, Barva2, ResultAsci

    Set_Kurzor Output_Handle, 1, 9, Barva1, Str2Cz
    Invoke FpuFLtoA64, Addr Realx10c, 16, Addr ResultAsci, STR_REG OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 9, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 1, 11, Barva1, Str3Cz
	Invoke Real8ToReal10, Realx8c
    Invoke FpuFLtoA64, Addr Result, 0, Addr ResultAsci, STR_REG OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 11, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 1, 13, Barva1, Str4Cz
	Invoke Real4ToReal10, REalx4c
    Invoke FpuFLtoA64, Addr Result, 15, Addr ResultAsci, STR_REG OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 13, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 1, 15, Barva1, Str5Cz
	Invoke Real4ToReal10, Dword4
    Invoke FpuFLtoA64, Addr Result, 15, Addr ResultAsci, STR_REG OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 15, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 1, 17, Barva1, Str6Cz
	Invoke Real8ToReal10, Qword8
    Invoke FpuFLtoA64, Addr Result, 10, Addr ResultAsci, STR_REG OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 17, Barva2, ResultAsci

    Set_Kurzor Output_Handle, 1, 19, Barva1, Str7Cz
    Invoke FpuFLtoA64, Addr TByte1, 16, Addr ResultAsci, STR_REG OR SRC1_REAL
	Set_Kurzor Output_Handle, 25, 19, Barva2, ResultAsci

	Set_Kurzor Output_Handle, 2, 21, Barva3, Str8Cz
	Set_Kurzor Output_Handle, 2, 22, Barva3, Str9Cz
    ret
Main EndP

End start
	
