; vim: ft=fasm :
;---------------------------------------------------------------------
include "include/win32a.inc"  ; macros for creating 'import' section
driver_init:
	invoke GetModuleHandle, 0
	mov [hinstance], eax

	invoke GetStdHandle, STD_INPUT_HANDLE
	mov [StdIn], eax
	invoke SetConsoleMode, eax, 5

	invoke GetStdHandle, STD_OUTPUT_HANDLE
	mov [StdOut], eax
	ret

code 'bye',bye
	invoke ExitProcess,0	; goodbye!!!

code 'cr',cr
	upsh 10
	call emit
	ret

code 'emit',emit
	upsh [no_echo]
	cmp eax, 1
	drop
	jz .a
	pusha
	mov [emit_buffer], eax	; keep it safe
	invoke WriteConsole, [StdOut], emit_buffer, 1, 0, 0
	popa
.a:	drop
	upsh 0
	upop [no_echo]
	ret

no_echo dd 0
code 'key',key
	upsh 1
	upop [no_echo]
	dup
	pusha
	invoke ReadConsole, [StdIn], emit_buffer, 1, written_buffer, 0
	popa
	mov eax, [emit_buffer]
	ret
;---------------------------------------------------------------------
hinstance rd 1
StdIn rd 1
StdOut rd 1
emit_buffer rd 1
written_buffer rd 1
; section .text
;---------------------------------------------------------------------

; windows-specific linkage stuff:
section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'

  import kernel,\
	 GetModuleHandle,'GetModuleHandleA',\
	 ExitProcess,'ExitProcess',\
	 GetStdHandle, 'GetStdHandle',\
	 WriteFile, 'WriteFile',\
	 ReadFile, 'ReadFile',\
	 ReadConsole, 'ReadConsoleA',\
	 WriteConsole, 'WriteConsoleA',\
	 SetConsoleMode, 'SetConsoleMode'

